1fd4e5da5Sopenharmony_ci// Copyright (c) 2017 Google Inc.
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 "source/opt/ir_context.h"
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ci#include <algorithm>
18fd4e5da5Sopenharmony_ci#include <memory>
19fd4e5da5Sopenharmony_ci#include <utility>
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_ci#include "OpenCLDebugInfo100.h"
22fd4e5da5Sopenharmony_ci#include "gmock/gmock.h"
23fd4e5da5Sopenharmony_ci#include "gtest/gtest.h"
24fd4e5da5Sopenharmony_ci#include "source/opt/pass.h"
25fd4e5da5Sopenharmony_ci#include "test/opt/pass_fixture.h"
26fd4e5da5Sopenharmony_ci#include "test/opt/pass_utils.h"
27fd4e5da5Sopenharmony_ci
28fd4e5da5Sopenharmony_cistatic const uint32_t kDebugDeclareOperandVariableIndex = 5;
29fd4e5da5Sopenharmony_cistatic const uint32_t kDebugValueOperandValueIndex = 5;
30fd4e5da5Sopenharmony_ci
31fd4e5da5Sopenharmony_cinamespace spvtools {
32fd4e5da5Sopenharmony_cinamespace opt {
33fd4e5da5Sopenharmony_cinamespace {
34fd4e5da5Sopenharmony_ci
35fd4e5da5Sopenharmony_ciusing Analysis = IRContext::Analysis;
36fd4e5da5Sopenharmony_ciusing ::testing::Each;
37fd4e5da5Sopenharmony_ciusing ::testing::UnorderedElementsAre;
38fd4e5da5Sopenharmony_ci
39fd4e5da5Sopenharmony_ciclass NoopPassPreservesNothing : public Pass {
40fd4e5da5Sopenharmony_ci public:
41fd4e5da5Sopenharmony_ci  NoopPassPreservesNothing(Status s) : Pass(), status_to_return_(s) {}
42fd4e5da5Sopenharmony_ci
43fd4e5da5Sopenharmony_ci  const char* name() const override { return "noop-pass"; }
44fd4e5da5Sopenharmony_ci  Status Process() override { return status_to_return_; }
45fd4e5da5Sopenharmony_ci
46fd4e5da5Sopenharmony_ci private:
47fd4e5da5Sopenharmony_ci  Status status_to_return_;
48fd4e5da5Sopenharmony_ci};
49fd4e5da5Sopenharmony_ci
50fd4e5da5Sopenharmony_ciclass NoopPassPreservesAll : public Pass {
51fd4e5da5Sopenharmony_ci public:
52fd4e5da5Sopenharmony_ci  NoopPassPreservesAll(Status s) : Pass(), status_to_return_(s) {}
53fd4e5da5Sopenharmony_ci
54fd4e5da5Sopenharmony_ci  const char* name() const override { return "noop-pass"; }
55fd4e5da5Sopenharmony_ci  Status Process() override { return status_to_return_; }
56fd4e5da5Sopenharmony_ci
57fd4e5da5Sopenharmony_ci  Analysis GetPreservedAnalyses() override {
58fd4e5da5Sopenharmony_ci    return Analysis(IRContext::kAnalysisEnd - 1);
59fd4e5da5Sopenharmony_ci  }
60fd4e5da5Sopenharmony_ci
61fd4e5da5Sopenharmony_ci private:
62fd4e5da5Sopenharmony_ci  Status status_to_return_;
63fd4e5da5Sopenharmony_ci};
64fd4e5da5Sopenharmony_ci
65fd4e5da5Sopenharmony_ciclass NoopPassPreservesFirst : public Pass {
66fd4e5da5Sopenharmony_ci public:
67fd4e5da5Sopenharmony_ci  NoopPassPreservesFirst(Status s) : Pass(), status_to_return_(s) {}
68fd4e5da5Sopenharmony_ci
69fd4e5da5Sopenharmony_ci  const char* name() const override { return "noop-pass"; }
70fd4e5da5Sopenharmony_ci  Status Process() override { return status_to_return_; }
71fd4e5da5Sopenharmony_ci
72fd4e5da5Sopenharmony_ci  Analysis GetPreservedAnalyses() override { return IRContext::kAnalysisBegin; }
73fd4e5da5Sopenharmony_ci
74fd4e5da5Sopenharmony_ci private:
75fd4e5da5Sopenharmony_ci  Status status_to_return_;
76fd4e5da5Sopenharmony_ci};
77fd4e5da5Sopenharmony_ci
78fd4e5da5Sopenharmony_ciusing IRContextTest = PassTest<::testing::Test>;
79fd4e5da5Sopenharmony_ci
80fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, IndividualValidAfterBuild) {
81fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module(new Module());
82fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
83fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
84fd4e5da5Sopenharmony_ci
85fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
86fd4e5da5Sopenharmony_ci       i <<= 1) {
87fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
88fd4e5da5Sopenharmony_ci    EXPECT_TRUE(localContext.AreAnalysesValid(i));
89fd4e5da5Sopenharmony_ci  }
90fd4e5da5Sopenharmony_ci}
91fd4e5da5Sopenharmony_ci
92fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, DontRebuildValidAnalysis) {
93fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module(new Module());
94fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
95fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
96fd4e5da5Sopenharmony_ci
97fd4e5da5Sopenharmony_ci  auto* oldCfg = localContext.cfg();
98fd4e5da5Sopenharmony_ci  auto* oldDefUse = localContext.get_def_use_mgr();
99fd4e5da5Sopenharmony_ci  localContext.BuildInvalidAnalyses(IRContext::kAnalysisCFG |
100fd4e5da5Sopenharmony_ci                                    IRContext::kAnalysisDefUse);
101fd4e5da5Sopenharmony_ci  auto* newCfg = localContext.cfg();
102fd4e5da5Sopenharmony_ci  auto* newDefUse = localContext.get_def_use_mgr();
103fd4e5da5Sopenharmony_ci  EXPECT_EQ(oldCfg, newCfg);
104fd4e5da5Sopenharmony_ci  EXPECT_EQ(oldDefUse, newDefUse);
105fd4e5da5Sopenharmony_ci}
106fd4e5da5Sopenharmony_ci
107fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, AllValidAfterBuild) {
108fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module = MakeUnique<Module>();
109fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
110fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
111fd4e5da5Sopenharmony_ci
112fd4e5da5Sopenharmony_ci  Analysis built_analyses = IRContext::kAnalysisNone;
113fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
114fd4e5da5Sopenharmony_ci       i <<= 1) {
115fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
116fd4e5da5Sopenharmony_ci    built_analyses |= i;
117fd4e5da5Sopenharmony_ci  }
118fd4e5da5Sopenharmony_ci  EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
119fd4e5da5Sopenharmony_ci}
120fd4e5da5Sopenharmony_ci
121fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, AllValidAfterPassNoChange) {
122fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module = MakeUnique<Module>();
123fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
124fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
125fd4e5da5Sopenharmony_ci
126fd4e5da5Sopenharmony_ci  Analysis built_analyses = IRContext::kAnalysisNone;
127fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
128fd4e5da5Sopenharmony_ci       i <<= 1) {
129fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
130fd4e5da5Sopenharmony_ci    built_analyses |= i;
131fd4e5da5Sopenharmony_ci  }
132fd4e5da5Sopenharmony_ci
133fd4e5da5Sopenharmony_ci  NoopPassPreservesNothing pass(Pass::Status::SuccessWithoutChange);
134fd4e5da5Sopenharmony_ci  Pass::Status s = pass.Run(&localContext);
135fd4e5da5Sopenharmony_ci  EXPECT_EQ(s, Pass::Status::SuccessWithoutChange);
136fd4e5da5Sopenharmony_ci  EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
137fd4e5da5Sopenharmony_ci}
138fd4e5da5Sopenharmony_ci
139fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, NoneValidAfterPassWithChange) {
140fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module = MakeUnique<Module>();
141fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
142fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
143fd4e5da5Sopenharmony_ci
144fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
145fd4e5da5Sopenharmony_ci       i <<= 1) {
146fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
147fd4e5da5Sopenharmony_ci  }
148fd4e5da5Sopenharmony_ci
149fd4e5da5Sopenharmony_ci  NoopPassPreservesNothing pass(Pass::Status::SuccessWithChange);
150fd4e5da5Sopenharmony_ci  Pass::Status s = pass.Run(&localContext);
151fd4e5da5Sopenharmony_ci  EXPECT_EQ(s, Pass::Status::SuccessWithChange);
152fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
153fd4e5da5Sopenharmony_ci       i <<= 1) {
154fd4e5da5Sopenharmony_ci    EXPECT_FALSE(localContext.AreAnalysesValid(i));
155fd4e5da5Sopenharmony_ci  }
156fd4e5da5Sopenharmony_ci}
157fd4e5da5Sopenharmony_ci
158fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, AllPreservedAfterPassWithChange) {
159fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module = MakeUnique<Module>();
160fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
161fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
162fd4e5da5Sopenharmony_ci
163fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
164fd4e5da5Sopenharmony_ci       i <<= 1) {
165fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
166fd4e5da5Sopenharmony_ci  }
167fd4e5da5Sopenharmony_ci
168fd4e5da5Sopenharmony_ci  NoopPassPreservesAll pass(Pass::Status::SuccessWithChange);
169fd4e5da5Sopenharmony_ci  Pass::Status s = pass.Run(&localContext);
170fd4e5da5Sopenharmony_ci  EXPECT_EQ(s, Pass::Status::SuccessWithChange);
171fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
172fd4e5da5Sopenharmony_ci       i <<= 1) {
173fd4e5da5Sopenharmony_ci    EXPECT_TRUE(localContext.AreAnalysesValid(i));
174fd4e5da5Sopenharmony_ci  }
175fd4e5da5Sopenharmony_ci}
176fd4e5da5Sopenharmony_ci
177fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, PreserveFirstOnlyAfterPassWithChange) {
178fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module = MakeUnique<Module>();
179fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
180fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
181fd4e5da5Sopenharmony_ci
182fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
183fd4e5da5Sopenharmony_ci       i <<= 1) {
184fd4e5da5Sopenharmony_ci    localContext.BuildInvalidAnalyses(i);
185fd4e5da5Sopenharmony_ci  }
186fd4e5da5Sopenharmony_ci
187fd4e5da5Sopenharmony_ci  NoopPassPreservesFirst pass(Pass::Status::SuccessWithChange);
188fd4e5da5Sopenharmony_ci  Pass::Status s = pass.Run(&localContext);
189fd4e5da5Sopenharmony_ci  EXPECT_EQ(s, Pass::Status::SuccessWithChange);
190fd4e5da5Sopenharmony_ci  EXPECT_TRUE(localContext.AreAnalysesValid(IRContext::kAnalysisBegin));
191fd4e5da5Sopenharmony_ci  for (Analysis i = IRContext::kAnalysisBegin << 1; i < IRContext::kAnalysisEnd;
192fd4e5da5Sopenharmony_ci       i <<= 1) {
193fd4e5da5Sopenharmony_ci    EXPECT_FALSE(localContext.AreAnalysesValid(i));
194fd4e5da5Sopenharmony_ci  }
195fd4e5da5Sopenharmony_ci}
196fd4e5da5Sopenharmony_ci
197fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillMemberName) {
198fd4e5da5Sopenharmony_ci  const std::string text = R"(
199fd4e5da5Sopenharmony_ci              OpCapability Shader
200fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
201fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
202fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
203fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
204fd4e5da5Sopenharmony_ci               OpSource GLSL 430
205fd4e5da5Sopenharmony_ci               OpName %3 "stuff"
206fd4e5da5Sopenharmony_ci               OpMemberName %3 0 "refZ"
207fd4e5da5Sopenharmony_ci               OpMemberDecorate %3 0 Offset 0
208fd4e5da5Sopenharmony_ci               OpDecorate %3 Block
209fd4e5da5Sopenharmony_ci          %4 = OpTypeFloat 32
210fd4e5da5Sopenharmony_ci          %3 = OpTypeStruct %4
211fd4e5da5Sopenharmony_ci          %5 = OpTypeVoid
212fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %5
213fd4e5da5Sopenharmony_ci          %2 = OpFunction %5 None %6
214fd4e5da5Sopenharmony_ci          %7 = OpLabel
215fd4e5da5Sopenharmony_ci               OpReturn
216fd4e5da5Sopenharmony_ci               OpFunctionEnd
217fd4e5da5Sopenharmony_ci)";
218fd4e5da5Sopenharmony_ci
219fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
220fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
221fd4e5da5Sopenharmony_ci
222fd4e5da5Sopenharmony_ci  // Build the decoration manager.
223fd4e5da5Sopenharmony_ci  context->get_decoration_mgr();
224fd4e5da5Sopenharmony_ci
225fd4e5da5Sopenharmony_ci  // Delete the OpTypeStruct.  Should delete the OpName, OpMemberName, and
226fd4e5da5Sopenharmony_ci  // OpMemberDecorate associated with it.
227fd4e5da5Sopenharmony_ci  context->KillDef(3);
228fd4e5da5Sopenharmony_ci
229fd4e5da5Sopenharmony_ci  // Make sure all of the name are removed.
230fd4e5da5Sopenharmony_ci  for (auto& inst : context->debugs2()) {
231fd4e5da5Sopenharmony_ci    EXPECT_EQ(inst.opcode(), spv::Op::OpNop);
232fd4e5da5Sopenharmony_ci  }
233fd4e5da5Sopenharmony_ci
234fd4e5da5Sopenharmony_ci  // Make sure all of the decorations are removed.
235fd4e5da5Sopenharmony_ci  for (auto& inst : context->annotations()) {
236fd4e5da5Sopenharmony_ci    EXPECT_EQ(inst.opcode(), spv::Op::OpNop);
237fd4e5da5Sopenharmony_ci  }
238fd4e5da5Sopenharmony_ci}
239fd4e5da5Sopenharmony_ci
240fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillGroupDecoration) {
241fd4e5da5Sopenharmony_ci  const std::string text = R"(
242fd4e5da5Sopenharmony_ci               OpCapability Shader
243fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
244fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
245fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
246fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
247fd4e5da5Sopenharmony_ci               OpSource GLSL 430
248fd4e5da5Sopenharmony_ci               OpDecorate %3 Restrict
249fd4e5da5Sopenharmony_ci          %3 = OpDecorationGroup
250fd4e5da5Sopenharmony_ci               OpGroupDecorate %3 %4 %5
251fd4e5da5Sopenharmony_ci          %6 = OpTypeFloat 32
252fd4e5da5Sopenharmony_ci          %7 = OpTypePointer Function %6
253fd4e5da5Sopenharmony_ci          %8 = OpTypeStruct %6
254fd4e5da5Sopenharmony_ci          %9 = OpTypeVoid
255fd4e5da5Sopenharmony_ci         %10 = OpTypeFunction %9
256fd4e5da5Sopenharmony_ci          %2 = OpFunction %9 None %10
257fd4e5da5Sopenharmony_ci         %11 = OpLabel
258fd4e5da5Sopenharmony_ci          %4 = OpVariable %7 Function
259fd4e5da5Sopenharmony_ci          %5 = OpVariable %7 Function
260fd4e5da5Sopenharmony_ci               OpReturn
261fd4e5da5Sopenharmony_ci               OpFunctionEnd
262fd4e5da5Sopenharmony_ci)";
263fd4e5da5Sopenharmony_ci
264fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
265fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
266fd4e5da5Sopenharmony_ci
267fd4e5da5Sopenharmony_ci  // Build the decoration manager.
268fd4e5da5Sopenharmony_ci  context->get_decoration_mgr();
269fd4e5da5Sopenharmony_ci
270fd4e5da5Sopenharmony_ci  // Delete the second variable.
271fd4e5da5Sopenharmony_ci  context->KillDef(5);
272fd4e5da5Sopenharmony_ci
273fd4e5da5Sopenharmony_ci  // The three decorations instructions should still be there.  The first two
274fd4e5da5Sopenharmony_ci  // should be the same, but the third should have %5 removed.
275fd4e5da5Sopenharmony_ci
276fd4e5da5Sopenharmony_ci  // Check the OpDecorate instruction
277fd4e5da5Sopenharmony_ci  auto inst = context->annotation_begin();
278fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->opcode(), spv::Op::OpDecorate);
279fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
280fd4e5da5Sopenharmony_ci
281fd4e5da5Sopenharmony_ci  // Check the OpDecorationGroup Instruction
282fd4e5da5Sopenharmony_ci  ++inst;
283fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->opcode(), spv::Op::OpDecorationGroup);
284fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->result_id(), 3);
285fd4e5da5Sopenharmony_ci
286fd4e5da5Sopenharmony_ci  // Check that %5 is no longer part of the group.
287fd4e5da5Sopenharmony_ci  ++inst;
288fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->opcode(), spv::Op::OpGroupDecorate);
289fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->NumInOperands(), 2);
290fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
291fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->GetSingleWordInOperand(1), 4);
292fd4e5da5Sopenharmony_ci
293fd4e5da5Sopenharmony_ci  // Check that we are at the end.
294fd4e5da5Sopenharmony_ci  ++inst;
295fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst, context->annotation_end());
296fd4e5da5Sopenharmony_ci}
297fd4e5da5Sopenharmony_ci
298fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, TakeNextUniqueIdIncrementing) {
299fd4e5da5Sopenharmony_ci  const uint32_t NUM_TESTS = 1000;
300fd4e5da5Sopenharmony_ci  IRContext localContext(SPV_ENV_UNIVERSAL_1_2, nullptr);
301fd4e5da5Sopenharmony_ci  for (uint32_t i = 1; i < NUM_TESTS; ++i)
302fd4e5da5Sopenharmony_ci    EXPECT_EQ(i, localContext.TakeNextUniqueId());
303fd4e5da5Sopenharmony_ci}
304fd4e5da5Sopenharmony_ci
305fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillGroupDecorationWitNoDecorations) {
306fd4e5da5Sopenharmony_ci  const std::string text = R"(
307fd4e5da5Sopenharmony_ci               OpCapability Shader
308fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
309fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
310fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
311fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
312fd4e5da5Sopenharmony_ci               OpSource GLSL 430
313fd4e5da5Sopenharmony_ci          %3 = OpDecorationGroup
314fd4e5da5Sopenharmony_ci               OpGroupDecorate %3 %4 %5
315fd4e5da5Sopenharmony_ci          %6 = OpTypeFloat 32
316fd4e5da5Sopenharmony_ci          %7 = OpTypePointer Function %6
317fd4e5da5Sopenharmony_ci          %8 = OpTypeStruct %6
318fd4e5da5Sopenharmony_ci          %9 = OpTypeVoid
319fd4e5da5Sopenharmony_ci         %10 = OpTypeFunction %9
320fd4e5da5Sopenharmony_ci          %2 = OpFunction %9 None %10
321fd4e5da5Sopenharmony_ci         %11 = OpLabel
322fd4e5da5Sopenharmony_ci          %4 = OpVariable %7 Function
323fd4e5da5Sopenharmony_ci          %5 = OpVariable %7 Function
324fd4e5da5Sopenharmony_ci               OpReturn
325fd4e5da5Sopenharmony_ci               OpFunctionEnd
326fd4e5da5Sopenharmony_ci)";
327fd4e5da5Sopenharmony_ci
328fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
329fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
330fd4e5da5Sopenharmony_ci
331fd4e5da5Sopenharmony_ci  // Build the decoration manager.
332fd4e5da5Sopenharmony_ci  context->get_decoration_mgr();
333fd4e5da5Sopenharmony_ci
334fd4e5da5Sopenharmony_ci  // Delete the second variable.
335fd4e5da5Sopenharmony_ci  context->KillDef(5);
336fd4e5da5Sopenharmony_ci
337fd4e5da5Sopenharmony_ci  // The two decoration instructions should still be there.  The first one
338fd4e5da5Sopenharmony_ci  // should be the same, but the second should have %5 removed.
339fd4e5da5Sopenharmony_ci
340fd4e5da5Sopenharmony_ci  // Check the OpDecorationGroup Instruction
341fd4e5da5Sopenharmony_ci  auto inst = context->annotation_begin();
342fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->opcode(), spv::Op::OpDecorationGroup);
343fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->result_id(), 3);
344fd4e5da5Sopenharmony_ci
345fd4e5da5Sopenharmony_ci  // Check that %5 is no longer part of the group.
346fd4e5da5Sopenharmony_ci  ++inst;
347fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->opcode(), spv::Op::OpGroupDecorate);
348fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->NumInOperands(), 2);
349fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
350fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst->GetSingleWordInOperand(1), 4);
351fd4e5da5Sopenharmony_ci
352fd4e5da5Sopenharmony_ci  // Check that we are at the end.
353fd4e5da5Sopenharmony_ci  ++inst;
354fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst, context->annotation_end());
355fd4e5da5Sopenharmony_ci}
356fd4e5da5Sopenharmony_ci
357fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillDecorationGroup) {
358fd4e5da5Sopenharmony_ci  const std::string text = R"(
359fd4e5da5Sopenharmony_ci               OpCapability Shader
360fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
361fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
362fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
363fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
364fd4e5da5Sopenharmony_ci               OpSource GLSL 430
365fd4e5da5Sopenharmony_ci          %3 = OpDecorationGroup
366fd4e5da5Sopenharmony_ci               OpGroupDecorate %3 %4 %5
367fd4e5da5Sopenharmony_ci          %6 = OpTypeFloat 32
368fd4e5da5Sopenharmony_ci          %7 = OpTypePointer Function %6
369fd4e5da5Sopenharmony_ci          %8 = OpTypeStruct %6
370fd4e5da5Sopenharmony_ci          %9 = OpTypeVoid
371fd4e5da5Sopenharmony_ci         %10 = OpTypeFunction %9
372fd4e5da5Sopenharmony_ci          %2 = OpFunction %9 None %10
373fd4e5da5Sopenharmony_ci         %11 = OpLabel
374fd4e5da5Sopenharmony_ci          %4 = OpVariable %7 Function
375fd4e5da5Sopenharmony_ci          %5 = OpVariable %7 Function
376fd4e5da5Sopenharmony_ci               OpReturn
377fd4e5da5Sopenharmony_ci               OpFunctionEnd
378fd4e5da5Sopenharmony_ci)";
379fd4e5da5Sopenharmony_ci
380fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
381fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
382fd4e5da5Sopenharmony_ci
383fd4e5da5Sopenharmony_ci  // Build the decoration manager.
384fd4e5da5Sopenharmony_ci  context->get_decoration_mgr();
385fd4e5da5Sopenharmony_ci
386fd4e5da5Sopenharmony_ci  // Delete the second variable.
387fd4e5da5Sopenharmony_ci  context->KillDef(3);
388fd4e5da5Sopenharmony_ci
389fd4e5da5Sopenharmony_ci  // Check the OpDecorationGroup Instruction is still there.
390fd4e5da5Sopenharmony_ci  EXPECT_TRUE(context->annotations().empty());
391fd4e5da5Sopenharmony_ci}
392fd4e5da5Sopenharmony_ci
393fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillFunctionFromDebugFunction) {
394fd4e5da5Sopenharmony_ci  const std::string text = R"(
395fd4e5da5Sopenharmony_ci               OpCapability Shader
396fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "OpenCL.DebugInfo.100"
397fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
398fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
399fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
400fd4e5da5Sopenharmony_ci          %3 = OpString "ps.hlsl"
401fd4e5da5Sopenharmony_ci          %4 = OpString "foo"
402fd4e5da5Sopenharmony_ci               OpSource HLSL 600
403fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
404fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
405fd4e5da5Sopenharmony_ci          %7 = OpExtInst %void %1 DebugSource %3
406fd4e5da5Sopenharmony_ci          %8 = OpExtInst %void %1 DebugCompilationUnit 1 4 %7 HLSL
407fd4e5da5Sopenharmony_ci          %9 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
408fd4e5da5Sopenharmony_ci         %10 = OpExtInst %void %1 DebugFunction %4 %9 %7 1 1 %8 %4 FlagIsProtected|FlagIsPrivate 1 %11
409fd4e5da5Sopenharmony_ci          %2 = OpFunction %void None %6
410fd4e5da5Sopenharmony_ci         %12 = OpLabel
411fd4e5da5Sopenharmony_ci               OpReturn
412fd4e5da5Sopenharmony_ci               OpFunctionEnd
413fd4e5da5Sopenharmony_ci         %11 = OpFunction %void None %6
414fd4e5da5Sopenharmony_ci         %13 = OpLabel
415fd4e5da5Sopenharmony_ci               OpReturn
416fd4e5da5Sopenharmony_ci               OpFunctionEnd
417fd4e5da5Sopenharmony_ci)";
418fd4e5da5Sopenharmony_ci
419fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
420fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
421fd4e5da5Sopenharmony_ci
422fd4e5da5Sopenharmony_ci  // Delete the second variable.
423fd4e5da5Sopenharmony_ci  context->KillDef(11);
424fd4e5da5Sopenharmony_ci
425fd4e5da5Sopenharmony_ci  // Get DebugInfoNone id.
426fd4e5da5Sopenharmony_ci  uint32_t debug_info_none_id = 0;
427fd4e5da5Sopenharmony_ci  for (auto it = context->ext_inst_debuginfo_begin();
428fd4e5da5Sopenharmony_ci       it != context->ext_inst_debuginfo_end(); ++it) {
429fd4e5da5Sopenharmony_ci    if (it->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugInfoNone) {
430fd4e5da5Sopenharmony_ci      debug_info_none_id = it->result_id();
431fd4e5da5Sopenharmony_ci    }
432fd4e5da5Sopenharmony_ci  }
433fd4e5da5Sopenharmony_ci  EXPECT_NE(0, debug_info_none_id);
434fd4e5da5Sopenharmony_ci
435fd4e5da5Sopenharmony_ci  // Check the Function operand of DebugFunction is DebugInfoNone.
436fd4e5da5Sopenharmony_ci  const uint32_t kDebugFunctionOperandFunctionIndex = 13;
437fd4e5da5Sopenharmony_ci  bool checked = false;
438fd4e5da5Sopenharmony_ci  for (auto it = context->ext_inst_debuginfo_begin();
439fd4e5da5Sopenharmony_ci       it != context->ext_inst_debuginfo_end(); ++it) {
440fd4e5da5Sopenharmony_ci    if (it->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugFunction) {
441fd4e5da5Sopenharmony_ci      EXPECT_FALSE(checked);
442fd4e5da5Sopenharmony_ci      EXPECT_EQ(it->GetOperand(kDebugFunctionOperandFunctionIndex).words[0],
443fd4e5da5Sopenharmony_ci                debug_info_none_id);
444fd4e5da5Sopenharmony_ci      checked = true;
445fd4e5da5Sopenharmony_ci    }
446fd4e5da5Sopenharmony_ci  }
447fd4e5da5Sopenharmony_ci  EXPECT_TRUE(checked);
448fd4e5da5Sopenharmony_ci}
449fd4e5da5Sopenharmony_ci
450fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, KillVariableFromDebugGlobalVariable) {
451fd4e5da5Sopenharmony_ci  const std::string text = R"(
452fd4e5da5Sopenharmony_ci               OpCapability Shader
453fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "OpenCL.DebugInfo.100"
454fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
455fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %2 "main"
456fd4e5da5Sopenharmony_ci               OpExecutionMode %2 OriginUpperLeft
457fd4e5da5Sopenharmony_ci          %3 = OpString "ps.hlsl"
458fd4e5da5Sopenharmony_ci          %4 = OpString "foo"
459fd4e5da5Sopenharmony_ci          %5 = OpString "int"
460fd4e5da5Sopenharmony_ci               OpSource HLSL 600
461fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
462fd4e5da5Sopenharmony_ci    %uint_32 = OpConstant %uint 32
463fd4e5da5Sopenharmony_ci%_ptr_Private_uint = OpTypePointer Private %uint
464fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
465fd4e5da5Sopenharmony_ci         %10 = OpTypeFunction %void
466fd4e5da5Sopenharmony_ci         %11 = OpVariable %_ptr_Private_uint Private
467fd4e5da5Sopenharmony_ci         %12 = OpExtInst %void %1 DebugSource %3
468fd4e5da5Sopenharmony_ci         %13 = OpExtInst %void %1 DebugCompilationUnit 1 4 %12 HLSL
469fd4e5da5Sopenharmony_ci         %14 = OpExtInst %void %1 DebugTypeBasic %5 %uint_32 Signed
470fd4e5da5Sopenharmony_ci         %15 = OpExtInst %void %1 DebugGlobalVariable %4 %14 %12 1 12 %13 %4 %11 FlagIsDefinition
471fd4e5da5Sopenharmony_ci          %2 = OpFunction %void None %10
472fd4e5da5Sopenharmony_ci         %16 = OpLabel
473fd4e5da5Sopenharmony_ci               OpReturn
474fd4e5da5Sopenharmony_ci               OpFunctionEnd
475fd4e5da5Sopenharmony_ci)";
476fd4e5da5Sopenharmony_ci
477fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
478fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
479fd4e5da5Sopenharmony_ci
480fd4e5da5Sopenharmony_ci  // Delete the second variable.
481fd4e5da5Sopenharmony_ci  context->KillDef(11);
482fd4e5da5Sopenharmony_ci
483fd4e5da5Sopenharmony_ci  // Get DebugInfoNone id.
484fd4e5da5Sopenharmony_ci  uint32_t debug_info_none_id = 0;
485fd4e5da5Sopenharmony_ci  for (auto it = context->ext_inst_debuginfo_begin();
486fd4e5da5Sopenharmony_ci       it != context->ext_inst_debuginfo_end(); ++it) {
487fd4e5da5Sopenharmony_ci    if (it->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugInfoNone) {
488fd4e5da5Sopenharmony_ci      debug_info_none_id = it->result_id();
489fd4e5da5Sopenharmony_ci    }
490fd4e5da5Sopenharmony_ci  }
491fd4e5da5Sopenharmony_ci  EXPECT_NE(0, debug_info_none_id);
492fd4e5da5Sopenharmony_ci
493fd4e5da5Sopenharmony_ci  // Check the Function operand of DebugFunction is DebugInfoNone.
494fd4e5da5Sopenharmony_ci  const uint32_t kDebugGlobalVariableOperandVariableIndex = 11;
495fd4e5da5Sopenharmony_ci  bool checked = false;
496fd4e5da5Sopenharmony_ci  for (auto it = context->ext_inst_debuginfo_begin();
497fd4e5da5Sopenharmony_ci       it != context->ext_inst_debuginfo_end(); ++it) {
498fd4e5da5Sopenharmony_ci    if (it->GetOpenCL100DebugOpcode() ==
499fd4e5da5Sopenharmony_ci        OpenCLDebugInfo100DebugGlobalVariable) {
500fd4e5da5Sopenharmony_ci      EXPECT_FALSE(checked);
501fd4e5da5Sopenharmony_ci      EXPECT_EQ(
502fd4e5da5Sopenharmony_ci          it->GetOperand(kDebugGlobalVariableOperandVariableIndex).words[0],
503fd4e5da5Sopenharmony_ci          debug_info_none_id);
504fd4e5da5Sopenharmony_ci      checked = true;
505fd4e5da5Sopenharmony_ci    }
506fd4e5da5Sopenharmony_ci  }
507fd4e5da5Sopenharmony_ci  EXPECT_TRUE(checked);
508fd4e5da5Sopenharmony_ci}
509fd4e5da5Sopenharmony_ci
510fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, BasicVisitFromEntryPoint) {
511fd4e5da5Sopenharmony_ci  // Make sure we visit the entry point, and the function it calls.
512fd4e5da5Sopenharmony_ci  // Do not visit Dead or Exported.
513fd4e5da5Sopenharmony_ci  const std::string text = R"(
514fd4e5da5Sopenharmony_ci               OpCapability Shader
515fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
516fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %10 "main"
517fd4e5da5Sopenharmony_ci               OpName %10 "main"
518fd4e5da5Sopenharmony_ci               OpName %Dead "Dead"
519fd4e5da5Sopenharmony_ci               OpName %11 "Constant"
520fd4e5da5Sopenharmony_ci               OpName %ExportedFunc "ExportedFunc"
521fd4e5da5Sopenharmony_ci               OpDecorate %ExportedFunc LinkageAttributes "ExportedFunc" Export
522fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
523fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
524fd4e5da5Sopenharmony_ci         %10 = OpFunction %void None %6
525fd4e5da5Sopenharmony_ci         %14 = OpLabel
526fd4e5da5Sopenharmony_ci         %15 = OpFunctionCall %void %11
527fd4e5da5Sopenharmony_ci         %16 = OpFunctionCall %void %11
528fd4e5da5Sopenharmony_ci               OpReturn
529fd4e5da5Sopenharmony_ci               OpFunctionEnd
530fd4e5da5Sopenharmony_ci         %11 = OpFunction %void None %6
531fd4e5da5Sopenharmony_ci         %18 = OpLabel
532fd4e5da5Sopenharmony_ci               OpReturn
533fd4e5da5Sopenharmony_ci               OpFunctionEnd
534fd4e5da5Sopenharmony_ci       %Dead = OpFunction %void None %6
535fd4e5da5Sopenharmony_ci         %19 = OpLabel
536fd4e5da5Sopenharmony_ci               OpReturn
537fd4e5da5Sopenharmony_ci               OpFunctionEnd
538fd4e5da5Sopenharmony_ci%ExportedFunc = OpFunction %void None %7
539fd4e5da5Sopenharmony_ci         %20 = OpLabel
540fd4e5da5Sopenharmony_ci         %21 = OpFunctionCall %void %11
541fd4e5da5Sopenharmony_ci               OpReturn
542fd4e5da5Sopenharmony_ci               OpFunctionEnd
543fd4e5da5Sopenharmony_ci)";
544fd4e5da5Sopenharmony_ci  // clang-format on
545fd4e5da5Sopenharmony_ci
546fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> localContext =
547fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
548fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
549fd4e5da5Sopenharmony_ci  EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
550fd4e5da5Sopenharmony_ci                                   << text << std::endl;
551fd4e5da5Sopenharmony_ci  std::vector<uint32_t> processed;
552fd4e5da5Sopenharmony_ci  Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
553fd4e5da5Sopenharmony_ci    processed.push_back(fp->result_id());
554fd4e5da5Sopenharmony_ci    return false;
555fd4e5da5Sopenharmony_ci  };
556fd4e5da5Sopenharmony_ci  localContext->ProcessEntryPointCallTree(mark_visited);
557fd4e5da5Sopenharmony_ci  EXPECT_THAT(processed, UnorderedElementsAre(10, 11));
558fd4e5da5Sopenharmony_ci}
559fd4e5da5Sopenharmony_ci
560fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, BasicVisitReachable) {
561fd4e5da5Sopenharmony_ci  // Make sure we visit the entry point, exported function, and the function
562fd4e5da5Sopenharmony_ci  // they call. Do not visit Dead.
563fd4e5da5Sopenharmony_ci  const std::string text = R"(
564fd4e5da5Sopenharmony_ci               OpCapability Shader
565fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
566fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %10 "main"
567fd4e5da5Sopenharmony_ci               OpName %10 "main"
568fd4e5da5Sopenharmony_ci               OpName %Dead "Dead"
569fd4e5da5Sopenharmony_ci               OpName %11 "Constant"
570fd4e5da5Sopenharmony_ci               OpName %12 "ExportedFunc"
571fd4e5da5Sopenharmony_ci               OpName %13 "Constant2"
572fd4e5da5Sopenharmony_ci               OpDecorate %12 LinkageAttributes "ExportedFunc" Export
573fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
574fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
575fd4e5da5Sopenharmony_ci         %10 = OpFunction %void None %6
576fd4e5da5Sopenharmony_ci         %14 = OpLabel
577fd4e5da5Sopenharmony_ci         %15 = OpFunctionCall %void %11
578fd4e5da5Sopenharmony_ci         %16 = OpFunctionCall %void %11
579fd4e5da5Sopenharmony_ci               OpReturn
580fd4e5da5Sopenharmony_ci               OpFunctionEnd
581fd4e5da5Sopenharmony_ci         %11 = OpFunction %void None %6
582fd4e5da5Sopenharmony_ci         %18 = OpLabel
583fd4e5da5Sopenharmony_ci               OpReturn
584fd4e5da5Sopenharmony_ci               OpFunctionEnd
585fd4e5da5Sopenharmony_ci       %Dead = OpFunction %void None %6
586fd4e5da5Sopenharmony_ci         %19 = OpLabel
587fd4e5da5Sopenharmony_ci               OpReturn
588fd4e5da5Sopenharmony_ci               OpFunctionEnd
589fd4e5da5Sopenharmony_ci         %12 = OpFunction %void None %6
590fd4e5da5Sopenharmony_ci         %20 = OpLabel
591fd4e5da5Sopenharmony_ci         %21 = OpFunctionCall %void %13
592fd4e5da5Sopenharmony_ci               OpReturn
593fd4e5da5Sopenharmony_ci               OpFunctionEnd
594fd4e5da5Sopenharmony_ci         %13 = OpFunction %void None %6
595fd4e5da5Sopenharmony_ci         %22 = OpLabel
596fd4e5da5Sopenharmony_ci               OpReturn
597fd4e5da5Sopenharmony_ci               OpFunctionEnd
598fd4e5da5Sopenharmony_ci)";
599fd4e5da5Sopenharmony_ci  // clang-format on
600fd4e5da5Sopenharmony_ci
601fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> localContext =
602fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
603fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
604fd4e5da5Sopenharmony_ci  EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
605fd4e5da5Sopenharmony_ci                                   << text << std::endl;
606fd4e5da5Sopenharmony_ci
607fd4e5da5Sopenharmony_ci  std::vector<uint32_t> processed;
608fd4e5da5Sopenharmony_ci  Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
609fd4e5da5Sopenharmony_ci    processed.push_back(fp->result_id());
610fd4e5da5Sopenharmony_ci    return false;
611fd4e5da5Sopenharmony_ci  };
612fd4e5da5Sopenharmony_ci  localContext->ProcessReachableCallTree(mark_visited);
613fd4e5da5Sopenharmony_ci  EXPECT_THAT(processed, UnorderedElementsAre(10, 11, 12, 13));
614fd4e5da5Sopenharmony_ci}
615fd4e5da5Sopenharmony_ci
616fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, BasicVisitOnlyOnce) {
617fd4e5da5Sopenharmony_ci  // Make sure we visit %12 only once, even if it is called from two different
618fd4e5da5Sopenharmony_ci  // functions.
619fd4e5da5Sopenharmony_ci  const std::string text = R"(
620fd4e5da5Sopenharmony_ci               OpCapability Shader
621fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
622fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %10 "main"
623fd4e5da5Sopenharmony_ci               OpName %10 "main"
624fd4e5da5Sopenharmony_ci               OpName %Dead "Dead"
625fd4e5da5Sopenharmony_ci               OpName %11 "Constant"
626fd4e5da5Sopenharmony_ci               OpName %12 "ExportedFunc"
627fd4e5da5Sopenharmony_ci               OpDecorate %12 LinkageAttributes "ExportedFunc" Export
628fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
629fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
630fd4e5da5Sopenharmony_ci         %10 = OpFunction %void None %6
631fd4e5da5Sopenharmony_ci         %14 = OpLabel
632fd4e5da5Sopenharmony_ci         %15 = OpFunctionCall %void %11
633fd4e5da5Sopenharmony_ci         %16 = OpFunctionCall %void %12
634fd4e5da5Sopenharmony_ci               OpReturn
635fd4e5da5Sopenharmony_ci               OpFunctionEnd
636fd4e5da5Sopenharmony_ci         %11 = OpFunction %void None %6
637fd4e5da5Sopenharmony_ci         %18 = OpLabel
638fd4e5da5Sopenharmony_ci         %19 = OpFunctionCall %void %12
639fd4e5da5Sopenharmony_ci               OpReturn
640fd4e5da5Sopenharmony_ci               OpFunctionEnd
641fd4e5da5Sopenharmony_ci       %Dead = OpFunction %void None %6
642fd4e5da5Sopenharmony_ci         %20 = OpLabel
643fd4e5da5Sopenharmony_ci               OpReturn
644fd4e5da5Sopenharmony_ci               OpFunctionEnd
645fd4e5da5Sopenharmony_ci         %12 = OpFunction %void None %6
646fd4e5da5Sopenharmony_ci         %21 = OpLabel
647fd4e5da5Sopenharmony_ci               OpReturn
648fd4e5da5Sopenharmony_ci               OpFunctionEnd
649fd4e5da5Sopenharmony_ci)";
650fd4e5da5Sopenharmony_ci  // clang-format on
651fd4e5da5Sopenharmony_ci
652fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> localContext =
653fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
654fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
655fd4e5da5Sopenharmony_ci  EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
656fd4e5da5Sopenharmony_ci                                   << text << std::endl;
657fd4e5da5Sopenharmony_ci
658fd4e5da5Sopenharmony_ci  std::vector<uint32_t> processed;
659fd4e5da5Sopenharmony_ci  Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
660fd4e5da5Sopenharmony_ci    processed.push_back(fp->result_id());
661fd4e5da5Sopenharmony_ci    return false;
662fd4e5da5Sopenharmony_ci  };
663fd4e5da5Sopenharmony_ci  localContext->ProcessReachableCallTree(mark_visited);
664fd4e5da5Sopenharmony_ci  EXPECT_THAT(processed, UnorderedElementsAre(10, 11, 12));
665fd4e5da5Sopenharmony_ci}
666fd4e5da5Sopenharmony_ci
667fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, BasicDontVisitExportedVariable) {
668fd4e5da5Sopenharmony_ci  // Make sure we only visit functions and not exported variables.
669fd4e5da5Sopenharmony_ci  const std::string text = R"(
670fd4e5da5Sopenharmony_ci               OpCapability Shader
671fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
672fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %10 "main"
673fd4e5da5Sopenharmony_ci               OpExecutionMode %10 OriginUpperLeft
674fd4e5da5Sopenharmony_ci               OpSource GLSL 150
675fd4e5da5Sopenharmony_ci               OpName %10 "main"
676fd4e5da5Sopenharmony_ci               OpName %12 "export_var"
677fd4e5da5Sopenharmony_ci               OpDecorate %12 LinkageAttributes "export_var" Export
678fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
679fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
680fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
681fd4e5da5Sopenharmony_ci  %float_1 = OpConstant %float 1
682fd4e5da5Sopenharmony_ci         %12 = OpVariable %float Output
683fd4e5da5Sopenharmony_ci         %10 = OpFunction %void None %6
684fd4e5da5Sopenharmony_ci         %14 = OpLabel
685fd4e5da5Sopenharmony_ci               OpStore %12 %float_1
686fd4e5da5Sopenharmony_ci               OpReturn
687fd4e5da5Sopenharmony_ci               OpFunctionEnd
688fd4e5da5Sopenharmony_ci)";
689fd4e5da5Sopenharmony_ci  // clang-format on
690fd4e5da5Sopenharmony_ci
691fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> localContext =
692fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
693fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
694fd4e5da5Sopenharmony_ci  EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
695fd4e5da5Sopenharmony_ci                                   << text << std::endl;
696fd4e5da5Sopenharmony_ci
697fd4e5da5Sopenharmony_ci  std::vector<uint32_t> processed;
698fd4e5da5Sopenharmony_ci  Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
699fd4e5da5Sopenharmony_ci    processed.push_back(fp->result_id());
700fd4e5da5Sopenharmony_ci    return false;
701fd4e5da5Sopenharmony_ci  };
702fd4e5da5Sopenharmony_ci  localContext->ProcessReachableCallTree(mark_visited);
703fd4e5da5Sopenharmony_ci  EXPECT_THAT(processed, UnorderedElementsAre(10));
704fd4e5da5Sopenharmony_ci}
705fd4e5da5Sopenharmony_ci
706fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, IdBoundTestAtLimit) {
707fd4e5da5Sopenharmony_ci  const std::string text = R"(
708fd4e5da5Sopenharmony_ciOpCapability Shader
709fd4e5da5Sopenharmony_ciOpCapability Linkage
710fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
711fd4e5da5Sopenharmony_ci%1 = OpTypeVoid
712fd4e5da5Sopenharmony_ci%2 = OpTypeFunction %1
713fd4e5da5Sopenharmony_ci%3 = OpFunction %1 None %2
714fd4e5da5Sopenharmony_ci%4 = OpLabel
715fd4e5da5Sopenharmony_ciOpReturn
716fd4e5da5Sopenharmony_ciOpFunctionEnd)";
717fd4e5da5Sopenharmony_ci
718fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
719fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
720fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
721fd4e5da5Sopenharmony_ci  uint32_t current_bound = context->module()->id_bound();
722fd4e5da5Sopenharmony_ci  context->set_max_id_bound(current_bound);
723fd4e5da5Sopenharmony_ci  uint32_t next_id_bound = context->TakeNextId();
724fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, 0);
725fd4e5da5Sopenharmony_ci  EXPECT_EQ(current_bound, context->module()->id_bound());
726fd4e5da5Sopenharmony_ci  next_id_bound = context->TakeNextId();
727fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, 0);
728fd4e5da5Sopenharmony_ci}
729fd4e5da5Sopenharmony_ci
730fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, IdBoundTestBelowLimit) {
731fd4e5da5Sopenharmony_ci  const std::string text = R"(
732fd4e5da5Sopenharmony_ciOpCapability Shader
733fd4e5da5Sopenharmony_ciOpCapability Linkage
734fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
735fd4e5da5Sopenharmony_ci%1 = OpTypeVoid
736fd4e5da5Sopenharmony_ci%2 = OpTypeFunction %1
737fd4e5da5Sopenharmony_ci%3 = OpFunction %1 None %2
738fd4e5da5Sopenharmony_ci%4 = OpLabel
739fd4e5da5Sopenharmony_ciOpReturn
740fd4e5da5Sopenharmony_ciOpFunctionEnd)";
741fd4e5da5Sopenharmony_ci
742fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
743fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
744fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
745fd4e5da5Sopenharmony_ci  uint32_t current_bound = context->module()->id_bound();
746fd4e5da5Sopenharmony_ci  context->set_max_id_bound(current_bound + 100);
747fd4e5da5Sopenharmony_ci  uint32_t next_id_bound = context->TakeNextId();
748fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, current_bound);
749fd4e5da5Sopenharmony_ci  EXPECT_EQ(current_bound + 1, context->module()->id_bound());
750fd4e5da5Sopenharmony_ci  next_id_bound = context->TakeNextId();
751fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, current_bound + 1);
752fd4e5da5Sopenharmony_ci}
753fd4e5da5Sopenharmony_ci
754fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, IdBoundTestNearLimit) {
755fd4e5da5Sopenharmony_ci  const std::string text = R"(
756fd4e5da5Sopenharmony_ciOpCapability Shader
757fd4e5da5Sopenharmony_ciOpCapability Linkage
758fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
759fd4e5da5Sopenharmony_ci%1 = OpTypeVoid
760fd4e5da5Sopenharmony_ci%2 = OpTypeFunction %1
761fd4e5da5Sopenharmony_ci%3 = OpFunction %1 None %2
762fd4e5da5Sopenharmony_ci%4 = OpLabel
763fd4e5da5Sopenharmony_ciOpReturn
764fd4e5da5Sopenharmony_ciOpFunctionEnd)";
765fd4e5da5Sopenharmony_ci
766fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
767fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
768fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
769fd4e5da5Sopenharmony_ci  uint32_t current_bound = context->module()->id_bound();
770fd4e5da5Sopenharmony_ci  context->set_max_id_bound(current_bound + 1);
771fd4e5da5Sopenharmony_ci  uint32_t next_id_bound = context->TakeNextId();
772fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, current_bound);
773fd4e5da5Sopenharmony_ci  EXPECT_EQ(current_bound + 1, context->module()->id_bound());
774fd4e5da5Sopenharmony_ci  next_id_bound = context->TakeNextId();
775fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, 0);
776fd4e5da5Sopenharmony_ci}
777fd4e5da5Sopenharmony_ci
778fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, IdBoundTestUIntMax) {
779fd4e5da5Sopenharmony_ci  const std::string text = R"(
780fd4e5da5Sopenharmony_ciOpCapability Shader
781fd4e5da5Sopenharmony_ciOpCapability Linkage
782fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
783fd4e5da5Sopenharmony_ci%1 = OpTypeVoid
784fd4e5da5Sopenharmony_ci%2 = OpTypeFunction %1
785fd4e5da5Sopenharmony_ci%3 = OpFunction %1 None %2
786fd4e5da5Sopenharmony_ci%4294967294 = OpLabel ; ID is UINT_MAX-1
787fd4e5da5Sopenharmony_ciOpReturn
788fd4e5da5Sopenharmony_ciOpFunctionEnd)";
789fd4e5da5Sopenharmony_ci
790fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> context =
791fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
792fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
793fd4e5da5Sopenharmony_ci  uint32_t current_bound = context->module()->id_bound();
794fd4e5da5Sopenharmony_ci
795fd4e5da5Sopenharmony_ci  // Expecting |BuildModule| to preserve the numeric ids.
796fd4e5da5Sopenharmony_ci  EXPECT_EQ(current_bound, std::numeric_limits<uint32_t>::max());
797fd4e5da5Sopenharmony_ci
798fd4e5da5Sopenharmony_ci  context->set_max_id_bound(current_bound);
799fd4e5da5Sopenharmony_ci  uint32_t next_id_bound = context->TakeNextId();
800fd4e5da5Sopenharmony_ci  EXPECT_EQ(next_id_bound, 0);
801fd4e5da5Sopenharmony_ci  EXPECT_EQ(current_bound, context->module()->id_bound());
802fd4e5da5Sopenharmony_ci}
803fd4e5da5Sopenharmony_ci
804fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, CfgAndDomAnalysis) {
805fd4e5da5Sopenharmony_ci  const std::string text = R"(
806fd4e5da5Sopenharmony_ciOpCapability Shader
807fd4e5da5Sopenharmony_ciOpCapability Linkage
808fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
809fd4e5da5Sopenharmony_ci%1 = OpTypeVoid
810fd4e5da5Sopenharmony_ci%2 = OpTypeFunction %1
811fd4e5da5Sopenharmony_ci%3 = OpFunction %1 None %2
812fd4e5da5Sopenharmony_ci%4 = OpLabel
813fd4e5da5Sopenharmony_ciOpReturn
814fd4e5da5Sopenharmony_ciOpFunctionEnd)";
815fd4e5da5Sopenharmony_ci
816fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
817fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
818fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
819fd4e5da5Sopenharmony_ci
820fd4e5da5Sopenharmony_ci  // Building the dominator analysis should build the CFG.
821fd4e5da5Sopenharmony_ci  ASSERT_TRUE(ctx->module()->begin() != ctx->module()->end());
822fd4e5da5Sopenharmony_ci  ctx->GetDominatorAnalysis(&*ctx->module()->begin());
823fd4e5da5Sopenharmony_ci
824fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisCFG));
825fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisDominatorAnalysis));
826fd4e5da5Sopenharmony_ci
827fd4e5da5Sopenharmony_ci  // Invalidating the CFG analysis should invalidate the dominator analysis.
828fd4e5da5Sopenharmony_ci  ctx->InvalidateAnalyses(IRContext::kAnalysisCFG);
829fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->AreAnalysesValid(IRContext::kAnalysisCFG));
830fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->AreAnalysesValid(IRContext::kAnalysisDominatorAnalysis));
831fd4e5da5Sopenharmony_ci}
832fd4e5da5Sopenharmony_ci
833fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, AsanErrorTest) {
834fd4e5da5Sopenharmony_ci  std::string shader = R"(
835fd4e5da5Sopenharmony_ci               OpCapability Shader
836fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
837fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
838fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %4 "main"
839fd4e5da5Sopenharmony_ci               OpExecutionMode %4 OriginUpperLeft
840fd4e5da5Sopenharmony_ci               OpSource ESSL 310
841fd4e5da5Sopenharmony_ci               OpName %4 "main"
842fd4e5da5Sopenharmony_ci               OpName %8 "x"
843fd4e5da5Sopenharmony_ci               OpName %10 "y"
844fd4e5da5Sopenharmony_ci               OpDecorate %8 RelaxedPrecision
845fd4e5da5Sopenharmony_ci               OpDecorate %10 RelaxedPrecision
846fd4e5da5Sopenharmony_ci               OpDecorate %11 RelaxedPrecision
847fd4e5da5Sopenharmony_ci          %2 = OpTypeVoid
848fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %2
849fd4e5da5Sopenharmony_ci          %6 = OpTypeInt 32 1
850fd4e5da5Sopenharmony_ci          %7 = OpTypePointer Function %6
851fd4e5da5Sopenharmony_ci          %9 = OpConstant %6 1
852fd4e5da5Sopenharmony_ci          %4 = OpFunction %2 None %3
853fd4e5da5Sopenharmony_ci          %5 = OpLabel
854fd4e5da5Sopenharmony_ci          %8 = OpVariable %7 Function
855fd4e5da5Sopenharmony_ci         %10 = OpVariable %7 Function
856fd4e5da5Sopenharmony_ci               OpStore %8 %9
857fd4e5da5Sopenharmony_ci         %11 = OpLoad %6 %8
858fd4e5da5Sopenharmony_ci	       OpBranch %20
859fd4e5da5Sopenharmony_ci	 %20 = OpLabel
860fd4e5da5Sopenharmony_ci	 %21 = OpPhi %6 %11 %5
861fd4e5da5Sopenharmony_ci         OpStore %10 %21
862fd4e5da5Sopenharmony_ci         OpReturn
863fd4e5da5Sopenharmony_ci         OpFunctionEnd
864fd4e5da5Sopenharmony_ci  )";
865fd4e5da5Sopenharmony_ci
866fd4e5da5Sopenharmony_ci  const auto env = SPV_ENV_UNIVERSAL_1_3;
867fd4e5da5Sopenharmony_ci  const auto consumer = nullptr;
868fd4e5da5Sopenharmony_ci  const auto context = BuildModule(
869fd4e5da5Sopenharmony_ci      env, consumer, shader, SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
870fd4e5da5Sopenharmony_ci
871fd4e5da5Sopenharmony_ci  opt::Function* fun =
872fd4e5da5Sopenharmony_ci      context->cfg()->block(5)->GetParent();  // Computes the CFG analysis
873fd4e5da5Sopenharmony_ci  opt::DominatorAnalysis* dom = nullptr;
874fd4e5da5Sopenharmony_ci  dom = context->GetDominatorAnalysis(fun);  // Computes the dominator analysis,
875fd4e5da5Sopenharmony_ci                                             // which depends on the CFG
876fd4e5da5Sopenharmony_ci                                             // analysis
877fd4e5da5Sopenharmony_ci  context->InvalidateAnalysesExceptFor(
878fd4e5da5Sopenharmony_ci      opt::IRContext::Analysis::kAnalysisDominatorAnalysis);  // Invalidates the
879fd4e5da5Sopenharmony_ci                                                              // CFG analysis
880fd4e5da5Sopenharmony_ci  dom = context->GetDominatorAnalysis(
881fd4e5da5Sopenharmony_ci      fun);  // Recompute the CFG analysis because the Dominator tree uses it.
882fd4e5da5Sopenharmony_ci  auto bb = dom->ImmediateDominator(5);
883fd4e5da5Sopenharmony_ci  std::cout
884fd4e5da5Sopenharmony_ci      << bb->id();  // Make sure asan does not complain about use after free.
885fd4e5da5Sopenharmony_ci}
886fd4e5da5Sopenharmony_ci
887fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, DebugInstructionReplaceSingleUse) {
888fd4e5da5Sopenharmony_ci  const std::string text = R"(
889fd4e5da5Sopenharmony_ciOpCapability Shader
890fd4e5da5Sopenharmony_ciOpCapability Linkage
891fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "OpenCL.DebugInfo.100"
892fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
893fd4e5da5Sopenharmony_ci%2 = OpString "test"
894fd4e5da5Sopenharmony_ci%3 = OpTypeVoid
895fd4e5da5Sopenharmony_ci%4 = OpTypeFunction %3
896fd4e5da5Sopenharmony_ci%5 = OpTypeFloat 32
897fd4e5da5Sopenharmony_ci%6 = OpTypePointer Function %5
898fd4e5da5Sopenharmony_ci%7 = OpConstant %5 0
899fd4e5da5Sopenharmony_ci%8 = OpTypeInt 32 0
900fd4e5da5Sopenharmony_ci%9 = OpConstant %8 32
901fd4e5da5Sopenharmony_ci%10 = OpExtInst %3 %1 DebugExpression
902fd4e5da5Sopenharmony_ci%11 = OpExtInst %3 %1 DebugSource %2
903fd4e5da5Sopenharmony_ci%12 = OpExtInst %3 %1 DebugCompilationUnit 1 4 %11 HLSL
904fd4e5da5Sopenharmony_ci%13 = OpExtInst %3 %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %3
905fd4e5da5Sopenharmony_ci%14 = OpExtInst %3 %1 DebugFunction %2 %13 %11 0 0 %12 %2 FlagIsProtected|FlagIsPrivate 0 %17
906fd4e5da5Sopenharmony_ci%15 = OpExtInst %3 %1 DebugTypeBasic %2 %9 Float
907fd4e5da5Sopenharmony_ci%16 = OpExtInst %3 %1 DebugLocalVariable %2 %15 %11 0 0 %14 FlagIsLocal
908fd4e5da5Sopenharmony_ci%17 = OpFunction %3 None %4
909fd4e5da5Sopenharmony_ci%18 = OpLabel
910fd4e5da5Sopenharmony_ci%19 = OpExtInst %3 %1 DebugScope %14
911fd4e5da5Sopenharmony_ci%20 = OpVariable %6 Function
912fd4e5da5Sopenharmony_ci%26 = OpVariable %6 Function
913fd4e5da5Sopenharmony_ciOpBranch %21
914fd4e5da5Sopenharmony_ci%21 = OpLabel
915fd4e5da5Sopenharmony_ci%22 = OpPhi %5 %7 %18
916fd4e5da5Sopenharmony_ciOpBranch %23
917fd4e5da5Sopenharmony_ci%23 = OpLabel
918fd4e5da5Sopenharmony_ciOpLine %2 0 0
919fd4e5da5Sopenharmony_ciOpStore %20 %7
920fd4e5da5Sopenharmony_ci%24 = OpExtInst %3 %1 DebugValue %16 %22 %10
921fd4e5da5Sopenharmony_ci%25 = OpExtInst %3 %1 DebugDeclare %16 %26 %10
922fd4e5da5Sopenharmony_ciOpReturn
923fd4e5da5Sopenharmony_ciOpFunctionEnd)";
924fd4e5da5Sopenharmony_ci
925fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
926fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
927fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
928fd4e5da5Sopenharmony_ci  ctx->BuildInvalidAnalyses(IRContext::kAnalysisDebugInfo);
929fd4e5da5Sopenharmony_ci  NoopPassPreservesAll pass(Pass::Status::SuccessWithChange);
930fd4e5da5Sopenharmony_ci  pass.Run(ctx.get());
931fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisDebugInfo));
932fd4e5da5Sopenharmony_ci
933fd4e5da5Sopenharmony_ci  auto* dbg_value = ctx->get_def_use_mgr()->GetDef(24);
934fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
935fd4e5da5Sopenharmony_ci              22);
936fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(22, 7));
937fd4e5da5Sopenharmony_ci  dbg_value = ctx->get_def_use_mgr()->GetDef(24);
938fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
939fd4e5da5Sopenharmony_ci              7);
940fd4e5da5Sopenharmony_ci
941fd4e5da5Sopenharmony_ci  auto* dbg_decl = ctx->get_def_use_mgr()->GetDef(25);
942fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
943fd4e5da5Sopenharmony_ci      dbg_decl->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 26);
944fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(26, 20));
945fd4e5da5Sopenharmony_ci  dbg_decl = ctx->get_def_use_mgr()->GetDef(25);
946fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
947fd4e5da5Sopenharmony_ci      dbg_decl->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 20);
948fd4e5da5Sopenharmony_ci}
949fd4e5da5Sopenharmony_ci
950fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, DebugInstructionReplaceAllUses) {
951fd4e5da5Sopenharmony_ci  const std::string text = R"(
952fd4e5da5Sopenharmony_ciOpCapability Shader
953fd4e5da5Sopenharmony_ciOpCapability Linkage
954fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "OpenCL.DebugInfo.100"
955fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
956fd4e5da5Sopenharmony_ci%2 = OpString "test"
957fd4e5da5Sopenharmony_ci%3 = OpTypeVoid
958fd4e5da5Sopenharmony_ci%4 = OpTypeFunction %3
959fd4e5da5Sopenharmony_ci%5 = OpTypeFloat 32
960fd4e5da5Sopenharmony_ci%6 = OpTypePointer Function %5
961fd4e5da5Sopenharmony_ci%7 = OpConstant %5 0
962fd4e5da5Sopenharmony_ci%8 = OpTypeInt 32 0
963fd4e5da5Sopenharmony_ci%9 = OpConstant %8 32
964fd4e5da5Sopenharmony_ci%10 = OpExtInst %3 %1 DebugExpression
965fd4e5da5Sopenharmony_ci%11 = OpExtInst %3 %1 DebugSource %2
966fd4e5da5Sopenharmony_ci%12 = OpExtInst %3 %1 DebugCompilationUnit 1 4 %11 HLSL
967fd4e5da5Sopenharmony_ci%13 = OpExtInst %3 %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %3
968fd4e5da5Sopenharmony_ci%14 = OpExtInst %3 %1 DebugFunction %2 %13 %11 0 0 %12 %2 FlagIsProtected|FlagIsPrivate 0 %17
969fd4e5da5Sopenharmony_ci%15 = OpExtInst %3 %1 DebugTypeBasic %2 %9 Float
970fd4e5da5Sopenharmony_ci%16 = OpExtInst %3 %1 DebugLocalVariable %2 %15 %11 0 0 %14 FlagIsLocal
971fd4e5da5Sopenharmony_ci%27 = OpExtInst %3 %1 DebugLocalVariable %2 %15 %11 1 0 %14 FlagIsLocal
972fd4e5da5Sopenharmony_ci%17 = OpFunction %3 None %4
973fd4e5da5Sopenharmony_ci%18 = OpLabel
974fd4e5da5Sopenharmony_ci%19 = OpExtInst %3 %1 DebugScope %14
975fd4e5da5Sopenharmony_ci%20 = OpVariable %6 Function
976fd4e5da5Sopenharmony_ci%26 = OpVariable %6 Function
977fd4e5da5Sopenharmony_ciOpBranch %21
978fd4e5da5Sopenharmony_ci%21 = OpLabel
979fd4e5da5Sopenharmony_ci%22 = OpPhi %5 %7 %18
980fd4e5da5Sopenharmony_ciOpBranch %23
981fd4e5da5Sopenharmony_ci%23 = OpLabel
982fd4e5da5Sopenharmony_ciOpLine %2 0 0
983fd4e5da5Sopenharmony_ciOpStore %20 %7
984fd4e5da5Sopenharmony_ci%24 = OpExtInst %3 %1 DebugValue %16 %22 %10
985fd4e5da5Sopenharmony_ci%25 = OpExtInst %3 %1 DebugDeclare %16 %26 %10
986fd4e5da5Sopenharmony_ci%28 = OpExtInst %3 %1 DebugValue %27 %22 %10
987fd4e5da5Sopenharmony_ci%29 = OpExtInst %3 %1 DebugDeclare %27 %26 %10
988fd4e5da5Sopenharmony_ciOpReturn
989fd4e5da5Sopenharmony_ciOpFunctionEnd)";
990fd4e5da5Sopenharmony_ci
991fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
992fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
993fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
994fd4e5da5Sopenharmony_ci  ctx->BuildInvalidAnalyses(IRContext::kAnalysisDebugInfo);
995fd4e5da5Sopenharmony_ci  NoopPassPreservesAll pass(Pass::Status::SuccessWithChange);
996fd4e5da5Sopenharmony_ci  pass.Run(ctx.get());
997fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisDebugInfo));
998fd4e5da5Sopenharmony_ci
999fd4e5da5Sopenharmony_ci  auto* dbg_value0 = ctx->get_def_use_mgr()->GetDef(24);
1000fd4e5da5Sopenharmony_ci  auto* dbg_value1 = ctx->get_def_use_mgr()->GetDef(28);
1001fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value0->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
1002fd4e5da5Sopenharmony_ci              22);
1003fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value1->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
1004fd4e5da5Sopenharmony_ci              22);
1005fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(22, 7));
1006fd4e5da5Sopenharmony_ci  dbg_value0 = ctx->get_def_use_mgr()->GetDef(24);
1007fd4e5da5Sopenharmony_ci  dbg_value1 = ctx->get_def_use_mgr()->GetDef(28);
1008fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value0->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
1009fd4e5da5Sopenharmony_ci              7);
1010fd4e5da5Sopenharmony_ci  EXPECT_TRUE(dbg_value1->GetSingleWordOperand(kDebugValueOperandValueIndex) ==
1011fd4e5da5Sopenharmony_ci              7);
1012fd4e5da5Sopenharmony_ci
1013fd4e5da5Sopenharmony_ci  auto* dbg_decl0 = ctx->get_def_use_mgr()->GetDef(25);
1014fd4e5da5Sopenharmony_ci  auto* dbg_decl1 = ctx->get_def_use_mgr()->GetDef(29);
1015fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1016fd4e5da5Sopenharmony_ci      dbg_decl0->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 26);
1017fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1018fd4e5da5Sopenharmony_ci      dbg_decl1->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 26);
1019fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(26, 20));
1020fd4e5da5Sopenharmony_ci  dbg_decl0 = ctx->get_def_use_mgr()->GetDef(25);
1021fd4e5da5Sopenharmony_ci  dbg_decl1 = ctx->get_def_use_mgr()->GetDef(29);
1022fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1023fd4e5da5Sopenharmony_ci      dbg_decl0->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 20);
1024fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1025fd4e5da5Sopenharmony_ci      dbg_decl1->GetSingleWordOperand(kDebugDeclareOperandVariableIndex) == 20);
1026fd4e5da5Sopenharmony_ci}
1027fd4e5da5Sopenharmony_ci
1028fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, DebugInstructionReplaceDebugScopeAndDebugInlinedAt) {
1029fd4e5da5Sopenharmony_ci  const std::string text = R"(
1030fd4e5da5Sopenharmony_ciOpCapability Shader
1031fd4e5da5Sopenharmony_ciOpCapability Linkage
1032fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "OpenCL.DebugInfo.100"
1033fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
1034fd4e5da5Sopenharmony_ci%2 = OpString "test"
1035fd4e5da5Sopenharmony_ci%3 = OpTypeVoid
1036fd4e5da5Sopenharmony_ci%4 = OpTypeFunction %3
1037fd4e5da5Sopenharmony_ci%5 = OpTypeFloat 32
1038fd4e5da5Sopenharmony_ci%6 = OpTypePointer Function %5
1039fd4e5da5Sopenharmony_ci%7 = OpConstant %5 0
1040fd4e5da5Sopenharmony_ci%8 = OpTypeInt 32 0
1041fd4e5da5Sopenharmony_ci%9 = OpConstant %8 32
1042fd4e5da5Sopenharmony_ci%10 = OpExtInst %3 %1 DebugExpression
1043fd4e5da5Sopenharmony_ci%11 = OpExtInst %3 %1 DebugSource %2
1044fd4e5da5Sopenharmony_ci%12 = OpExtInst %3 %1 DebugCompilationUnit 1 4 %11 HLSL
1045fd4e5da5Sopenharmony_ci%13 = OpExtInst %3 %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %3
1046fd4e5da5Sopenharmony_ci%14 = OpExtInst %3 %1 DebugFunction %2 %13 %11 0 0 %12 %2 FlagIsProtected|FlagIsPrivate 0 %17
1047fd4e5da5Sopenharmony_ci%15 = OpExtInst %3 %1 DebugInfoNone
1048fd4e5da5Sopenharmony_ci%16 = OpExtInst %3 %1 DebugFunction %2 %13 %11 10 10 %12 %2 FlagIsProtected|FlagIsPrivate 0 %15
1049fd4e5da5Sopenharmony_ci%25 = OpExtInst %3 %1 DebugInlinedAt 0 %14
1050fd4e5da5Sopenharmony_ci%26 = OpExtInst %3 %1 DebugInlinedAt 2 %14
1051fd4e5da5Sopenharmony_ci%17 = OpFunction %3 None %4
1052fd4e5da5Sopenharmony_ci%18 = OpLabel
1053fd4e5da5Sopenharmony_ci%19 = OpExtInst %3 %1 DebugScope %14
1054fd4e5da5Sopenharmony_ci%20 = OpVariable %6 Function
1055fd4e5da5Sopenharmony_ciOpBranch %21
1056fd4e5da5Sopenharmony_ci%21 = OpLabel
1057fd4e5da5Sopenharmony_ci%24 = OpExtInst %3 %1 DebugScope %16
1058fd4e5da5Sopenharmony_ci%22 = OpPhi %5 %7 %18
1059fd4e5da5Sopenharmony_ciOpBranch %23
1060fd4e5da5Sopenharmony_ci%23 = OpLabel
1061fd4e5da5Sopenharmony_ci%27 = OpExtInst %3 %1 DebugScope %16 %25
1062fd4e5da5Sopenharmony_ciOpLine %2 0 0
1063fd4e5da5Sopenharmony_ci%28 = OpFAdd %5 %7 %7
1064fd4e5da5Sopenharmony_ciOpStore %20 %28
1065fd4e5da5Sopenharmony_ciOpReturn
1066fd4e5da5Sopenharmony_ciOpFunctionEnd)";
1067fd4e5da5Sopenharmony_ci
1068fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1069fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
1070fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1071fd4e5da5Sopenharmony_ci  ctx->BuildInvalidAnalyses(IRContext::kAnalysisDebugInfo);
1072fd4e5da5Sopenharmony_ci  NoopPassPreservesAll pass(Pass::Status::SuccessWithChange);
1073fd4e5da5Sopenharmony_ci  pass.Run(ctx.get());
1074fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisDebugInfo));
1075fd4e5da5Sopenharmony_ci
1076fd4e5da5Sopenharmony_ci  auto* inst0 = ctx->get_def_use_mgr()->GetDef(20);
1077fd4e5da5Sopenharmony_ci  auto* inst1 = ctx->get_def_use_mgr()->GetDef(22);
1078fd4e5da5Sopenharmony_ci  auto* inst2 = ctx->get_def_use_mgr()->GetDef(28);
1079fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst0->GetDebugScope().GetLexicalScope(), 14);
1080fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst1->GetDebugScope().GetLexicalScope(), 16);
1081fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst2->GetDebugScope().GetLexicalScope(), 16);
1082fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst2->GetDebugInlinedAt(), 25);
1083fd4e5da5Sopenharmony_ci
1084fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(14, 12));
1085fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(16, 14));
1086fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(25, 26));
1087fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst0->GetDebugScope().GetLexicalScope(), 12);
1088fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst1->GetDebugScope().GetLexicalScope(), 14);
1089fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst2->GetDebugScope().GetLexicalScope(), 14);
1090fd4e5da5Sopenharmony_ci  EXPECT_EQ(inst2->GetDebugInlinedAt(), 26);
1091fd4e5da5Sopenharmony_ci}
1092fd4e5da5Sopenharmony_ci
1093fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, AddDebugValueAfterReplaceUse) {
1094fd4e5da5Sopenharmony_ci  const std::string text = R"(
1095fd4e5da5Sopenharmony_ciOpCapability Shader
1096fd4e5da5Sopenharmony_ciOpCapability Linkage
1097fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "OpenCL.DebugInfo.100"
1098fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
1099fd4e5da5Sopenharmony_ci%2 = OpString "test"
1100fd4e5da5Sopenharmony_ci%3 = OpTypeVoid
1101fd4e5da5Sopenharmony_ci%4 = OpTypeFunction %3
1102fd4e5da5Sopenharmony_ci%5 = OpTypeFloat 32
1103fd4e5da5Sopenharmony_ci%6 = OpTypePointer Function %5
1104fd4e5da5Sopenharmony_ci%7 = OpConstant %5 0
1105fd4e5da5Sopenharmony_ci%8 = OpTypeInt 32 0
1106fd4e5da5Sopenharmony_ci%9 = OpConstant %8 32
1107fd4e5da5Sopenharmony_ci%10 = OpExtInst %3 %1 DebugExpression
1108fd4e5da5Sopenharmony_ci%11 = OpExtInst %3 %1 DebugSource %2
1109fd4e5da5Sopenharmony_ci%12 = OpExtInst %3 %1 DebugCompilationUnit 1 4 %11 HLSL
1110fd4e5da5Sopenharmony_ci%13 = OpExtInst %3 %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %3
1111fd4e5da5Sopenharmony_ci%14 = OpExtInst %3 %1 DebugFunction %2 %13 %11 0 0 %12 %2 FlagIsProtected|FlagIsPrivate 0 %17
1112fd4e5da5Sopenharmony_ci%15 = OpExtInst %3 %1 DebugTypeBasic %2 %9 Float
1113fd4e5da5Sopenharmony_ci%16 = OpExtInst %3 %1 DebugLocalVariable %2 %15 %11 0 0 %14 FlagIsLocal
1114fd4e5da5Sopenharmony_ci%17 = OpFunction %3 None %4
1115fd4e5da5Sopenharmony_ci%18 = OpLabel
1116fd4e5da5Sopenharmony_ci%19 = OpExtInst %3 %1 DebugScope %14
1117fd4e5da5Sopenharmony_ci%20 = OpVariable %6 Function
1118fd4e5da5Sopenharmony_ci%26 = OpVariable %6 Function
1119fd4e5da5Sopenharmony_ciOpBranch %21
1120fd4e5da5Sopenharmony_ci%21 = OpLabel
1121fd4e5da5Sopenharmony_ci%27 = OpExtInst %3 %1 DebugScope %14
1122fd4e5da5Sopenharmony_ci%22 = OpPhi %5 %7 %18
1123fd4e5da5Sopenharmony_ciOpBranch %23
1124fd4e5da5Sopenharmony_ci%23 = OpLabel
1125fd4e5da5Sopenharmony_ci%28 = OpExtInst %3 %1 DebugScope %14
1126fd4e5da5Sopenharmony_ciOpLine %2 0 0
1127fd4e5da5Sopenharmony_ciOpStore %20 %7
1128fd4e5da5Sopenharmony_ci%24 = OpExtInst %3 %1 DebugValue %16 %22 %10
1129fd4e5da5Sopenharmony_ci%25 = OpExtInst %3 %1 DebugDeclare %16 %26 %10
1130fd4e5da5Sopenharmony_ciOpReturn
1131fd4e5da5Sopenharmony_ciOpFunctionEnd)";
1132fd4e5da5Sopenharmony_ci
1133fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1134fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
1135fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1136fd4e5da5Sopenharmony_ci  ctx->BuildInvalidAnalyses(IRContext::kAnalysisDebugInfo);
1137fd4e5da5Sopenharmony_ci  NoopPassPreservesAll pass(Pass::Status::SuccessWithChange);
1138fd4e5da5Sopenharmony_ci  pass.Run(ctx.get());
1139fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->AreAnalysesValid(IRContext::kAnalysisDebugInfo));
1140fd4e5da5Sopenharmony_ci
1141fd4e5da5Sopenharmony_ci  // Replace all uses of result it '26' with '20'
1142fd4e5da5Sopenharmony_ci  auto* dbg_decl = ctx->get_def_use_mgr()->GetDef(25);
1143fd4e5da5Sopenharmony_ci  EXPECT_EQ(dbg_decl->GetSingleWordOperand(kDebugDeclareOperandVariableIndex),
1144fd4e5da5Sopenharmony_ci            26);
1145fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->ReplaceAllUsesWith(26, 20));
1146fd4e5da5Sopenharmony_ci  dbg_decl = ctx->get_def_use_mgr()->GetDef(25);
1147fd4e5da5Sopenharmony_ci  EXPECT_EQ(dbg_decl->GetSingleWordOperand(kDebugDeclareOperandVariableIndex),
1148fd4e5da5Sopenharmony_ci            20);
1149fd4e5da5Sopenharmony_ci}
1150fd4e5da5Sopenharmony_ci
1151fd4e5da5Sopenharmony_cistruct TargetEnvCompareTestData {
1152fd4e5da5Sopenharmony_ci  spv_target_env later_env, earlier_env;
1153fd4e5da5Sopenharmony_ci};
1154fd4e5da5Sopenharmony_ci
1155fd4e5da5Sopenharmony_ciusing TargetEnvCompareTest = ::testing::TestWithParam<TargetEnvCompareTestData>;
1156fd4e5da5Sopenharmony_ci
1157fd4e5da5Sopenharmony_ciTEST_P(TargetEnvCompareTest, Case) {
1158fd4e5da5Sopenharmony_ci  // If new environments are added, then we must update the list of tests.
1159fd4e5da5Sopenharmony_ci  ASSERT_EQ(SPV_ENV_VULKAN_1_3 + 1, SPV_ENV_MAX);
1160fd4e5da5Sopenharmony_ci
1161fd4e5da5Sopenharmony_ci  const auto& tc = GetParam();
1162fd4e5da5Sopenharmony_ci
1163fd4e5da5Sopenharmony_ci  std::unique_ptr<Module> module(new Module());
1164fd4e5da5Sopenharmony_ci  IRContext localContext(tc.later_env, std::move(module),
1165fd4e5da5Sopenharmony_ci                         spvtools::MessageConsumer());
1166fd4e5da5Sopenharmony_ci  EXPECT_TRUE(localContext.IsTargetEnvAtLeast(tc.earlier_env));
1167fd4e5da5Sopenharmony_ci
1168fd4e5da5Sopenharmony_ci  if (tc.earlier_env != tc.later_env) {
1169fd4e5da5Sopenharmony_ci    std::unique_ptr<Module> module(new Module());
1170fd4e5da5Sopenharmony_ci    IRContext localContext(tc.earlier_env, std::move(module),
1171fd4e5da5Sopenharmony_ci                           spvtools::MessageConsumer());
1172fd4e5da5Sopenharmony_ci    EXPECT_FALSE(localContext.IsTargetEnvAtLeast(tc.later_env));
1173fd4e5da5Sopenharmony_ci  }
1174fd4e5da5Sopenharmony_ci}
1175fd4e5da5Sopenharmony_ci
1176fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, ReturnsTrueWhenExtensionIsRemoved) {
1177fd4e5da5Sopenharmony_ci  const std::string text = R"(
1178fd4e5da5Sopenharmony_ci               OpCapability Shader
1179fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1180fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1181fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1182fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1183fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1184fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1185fd4e5da5Sopenharmony_ci          %9 = OpLabel
1186fd4e5da5Sopenharmony_ci               OpReturn
1187fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1188fd4e5da5Sopenharmony_ci
1189fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1190fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1191fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1192fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1193fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1194fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1195fd4e5da5Sopenharmony_ci            1);
1196fd4e5da5Sopenharmony_ci
1197fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveExtension(kSPV_KHR_shader_clock));
1198fd4e5da5Sopenharmony_ci
1199fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1200fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1201fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1202fd4e5da5Sopenharmony_ci            0);
1203fd4e5da5Sopenharmony_ci}
1204fd4e5da5Sopenharmony_ci
1205fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, ReturnsFalseWhenExtensionIsNotRemoved) {
1206fd4e5da5Sopenharmony_ci  const std::string text = R"(
1207fd4e5da5Sopenharmony_ci               OpCapability Shader
1208fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_device_group"
1209fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1210fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1211fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1212fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1213fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1214fd4e5da5Sopenharmony_ci          %9 = OpLabel
1215fd4e5da5Sopenharmony_ci               OpReturn
1216fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1217fd4e5da5Sopenharmony_ci
1218fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1219fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1220fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1221fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1222fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1223fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1224fd4e5da5Sopenharmony_ci            1);
1225fd4e5da5Sopenharmony_ci
1226fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->RemoveExtension(kSPV_KHR_shader_clock));
1227fd4e5da5Sopenharmony_ci
1228fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1229fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1230fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1231fd4e5da5Sopenharmony_ci            1);
1232fd4e5da5Sopenharmony_ci}
1233fd4e5da5Sopenharmony_ci
1234fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, RemovesExtensionIfLast) {
1235fd4e5da5Sopenharmony_ci  const std::string text = R"(
1236fd4e5da5Sopenharmony_ci               OpCapability Shader
1237fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_device_group"
1238fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1239fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1240fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1241fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1242fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1243fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1244fd4e5da5Sopenharmony_ci          %9 = OpLabel
1245fd4e5da5Sopenharmony_ci               OpReturn
1246fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1247fd4e5da5Sopenharmony_ci
1248fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1249fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1250fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1251fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1252fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1253fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1254fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1255fd4e5da5Sopenharmony_ci            2);
1256fd4e5da5Sopenharmony_ci
1257fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveExtension(kSPV_KHR_shader_clock));
1258fd4e5da5Sopenharmony_ci
1259fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1260fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1261fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1262fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1263fd4e5da5Sopenharmony_ci            1);
1264fd4e5da5Sopenharmony_ci}
1265fd4e5da5Sopenharmony_ci
1266fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, RemovesExtensionIfFirst) {
1267fd4e5da5Sopenharmony_ci  const std::string text = R"(
1268fd4e5da5Sopenharmony_ci               OpCapability Shader
1269fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1270fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_device_group"
1271fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1272fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1273fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1274fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1275fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1276fd4e5da5Sopenharmony_ci          %9 = OpLabel
1277fd4e5da5Sopenharmony_ci               OpReturn
1278fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1279fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1280fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1281fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1282fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1283fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1284fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1285fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1286fd4e5da5Sopenharmony_ci            2);
1287fd4e5da5Sopenharmony_ci
1288fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveExtension(kSPV_KHR_shader_clock));
1289fd4e5da5Sopenharmony_ci
1290fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_device_group));
1291fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1292fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1293fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1294fd4e5da5Sopenharmony_ci            1);
1295fd4e5da5Sopenharmony_ci}
1296fd4e5da5Sopenharmony_ci
1297fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, RemovesMultipleExtensions) {
1298fd4e5da5Sopenharmony_ci  const std::string text = R"(
1299fd4e5da5Sopenharmony_ci               OpCapability Shader
1300fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1301fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1302fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1303fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1304fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1305fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1306fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1307fd4e5da5Sopenharmony_ci          %9 = OpLabel
1308fd4e5da5Sopenharmony_ci               OpReturn
1309fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1310fd4e5da5Sopenharmony_ci
1311fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1312fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1313fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1314fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1315fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1316fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1317fd4e5da5Sopenharmony_ci            2);
1318fd4e5da5Sopenharmony_ci
1319fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveExtension(kSPV_KHR_shader_clock));
1320fd4e5da5Sopenharmony_ci
1321fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->get_feature_mgr()->HasExtension(kSPV_KHR_shader_clock));
1322fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->extension_begin(),
1323fd4e5da5Sopenharmony_ci                          ctx->module()->extension_end()),
1324fd4e5da5Sopenharmony_ci            0);
1325fd4e5da5Sopenharmony_ci}
1326fd4e5da5Sopenharmony_ci
1327fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, ReturnsTrueWhenCapabilityIsRemoved) {
1328fd4e5da5Sopenharmony_ci  const std::string text = R"(
1329fd4e5da5Sopenharmony_ci               OpCapability Shader
1330fd4e5da5Sopenharmony_ci               OpCapability ShaderClockKHR
1331fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_shader_clock"
1332fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1333fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1334fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1335fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1336fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1337fd4e5da5Sopenharmony_ci          %9 = OpLabel
1338fd4e5da5Sopenharmony_ci               OpReturn
1339fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1340fd4e5da5Sopenharmony_ci
1341fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1342fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1343fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1344fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1345fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::ShaderClockKHR));
1346fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1347fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1348fd4e5da5Sopenharmony_ci            2);
1349fd4e5da5Sopenharmony_ci
1350fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveCapability(spv::Capability::ShaderClockKHR));
1351fd4e5da5Sopenharmony_ci
1352fd4e5da5Sopenharmony_ci  EXPECT_FALSE(
1353fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::ShaderClockKHR));
1354fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1355fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1356fd4e5da5Sopenharmony_ci            1);
1357fd4e5da5Sopenharmony_ci}
1358fd4e5da5Sopenharmony_ci
1359fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, ReturnsFalseWhenCapabilityIsNotRemoved) {
1360fd4e5da5Sopenharmony_ci  const std::string text = R"(
1361fd4e5da5Sopenharmony_ci               OpCapability Shader
1362fd4e5da5Sopenharmony_ci               OpCapability DeviceGroup
1363fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_device_group"
1364fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1365fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1366fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1367fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1368fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1369fd4e5da5Sopenharmony_ci          %9 = OpLabel
1370fd4e5da5Sopenharmony_ci               OpReturn
1371fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1372fd4e5da5Sopenharmony_ci
1373fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1374fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1375fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1376fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1377fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::DeviceGroup));
1378fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1379fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1380fd4e5da5Sopenharmony_ci            2);
1381fd4e5da5Sopenharmony_ci
1382fd4e5da5Sopenharmony_ci  EXPECT_FALSE(ctx->RemoveCapability(spv::Capability::ShaderClockKHR));
1383fd4e5da5Sopenharmony_ci
1384fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1385fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::DeviceGroup));
1386fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1387fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1388fd4e5da5Sopenharmony_ci            2);
1389fd4e5da5Sopenharmony_ci}
1390fd4e5da5Sopenharmony_ci
1391fd4e5da5Sopenharmony_ciTEST_F(IRContextTest, RemovesMultipleCapabilities) {
1392fd4e5da5Sopenharmony_ci  const std::string text = R"(
1393fd4e5da5Sopenharmony_ci               OpCapability Shader
1394fd4e5da5Sopenharmony_ci               OpCapability DeviceGroup
1395fd4e5da5Sopenharmony_ci               OpCapability DeviceGroup
1396fd4e5da5Sopenharmony_ci               OpExtension "SPV_KHR_device_group"
1397fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1398fd4e5da5Sopenharmony_ci               OpEntryPoint GLCompute %1 "main"
1399fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1400fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
1401fd4e5da5Sopenharmony_ci          %1 = OpFunction %void None %6
1402fd4e5da5Sopenharmony_ci          %9 = OpLabel
1403fd4e5da5Sopenharmony_ci               OpReturn
1404fd4e5da5Sopenharmony_ci               OpFunctionEnd)";
1405fd4e5da5Sopenharmony_ci
1406fd4e5da5Sopenharmony_ci  std::unique_ptr<IRContext> ctx =
1407fd4e5da5Sopenharmony_ci      BuildModule(SPV_ENV_UNIVERSAL_1_6, nullptr, text,
1408fd4e5da5Sopenharmony_ci                  SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1409fd4e5da5Sopenharmony_ci  EXPECT_TRUE(
1410fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::DeviceGroup));
1411fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1412fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1413fd4e5da5Sopenharmony_ci            3);
1414fd4e5da5Sopenharmony_ci
1415fd4e5da5Sopenharmony_ci  EXPECT_TRUE(ctx->RemoveCapability(spv::Capability::DeviceGroup));
1416fd4e5da5Sopenharmony_ci
1417fd4e5da5Sopenharmony_ci  EXPECT_FALSE(
1418fd4e5da5Sopenharmony_ci      ctx->get_feature_mgr()->HasCapability(spv::Capability::DeviceGroup));
1419fd4e5da5Sopenharmony_ci  EXPECT_EQ(std::distance(ctx->module()->capability_begin(),
1420fd4e5da5Sopenharmony_ci                          ctx->module()->capability_end()),
1421fd4e5da5Sopenharmony_ci            1);
1422fd4e5da5Sopenharmony_ci}
1423fd4e5da5Sopenharmony_ci
1424fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(
1425fd4e5da5Sopenharmony_ci    TestCase, TargetEnvCompareTest,
1426fd4e5da5Sopenharmony_ci    ::testing::Values(
1427fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_0},
1428fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_1, SPV_ENV_UNIVERSAL_1_0},
1429fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_0},
1430fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_0},
1431fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_UNIVERSAL_1_0},
1432fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_0},
1433fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_0},
1434fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_1, SPV_ENV_UNIVERSAL_1_1},
1435fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_1},
1436fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_1},
1437fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_UNIVERSAL_1_1},
1438fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_1},
1439fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_1},
1440fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_2},
1441fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_2},
1442fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_UNIVERSAL_1_2},
1443fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_2},
1444fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_2},
1445fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_3},
1446fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_UNIVERSAL_1_3},
1447fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_3},
1448fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_3},
1449fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_UNIVERSAL_1_4},
1450fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_4},
1451fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_4},
1452fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_UNIVERSAL_1_5},
1453fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_5},
1454fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_UNIVERSAL_1_6},
1455fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_0, SPV_ENV_UNIVERSAL_1_0},
1456fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_1, SPV_ENV_VULKAN_1_0},
1457fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_2, SPV_ENV_VULKAN_1_0},
1458fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_3, SPV_ENV_VULKAN_1_0},
1459fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_VULKAN_1_0},
1460fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_VULKAN_1_0},
1461fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_VULKAN_1_0},
1462fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_1, SPV_ENV_UNIVERSAL_1_0},
1463fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_1, SPV_ENV_UNIVERSAL_1_1},
1464fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_1, SPV_ENV_UNIVERSAL_1_2},
1465fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_1, SPV_ENV_UNIVERSAL_1_3},
1466fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_4, SPV_ENV_VULKAN_1_1},
1467fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_5, SPV_ENV_VULKAN_1_1},
1468fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_VULKAN_1_1},
1469fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_0},
1470fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_1},
1471fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_2},
1472fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_3},
1473fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_4},
1474fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_2, SPV_ENV_UNIVERSAL_1_5},
1475fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_UNIVERSAL_1_6, SPV_ENV_VULKAN_1_2},
1476fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_0},
1477fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_1},
1478fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_2},
1479fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_3},
1480fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_4},
1481fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_5},
1482fd4e5da5Sopenharmony_ci        TargetEnvCompareTestData{SPV_ENV_VULKAN_1_3, SPV_ENV_UNIVERSAL_1_6}));
1483fd4e5da5Sopenharmony_ci
1484fd4e5da5Sopenharmony_ci}  // namespace
1485fd4e5da5Sopenharmony_ci}  // namespace opt
1486fd4e5da5Sopenharmony_ci}  // namespace spvtools
1487