xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/tests/llvm_irgen_test_base.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/llvm_ir/llvm_util.h"
22 #include "tensorflow/compiler/xla/tests/filecheck.h"
23 #include "tensorflow/core/lib/core/status_test_util.h"
24 #include "tensorflow/core/platform/test.h"
25 
26 namespace xla {
27 
SetIrHook(bool match_optimized_ir)28 void LlvmIrGenTestBase::SetIrHook(bool match_optimized_ir) {
29   auto llvm_compiler = GetLLVMCompiler();
30   using std::placeholders::_1;
31 
32   // Add the IR inspection hook to the LLVM compiler.
33   if (match_optimized_ir) {
34     llvm_compiler->SetPostOptimizationHook(
35         std::bind(&LlvmIrGenTestBase::IrHook, this, _1));
36   } else {
37     llvm_compiler->SetPreOptimizationHook(
38         std::bind(&LlvmIrGenTestBase::IrHook, this, _1));
39   }
40 }
41 
ResetIrHook()42 void LlvmIrGenTestBase::ResetIrHook() {
43   auto llvm_compiler = GetLLVMCompiler();
44 
45   llvm_compiler->RemovePreOptimizationHook();
46   llvm_compiler->RemovePostOptimizationHook();
47 }
48 
CompileAndVerifyIr(std::unique_ptr<HloModule> hlo_module,const std::string & pattern,bool match_optimized_ir)49 void LlvmIrGenTestBase::CompileAndVerifyIr(
50     std::unique_ptr<HloModule> hlo_module, const std::string& pattern,
51     bool match_optimized_ir) {
52   SetIrHook(match_optimized_ir);
53   Status status = CompileToExecutable(std::move(hlo_module)).status();
54   ResetIrHook();
55   TF_ASSERT_OK(status);
56 
57   StatusOr<bool> filecheck_result = RunFileCheck(ir_, pattern);
58   TF_ASSERT_OK(filecheck_result.status());
59   EXPECT_TRUE(filecheck_result.ValueOrDie()) << "Full IR: " << ir_;
60 }
61 
CompileAndVerifyIr(const std::string & hlo_text,const std::string & expected_llvm_ir,bool match_optimized_ir)62 void LlvmIrGenTestBase::CompileAndVerifyIr(const std::string& hlo_text,
63                                            const std::string& expected_llvm_ir,
64                                            bool match_optimized_ir) {
65   HloModuleConfig config;
66   config.set_debug_options(GetDebugOptionsForTest());
67   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
68                           ParseAndReturnVerifiedModule(hlo_text, config));
69   CompileAndVerifyIr(std::move(module), expected_llvm_ir, match_optimized_ir);
70 }
71 
CompileAheadOfTimeAndVerifyIr(std::unique_ptr<HloModule> hlo_module,const AotCompilationOptions & options,const std::string & pattern,bool match_optimized_ir)72 void LlvmIrGenTestBase::CompileAheadOfTimeAndVerifyIr(
73     std::unique_ptr<HloModule> hlo_module, const AotCompilationOptions& options,
74     const std::string& pattern, bool match_optimized_ir) {
75   SetIrHook(match_optimized_ir);
76   Status status =
77       CompileToAotCompilationResult(std::move(hlo_module), options).status();
78   ResetIrHook();
79   TF_ASSERT_OK(status);
80 
81   StatusOr<bool> filecheck_result = RunFileCheck(ir_, pattern);
82   ASSERT_TRUE(filecheck_result.ok());
83   EXPECT_TRUE(filecheck_result.ValueOrDie()) << "Full IR: " << ir_;
84 }
85 
MatchOptimizedHlo(absl::string_view hlo,absl::string_view pattern,bool print_operand_shape)86 void LlvmIrGenTestBase::MatchOptimizedHlo(absl::string_view hlo,
87                                           absl::string_view pattern,
88                                           bool print_operand_shape) {
89   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> optimized_module,
90                           GetOptimizedModule(hlo));
91   HloPrintOptions print_opts;
92   print_opts.set_print_operand_shape(print_operand_shape);
93   StatusOr<bool> filecheck_result =
94       RunFileCheck(optimized_module->ToString(print_opts), pattern);
95   TF_ASSERT_OK(filecheck_result.status());
96   EXPECT_TRUE(filecheck_result.ValueOrDie());
97 }
98 
GetOptimizedModule(absl::string_view hlo)99 StatusOr<std::unique_ptr<HloModule>> LlvmIrGenTestBase::GetOptimizedModule(
100     absl::string_view hlo) {
101   TF_ASSIGN_OR_RETURN(
102       std::unique_ptr<HloModule> module,
103       ParseAndReturnVerifiedModule(hlo, GetModuleConfigForTest()));
104   return backend().compiler()->RunHloPasses(
105       std::move(module), backend().default_stream_executor(),
106       backend().default_stream_executor()->GetAllocator());
107 }
108 
GetOptimizedModule(std::unique_ptr<HloModule> hlo_module)109 StatusOr<std::unique_ptr<HloModule>> LlvmIrGenTestBase::GetOptimizedModule(
110     std::unique_ptr<HloModule> hlo_module) {
111   return backend().compiler()->RunHloPasses(
112       std::move(hlo_module), backend().default_stream_executor(),
113       backend().default_stream_executor()->GetAllocator());
114 }
115 
GetLLVMCompiler()116 LLVMCompiler* LlvmIrGenTestBase::GetLLVMCompiler() {
117   return static_cast<LLVMCompiler*>(backend().compiler());
118 }
119 
IrHook(const llvm::Module & module)120 Status LlvmIrGenTestBase::IrHook(const llvm::Module& module) {
121   ir_ = llvm_ir::DumpModuleToString(module);
122   return OkStatus();
123 }
124 
125 }  // namespace xla
126