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 #include "tensorflow/compiler/xla/tests/llvm_irgen_test_base.h"
17
18 #include <functional>
19 #include <utility>
20
21 #include "tensorflow/compiler/xla/service/hlo_parser.h"
22 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
23 #include "tensorflow/compiler/xla/tests/filecheck.h"
24 #include "tensorflow/core/lib/core/status_test_util.h"
25 #include "tensorflow/core/platform/test.h"
26
27 namespace xla {
28
SetIrHook(bool match_optimized_ir)29 void LlvmIrGenTestBase::SetIrHook(bool match_optimized_ir) {
30 auto llvm_compiler = GetLLVMCompiler();
31 using std::placeholders::_1;
32
33 // Add the IR inspection hook to the LLVM compiler.
34 if (match_optimized_ir) {
35 llvm_compiler->SetPostOptimizationHook(
36 std::bind(&LlvmIrGenTestBase::IrHook, this, _1));
37 } else {
38 llvm_compiler->SetPreOptimizationHook(
39 std::bind(&LlvmIrGenTestBase::IrHook, this, _1));
40 }
41 }
42
ResetIrHook()43 void LlvmIrGenTestBase::ResetIrHook() {
44 auto llvm_compiler = GetLLVMCompiler();
45
46 llvm_compiler->RemovePreOptimizationHook();
47 llvm_compiler->RemovePostOptimizationHook();
48 }
49
CompileAndVerifyIr(std::unique_ptr<HloModule> hlo_module,const string & pattern,bool match_optimized_ir)50 void LlvmIrGenTestBase::CompileAndVerifyIr(
51 std::unique_ptr<HloModule> hlo_module, const string& pattern,
52 bool match_optimized_ir) {
53 SetIrHook(match_optimized_ir);
54 Status status = CompileToExecutable(std::move(hlo_module)).status();
55 ResetIrHook();
56 TF_ASSERT_OK(status);
57
58 StatusOr<bool> filecheck_result = RunFileCheck(ir_, pattern);
59 TF_ASSERT_OK(filecheck_result.status());
60 EXPECT_TRUE(filecheck_result.ValueOrDie());
61 }
62
CompileAndVerifyIr(const string & hlo_text,const string & expected_llvm_ir,bool match_optimized_ir)63 void LlvmIrGenTestBase::CompileAndVerifyIr(const string& hlo_text,
64 const string& expected_llvm_ir,
65 bool match_optimized_ir) {
66 HloModuleConfig config;
67 config.set_debug_options(GetDebugOptionsForTest());
68 TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
69 ParseHloString(hlo_text, config));
70 CompileAndVerifyIr(std::move(module), expected_llvm_ir, match_optimized_ir);
71 }
72
CompileAheadOfTimeAndVerifyIr(std::unique_ptr<HloModule> hlo_module,const AotCompilationOptions & options,const string & pattern,bool match_optimized_ir)73 void LlvmIrGenTestBase::CompileAheadOfTimeAndVerifyIr(
74 std::unique_ptr<HloModule> hlo_module, const AotCompilationOptions& options,
75 const string& pattern, bool match_optimized_ir) {
76 SetIrHook(match_optimized_ir);
77 Status status =
78 CompileToAotCompilationResult(std::move(hlo_module), options).status();
79 ResetIrHook();
80 TF_ASSERT_OK(status);
81
82 StatusOr<bool> filecheck_result = RunFileCheck(ir_, pattern);
83 ASSERT_TRUE(filecheck_result.ok());
84 EXPECT_TRUE(filecheck_result.ValueOrDie());
85 }
86
GetLLVMCompiler()87 LLVMCompiler* LlvmIrGenTestBase::GetLLVMCompiler() {
88 return static_cast<LLVMCompiler*>(backend().compiler());
89 }
90
IrHook(const llvm::Module & module)91 Status LlvmIrGenTestBase::IrHook(const llvm::Module& module) {
92 ir_ = llvm_ir::DumpModuleToString(module);
93 return Status::OK();
94 }
95
96 } // namespace xla
97