1 // Copyright (c) 2017 Pierre Moreau
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/opt/build_module.h"
22 #include "source/opt/decoration_manager.h"
23 #include "source/opt/ir_context.h"
24 #include "source/spirv_constant.h"
25 #include "source/util/string_utils.h"
26 #include "test/unit_spirv.h"
27
28 namespace spvtools {
29 namespace opt {
30 namespace analysis {
31 namespace {
32
33 using utils::MakeVector;
34
35 class DecorationManagerTest : public ::testing::Test {
36 public:
DecorationManagerTest()37 DecorationManagerTest()
38 : tools_(SPV_ENV_UNIVERSAL_1_2),
39 context_(),
40 consumer_([this](spv_message_level_t level, const char*,
41 const spv_position_t& position, const char* message) {
42 if (!error_message_.empty()) error_message_ += "\n";
43 switch (level) {
44 case SPV_MSG_FATAL:
45 case SPV_MSG_INTERNAL_ERROR:
46 case SPV_MSG_ERROR:
47 error_message_ += "ERROR";
48 break;
49 case SPV_MSG_WARNING:
50 error_message_ += "WARNING";
51 break;
52 case SPV_MSG_INFO:
53 error_message_ += "INFO";
54 break;
55 case SPV_MSG_DEBUG:
56 error_message_ += "DEBUG";
57 break;
58 }
59 error_message_ +=
60 ": " + std::to_string(position.index) + ": " + message;
61 }),
62 disassemble_options_(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER),
63 error_message_() {
64 tools_.SetMessageConsumer(consumer_);
65 }
66
TearDown()67 void TearDown() override { error_message_.clear(); }
68
GetDecorationManager(const std::string & text)69 DecorationManager* GetDecorationManager(const std::string& text) {
70 context_ = BuildModule(SPV_ENV_UNIVERSAL_1_2, consumer_, text);
71 if (context_.get())
72 return context_->get_decoration_mgr();
73 else
74 return nullptr;
75 }
76
77 // Disassembles |binary| and outputs the result in |text|. If |text| is a
78 // null pointer, SPV_ERROR_INVALID_POINTER is returned.
Disassemble(const std::vector<uint32_t> & binary,std::string * text)79 spv_result_t Disassemble(const std::vector<uint32_t>& binary,
80 std::string* text) {
81 if (!text) return SPV_ERROR_INVALID_POINTER;
82 return tools_.Disassemble(binary, text, disassemble_options_)
83 ? SPV_SUCCESS
84 : SPV_ERROR_INVALID_BINARY;
85 }
86
87 // Returns the accumulated error messages for the test.
GetErrorMessage() const88 std::string GetErrorMessage() const { return error_message_; }
89
ToText(const std::vector<Instruction * > & inst)90 std::string ToText(const std::vector<Instruction*>& inst) {
91 std::vector<uint32_t> binary = {SpvMagicNumber, 0x10200, 0u, 2u, 0u};
92 for (const Instruction* i : inst)
93 i->ToBinaryWithoutAttachedDebugInsts(&binary);
94 std::string text;
95 Disassemble(binary, &text);
96 return text;
97 }
98
ModuleToText()99 std::string ModuleToText() {
100 std::vector<uint32_t> binary;
101 context_->module()->ToBinary(&binary, false);
102 std::string text;
103 Disassemble(binary, &text);
104 return text;
105 }
106
GetConsumer()107 spvtools::MessageConsumer GetConsumer() { return consumer_; }
108
109 private:
110 // An instance for calling SPIRV-Tools functionalities.
111 spvtools::SpirvTools tools_;
112 std::unique_ptr<IRContext> context_;
113 spvtools::MessageConsumer consumer_;
114 uint32_t disassemble_options_;
115 std::string error_message_;
116 };
117
TEST_F(DecorationManagerTest,ComparingDecorationsWithDiffOpcodesDecorateDecorateId)118 TEST_F(DecorationManagerTest,
119 ComparingDecorationsWithDiffOpcodesDecorateDecorateId) {
120 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
121 // This parameter can be interpreted both as { SpvDecorationConstant }
122 // and also as a list of IDs: { 22 }
123 const std::vector<uint32_t> param{SpvDecorationConstant};
124 // OpDecorate %1 Constant
125 Instruction inst1(
126 &ir_context, SpvOpDecorate, 0u, 0u,
127 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, param}});
128 // OpDecorateId %1 %22 ; 'Constant' is decoration number 22
129 Instruction inst2(
130 &ir_context, SpvOpDecorateId, 0u, 0u,
131 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, param}});
132 DecorationManager* decoManager = ir_context.get_decoration_mgr();
133 EXPECT_THAT(GetErrorMessage(), "");
134 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
135 }
136
TEST_F(DecorationManagerTest,ComparingDecorationsWithDiffOpcodesDecorateDecorateString)137 TEST_F(DecorationManagerTest,
138 ComparingDecorationsWithDiffOpcodesDecorateDecorateString) {
139 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
140 // This parameter can be interpreted both as { SpvDecorationConstant }
141 // and also as a null-terminated string with a single character with value 22.
142 const std::vector<uint32_t> param{SpvDecorationConstant};
143 // OpDecorate %1 Constant
144 Instruction inst1(
145 &ir_context, SpvOpDecorate, 0u, 0u,
146 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, param}});
147 // OpDecorateStringGOOGLE %1 !22
148 Instruction inst2(
149 &ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
150 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_LITERAL_STRING, param}});
151 DecorationManager* decoManager = ir_context.get_decoration_mgr();
152 EXPECT_THAT(GetErrorMessage(), "");
153 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
154 }
155
TEST_F(DecorationManagerTest,ComparingDecorationsWithDiffDecorateParam)156 TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateParam) {
157 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
158 // OpDecorate %1 Constant
159 Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
160 {{SPV_OPERAND_TYPE_ID, {1u}},
161 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
162 // OpDecorate %1 Restrict
163 Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
164 {{SPV_OPERAND_TYPE_ID, {1u}},
165 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationRestrict}}});
166 DecorationManager* decoManager = ir_context.get_decoration_mgr();
167 EXPECT_THAT(GetErrorMessage(), "");
168 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
169 }
170
TEST_F(DecorationManagerTest,ComparingDecorationsWithDiffDecorateIdParam)171 TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateIdParam) {
172 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
173 // OpDecorate %1 Constant
174 Instruction inst1(
175 &ir_context, SpvOpDecorateId, 0u, 0u,
176 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, {555}}});
177 // OpDecorate %1 Restrict
178 Instruction inst2(
179 &ir_context, SpvOpDecorateId, 0u, 0u,
180 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, {666}}});
181 DecorationManager* decoManager = ir_context.get_decoration_mgr();
182 EXPECT_THAT(GetErrorMessage(), "");
183 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
184 }
185
TEST_F(DecorationManagerTest,ComparingDecorationsWithDiffDecorateStringParam)186 TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateStringParam) {
187 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
188 // OpDecorate %1 Constant
189 Instruction inst1(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
190 {{SPV_OPERAND_TYPE_ID, {1u}},
191 {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("Hello!")}});
192 // OpDecorate %1 Restrict
193 Instruction inst2(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
194 {{SPV_OPERAND_TYPE_ID, {1u}},
195 {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("Hellx")}});
196 DecorationManager* decoManager = ir_context.get_decoration_mgr();
197 EXPECT_THAT(GetErrorMessage(), "");
198 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
199 }
200
TEST_F(DecorationManagerTest,ComparingSameDecorationsOnDiffTargetAllowed)201 TEST_F(DecorationManagerTest, ComparingSameDecorationsOnDiffTargetAllowed) {
202 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
203 // OpDecorate %1 Constant
204 Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
205 {{SPV_OPERAND_TYPE_ID, {1u}},
206 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
207 // OpDecorate %2 Constant
208 Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
209 {{SPV_OPERAND_TYPE_ID, {2u}},
210 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
211 DecorationManager* decoManager = ir_context.get_decoration_mgr();
212 EXPECT_THAT(GetErrorMessage(), "");
213 EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
214 }
215
TEST_F(DecorationManagerTest,ComparingSameDecorationIdsOnDiffTargetAllowed)216 TEST_F(DecorationManagerTest, ComparingSameDecorationIdsOnDiffTargetAllowed) {
217 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
218 Instruction inst1(
219 &ir_context, SpvOpDecorateId, 0u, 0u,
220 {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, {44}}});
221 Instruction inst2(
222 &ir_context, SpvOpDecorateId, 0u, 0u,
223 {{SPV_OPERAND_TYPE_ID, {2u}}, {SPV_OPERAND_TYPE_DECORATION, {44}}});
224 DecorationManager* decoManager = ir_context.get_decoration_mgr();
225 EXPECT_THAT(GetErrorMessage(), "");
226 EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
227 }
228
TEST_F(DecorationManagerTest,ComparingSameDecorationStringsOnDiffTargetAllowed)229 TEST_F(DecorationManagerTest,
230 ComparingSameDecorationStringsOnDiffTargetAllowed) {
231 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
232 Instruction inst1(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
233 {{SPV_OPERAND_TYPE_ID, {1u}},
234 {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("hello")}});
235 Instruction inst2(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
236 {{SPV_OPERAND_TYPE_ID, {2u}},
237 {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("hello")}});
238 DecorationManager* decoManager = ir_context.get_decoration_mgr();
239 EXPECT_THAT(GetErrorMessage(), "");
240 EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
241 }
242
TEST_F(DecorationManagerTest,ComparingSameDecorationsOnDiffTargetDisallowed)243 TEST_F(DecorationManagerTest, ComparingSameDecorationsOnDiffTargetDisallowed) {
244 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
245 // OpDecorate %1 Constant
246 Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
247 {{SPV_OPERAND_TYPE_ID, {1u}},
248 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
249 // OpDecorate %2 Constant
250 Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
251 {{SPV_OPERAND_TYPE_ID, {2u}},
252 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
253 DecorationManager* decoManager = ir_context.get_decoration_mgr();
254 EXPECT_THAT(GetErrorMessage(), "");
255 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, false));
256 }
257
TEST_F(DecorationManagerTest,ComparingMemberDecorationsOnSameTypeDiffMember)258 TEST_F(DecorationManagerTest, ComparingMemberDecorationsOnSameTypeDiffMember) {
259 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
260 // OpMemberDecorate %1 0 Constant
261 Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
262 {{SPV_OPERAND_TYPE_ID, {1u}},
263 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
264 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
265 // OpMemberDecorate %1 1 Constant
266 Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
267 {{SPV_OPERAND_TYPE_ID, {1u}},
268 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {1u}},
269 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
270 DecorationManager* decoManager = ir_context.get_decoration_mgr();
271 EXPECT_THAT(GetErrorMessage(), "");
272 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
273 }
274
TEST_F(DecorationManagerTest,ComparingSameMemberDecorationsOnDiffTargetAllowed)275 TEST_F(DecorationManagerTest,
276 ComparingSameMemberDecorationsOnDiffTargetAllowed) {
277 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
278 // OpMemberDecorate %1 0 Constant
279 Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
280 {{SPV_OPERAND_TYPE_ID, {1u}},
281 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
282 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
283 // OpMemberDecorate %2 0 Constant
284 Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
285 {{SPV_OPERAND_TYPE_ID, {2u}},
286 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
287 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
288 DecorationManager* decoManager = ir_context.get_decoration_mgr();
289 EXPECT_THAT(GetErrorMessage(), "");
290 EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
291 }
292
TEST_F(DecorationManagerTest,ComparingSameMemberDecorationsOnDiffTargetDisallowed)293 TEST_F(DecorationManagerTest,
294 ComparingSameMemberDecorationsOnDiffTargetDisallowed) {
295 IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
296 // OpMemberDecorate %1 0 Constant
297 Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
298 {{SPV_OPERAND_TYPE_ID, {1u}},
299 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
300 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
301 // OpMemberDecorate %2 0 Constant
302 Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
303 {{SPV_OPERAND_TYPE_ID, {2u}},
304 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
305 {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
306 DecorationManager* decoManager = ir_context.get_decoration_mgr();
307 EXPECT_THAT(GetErrorMessage(), "");
308 EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, false));
309 }
310
TEST_F(DecorationManagerTest,RemoveDecorationFromVariable)311 TEST_F(DecorationManagerTest, RemoveDecorationFromVariable) {
312 const std::string spirv = R"(
313 OpCapability Shader
314 OpCapability Linkage
315 OpMemoryModel Logical GLSL450
316 OpDecorate %1 Constant
317 OpDecorate %2 Restrict
318 %2 = OpDecorationGroup
319 OpGroupDecorate %2 %1 %3
320 %4 = OpTypeInt 32 0
321 %1 = OpVariable %4 Uniform
322 %3 = OpVariable %4 Uniform
323 )";
324 DecorationManager* decoManager = GetDecorationManager(spirv);
325 EXPECT_THAT(GetErrorMessage(), "");
326 decoManager->RemoveDecorationsFrom(1u);
327 auto decorations = decoManager->GetDecorationsFor(1u, false);
328 EXPECT_THAT(GetErrorMessage(), "");
329 EXPECT_TRUE(decorations.empty());
330 decorations = decoManager->GetDecorationsFor(3u, false);
331 EXPECT_THAT(GetErrorMessage(), "");
332
333 const std::string expected_decorations = R"(OpDecorate %2 Restrict
334 )";
335 EXPECT_THAT(ToText(decorations), expected_decorations);
336
337 const std::string expected_binary = R"(OpCapability Shader
338 OpCapability Linkage
339 OpMemoryModel Logical GLSL450
340 OpDecorate %2 Restrict
341 %2 = OpDecorationGroup
342 OpGroupDecorate %2 %3
343 %4 = OpTypeInt 32 0
344 %1 = OpVariable %4 Uniform
345 %3 = OpVariable %4 Uniform
346 )";
347 EXPECT_THAT(ModuleToText(), expected_binary);
348 }
349
TEST_F(DecorationManagerTest,RemoveDecorationStringFromVariable)350 TEST_F(DecorationManagerTest, RemoveDecorationStringFromVariable) {
351 const std::string spirv = R"(
352 OpCapability Shader
353 OpCapability Linkage
354 OpExtension "SPV_GOOGLE_hlsl_functionality1"
355 OpExtension "SPV_GOOGLE_decorate_string"
356 OpMemoryModel Logical GLSL450
357 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello world"
358 OpDecorate %2 Restrict
359 %2 = OpDecorationGroup
360 OpGroupDecorate %2 %1 %3
361 %4 = OpTypeInt 32 0
362 %1 = OpVariable %4 Uniform
363 %3 = OpVariable %4 Uniform
364 )";
365 DecorationManager* decoManager = GetDecorationManager(spirv);
366 EXPECT_THAT(GetErrorMessage(), "");
367 decoManager->RemoveDecorationsFrom(1u);
368 auto decorations = decoManager->GetDecorationsFor(1u, false);
369 EXPECT_THAT(GetErrorMessage(), "");
370 EXPECT_TRUE(decorations.empty());
371 decorations = decoManager->GetDecorationsFor(3u, false);
372 EXPECT_THAT(GetErrorMessage(), "");
373
374 const std::string expected_decorations = R"(OpDecorate %2 Restrict
375 )";
376 EXPECT_THAT(ToText(decorations), expected_decorations);
377
378 const std::string expected_binary = R"(OpCapability Shader
379 OpCapability Linkage
380 OpExtension "SPV_GOOGLE_hlsl_functionality1"
381 OpExtension "SPV_GOOGLE_decorate_string"
382 OpMemoryModel Logical GLSL450
383 OpDecorate %2 Restrict
384 %2 = OpDecorationGroup
385 OpGroupDecorate %2 %3
386 %4 = OpTypeInt 32 0
387 %1 = OpVariable %4 Uniform
388 %3 = OpVariable %4 Uniform
389 )";
390 EXPECT_THAT(ModuleToText(), expected_binary);
391 }
392
TEST_F(DecorationManagerTest,RemoveDecorationFromDecorationGroup)393 TEST_F(DecorationManagerTest, RemoveDecorationFromDecorationGroup) {
394 const std::string spirv = R"(
395 OpCapability Shader
396 OpCapability Linkage
397 OpMemoryModel Logical GLSL450
398 OpDecorate %1 Constant
399 OpDecorate %2 Restrict
400 %2 = OpDecorationGroup
401 OpGroupDecorate %2 %1 %3
402 %4 = OpTypeInt 32 0
403 %1 = OpVariable %4 Uniform
404 %3 = OpVariable %4 Uniform
405 )";
406 DecorationManager* decoManager = GetDecorationManager(spirv);
407 EXPECT_THAT(GetErrorMessage(), "");
408 decoManager->RemoveDecorationsFrom(2u);
409 auto decorations = decoManager->GetDecorationsFor(2u, false);
410 EXPECT_THAT(GetErrorMessage(), "");
411 EXPECT_TRUE(decorations.empty());
412 decorations = decoManager->GetDecorationsFor(1u, false);
413 EXPECT_THAT(GetErrorMessage(), "");
414
415 const std::string expected_decorations = R"(OpDecorate %1 Constant
416 )";
417 EXPECT_THAT(ToText(decorations), expected_decorations);
418 decorations = decoManager->GetDecorationsFor(3u, false);
419 EXPECT_THAT(GetErrorMessage(), "");
420 EXPECT_THAT(ToText(decorations), "");
421
422 const std::string expected_binary = R"(OpCapability Shader
423 OpCapability Linkage
424 OpMemoryModel Logical GLSL450
425 OpDecorate %1 Constant
426 %2 = OpDecorationGroup
427 %4 = OpTypeInt 32 0
428 %1 = OpVariable %4 Uniform
429 %3 = OpVariable %4 Uniform
430 )";
431 EXPECT_THAT(ModuleToText(), expected_binary);
432 }
433
TEST_F(DecorationManagerTest,RemoveDecorationFromDecorationGroupKeepDeadDecorations)434 TEST_F(DecorationManagerTest,
435 RemoveDecorationFromDecorationGroupKeepDeadDecorations) {
436 const std::string spirv = R"(
437 OpCapability Shader
438 OpCapability Linkage
439 OpMemoryModel Logical GLSL450
440 OpDecorate %1 Constant
441 OpDecorate %2 Restrict
442 %2 = OpDecorationGroup
443 OpGroupDecorate %2 %1
444 %3 = OpTypeInt 32 0
445 %1 = OpVariable %3 Uniform
446 )";
447 DecorationManager* decoManager = GetDecorationManager(spirv);
448 EXPECT_THAT(GetErrorMessage(), "");
449 decoManager->RemoveDecorationsFrom(1u);
450 auto decorations = decoManager->GetDecorationsFor(1u, false);
451 EXPECT_THAT(GetErrorMessage(), "");
452 EXPECT_TRUE(decorations.empty());
453 decorations = decoManager->GetDecorationsFor(2u, false);
454 EXPECT_THAT(GetErrorMessage(), "");
455
456 const std::string expected_decorations = R"(OpDecorate %2 Restrict
457 )";
458 EXPECT_THAT(ToText(decorations), expected_decorations);
459
460 const std::string expected_binary = R"(OpCapability Shader
461 OpCapability Linkage
462 OpMemoryModel Logical GLSL450
463 OpDecorate %2 Restrict
464 %2 = OpDecorationGroup
465 %3 = OpTypeInt 32 0
466 %1 = OpVariable %3 Uniform
467 )";
468 EXPECT_THAT(ModuleToText(), expected_binary);
469 }
470
TEST_F(DecorationManagerTest,RemoveAllDecorationsAppliedByGroup)471 TEST_F(DecorationManagerTest, RemoveAllDecorationsAppliedByGroup) {
472 const std::string spirv = R"(
473 OpCapability Shader
474 OpCapability Linkage
475 OpMemoryModel Logical GLSL450
476 OpDecorate %1 Constant
477 OpDecorate %2 Restrict
478 %2 = OpDecorationGroup
479 OpGroupDecorate %2 %1
480 OpDecorate %3 BuiltIn VertexId
481 %3 = OpDecorationGroup
482 OpGroupDecorate %3 %1
483 %4 = OpTypeInt 32 0
484 %1 = OpVariable %4 Input
485 )";
486 DecorationManager* decoManager = GetDecorationManager(spirv);
487 EXPECT_THAT(GetErrorMessage(), "");
488 decoManager->RemoveDecorationsFrom(1u, [](const Instruction& inst) {
489 return inst.opcode() == SpvOpDecorate &&
490 inst.GetSingleWordInOperand(0u) == 3u;
491 });
492 auto decorations = decoManager->GetDecorationsFor(1u, false);
493 EXPECT_THAT(GetErrorMessage(), "");
494
495 std::string expected_decorations = R"(OpDecorate %1 Constant
496 OpDecorate %2 Restrict
497 )";
498 EXPECT_THAT(ToText(decorations), expected_decorations);
499 decorations = decoManager->GetDecorationsFor(2u, false);
500 EXPECT_THAT(GetErrorMessage(), "");
501
502 expected_decorations = R"(OpDecorate %2 Restrict
503 )";
504 EXPECT_THAT(ToText(decorations), expected_decorations);
505
506 const std::string expected_binary = R"(OpCapability Shader
507 OpCapability Linkage
508 OpMemoryModel Logical GLSL450
509 OpDecorate %1 Constant
510 OpDecorate %2 Restrict
511 %2 = OpDecorationGroup
512 OpGroupDecorate %2 %1
513 OpDecorate %3 BuiltIn VertexId
514 %3 = OpDecorationGroup
515 %4 = OpTypeInt 32 0
516 %1 = OpVariable %4 Input
517 )";
518 EXPECT_THAT(ModuleToText(), expected_binary);
519 }
520
TEST_F(DecorationManagerTest,RemoveSomeDecorationsAppliedByGroup)521 TEST_F(DecorationManagerTest, RemoveSomeDecorationsAppliedByGroup) {
522 const std::string spirv = R"(
523 OpCapability Shader
524 OpCapability Linkage
525 OpMemoryModel Logical GLSL450
526 OpDecorate %1 Constant
527 OpDecorate %2 Restrict
528 %2 = OpDecorationGroup
529 OpGroupDecorate %2 %1
530 OpDecorate %3 BuiltIn VertexId
531 OpDecorate %3 Invariant
532 %3 = OpDecorationGroup
533 OpGroupDecorate %3 %1
534 %uint = OpTypeInt 32 0
535 %1 = OpVariable %uint Input
536 )";
537 DecorationManager* decoManager = GetDecorationManager(spirv);
538 EXPECT_THAT(GetErrorMessage(), "");
539 decoManager->RemoveDecorationsFrom(1u, [](const Instruction& inst) {
540 return inst.opcode() == SpvOpDecorate &&
541 inst.GetSingleWordInOperand(0u) == 3u &&
542 inst.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn;
543 });
544 auto decorations = decoManager->GetDecorationsFor(1u, false);
545 EXPECT_THAT(GetErrorMessage(), "");
546
547 std::string expected_decorations = R"(OpDecorate %1 Constant
548 OpDecorate %1 Invariant
549 OpDecorate %2 Restrict
550 )";
551 EXPECT_THAT(ToText(decorations), expected_decorations);
552 decorations = decoManager->GetDecorationsFor(2u, false);
553 EXPECT_THAT(GetErrorMessage(), "");
554
555 expected_decorations = R"(OpDecorate %2 Restrict
556 )";
557 EXPECT_THAT(ToText(decorations), expected_decorations);
558
559 const std::string expected_binary = R"(OpCapability Shader
560 OpCapability Linkage
561 OpMemoryModel Logical GLSL450
562 OpDecorate %1 Constant
563 OpDecorate %2 Restrict
564 %2 = OpDecorationGroup
565 OpGroupDecorate %2 %1
566 OpDecorate %3 BuiltIn VertexId
567 OpDecorate %3 Invariant
568 %3 = OpDecorationGroup
569 OpDecorate %1 Invariant
570 %4 = OpTypeInt 32 0
571 %1 = OpVariable %4 Input
572 )";
573 EXPECT_THAT(ModuleToText(), expected_binary);
574 }
575
TEST_F(DecorationManagerTest,RemoveDecorationDecorate)576 TEST_F(DecorationManagerTest, RemoveDecorationDecorate) {
577 const std::string spirv = R"(
578 OpCapability Shader
579 OpCapability Linkage
580 OpMemoryModel Logical GLSL450
581 OpDecorate %1 Constant
582 OpDecorate %1 Restrict
583 %2 = OpTypeInt 32 0
584 %1 = OpVariable %2 Uniform
585 )";
586 DecorationManager* decoManager = GetDecorationManager(spirv);
587 EXPECT_THAT(GetErrorMessage(), "");
588 auto decorations = decoManager->GetDecorationsFor(1u, false);
589 decoManager->RemoveDecoration(decorations.front());
590 decorations = decoManager->GetDecorationsFor(1u, false);
591 EXPECT_THAT(GetErrorMessage(), "");
592
593 const std::string expected_decorations = R"(OpDecorate %1 Restrict
594 )";
595 EXPECT_THAT(ToText(decorations), expected_decorations);
596 }
597
TEST_F(DecorationManagerTest,RemoveDecorationStringDecorate)598 TEST_F(DecorationManagerTest, RemoveDecorationStringDecorate) {
599 const std::string spirv = R"(
600 OpCapability Shader
601 OpCapability Linkage
602 OpExtension "SPV_GOOGLE_hlsl_functionality1"
603 OpExtension "SPV_GOOGLE_decorate_string"
604 OpMemoryModel Logical GLSL450
605 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "foobar"
606 OpDecorate %1 Restrict
607 %2 = OpTypeInt 32 0
608 %1 = OpVariable %2 Uniform
609 )";
610 DecorationManager* decoManager = GetDecorationManager(spirv);
611 EXPECT_THAT(GetErrorMessage(), "");
612 auto decorations = decoManager->GetDecorationsFor(1u, false);
613 decoManager->RemoveDecoration(decorations.front());
614 decorations = decoManager->GetDecorationsFor(1u, false);
615 EXPECT_THAT(GetErrorMessage(), "");
616
617 const std::string expected_decorations = R"(OpDecorate %1 Restrict
618 )";
619 EXPECT_THAT(ToText(decorations), expected_decorations);
620 }
621
TEST_F(DecorationManagerTest,CloneDecorations)622 TEST_F(DecorationManagerTest, CloneDecorations) {
623 const std::string spirv = R"(
624 OpCapability Shader
625 OpCapability Linkage
626 OpMemoryModel Logical GLSL450
627 OpDecorate %1 Constant
628 OpDecorate %2 Restrict
629 %2 = OpDecorationGroup
630 OpGroupDecorate %2 %1
631 OpDecorate %3 BuiltIn VertexId
632 OpDecorate %3 Invariant
633 %3 = OpDecorationGroup
634 OpGroupDecorate %3 %1
635 %4 = OpTypeInt 32 0
636 %1 = OpVariable %4 Input
637 %5 = OpVariable %4 Input
638 )";
639 DecorationManager* decoManager = GetDecorationManager(spirv);
640 EXPECT_THAT(GetErrorMessage(), "");
641
642 // Check cloning OpDecorate including group decorations.
643 auto decorations = decoManager->GetDecorationsFor(5u, false);
644 EXPECT_THAT(GetErrorMessage(), "");
645 EXPECT_TRUE(decorations.empty());
646
647 decoManager->CloneDecorations(1u, 5u);
648 decorations = decoManager->GetDecorationsFor(5u, false);
649 EXPECT_THAT(GetErrorMessage(), "");
650
651 std::string expected_decorations = R"(OpDecorate %5 Constant
652 OpDecorate %2 Restrict
653 OpDecorate %3 BuiltIn VertexId
654 OpDecorate %3 Invariant
655 )";
656 EXPECT_THAT(ToText(decorations), expected_decorations);
657
658 // Check that bookkeeping for ID 2 remains the same.
659 decorations = decoManager->GetDecorationsFor(2u, false);
660 EXPECT_THAT(GetErrorMessage(), "");
661
662 expected_decorations = R"(OpDecorate %2 Restrict
663 )";
664 EXPECT_THAT(ToText(decorations), expected_decorations);
665
666 const std::string expected_binary = R"(OpCapability Shader
667 OpCapability Linkage
668 OpMemoryModel Logical GLSL450
669 OpDecorate %1 Constant
670 OpDecorate %2 Restrict
671 %2 = OpDecorationGroup
672 OpGroupDecorate %2 %1 %5
673 OpDecorate %3 BuiltIn VertexId
674 OpDecorate %3 Invariant
675 %3 = OpDecorationGroup
676 OpGroupDecorate %3 %1 %5
677 OpDecorate %5 Constant
678 %4 = OpTypeInt 32 0
679 %1 = OpVariable %4 Input
680 %5 = OpVariable %4 Input
681 )";
682 EXPECT_THAT(ModuleToText(), expected_binary);
683 }
684
TEST_F(DecorationManagerTest,CloneDecorationsStringAndId)685 TEST_F(DecorationManagerTest, CloneDecorationsStringAndId) {
686 const std::string spirv = R"(OpCapability Shader
687 OpCapability Linkage
688 OpExtension "SPV_GOOGLE_hlsl_functionality1"
689 OpExtension "SPV_GOOGLE_decorate_string"
690 OpMemoryModel Logical GLSL450
691 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "blah"
692 OpDecorateId %1 HlslCounterBufferGOOGLE %2
693 OpDecorate %1 Aliased
694 %3 = OpTypeInt 32 0
695 %4 = OpTypePointer Uniform %3
696 %1 = OpVariable %4 Uniform
697 %2 = OpVariable %4 Uniform
698 %5 = OpVariable %4 Uniform
699 )";
700 DecorationManager* decoManager = GetDecorationManager(spirv);
701 EXPECT_THAT(GetErrorMessage(), "");
702
703 // Check cloning OpDecorate including group decorations.
704 auto decorations = decoManager->GetDecorationsFor(5u, false);
705 EXPECT_THAT(GetErrorMessage(), "");
706 EXPECT_TRUE(decorations.empty());
707
708 decoManager->CloneDecorations(1u, 5u);
709 decorations = decoManager->GetDecorationsFor(5u, false);
710 EXPECT_THAT(GetErrorMessage(), "");
711
712 std::string expected_decorations =
713 R"(OpDecorateString %5 UserSemantic "blah"
714 OpDecorateId %5 CounterBuffer %2
715 OpDecorate %5 Aliased
716 )";
717 EXPECT_THAT(ToText(decorations), expected_decorations);
718
719 const std::string expected_binary = R"(OpCapability Shader
720 OpCapability Linkage
721 OpExtension "SPV_GOOGLE_hlsl_functionality1"
722 OpExtension "SPV_GOOGLE_decorate_string"
723 OpMemoryModel Logical GLSL450
724 OpDecorateString %1 UserSemantic "blah"
725 OpDecorateId %1 CounterBuffer %2
726 OpDecorate %1 Aliased
727 OpDecorateString %5 UserSemantic "blah"
728 OpDecorateId %5 CounterBuffer %2
729 OpDecorate %5 Aliased
730 %3 = OpTypeInt 32 0
731 %4 = OpTypePointer Uniform %3
732 %1 = OpVariable %4 Uniform
733 %2 = OpVariable %4 Uniform
734 %5 = OpVariable %4 Uniform
735 )";
736 EXPECT_THAT(ModuleToText(), expected_binary);
737 }
738
TEST_F(DecorationManagerTest,CloneSomeDecorations)739 TEST_F(DecorationManagerTest, CloneSomeDecorations) {
740 const std::string spirv = R"(OpCapability Shader
741 OpCapability Linkage
742 OpExtension "SPV_GOOGLE_hlsl_functionality1"
743 OpExtension "SPV_GOOGLE_decorate_string"
744 OpMemoryModel Logical GLSL450
745 OpDecorate %1 RelaxedPrecision
746 OpDecorate %1 Restrict
747 %2 = OpTypeInt 32 0
748 %3 = OpTypePointer Function %2
749 %4 = OpTypeVoid
750 %5 = OpTypeFunction %4
751 %6 = OpFunction %4 None %5
752 %7 = OpLabel
753 %1 = OpVariable %3 Function
754 %8 = OpUndef %2
755 OpReturn
756 OpFunctionEnd
757 )";
758 DecorationManager* decoManager = GetDecorationManager(spirv);
759 EXPECT_EQ(GetErrorMessage(), "");
760
761 // Check cloning OpDecorate including group decorations.
762 auto decorations = decoManager->GetDecorationsFor(8u, false);
763 EXPECT_EQ(GetErrorMessage(), "");
764 EXPECT_TRUE(decorations.empty());
765
766 decoManager->CloneDecorations(1u, 8u, {SpvDecorationRelaxedPrecision});
767 decorations = decoManager->GetDecorationsFor(8u, false);
768 EXPECT_THAT(GetErrorMessage(), "");
769
770 std::string expected_decorations =
771 R"(OpDecorate %8 RelaxedPrecision
772 )";
773 EXPECT_EQ(ToText(decorations), expected_decorations);
774
775 const std::string expected_binary = R"(OpCapability Shader
776 OpCapability Linkage
777 OpExtension "SPV_GOOGLE_hlsl_functionality1"
778 OpExtension "SPV_GOOGLE_decorate_string"
779 OpMemoryModel Logical GLSL450
780 OpDecorate %1 RelaxedPrecision
781 OpDecorate %1 Restrict
782 OpDecorate %8 RelaxedPrecision
783 %2 = OpTypeInt 32 0
784 %3 = OpTypePointer Function %2
785 %4 = OpTypeVoid
786 %5 = OpTypeFunction %4
787 %6 = OpFunction %4 None %5
788 %7 = OpLabel
789 %1 = OpVariable %3 Function
790 %8 = OpUndef %2
791 OpReturn
792 OpFunctionEnd
793 )";
794 EXPECT_EQ(ModuleToText(), expected_binary);
795 }
796
797 // Test cloning decoration for an id that is decorated via a group decoration.
TEST_F(DecorationManagerTest,CloneSomeGroupDecorations)798 TEST_F(DecorationManagerTest, CloneSomeGroupDecorations) {
799 const std::string spirv = R"(OpCapability Shader
800 OpCapability Linkage
801 OpMemoryModel Logical GLSL450
802 OpDecorate %1 RelaxedPrecision
803 OpDecorate %1 Restrict
804 %1 = OpDecorationGroup
805 OpGroupDecorate %1 %2
806 %3 = OpTypeInt 32 0
807 %4 = OpTypePointer Function %3
808 %5 = OpTypeVoid
809 %6 = OpTypeFunction %5
810 %7 = OpFunction %5 None %6
811 %8 = OpLabel
812 %2 = OpVariable %4 Function
813 %9 = OpUndef %3
814 OpReturn
815 OpFunctionEnd
816 )";
817 DecorationManager* decoManager = GetDecorationManager(spirv);
818 EXPECT_EQ(GetErrorMessage(), "");
819
820 // Check cloning OpDecorate including group decorations.
821 auto decorations = decoManager->GetDecorationsFor(9u, false);
822 EXPECT_EQ(GetErrorMessage(), "");
823 EXPECT_TRUE(decorations.empty());
824
825 decoManager->CloneDecorations(2u, 9u, {SpvDecorationRelaxedPrecision});
826 decorations = decoManager->GetDecorationsFor(9u, false);
827 EXPECT_THAT(GetErrorMessage(), "");
828
829 std::string expected_decorations =
830 R"(OpDecorate %9 RelaxedPrecision
831 )";
832 EXPECT_EQ(ToText(decorations), expected_decorations);
833
834 const std::string expected_binary = R"(OpCapability Shader
835 OpCapability Linkage
836 OpMemoryModel Logical GLSL450
837 OpDecorate %1 RelaxedPrecision
838 OpDecorate %1 Restrict
839 %1 = OpDecorationGroup
840 OpGroupDecorate %1 %2
841 OpDecorate %9 RelaxedPrecision
842 %3 = OpTypeInt 32 0
843 %4 = OpTypePointer Function %3
844 %5 = OpTypeVoid
845 %6 = OpTypeFunction %5
846 %7 = OpFunction %5 None %6
847 %8 = OpLabel
848 %2 = OpVariable %4 Function
849 %9 = OpUndef %3
850 OpReturn
851 OpFunctionEnd
852 )";
853 EXPECT_EQ(ModuleToText(), expected_binary);
854 }
855
TEST_F(DecorationManagerTest,HaveTheSameDecorationsWithoutGroupsTrue)856 TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithoutGroupsTrue) {
857 const std::string spirv = R"(
858 OpCapability Shader
859 OpCapability Linkage
860 OpMemoryModel Logical GLSL450
861 OpDecorate %1 Restrict
862 OpDecorate %2 Constant
863 OpDecorate %2 Restrict
864 OpDecorate %1 Constant
865 %u32 = OpTypeInt 32 0
866 %1 = OpVariable %u32 Uniform
867 %2 = OpVariable %u32 Uniform
868 )";
869 DecorationManager* decoManager = GetDecorationManager(spirv);
870 EXPECT_THAT(GetErrorMessage(), "");
871 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
872 }
873
TEST_F(DecorationManagerTest,HaveTheSameDecorationsWithoutGroupsFalse)874 TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithoutGroupsFalse) {
875 const std::string spirv = R"(
876 OpCapability Shader
877 OpCapability Linkage
878 OpMemoryModel Logical GLSL450
879 OpDecorate %1 Restrict
880 OpDecorate %2 Constant
881 OpDecorate %2 Restrict
882 %u32 = OpTypeInt 32 0
883 %1 = OpVariable %u32 Uniform
884 %2 = OpVariable %u32 Uniform
885 )";
886 DecorationManager* decoManager = GetDecorationManager(spirv);
887 EXPECT_THAT(GetErrorMessage(), "");
888 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
889 }
890
TEST_F(DecorationManagerTest,HaveTheSameDecorationsIdWithoutGroupsTrue)891 TEST_F(DecorationManagerTest, HaveTheSameDecorationsIdWithoutGroupsTrue) {
892 const std::string spirv = R"(
893 OpCapability Kernel
894 OpCapability Linkage
895 OpMemoryModel Logical GLSL450
896 OpDecorateId %1 AlignmentId %nine
897 OpDecorateId %3 MaxByteOffsetId %nine
898 OpDecorateId %3 AlignmentId %nine
899 OpDecorateId %1 MaxByteOffsetId %nine
900 %u32 = OpTypeInt 32 0
901 %nine = OpConstant %u32 9
902 %1 = OpVariable %u32 Uniform
903 %3 = OpVariable %u32 Uniform
904 )";
905 DecorationManager* decoManager = GetDecorationManager(spirv);
906 EXPECT_THAT(GetErrorMessage(), "");
907 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 3u));
908 }
909
TEST_F(DecorationManagerTest,HaveTheSameDecorationsIdWithoutGroupsFalse)910 TEST_F(DecorationManagerTest, HaveTheSameDecorationsIdWithoutGroupsFalse) {
911 const std::string spirv = R"(
912 OpCapability Kernel
913 OpCapability Linkage
914 OpMemoryModel Logical GLSL450
915 OpDecorateId %1 AlignmentId %nine
916 OpDecorateId %2 MaxByteOffsetId %nine
917 OpDecorateId %2 AlignmentId %nine
918 %u32 = OpTypeInt 32 0
919 %nine = OpConstant %u32 9
920 %1 = OpVariable %u32 Uniform
921 %2 = OpVariable %u32 Uniform
922 )";
923 DecorationManager* decoManager = GetDecorationManager(spirv);
924 EXPECT_THAT(GetErrorMessage(), "");
925 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
926 }
927
TEST_F(DecorationManagerTest,HaveTheSameDecorationsStringWithoutGroupsTrue)928 TEST_F(DecorationManagerTest, HaveTheSameDecorationsStringWithoutGroupsTrue) {
929 const std::string spirv = R"(
930 OpCapability Kernel
931 OpCapability Linkage
932 OpExtension "SPV_GOOGLE_hlsl_functionality1"
933 OpExtension "SPV_GOOGLE_decorate_string"
934 OpMemoryModel Logical GLSL450
935 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
936 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
937 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
938 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "world"
939 %u32 = OpTypeInt 32 0
940 %1 = OpVariable %u32 Uniform
941 %2 = OpVariable %u32 Uniform
942 )";
943 DecorationManager* decoManager = GetDecorationManager(spirv);
944 EXPECT_THAT(GetErrorMessage(), "");
945 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
946 }
947
TEST_F(DecorationManagerTest,HaveTheSameDecorationsStringWithoutGroupsFalse)948 TEST_F(DecorationManagerTest, HaveTheSameDecorationsStringWithoutGroupsFalse) {
949 const std::string spirv = R"(
950 OpCapability Kernel
951 OpCapability Linkage
952 OpExtension "SPV_GOOGLE_hlsl_functionality1"
953 OpExtension "SPV_GOOGLE_decorate_string"
954 OpMemoryModel Logical GLSL450
955 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
956 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
957 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
958 %u32 = OpTypeInt 32 0
959 %1 = OpVariable %u32 Uniform
960 %2 = OpVariable %u32 Uniform
961 )";
962 DecorationManager* decoManager = GetDecorationManager(spirv);
963 EXPECT_THAT(GetErrorMessage(), "");
964 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
965 }
966
TEST_F(DecorationManagerTest,HaveTheSameDecorationsWithGroupsTrue)967 TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithGroupsTrue) {
968 const std::string spirv = R"(
969 OpCapability Shader
970 OpCapability Linkage
971 OpMemoryModel Logical GLSL450
972 OpDecorate %1 Restrict
973 OpDecorate %2 Constant
974 OpDecorate %1 Constant
975 OpDecorate %3 Restrict
976 %3 = OpDecorationGroup
977 OpGroupDecorate %3 %2
978 OpDecorate %4 Invariant
979 %4 = OpDecorationGroup
980 OpGroupDecorate %4 %1 %2
981 %u32 = OpTypeInt 32 0
982 %1 = OpVariable %u32 Uniform
983 %2 = OpVariable %u32 Uniform
984 )";
985 DecorationManager* decoManager = GetDecorationManager(spirv);
986 EXPECT_THAT(GetErrorMessage(), "");
987 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
988 }
989
TEST_F(DecorationManagerTest,HaveTheSameDecorationsWithGroupsFalse)990 TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithGroupsFalse) {
991 const std::string spirv = R"(
992 OpCapability Shader
993 OpCapability Linkage
994 OpMemoryModel Logical GLSL450
995 OpDecorate %1 Restrict
996 OpDecorate %2 Constant
997 OpDecorate %1 Constant
998 OpDecorate %4 Invariant
999 %4 = OpDecorationGroup
1000 OpGroupDecorate %4 %1 %2
1001 %u32 = OpTypeInt 32 0
1002 %1 = OpVariable %u32 Uniform
1003 %2 = OpVariable %u32 Uniform
1004 )";
1005 DecorationManager* decoManager = GetDecorationManager(spirv);
1006 EXPECT_THAT(GetErrorMessage(), "");
1007 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1008 }
1009
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDuplicateDecorations)1010 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDuplicateDecorations) {
1011 const std::string spirv = R"(
1012 OpCapability Shader
1013 OpCapability Linkage
1014 OpMemoryModel Logical GLSL450
1015 OpDecorate %1 Constant
1016 OpDecorate %2 Constant
1017 OpDecorate %2 Constant
1018 %u32 = OpTypeInt 32 0
1019 %1 = OpVariable %u32 Uniform
1020 %2 = OpVariable %u32 Uniform
1021 )";
1022 DecorationManager* decoManager = GetDecorationManager(spirv);
1023 EXPECT_THAT(GetErrorMessage(), "");
1024 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
1025 }
1026
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDifferentVariations)1027 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentVariations) {
1028 const std::string spirv = R"(
1029 OpCapability Shader
1030 OpCapability Linkage
1031 OpMemoryModel Logical GLSL450
1032 OpDecorate %1 Location 0
1033 OpDecorate %2 Location 1
1034 %u32 = OpTypeInt 32 0
1035 %1 = OpVariable %u32 Uniform
1036 %2 = OpVariable %u32 Uniform
1037 )";
1038 DecorationManager* decoManager = GetDecorationManager(spirv);
1039 EXPECT_THAT(GetErrorMessage(), "");
1040 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1041 }
1042
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDuplicateMemberDecorations)1043 TEST_F(DecorationManagerTest,
1044 HaveTheSameDecorationsDuplicateMemberDecorations) {
1045 const std::string spirv = R"(
1046 OpCapability Shader
1047 OpCapability Linkage
1048 OpMemoryModel Logical GLSL450
1049 OpMemberDecorate %1 0 Location 0
1050 OpMemberDecorate %2 0 Location 0
1051 OpMemberDecorate %2 0 Location 0
1052 %u32 = OpTypeInt 32 0
1053 %1 = OpTypeStruct %u32 %u32
1054 %2 = OpTypeStruct %u32 %u32
1055 )";
1056 DecorationManager* decoManager = GetDecorationManager(spirv);
1057 EXPECT_THAT(GetErrorMessage(), "");
1058 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
1059 }
1060
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDifferentMemberSameDecoration)1061 TEST_F(DecorationManagerTest,
1062 HaveTheSameDecorationsDifferentMemberSameDecoration) {
1063 const std::string spirv = R"(
1064 OpCapability Shader
1065 OpCapability Linkage
1066 OpMemoryModel Logical GLSL450
1067 OpMemberDecorate %1 0 Location 0
1068 OpMemberDecorate %2 1 Location 0
1069 %u32 = OpTypeInt 32 0
1070 %1 = OpTypeStruct %u32 %u32
1071 %2 = OpTypeStruct %u32 %u32
1072 )";
1073 DecorationManager* decoManager = GetDecorationManager(spirv);
1074 EXPECT_THAT(GetErrorMessage(), "");
1075 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1076 }
1077
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDifferentMemberVariations)1078 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentMemberVariations) {
1079 const std::string spirv = R"(
1080 OpCapability Shader
1081 OpCapability Linkage
1082 OpMemoryModel Logical GLSL450
1083 OpMemberDecorate %1 0 Location 0
1084 OpMemberDecorate %2 0 Location 1
1085 %u32 = OpTypeInt 32 0
1086 %1 = OpTypeStruct %u32 %u32
1087 %2 = OpTypeStruct %u32 %u32
1088 )";
1089 DecorationManager* decoManager = GetDecorationManager(spirv);
1090 EXPECT_THAT(GetErrorMessage(), "");
1091 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1092 }
1093
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDuplicateIdDecorations)1094 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDuplicateIdDecorations) {
1095 const std::string spirv = R"(
1096 OpCapability Shader
1097 OpCapability Linkage
1098 OpMemoryModel Logical GLSL450
1099 OpDecorateId %1 AlignmentId %2
1100 OpDecorateId %3 AlignmentId %2
1101 OpDecorateId %3 AlignmentId %2
1102 %u32 = OpTypeInt 32 0
1103 %1 = OpVariable %u32 Uniform
1104 %3 = OpVariable %u32 Uniform
1105 %2 = OpSpecConstant %u32 0
1106 )";
1107 DecorationManager* decoManager = GetDecorationManager(spirv);
1108 EXPECT_THAT(GetErrorMessage(), "");
1109 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 3u));
1110 }
1111
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDuplicateStringDecorations)1112 TEST_F(DecorationManagerTest,
1113 HaveTheSameDecorationsDuplicateStringDecorations) {
1114 const std::string spirv = R"(
1115 OpCapability Shader
1116 OpCapability Linkage
1117 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1118 OpExtension "SPV_GOOGLE_decorate_string"
1119 OpMemoryModel Logical GLSL450
1120 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
1121 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
1122 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
1123 %u32 = OpTypeInt 32 0
1124 %1 = OpVariable %u32 Uniform
1125 %2 = OpVariable %u32 Uniform
1126 )";
1127 DecorationManager* decoManager = GetDecorationManager(spirv);
1128 EXPECT_THAT(GetErrorMessage(), "");
1129 EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
1130 }
1131
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDifferentIdVariations)1132 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentIdVariations) {
1133 const std::string spirv = R"(
1134 OpCapability Shader
1135 OpCapability Linkage
1136 OpMemoryModel Logical GLSL450
1137 OpDecorateId %1 AlignmentId %2
1138 OpDecorateId %3 AlignmentId %4
1139 %u32 = OpTypeInt 32 0
1140 %1 = OpVariable %u32 Uniform
1141 %3 = OpVariable %u32 Uniform
1142 %2 = OpSpecConstant %u32 0
1143 %4 = OpSpecConstant %u32 0
1144 )";
1145 DecorationManager* decoManager = GetDecorationManager(spirv);
1146 EXPECT_THAT(GetErrorMessage(), "");
1147 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1148 }
1149
TEST_F(DecorationManagerTest,HaveTheSameDecorationsDifferentStringVariations)1150 TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentStringVariations) {
1151 const std::string spirv = R"(
1152 OpCapability Shader
1153 OpCapability Linkage
1154 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1155 OpExtension "SPV_GOOGLE_decorate_string"
1156 OpMemoryModel Logical GLSL450
1157 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
1158 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
1159 )";
1160 DecorationManager* decoManager = GetDecorationManager(spirv);
1161 EXPECT_THAT(GetErrorMessage(), "");
1162 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1163 }
1164
TEST_F(DecorationManagerTest,HaveTheSameDecorationsLeftSymmetry)1165 TEST_F(DecorationManagerTest, HaveTheSameDecorationsLeftSymmetry) {
1166 // Left being a subset of right is not enough.
1167 const std::string spirv = R"(
1168 OpCapability Shader
1169 OpCapability Linkage
1170 OpMemoryModel Logical GLSL450
1171 OpDecorate %1 Constant
1172 OpDecorate %1 Constant
1173 OpDecorate %2 Constant
1174 OpDecorate %2 Restrict
1175 %u32 = OpTypeInt 32 0
1176 %1 = OpVariable %u32 Uniform
1177 %2 = OpVariable %u32 Uniform
1178 )";
1179 DecorationManager* decoManager = GetDecorationManager(spirv);
1180 EXPECT_THAT(GetErrorMessage(), "");
1181 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1182 }
1183
TEST_F(DecorationManagerTest,HaveTheSameDecorationsRightSymmetry)1184 TEST_F(DecorationManagerTest, HaveTheSameDecorationsRightSymmetry) {
1185 // Right being a subset of left is not enough.
1186 const std::string spirv = R"(
1187 OpCapability Shader
1188 OpCapability Linkage
1189 OpMemoryModel Logical GLSL450
1190 OpDecorate %1 Constant
1191 OpDecorate %1 Restrict
1192 OpDecorate %2 Constant
1193 OpDecorate %2 Constant
1194 %u32 = OpTypeInt 32 0
1195 %1 = OpVariable %u32 Uniform
1196 %2 = OpVariable %u32 Uniform
1197 )";
1198 DecorationManager* decoManager = GetDecorationManager(spirv);
1199 EXPECT_THAT(GetErrorMessage(), "");
1200 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1201 }
1202
TEST_F(DecorationManagerTest,HaveTheSameDecorationIdsLeftSymmetry)1203 TEST_F(DecorationManagerTest, HaveTheSameDecorationIdsLeftSymmetry) {
1204 const std::string spirv = R"(
1205 OpCapability Kernel
1206 OpCapability Linkage
1207 OpMemoryModel Logical GLSL450
1208 OpDecorateId %1 AlignmentId %nine
1209 OpDecorateId %1 AlignmentId %nine
1210 OpDecorateId %2 AlignmentId %nine
1211 OpDecorateId %2 MaxByteOffsetId %nine
1212 %u32 = OpTypeInt 32 0
1213 %nine = OpConstant %u32 9
1214 %1 = OpVariable %u32 Uniform
1215 %2 = OpVariable %u32 Uniform
1216 )";
1217 DecorationManager* decoManager = GetDecorationManager(spirv);
1218 EXPECT_THAT(GetErrorMessage(), "");
1219 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1220 }
1221
TEST_F(DecorationManagerTest,HaveTheSameDecorationIdsRightSymmetry)1222 TEST_F(DecorationManagerTest, HaveTheSameDecorationIdsRightSymmetry) {
1223 const std::string spirv = R"(
1224 OpCapability Kernel
1225 OpCapability Linkage
1226 OpMemoryModel Logical GLSL450
1227 OpDecorateId %1 AlignmentId %nine
1228 OpDecorateId %1 MaxByteOffsetId %nine
1229 OpDecorateId %2 AlignmentId %nine
1230 OpDecorateId %2 AlignmentId %nine
1231 %u32 = OpTypeInt 32 0
1232 %nine = OpConstant %u32 9
1233 %1 = OpVariable %u32 Uniform
1234 %2 = OpVariable %u32 Uniform
1235 )";
1236 DecorationManager* decoManager = GetDecorationManager(spirv);
1237 EXPECT_THAT(GetErrorMessage(), "");
1238 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1239 }
1240
TEST_F(DecorationManagerTest,HaveTheSameDecorationStringsLeftSymmetry)1241 TEST_F(DecorationManagerTest, HaveTheSameDecorationStringsLeftSymmetry) {
1242 const std::string spirv = R"(
1243 OpCapability Kernel
1244 OpCapability Linkage
1245 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1246 OpExtension "SPV_GOOGLE_decorate_string"
1247 OpMemoryModel Logical GLSL450
1248 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
1249 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
1250 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
1251 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
1252 %u32 = OpTypeInt 32 0
1253 %1 = OpVariable %u32 Uniform
1254 %2 = OpVariable %u32 Uniform
1255 )";
1256 DecorationManager* decoManager = GetDecorationManager(spirv);
1257 EXPECT_THAT(GetErrorMessage(), "");
1258 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1259 }
1260
TEST_F(DecorationManagerTest,HaveTheSameDecorationStringsRightSymmetry)1261 TEST_F(DecorationManagerTest, HaveTheSameDecorationStringsRightSymmetry) {
1262 const std::string spirv = R"(
1263 OpCapability Kernel
1264 OpCapability Linkage
1265 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1266 OpExtension "SPV_GOOGLE_decorate_string"
1267 OpMemoryModel Logical GLSL450
1268 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
1269 OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "world"
1270 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
1271 OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
1272 %u32 = OpTypeInt 32 0
1273 %1 = OpVariable %u32 Uniform
1274 %2 = OpVariable %u32 Uniform
1275 )";
1276 DecorationManager* decoManager = GetDecorationManager(spirv);
1277 EXPECT_THAT(GetErrorMessage(), "");
1278 EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
1279 }
1280
TEST_F(DecorationManagerTest,SubSetTestOpDecorate1)1281 TEST_F(DecorationManagerTest, SubSetTestOpDecorate1) {
1282 const std::string spirv = R"(
1283 OpCapability Shader
1284 OpCapability Linkage
1285 OpMemoryModel Logical GLSL450
1286 OpDecorate %1 Restrict
1287 OpDecorate %2 Constant
1288 OpDecorate %2 Restrict
1289 OpDecorate %1 Constant
1290 %u32 = OpTypeInt 32 0
1291 %1 = OpVariable %u32 Uniform
1292 %2 = OpVariable %u32 Uniform
1293 )";
1294 DecorationManager* decoManager = GetDecorationManager(spirv);
1295 EXPECT_THAT(GetErrorMessage(), "");
1296 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1297 }
1298
TEST_F(DecorationManagerTest,SubSetTestOpDecorate2)1299 TEST_F(DecorationManagerTest, SubSetTestOpDecorate2) {
1300 const std::string spirv = R"(
1301 OpCapability Shader
1302 OpCapability Linkage
1303 OpMemoryModel Logical GLSL450
1304 OpDecorate %1 Restrict
1305 OpDecorate %2 Constant
1306 OpDecorate %2 Restrict
1307 %u32 = OpTypeInt 32 0
1308 %1 = OpVariable %u32 Uniform
1309 %2 = OpVariable %u32 Uniform
1310 )";
1311 DecorationManager* decoManager = GetDecorationManager(spirv);
1312 EXPECT_THAT(GetErrorMessage(), "");
1313 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1314 }
1315
TEST_F(DecorationManagerTest,SubSetTestOpDecorate3)1316 TEST_F(DecorationManagerTest, SubSetTestOpDecorate3) {
1317 const std::string spirv = R"(
1318 OpCapability Shader
1319 OpCapability Linkage
1320 OpMemoryModel Logical GLSL450
1321 OpDecorate %1 Constant
1322 OpDecorate %2 Constant
1323 OpDecorate %2 Restrict
1324 %u32 = OpTypeInt 32 0
1325 %1 = OpVariable %u32 Uniform
1326 %2 = OpVariable %u32 Uniform
1327 )";
1328 DecorationManager* decoManager = GetDecorationManager(spirv);
1329 EXPECT_THAT(GetErrorMessage(), "");
1330 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1331 }
1332
TEST_F(DecorationManagerTest,SubSetTestOpDecorate4)1333 TEST_F(DecorationManagerTest, SubSetTestOpDecorate4) {
1334 const std::string spirv = R"(
1335 OpCapability Shader
1336 OpCapability Linkage
1337 OpMemoryModel Logical GLSL450
1338 OpDecorate %1 Restrict
1339 OpDecorate %2 Constant
1340 OpDecorate %2 Restrict
1341 OpDecorate %1 Constant
1342 %u32 = OpTypeInt 32 0
1343 %1 = OpVariable %u32 Uniform
1344 %2 = OpVariable %u32 Uniform
1345 )";
1346 DecorationManager* decoManager = GetDecorationManager(spirv);
1347 EXPECT_THAT(GetErrorMessage(), "");
1348 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1349 }
1350
TEST_F(DecorationManagerTest,SubSetTestOpDecorate5)1351 TEST_F(DecorationManagerTest, SubSetTestOpDecorate5) {
1352 const std::string spirv = R"(
1353 OpCapability Shader
1354 OpCapability Linkage
1355 OpMemoryModel Logical GLSL450
1356 OpDecorate %1 Restrict
1357 OpDecorate %2 Constant
1358 OpDecorate %2 Restrict
1359 %u32 = OpTypeInt 32 0
1360 %1 = OpVariable %u32 Uniform
1361 %2 = OpVariable %u32 Uniform
1362 )";
1363 DecorationManager* decoManager = GetDecorationManager(spirv);
1364 EXPECT_THAT(GetErrorMessage(), "");
1365 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1366 }
1367
TEST_F(DecorationManagerTest,SubSetTestOpDecorate6)1368 TEST_F(DecorationManagerTest, SubSetTestOpDecorate6) {
1369 const std::string spirv = R"(
1370 OpCapability Shader
1371 OpCapability Linkage
1372 OpMemoryModel Logical GLSL450
1373 OpDecorate %1 Constant
1374 OpDecorate %2 Constant
1375 OpDecorate %2 Restrict
1376 %u32 = OpTypeInt 32 0
1377 %1 = OpVariable %u32 Uniform
1378 %2 = OpVariable %u32 Uniform
1379 )";
1380 DecorationManager* decoManager = GetDecorationManager(spirv);
1381 EXPECT_THAT(GetErrorMessage(), "");
1382 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1383 }
1384
TEST_F(DecorationManagerTest,SubSetTestOpDecorate7)1385 TEST_F(DecorationManagerTest, SubSetTestOpDecorate7) {
1386 const std::string spirv = R"(
1387 OpCapability Shader
1388 OpCapability Linkage
1389 OpMemoryModel Logical GLSL450
1390 OpDecorate %1 Constant
1391 OpDecorate %2 Constant
1392 OpDecorate %2 Restrict
1393 OpDecorate %1 Invariant
1394 %u32 = OpTypeInt 32 0
1395 %1 = OpVariable %u32 Uniform
1396 %2 = OpVariable %u32 Uniform
1397 )";
1398 DecorationManager* decoManager = GetDecorationManager(spirv);
1399 EXPECT_THAT(GetErrorMessage(), "");
1400 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1401 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1402 }
1403
TEST_F(DecorationManagerTest,SubSetTestOpMemberDecorate1)1404 TEST_F(DecorationManagerTest, SubSetTestOpMemberDecorate1) {
1405 const std::string spirv = R"(
1406 OpCapability Shader
1407 OpCapability Linkage
1408 OpMemoryModel Logical GLSL450
1409 OpMemberDecorate %1 0 Offset 0
1410 OpMemberDecorate %1 0 Offset 4
1411 OpMemberDecorate %2 0 Offset 0
1412 OpMemberDecorate %2 0 Offset 4
1413 %u32 = OpTypeInt 32 0
1414 %1 = OpTypeStruct %u32 %u32 %u32
1415 %2 = OpTypeStruct %u32 %u32 %u32
1416 )";
1417 DecorationManager* decoManager = GetDecorationManager(spirv);
1418 EXPECT_THAT(GetErrorMessage(), "");
1419 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1420 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1421 }
1422
TEST_F(DecorationManagerTest,SubSetTestOpMemberDecorate2)1423 TEST_F(DecorationManagerTest, SubSetTestOpMemberDecorate2) {
1424 const std::string spirv = R"(
1425 OpCapability Shader
1426 OpCapability Linkage
1427 OpMemoryModel Logical GLSL450
1428 OpMemberDecorate %1 0 Offset 0
1429 OpMemberDecorate %2 0 Offset 0
1430 OpMemberDecorate %2 0 Offset 4
1431 %u32 = OpTypeInt 32 0
1432 %1 = OpTypeStruct %u32 %u32 %u32
1433 %2 = OpTypeStruct %u32 %u32 %u32
1434 )";
1435 DecorationManager* decoManager = GetDecorationManager(spirv);
1436 EXPECT_THAT(GetErrorMessage(), "");
1437 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1438 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1439 }
1440
TEST_F(DecorationManagerTest,SubSetTestOpDecorateId1)1441 TEST_F(DecorationManagerTest, SubSetTestOpDecorateId1) {
1442 const std::string spirv = R"(
1443 OpCapability Shader
1444 OpCapability Linkage
1445 OpMemoryModel Logical GLSL450
1446 OpDecorateId %1 AlignmentId %2
1447 %u32 = OpTypeInt 32 0
1448 %1 = OpVariable %u32 Uniform
1449 %3 = OpVariable %u32 Uniform
1450 %2 = OpSpecConstant %u32 0
1451 )";
1452 DecorationManager* decoManager = GetDecorationManager(spirv);
1453 EXPECT_THAT(GetErrorMessage(), "");
1454 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(1u, 3u));
1455 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(3u, 1u));
1456 }
1457
TEST_F(DecorationManagerTest,SubSetTestOpDecorateId2)1458 TEST_F(DecorationManagerTest, SubSetTestOpDecorateId2) {
1459 const std::string spirv = R"(
1460 OpCapability Shader
1461 OpCapability Linkage
1462 OpMemoryModel Logical GLSL450
1463 OpDecorateId %1 AlignmentId %2
1464 OpDecorateId %3 AlignmentId %4
1465 %u32 = OpTypeInt 32 0
1466 %1 = OpVariable %u32 Uniform
1467 %3 = OpVariable %u32 Uniform
1468 %2 = OpSpecConstant %u32 0
1469 %4 = OpSpecConstant %u32 1
1470 )";
1471 DecorationManager* decoManager = GetDecorationManager(spirv);
1472 EXPECT_THAT(GetErrorMessage(), "");
1473 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(1u, 3u));
1474 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(3u, 1u));
1475 }
1476
TEST_F(DecorationManagerTest,SubSetTestOpDecorateString1)1477 TEST_F(DecorationManagerTest, SubSetTestOpDecorateString1) {
1478 const std::string spirv = R"(
1479 OpCapability Shader
1480 OpCapability Linkage
1481 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1482 OpExtension "SPV_GOOGLE_decorate_string"
1483 OpMemoryModel Logical GLSL450
1484 OpDecorateString %1 HlslSemanticGOOGLE "hello"
1485 OpDecorateString %2 HlslSemanticGOOGLE "world"
1486 )";
1487 DecorationManager* decoManager = GetDecorationManager(spirv);
1488 EXPECT_THAT(GetErrorMessage(), "");
1489 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1490 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1491 }
1492
TEST_F(DecorationManagerTest,SubSetTestOpDecorateString2)1493 TEST_F(DecorationManagerTest, SubSetTestOpDecorateString2) {
1494 const std::string spirv = R"(
1495 OpCapability Shader
1496 OpCapability Linkage
1497 OpExtension "SPV_GOOGLE_hlsl_functionality1"
1498 OpExtension "SPV_GOOGLE_decorate_string"
1499 OpMemoryModel Logical GLSL450
1500 OpDecorateString %1 HlslSemanticGOOGLE "hello"
1501 )";
1502 DecorationManager* decoManager = GetDecorationManager(spirv);
1503 EXPECT_THAT(GetErrorMessage(), "");
1504 EXPECT_FALSE(decoManager->HaveSubsetOfDecorations(1u, 2u));
1505 EXPECT_TRUE(decoManager->HaveSubsetOfDecorations(2u, 1u));
1506 }
1507 } // namespace
1508 } // namespace analysis
1509 } // namespace opt
1510 } // namespace spvtools
1511