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