1 /* Copyright 2018 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/service/llvm_ir/alias_analysis.h"
17
18 #include <memory>
19 #include <utility>
20
21 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
22 #include "tensorflow/compiler/xla/service/custom_call_target_registry.h"
23 #include "tensorflow/compiler/xla/tests/filecheck.h"
24 #include "tensorflow/core/platform/test.h"
25
26 namespace xla {
27 namespace cpu {
28 namespace {
29 class AliasAnalysisTest : public CpuCodegenTest {};
30
FakeCustomCallTarget(float * out,float ** in)31 void FakeCustomCallTarget(float* out, float** in) {}
32
33 XLA_CPU_REGISTER_CUSTOM_CALL_TARGET(FakeCustomCallTarget);
34
TEST_F(AliasAnalysisTest,EmbeddedComputationParamsMayAliasTemps)35 TEST_F(AliasAnalysisTest, EmbeddedComputationParamsMayAliasTemps) {
36 const char* hlo_string = R"(
37 HloModule while
38
39 body {
40 const.0.125 = f32[] constant(0.125)
41 body.state = f32[] parameter(0)
42 ROOT add.2.2 = f32[] add(const.0.125, body.state)
43 }
44
45 condition {
46 const.100 = f32[] constant(100)
47 condition.state = f32[] parameter(0)
48 addend = f32[] custom-call(condition.state), custom_call_target="FakeCustomCallTarget"
49 add = f32[] add(addend, condition.state)
50 ROOT greater-than = pred[] compare(const.100, add), direction=GT
51 }
52
53 ENTRY while3 {
54 const.0 = f32[] constant(0)
55 ROOT while = f32[] while(const.0), condition=condition, body=body
56 }
57 )";
58
59 CompileAndVerifyIr(hlo_string, R"(
60 ; CHECK-LABEL: @body(i8* %retval
61 ; CHECK: %[[add_result:.*]] = fadd reassoc nsz contract float %[[fadd_lhs:.*]], %[[fadd_rhs:.*]]
62 ; CHECK: store float %[[add_result]], float* %[[store_dest:.*]], align 4, !alias.scope ![[alias_scope_md_for_store:[0-9]+]]
63 ;
64 ; CHECK-LABEL: @condition(i8* %retval, i8* noalias %run_options, i8** noalias %params
65 ; CHECK: %[[cond_state_buf_ptr:.*]] = getelementptr inbounds i8*, i8** %buffer_table, i64 0
66 ; CHECK: %[[cond_state_buf_untyped:.*]] = load i8*, i8** %[[cond_state_buf_ptr]]
67 ; CHECK: %[[cond_state_buf_typed:.*]] = bitcast i8* %[[cond_state_buf_untyped]] to float*
68 ; CHECK: load float, float* %[[cond_state_buf_typed]], align 4, !alias.scope ![[alias_scope_md_for_store]], !noalias ![[noalias_md_for_load:.*]]
69 ;
70 ; CHECK-LABEL: @while3(
71
72 ![[alias_scope_md_for_store]] = !{![[buffer_idx_0:.*]]}
73 ![[buffer_idx_0]] = !{!"buffer: {index:0, offset:0, size:4}", ![[aa_md_root:.*]]}
74 ![[aa_md_root]] = !{!"XLA global AA domain"}
75 ![[buffer_idx_1:.*]] = !{!"buffer: {index:1, offset:0, size:4}", !3}
76 ![[buffer_idx_1_offset_16:.*]] = !{!"buffer: {index:1, offset:16, size:1}", !3}
77 ![[noalias_md_for_load]] = !{![[buffer_idx_1_offset_16]], ![[buffer_idx_1]]}
78 }
79 )");
80 }
81
82 } // namespace
83 } // namespace cpu
84 } // namespace xla
85