• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::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 std::string& hlo_text,
44                           const std::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 std::string& pattern,
59                                      bool match_optimized_ir);
60 
61   // Compiles the given `hlo` with optimizations, and verifies that optimized
62   // HLO matches the given FileCheck pattern.
63   void MatchOptimizedHlo(absl::string_view hlo, absl::string_view pattern,
64                          bool print_operand_shape = false);
65 
66   // LikeMatchOptimizedHlo, but checks operand shapes as well.
MatchOptimizedHloWithShapes(absl::string_view hlo,absl::string_view pattern)67   void MatchOptimizedHloWithShapes(absl::string_view hlo,
68                                    absl::string_view pattern) {
69     MatchOptimizedHlo(hlo, pattern, /*print_operand_shape=*/true);
70   }
71 
72   // Compiles and returns module with optimizations from a given HLO.
73   StatusOr<std::unique_ptr<HloModule>> GetOptimizedModule(
74       absl::string_view hlo);
75 
76   StatusOr<std::unique_ptr<HloModule>> GetOptimizedModule(
77       std::unique_ptr<HloModule> hlo_module);
78 
79  private:
80   LLVMCompiler* GetLLVMCompiler();
81 
82   void SetIrHook(bool match_optimized_ir);
83   void ResetIrHook();
84 
85   std::string ir_;
86   Status IrHook(const llvm::Module& module);
87 };
88 
89 }  // namespace xla
90 
91 #endif  // TENSORFLOW_COMPILER_XLA_TESTS_LLVM_IRGEN_TEST_BASE_H_
92