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 <memory>
17
18 #include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
19 #include "tensorflow/compiler/xla/service/cpu/test_target_triple_helper.h"
20 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
21
22 namespace xla {
23 namespace cpu {
24 namespace {
25
26 using CpuOutfeedTest = CpuCodegenTest;
27
TEST_F(CpuOutfeedTest,OutfeedRoot)28 TEST_F(CpuOutfeedTest, OutfeedRoot) {
29 const std::string hlo_text = R"(
30 HloModule Outfeed
31
32 ENTRY main {
33 const_a = f32[2,3,2] constant(
34 {{{1, 2}, {1001, 1002}, {2001, 2002}},
35 {{2, 1}, {2001, 3002}, {2001, 2002}}})
36
37 token0 = token[] after-all()
38 outfeed = token[] outfeed(f32[2,3,2] const_a, token0)
39 ROOT root = () tuple()
40 }
41 )";
42
43 std::string filecheck_pattern = R"(
44 CHECK: private unnamed_addr constant [48 x i8]
45 )";
46
47 TF_ASSERT_OK_AND_ASSIGN(auto module, ParseAndReturnVerifiedModule(hlo_text));
48
49 CpuAotCompilationOptions options{
50 /*triple=*/kTargetTripleForHost, /*cpu_name=*/kTargetCpuForHost,
51 /*features=*/"",
52 /*entry_point_name=*/"entry",
53 /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};
54
55 CompileAheadOfTimeAndVerifyIr(std::move(module), options, filecheck_pattern,
56 /*match_optimized_ir=*/false);
57 }
58
TEST_F(CpuOutfeedTest,OutfeedEmpty)59 TEST_F(CpuOutfeedTest, OutfeedEmpty) {
60 const std::string hlo_text = R"(
61 HloModule Outfeed
62
63 ENTRY main {
64 const_a = f32[2,0] constant({{}, {}})
65 token0 = token[] after-all()
66 outfeed = token[] outfeed(f32[2,0] const_a, token0)
67 ROOT root = () tuple()
68 }
69 )";
70
71 std::string filecheck_pattern = R"(
72 CHECK: private unnamed_addr constant [0 x i8]
73 )";
74
75 TF_ASSERT_OK_AND_ASSIGN(auto module, ParseAndReturnVerifiedModule(hlo_text));
76
77 CpuAotCompilationOptions options{
78 /*triple=*/kTargetTripleForHost, /*cpu_name=*/kTargetCpuForHost,
79 /*features=*/"",
80 /*entry_point_name=*/"entry",
81 /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};
82
83 CompileAheadOfTimeAndVerifyIr(std::move(module), options, filecheck_pattern,
84 /*match_optimized_ir=*/false);
85 }
86
TEST_F(CpuOutfeedTest,OutfeedTokenInTuple)87 TEST_F(CpuOutfeedTest, OutfeedTokenInTuple) {
88 const std::string hlo_text = R"(
89 HloModule OutfeedTokenInTuple
90
91 ENTRY main {
92 const = f32[] constant(42)
93 epoch = token[] after-all()
94 outfeed.tok = token[] outfeed(const, epoch)
95 ROOT root = (token[], f32[]) tuple(outfeed.tok, const)
96 }
97 )";
98
99 std::string filecheck_pattern = R"(
100 CHECK: Outfeed
101 )";
102
103 TF_ASSERT_OK_AND_ASSIGN(auto module, ParseAndReturnVerifiedModule(hlo_text));
104
105 CpuAotCompilationOptions options{
106 /*triple=*/kTargetTripleForHost, /*cpu_name=*/kTargetCpuForHost,
107 /*features=*/"",
108 /*entry_point_name=*/"entry",
109 /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};
110
111 CompileAheadOfTimeAndVerifyIr(std::move(module), options, filecheck_pattern,
112 /*match_optimized_ir=*/false);
113 }
114 } // namespace
115 } // namespace cpu
116 } // namespace xla
117