xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tools/kernel_gen/tf_jit_cache.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_MLIR_TOOLS_KERNEL_GEN_TF_JIT_CACHE_H_
17 #define TENSORFLOW_COMPILER_MLIR_TOOLS_KERNEL_GEN_TF_JIT_CACHE_H_
18 
19 #include <functional>
20 #include <string>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "mlir/ExecutionEngine/ExecutionEngine.h"  // from @llvm-project
24 #include "tensorflow/core/framework/resource_op_kernel.h"
25 
26 namespace mlir {
27 namespace kernel_gen {
28 namespace tf_framework {
29 
30 class JITCache : public tensorflow::ResourceBase {
31  public:
32   static constexpr const char* kDefaultResourceName = "mlir-jit-cache";
33   static tensorflow::Status Create(JITCache** dst);
34 
35   std::string DebugString() const override;
36   ExecutionEngine* LookupOrCompile(
37       const std::string code,
38       std::function<llvm::Expected<std::unique_ptr<ExecutionEngine>>()>
39           compile_callback);
40   size_t Size();
41 
42  private:
43   tensorflow::mutex mu_;
44   absl::flat_hash_map<std::string, std::unique_ptr<ExecutionEngine>>
45       execution_engine_by_key_ TF_GUARDED_BY(mu_);
46 };
47 
48 }  // namespace tf_framework
49 }  // namespace kernel_gen
50 }  // namespace mlir
51 
52 #endif  // TENSORFLOW_COMPILER_MLIR_TOOLS_KERNEL_GEN_TF_JIT_CACHE_H_
53