1fd4e5da5Sopenharmony_ci// Copyright (c) 2018 Google LLC
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include <memory>
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ci#include "gtest/gtest.h"
18fd4e5da5Sopenharmony_ci#include "source/opt/build_module.h"
19fd4e5da5Sopenharmony_ci#include "source/opt/constants.h"
20fd4e5da5Sopenharmony_ci#include "source/opt/ir_context.h"
21fd4e5da5Sopenharmony_ci
22fd4e5da5Sopenharmony_cinamespace spvtools {
23fd4e5da5Sopenharmony_cinamespace opt {
24fd4e5da5Sopenharmony_cinamespace analysis {
25fd4e5da5Sopenharmony_cinamespace {
26fd4e5da5Sopenharmony_ci
27fd4e5da5Sopenharmony_ciusing ConstantManagerTest = ::testing::Test;
28fd4e5da5Sopenharmony_ci
29fd4e5da5Sopenharmony_ciTEST_F(ConstantManagerTest, GetDefiningInstruction) {
30fd4e5da5Sopenharmony_ci  const std::string text = R"(
31fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 0
32fd4e5da5Sopenharmony_ci%1 = OpTypeStruct %int
33fd4e5da5Sopenharmony_ci%2 = OpTypeStruct %int
34fd4e5da5Sopenharmony_ci  )";
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
37fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
38fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
39fd4e5da5Sopenharmony_ci  ASSERT_NE(context, nullptr);
40fd4e5da5Sopenharmony_ci
41fd4e5da5Sopenharmony_ci  Type* struct_type_1 = context->get_type_mgr()->GetType(1);
42fd4e5da5Sopenharmony_ci  StructConstant struct_const_1(struct_type_1->AsStruct());
43fd4e5da5Sopenharmony_ci  Instruction* const_inst_1 =
44fd4e5da5Sopenharmony_ci      context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
45fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_1->type_id(), 1);
46fd4e5da5Sopenharmony_ci
47fd4e5da5Sopenharmony_ci  Type* struct_type_2 = context->get_type_mgr()->GetType(2);
48fd4e5da5Sopenharmony_ci  StructConstant struct_const_2(struct_type_2->AsStruct());
49fd4e5da5Sopenharmony_ci  Instruction* const_inst_2 =
50fd4e5da5Sopenharmony_ci      context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
51fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_2->type_id(), 2);
52fd4e5da5Sopenharmony_ci}
53fd4e5da5Sopenharmony_ci
54fd4e5da5Sopenharmony_ciTEST_F(ConstantManagerTest, GetDefiningInstruction2) {
55fd4e5da5Sopenharmony_ci  const std::string text = R"(
56fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 0
57fd4e5da5Sopenharmony_ci%1 = OpTypeStruct %int
58fd4e5da5Sopenharmony_ci%2 = OpTypeStruct %int
59fd4e5da5Sopenharmony_ci%3 = OpConstantNull %1
60fd4e5da5Sopenharmony_ci%4 = OpConstantNull %2
61fd4e5da5Sopenharmony_ci  )";
62fd4e5da5Sopenharmony_ci
63fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
64fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
65fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
66fd4e5da5Sopenharmony_ci  ASSERT_NE(context, nullptr);
67fd4e5da5Sopenharmony_ci
68fd4e5da5Sopenharmony_ci  Type* struct_type_1 = context->get_type_mgr()->GetType(1);
69fd4e5da5Sopenharmony_ci  NullConstant struct_const_1(struct_type_1->AsStruct());
70fd4e5da5Sopenharmony_ci  Instruction* const_inst_1 =
71fd4e5da5Sopenharmony_ci      context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
72fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_1->type_id(), 1);
73fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_1->result_id(), 3);
74fd4e5da5Sopenharmony_ci
75fd4e5da5Sopenharmony_ci  Type* struct_type_2 = context->get_type_mgr()->GetType(2);
76fd4e5da5Sopenharmony_ci  NullConstant struct_const_2(struct_type_2->AsStruct());
77fd4e5da5Sopenharmony_ci  Instruction* const_inst_2 =
78fd4e5da5Sopenharmony_ci      context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
79fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_2->type_id(), 2);
80fd4e5da5Sopenharmony_ci  EXPECT_EQ(const_inst_2->result_id(), 4);
81fd4e5da5Sopenharmony_ci}
82fd4e5da5Sopenharmony_ci
83fd4e5da5Sopenharmony_ciTEST_F(ConstantManagerTest, GetDefiningInstructionIdOverflow) {
84fd4e5da5Sopenharmony_ci  const std::string text = R"(
85fd4e5da5Sopenharmony_ci%1 = OpTypeInt 32 0
86fd4e5da5Sopenharmony_ci%3 = OpConstant %1 1
87fd4e5da5Sopenharmony_ci%4 = OpConstant %1 2
88fd4e5da5Sopenharmony_ci  )";
89fd4e5da5Sopenharmony_ci
90fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
91fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
92fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
93fd4e5da5Sopenharmony_ci  ASSERT_NE(context, nullptr);
94fd4e5da5Sopenharmony_ci
95fd4e5da5Sopenharmony_ci  // Set the id bound to the max, so the new constant cannot be generated.
96fd4e5da5Sopenharmony_ci  context->module()->SetIdBound(context->max_id_bound());
97fd4e5da5Sopenharmony_ci
98fd4e5da5Sopenharmony_ci  Type* int_type = context->get_type_mgr()->GetType(1);
99fd4e5da5Sopenharmony_ci  IntConstant int_constant(int_type->AsInteger(), {3});
100fd4e5da5Sopenharmony_ci  Instruction* inst =
101fd4e5da5Sopenharmony_ci      context->get_constant_mgr()->GetDefiningInstruction(&int_constant, 1);
102fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst, nullptr);
103fd4e5da5Sopenharmony_ci}
104fd4e5da5Sopenharmony_ci
105fd4e5da5Sopenharmony_ci}  // namespace
106fd4e5da5Sopenharmony_ci}  // namespace analysis
107fd4e5da5Sopenharmony_ci}  // namespace opt
108fd4e5da5Sopenharmony_ci}  // namespace spvtools
109