• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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_XLA_SERVICE_LLVM_COMPILER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_COMPILER_H_
18 
19 #include "llvm/IR/Module.h"
20 #include "tensorflow/compiler/xla/service/compiler.h"
21 
22 namespace xla {
23 
24 // Interface for an LLVM-based compiler. This provides the ability to register
25 // hooks to inspect the LLVM IR during compilation, both before and after
26 // optimizations are applied.
27 //
28 // Hooks get called once per HLO module being compiled. The following should not
29 // be relied on:
30 // * The order in which hooks get called.
31 // * Whether or not a hook gets called if a compilation exits with a non-OK
32 //   status.
33 class LLVMCompiler : public Compiler {
34  public:
~LLVMCompiler()35   ~LLVMCompiler() override {}
36 
37   // A callback of this type can be run before and/or after IR-level
38   // optimization to e.g. dump out the generated IR to disk or gather some
39   // statistics.
40   using ModuleHook = std::function<void(const llvm::Module&)>;
41 
SetPreOptimizationHook(ModuleHook hook)42   void SetPreOptimizationHook(ModuleHook hook) {
43     CHECK(!user_pre_optimization_hook_)
44         << "Pre-optimization hook is already set";
45     CHECK(hook) << "hook cannot be null";
46     user_pre_optimization_hook_ = hook;
47   }
48 
RemovePreOptimizationHook()49   void RemovePreOptimizationHook() { user_pre_optimization_hook_ = nullptr; }
50 
SetPostOptimizationHook(ModuleHook hook)51   void SetPostOptimizationHook(ModuleHook hook) {
52     CHECK(!user_post_optimization_hook_)
53         << "Post-optimization hook is already set";
54     CHECK(hook) << "hook cannot be null";
55     user_post_optimization_hook_ = hook;
56   }
57 
RemovePostOptimizationHook()58   void RemovePostOptimizationHook() { user_post_optimization_hook_ = nullptr; }
59 
60   // Bring in
61   //   StatusOr<std::unique_ptr<Executable>> RunBackend(
62   //       std::unique_ptr<HloModule> module,
63   //       se::StreamExecutor* stream_exec,
64   //       se::DeviceMemoryAllocator* device_allocator)
65   //   StatusOr<std::unique_ptr<HloModule>> RunHloPasses(
66   //       std::unique_ptr<HloModule> module,
67   //       se::StreamExecutor* stream_exec,
68   //       se::DeviceMemoryAllocator* device_allocator)
69   using Compiler::Compile;
70   using Compiler::RunBackend;
71   using Compiler::RunHloPasses;
72 
73   StatusOr<std::vector<std::unique_ptr<Executable>>> Compile(
74       std::unique_ptr<HloModuleGroup> module_group,
75       std::vector<std::vector<se::StreamExecutor*>> stream_execs,
76       const CompileOptions& options) override;
77 
78  protected:
79   ModuleHook user_pre_optimization_hook_;
80   ModuleHook user_post_optimization_hook_;
81 };
82 
83 }  // namespace xla
84 
85 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_COMPILER_H_
86