decoration_manager_test.cpp 50 KB

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