Home | History | Annotate | Download | only in service
      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:
     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<Status(const llvm::Module&)>;
     41 
     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 
     49   void RemovePreOptimizationHook() { user_pre_optimization_hook_ = nullptr; }
     50 
     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 
     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   //       perftools::gputools::StreamExecutor* stream_exec,
     64   //       DeviceMemoryAllocator* device_allocator)
     65   //   StatusOr<std::unique_ptr<HloModule>> RunHloPasses(
     66   //       std::unique_ptr<HloModule> module,
     67   //       perftools::gputools::StreamExecutor* stream_exec,
     68   //       DeviceMemoryAllocator* device_allocator)
     69   using Compiler::RunBackend;
     70   using Compiler::RunHloPasses;
     71 
     72   StatusOr<std::vector<std::unique_ptr<Executable>>> Compile(
     73       std::vector<std::unique_ptr<HloModule>> modules,
     74       std::vector<std::vector<perftools::gputools::StreamExecutor*>>
     75           stream_execs,
     76       DeviceMemoryAllocator* device_allocator) 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