1 // Copyright (c) 2018 Google LLC
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 #include <memory>
16 
17 #include "gtest/gtest.h"
18 #include "source/opt/build_module.h"
19 #include "source/opt/constants.h"
20 #include "source/opt/ir_context.h"
21 
22 namespace spvtools {
23 namespace opt {
24 namespace analysis {
25 namespace {
26 
27 using ConstantManagerTest = ::testing::Test;
28 
TEST_F(ConstantManagerTest,GetDefiningInstruction)29 TEST_F(ConstantManagerTest, GetDefiningInstruction) {
30   const std::string text = R"(
31 %int = OpTypeInt 32 0
32 %1 = OpTypeStruct %int
33 %2 = OpTypeStruct %int
34   )";
35 
36   std::unique_ptr<IRContext> context =
37       BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
38                   SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
39   ASSERT_NE(context, nullptr);
40 
41   Type* struct_type_1 = context->get_type_mgr()->GetType(1);
42   StructConstant struct_const_1(struct_type_1->AsStruct());
43   Instruction* const_inst_1 =
44       context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
45   EXPECT_EQ(const_inst_1->type_id(), 1);
46 
47   Type* struct_type_2 = context->get_type_mgr()->GetType(2);
48   StructConstant struct_const_2(struct_type_2->AsStruct());
49   Instruction* const_inst_2 =
50       context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
51   EXPECT_EQ(const_inst_2->type_id(), 2);
52 }
53 
TEST_F(ConstantManagerTest,GetDefiningInstruction2)54 TEST_F(ConstantManagerTest, GetDefiningInstruction2) {
55   const std::string text = R"(
56 %int = OpTypeInt 32 0
57 %1 = OpTypeStruct %int
58 %2 = OpTypeStruct %int
59 %3 = OpConstantNull %1
60 %4 = OpConstantNull %2
61   )";
62 
63   std::unique_ptr<IRContext> context =
64       BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
65                   SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
66   ASSERT_NE(context, nullptr);
67 
68   Type* struct_type_1 = context->get_type_mgr()->GetType(1);
69   NullConstant struct_const_1(struct_type_1->AsStruct());
70   Instruction* const_inst_1 =
71       context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
72   EXPECT_EQ(const_inst_1->type_id(), 1);
73   EXPECT_EQ(const_inst_1->result_id(), 3);
74 
75   Type* struct_type_2 = context->get_type_mgr()->GetType(2);
76   NullConstant struct_const_2(struct_type_2->AsStruct());
77   Instruction* const_inst_2 =
78       context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
79   EXPECT_EQ(const_inst_2->type_id(), 2);
80   EXPECT_EQ(const_inst_2->result_id(), 4);
81 }
82 
TEST_F(ConstantManagerTest,GetDefiningInstructionIdOverflow)83 TEST_F(ConstantManagerTest, GetDefiningInstructionIdOverflow) {
84   const std::string text = R"(
85 %1 = OpTypeInt 32 0
86 %3 = OpConstant %1 1
87 %4 = OpConstant %1 2
88   )";
89 
90   std::unique_ptr<IRContext> context =
91       BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
92                   SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
93   ASSERT_NE(context, nullptr);
94 
95   // Set the id bound to the max, so the new constant cannot be generated.
96   context->module()->SetIdBound(context->max_id_bound());
97 
98   Type* int_type = context->get_type_mgr()->GetType(1);
99   IntConstant int_constant(int_type->AsInteger(), {3});
100   Instruction* inst =
101       context->get_constant_mgr()->GetDefiningInstruction(&int_constant, 1);
102   EXPECT_EQ(inst, nullptr);
103 }
104 
105 }  // namespace
106 }  // namespace analysis
107 }  // namespace opt
108 }  // namespace spvtools
109