Home | History | Annotate | Download | only in cpu
      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_CPU_COMPILER_FUNCTOR_H_
     17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_COMPILER_FUNCTOR_H_
     18 
     19 #include "llvm/IR/LegacyPassManager.h"
     20 #include "llvm/IR/Module.h"
     21 #include "llvm/Object/ObjectFile.h"
     22 #include "llvm/Target/TargetMachine.h"
     23 #include "tensorflow/compiler/xla/service/cpu/disassembler.h"
     24 #include "tensorflow/compiler/xla/service/llvm_compiler.h"
     25 #include "tensorflow/core/platform/logging.h"
     26 
     27 namespace xla {
     28 namespace cpu {
     29 
     30 // Functor class for compiling an LLVM module down to an object file. For use by
     31 // Orc JIT compile layer.
     32 class CompilerFunctor {
     33  public:
     34   explicit CompilerFunctor(
     35       llvm::TargetMachine* target_machine, const Disassembler* disassembler,
     36       int opt_level, bool optimize_for_size, bool enable_fast_math,
     37       bool disable_expensive_passes,
     38       LLVMCompiler::ModuleHook pre_optimization_hook = nullptr,
     39       LLVMCompiler::ModuleHook post_optimization_hook = nullptr)
     40       : target_machine_(target_machine),
     41         disassembler_(CHECK_NOTNULL(disassembler)),
     42         opt_level_(opt_level),
     43         optimize_for_size_(optimize_for_size),
     44         enable_fast_math_(enable_fast_math),
     45         disable_expensive_passes_(disable_expensive_passes),
     46         pre_optimization_hook_(pre_optimization_hook),
     47         post_optimization_hook_(post_optimization_hook) {}
     48 
     49   // Compile a Module to an ObjectFile.
     50   llvm::object::OwningBinary<llvm::object::ObjectFile> operator()(
     51       llvm::Module& module) const;  // NOLINT
     52 
     53  private:
     54   // Populates the given pass manager with TargetLibraryInfo and
     55   // TargetTransformInfo passes.
     56   void AddTargetInfoPasses(llvm::legacy::PassManagerBase* passes) const;
     57 
     58   // Populates the given pass managers based on the optimization level.
     59   void AddOptimizationPasses(llvm::legacy::PassManagerBase* module_passes,
     60                              llvm::legacy::FunctionPassManager* function_passes,
     61                              unsigned opt_level, unsigned size_level) const;
     62 
     63   llvm::TargetMachine* target_machine_;
     64   const Disassembler* disassembler_;
     65   const unsigned opt_level_;
     66   const bool optimize_for_size_;
     67   const bool enable_fast_math_;
     68   const bool disable_expensive_passes_;
     69   LLVMCompiler::ModuleHook pre_optimization_hook_;
     70   LLVMCompiler::ModuleHook post_optimization_hook_;
     71 };
     72 
     73 }  // namespace cpu
     74 }  // namespace xla
     75 
     76 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_COMPILER_FUNCTOR_H_
     77