1 /* Copyright 2019 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 <algorithm>
17 #include <string>
18
19 #include "absl/strings/ascii.h"
20 #include "absl/strings/str_cat.h"
21 #include "llvm-c/Target.h"
22 #include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
23 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
24 #include "tensorflow/compiler/xla/service/hlo_computation.h"
25 #include "tensorflow/core/platform/test.h"
26
27 namespace xla {
28 namespace cpu {
29 namespace {
30
31 const char* const kTriple_x86_64 = "x86_64-pc-linux";
32 const char* const kTriple_android_arm = "armv7-none-android";
33
34 struct ProfilingTestSpec {
35 absl::string_view triple;
36 absl::string_view check_lines;
37 };
38
39 // Tests that profiling intrinsics get inserted.
40 class CpuProfilingTest
41 : public CpuCodegenTest,
42 public ::testing::WithParamInterface<ProfilingTestSpec> {
43 public:
Name(const::testing::TestParamInfo<ProfilingTestSpec> & info)44 static std::string Name(
45 const ::testing::TestParamInfo<ProfilingTestSpec>& info) {
46 auto spec = info.param;
47
48 std::string triple{spec.triple.data(), spec.triple.size()};
49 if (triple == kTriple_x86_64) {
50 triple = "x86_64";
51 } else if (triple == kTriple_android_arm) {
52 triple = "android_arm";
53 } else {
54 triple = "Unknown";
55 }
56
57 return triple;
58 }
59 };
60
TEST_P(CpuProfilingTest,DoIt)61 TEST_P(CpuProfilingTest, DoIt) {
62 HloComputation::Builder builder(TestName());
63 ProfilingTestSpec spec = GetParam();
64
65 LLVMInitializeX86Target();
66 LLVMInitializeX86TargetInfo();
67 LLVMInitializeX86TargetMC();
68 LLVMInitializeARMTarget();
69 LLVMInitializeARMTargetInfo();
70 LLVMInitializeARMTargetMC();
71
72 std::string triple{spec.triple.data(), spec.triple.size()};
73
74 CpuAotCompilationOptions options{
75 /*triple=*/triple, /*cpu_name=*/"", /*features=*/"",
76 /*entry_point_name=*/"entry",
77 /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};
78
79 constexpr char hlo_text[] = R"(HloModule test
80 ENTRY test {
81 p = f32[1024] parameter(0)
82 ROOT sin = f32[1024] sine(p)
83 })";
84
85 auto config = GetModuleConfigForTest();
86 auto debug_options = config.debug_options();
87 debug_options.set_xla_hlo_profile(true);
88 config.set_debug_options(debug_options);
89 auto hlo_module = ParseAndReturnVerifiedModule(hlo_text, config);
90
91 std::string check_lines{spec.check_lines.data(), spec.check_lines.size()};
92
93 CompileAheadOfTimeAndVerifyIr(std::move(hlo_module).ValueOrDie(), options,
94 check_lines,
95 /*match_optimized_ir=*/true);
96 }
97
98 ProfilingTestSpec CpuProfilingTestCases[] = {
99 ProfilingTestSpec{
100 kTriple_x86_64,
101 R"(CHECK: [[START:%[^ ]*]] = tail call { i64, i32 } @llvm.x86.rdtscp
102 CHECK: extractvalue { i64, i32 } [[START]], 0
103 CHECK: [[STOP:%[^ ]*]] = tail call { i64, i32 } @llvm.x86.rdtscp
104 CHECK: extractvalue { i64, i32 } [[STOP]], 0)"},
105 ProfilingTestSpec{kTriple_android_arm,
106 R"(CHECK: call i64 @llvm.readcyclecounter
107 CHECK: call i64 @llvm.readcyclecounter)"},
108 };
109
110 INSTANTIATE_TEST_SUITE_P(CpuProfilingTestInstantiation, CpuProfilingTest,
111 ::testing::ValuesIn(CpuProfilingTestCases),
112 CpuProfilingTest::Name);
113
114 } // namespace
115 } // namespace cpu
116 } // namespace xla
117