1 /* Copyright 2022 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 <memory>
17 #include <string>
18
19 #include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
20 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
21
22 namespace xla {
23 namespace cpu {
24 namespace {
25
26 // Verifies fix for b/233647273.
TEST_F(CpuCodegenTest,While)27 TEST_F(CpuCodegenTest, While) {
28 const std::string hlo_text = R"(
29 HloModule module
30
31 f1 {
32 f1.p0 = s32[] parameter(0)
33 ROOT f1.sum = s32[] add(f1.p0, f1.p0)
34 }
35
36 f2 {
37 f2.p0 = s32[] parameter(0)
38 f2.p1 = s32[] parameter(1)
39 ROOT f2.sum = s32[] add(f2.p0, f2.p1)
40 }
41
42 body {
43 body.p0 = s32[] parameter(0)
44 sum2 = s32[] fusion(body.p0), kind=kLoop, calls=f1
45 ROOT sum3 = s32[] fusion(sum2, body.p0), kind=kLoop, calls=f2
46 }
47
48 cond {
49 cond.p0 = s32[] parameter(0)
50 cond.c1 = s32[] constant(1)
51 ROOT cond.root = pred[] compare(cond.p0, cond.c1), direction=EQ
52 }
53
54 ENTRY entry {
55 entry.c1 = s32[] constant(1)
56 ROOT entry.root = s32[] while(entry.c1), condition=cond, body=body
57 }
58 )";
59
60 TF_ASSERT_OK_AND_ASSIGN(auto module, ParseAndReturnVerifiedModule(hlo_text));
61
62 // Compile and execute the computation.
63 auto result = ExecuteAndTransfer(module->Clone(), {});
64
65 // Check the output correctness.
66 LiteralTestUtil::ExpectR0Equal(3, result);
67 }
68
69 } // namespace
70 } // namespace cpu
71 } // namespace xla
72