1 /* Copyright 2020 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 CpuDynamicShapeTest = CpuCodegenTest;
27
TEST_F(CpuDynamicShapeTest,DynamicShapeR2)28 TEST_F(CpuDynamicShapeTest, DynamicShapeR2) {
29 HloComputation::Builder builder(TestName());
30
31 xla::Shape dyn_input_shape = xla::ShapeUtil::MakeShape(xla::F32, {2, 4});
32 dyn_input_shape.set_dynamic_dimension(0, true);
33 HloInstruction* param_x = builder.AddInstruction(
34 HloInstruction::CreateParameter(0, dyn_input_shape, "x"));
35
36 builder.AddInstruction(HloInstruction::CreateUnary(
37 dyn_input_shape, HloOpcode::kNegate, param_x));
38 auto hlo_module = CreateNewVerifiedModule();
39 hlo_module->AddEntryComputation(builder.Build());
40
41 std::string filecheck_pattern = R"(
42 ; CHECK: %[[dyn_dim_size:.*]] = load i32, ptr
43 ; CHECK: %[[i64_dyn_dim_size:.*]] = sext i32 %[[dyn_dim_size:.*]] to i64
44 ; CHECK: icmp uge i64 %[[custom:.*]], %[[i64_dyn_dim_size:.*]]
45 ; CHECK: %[[multiplier:.*]] = mul i64 1, %[[i64_dyn_dim_size:.*]]
46 ; CHECK: mul nuw nsw i64 %[[custom:.*]], %[[multiplier:.*]]
47 )";
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(hlo_module), options,
56 filecheck_pattern,
57 /*match_optimized_ir=*/false);
58 }
59
60 } // namespace
61 } // namespace cpu
62 } // namespace xla
63