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_TESTS_LLVM_IRGEN_TEST_BASE_H_ 17 #define TENSORFLOW_COMPILER_XLA_TESTS_LLVM_IRGEN_TEST_BASE_H_ 18 19 #include <string> 20 21 #include "tensorflow/compiler/xla/service/llvm_compiler.h" 22 #include "tensorflow/compiler/xla/tests/codegen_test_base.h" 23 24 namespace xla { 25 26 // Tests that verify IR emitted by the CPU/GPU backend is as expected. 27 class LlvmIrGenTestBase : public CodegenTestBase { 28 protected: 29 // Compiles the given HLO module to LLVM IR and verifies the IR matches the 30 // given pattern. `pattern` is in the FileCheck pattern matching syntax 31 // (http://llvm.org/docs/CommandGuide/FileCheck.html). 32 // 33 // This function invokes the JIT compiler. 34 // 35 // If `match_optimized_ir` is true, match the version of the IR after internal 36 // optimizations are applied; otherwise, the IR before optimizations is 37 // matched. 38 void CompileAndVerifyIr(std::unique_ptr<HloModule> hlo_module, 39 const string& pattern, bool match_optimized_ir); 40 41 // A thin wrapper around CompileAndVerifyIr that parses `hlo_text` to create 42 // an HLO module. 43 void CompileAndVerifyIr(const string& hlo_text, 44 const string& expected_llvm_ir, 45 bool match_optimized_ir = false); 46 47 // Compiles the given HLO module to LLVM IR and verifies the IR matches the 48 // given pattern. `pattern` is in the FileCheck pattern matching syntax 49 // (http://llvm.org/docs/CommandGuide/FileCheck.html). 50 // 51 // This function invokes the AOT compiler, with options in `options`. 52 // 53 // If `match_optimized_ir` is true, match the version of the IR after internal 54 // optimizations are applied; otherwise, the IR before optimizations is 55 // matched. 56 void CompileAheadOfTimeAndVerifyIr(std::unique_ptr<HloModule> hlo_module, 57 const AotCompilationOptions& options, 58 const string& pattern, 59 bool match_optimized_ir); 60 61 private: 62 LLVMCompiler* GetLLVMCompiler(); 63 64 void SetIrHook(bool match_optimized_ir); 65 void ResetIrHook(); 66 67 string ir_; 68 Status IrHook(const llvm::Module& module); 69 }; 70 71 } // namespace xla 72 73 #endif // TENSORFLOW_COMPILER_XLA_TESTS_LLVM_IRGEN_TEST_BASE_H_ 74