decoration_manager_test.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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 <iostream>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "source/opt/build_module.h"
  20. #include "source/opt/decoration_manager.h"
  21. #include "source/opt/ir_context.h"
  22. #include "source/spirv_constant.h"
  23. #include "test/unit_spirv.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace analysis {
  27. namespace {
  28. using spvtest::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 = {SpvMagicNumber, 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 interprated both as { SpvDecorationConstant }
  107. // and also as a list of IDs: { 22 }
  108. const std::vector<uint32_t> param{SpvDecorationConstant};
  109. // OpDecorate %1 Constant
  110. Instruction inst1(
  111. &ir_context, SpvOpDecorate, 0u, 0u,
  112. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, param}});
  113. // OpDecorateId %1 %22 ; 'Constant' is decoration number 22
  114. Instruction inst2(
  115. &ir_context, SpvOpDecorateId, 0u, 0u,
  116. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, param}});
  117. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  118. EXPECT_THAT(GetErrorMessage(), "");
  119. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  120. }
  121. TEST_F(DecorationManagerTest,
  122. ComparingDecorationsWithDiffOpcodesDecorateDecorateString) {
  123. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  124. // This parameter can be interprated both as { SpvDecorationConstant }
  125. // and also as a null-terminated string with a single character with value 22.
  126. const std::vector<uint32_t> param{SpvDecorationConstant};
  127. // OpDecorate %1 Constant
  128. Instruction inst1(
  129. &ir_context, SpvOpDecorate, 0u, 0u,
  130. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, param}});
  131. // OpDecorateStringGOOGLE %1 !22
  132. Instruction inst2(
  133. &ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
  134. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_LITERAL_STRING, param}});
  135. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  136. EXPECT_THAT(GetErrorMessage(), "");
  137. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  138. }
  139. TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateParam) {
  140. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  141. // OpDecorate %1 Constant
  142. Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
  143. {{SPV_OPERAND_TYPE_ID, {1u}},
  144. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  145. // OpDecorate %1 Restrict
  146. Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
  147. {{SPV_OPERAND_TYPE_ID, {1u}},
  148. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationRestrict}}});
  149. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  150. EXPECT_THAT(GetErrorMessage(), "");
  151. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  152. }
  153. TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateIdParam) {
  154. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  155. // OpDecorate %1 Constant
  156. Instruction inst1(
  157. &ir_context, SpvOpDecorateId, 0u, 0u,
  158. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, {555}}});
  159. // OpDecorate %1 Restrict
  160. Instruction inst2(
  161. &ir_context, SpvOpDecorateId, 0u, 0u,
  162. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_ID, {666}}});
  163. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  164. EXPECT_THAT(GetErrorMessage(), "");
  165. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  166. }
  167. TEST_F(DecorationManagerTest, ComparingDecorationsWithDiffDecorateStringParam) {
  168. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  169. // OpDecorate %1 Constant
  170. Instruction inst1(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
  171. {{SPV_OPERAND_TYPE_ID, {1u}},
  172. {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("Hello!")}});
  173. // OpDecorate %1 Restrict
  174. Instruction inst2(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
  175. {{SPV_OPERAND_TYPE_ID, {1u}},
  176. {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("Hellx")}});
  177. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  178. EXPECT_THAT(GetErrorMessage(), "");
  179. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  180. }
  181. TEST_F(DecorationManagerTest, ComparingSameDecorationsOnDiffTargetAllowed) {
  182. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  183. // OpDecorate %1 Constant
  184. Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
  185. {{SPV_OPERAND_TYPE_ID, {1u}},
  186. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  187. // OpDecorate %2 Constant
  188. Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
  189. {{SPV_OPERAND_TYPE_ID, {2u}},
  190. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  191. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  192. EXPECT_THAT(GetErrorMessage(), "");
  193. EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  194. }
  195. TEST_F(DecorationManagerTest, ComparingSameDecorationIdsOnDiffTargetAllowed) {
  196. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  197. Instruction inst1(
  198. &ir_context, SpvOpDecorateId, 0u, 0u,
  199. {{SPV_OPERAND_TYPE_ID, {1u}}, {SPV_OPERAND_TYPE_DECORATION, {44}}});
  200. Instruction inst2(
  201. &ir_context, SpvOpDecorateId, 0u, 0u,
  202. {{SPV_OPERAND_TYPE_ID, {2u}}, {SPV_OPERAND_TYPE_DECORATION, {44}}});
  203. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  204. EXPECT_THAT(GetErrorMessage(), "");
  205. EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  206. }
  207. TEST_F(DecorationManagerTest,
  208. ComparingSameDecorationStringsOnDiffTargetAllowed) {
  209. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  210. Instruction inst1(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
  211. {{SPV_OPERAND_TYPE_ID, {1u}},
  212. {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("hello")}});
  213. Instruction inst2(&ir_context, SpvOpDecorateStringGOOGLE, 0u, 0u,
  214. {{SPV_OPERAND_TYPE_ID, {2u}},
  215. {SPV_OPERAND_TYPE_LITERAL_STRING, MakeVector("hello")}});
  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, ComparingSameDecorationsOnDiffTargetDisallowed) {
  221. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  222. // OpDecorate %1 Constant
  223. Instruction inst1(&ir_context, SpvOpDecorate, 0u, 0u,
  224. {{SPV_OPERAND_TYPE_ID, {1u}},
  225. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  226. // OpDecorate %2 Constant
  227. Instruction inst2(&ir_context, SpvOpDecorate, 0u, 0u,
  228. {{SPV_OPERAND_TYPE_ID, {2u}},
  229. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  230. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  231. EXPECT_THAT(GetErrorMessage(), "");
  232. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, false));
  233. }
  234. TEST_F(DecorationManagerTest, ComparingMemberDecorationsOnSameTypeDiffMember) {
  235. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  236. // OpMemberDecorate %1 0 Constant
  237. Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  238. {{SPV_OPERAND_TYPE_ID, {1u}},
  239. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
  240. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  241. // OpMemberDecorate %1 1 Constant
  242. Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  243. {{SPV_OPERAND_TYPE_ID, {1u}},
  244. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {1u}},
  245. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  246. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  247. EXPECT_THAT(GetErrorMessage(), "");
  248. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  249. }
  250. TEST_F(DecorationManagerTest,
  251. ComparingSameMemberDecorationsOnDiffTargetAllowed) {
  252. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  253. // OpMemberDecorate %1 0 Constant
  254. Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  255. {{SPV_OPERAND_TYPE_ID, {1u}},
  256. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
  257. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  258. // OpMemberDecorate %2 0 Constant
  259. Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  260. {{SPV_OPERAND_TYPE_ID, {2u}},
  261. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
  262. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  263. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  264. EXPECT_THAT(GetErrorMessage(), "");
  265. EXPECT_TRUE(decoManager->AreDecorationsTheSame(&inst1, &inst2, true));
  266. }
  267. TEST_F(DecorationManagerTest,
  268. ComparingSameMemberDecorationsOnDiffTargetDisallowed) {
  269. IRContext ir_context(SPV_ENV_UNIVERSAL_1_2, GetConsumer());
  270. // OpMemberDecorate %1 0 Constant
  271. Instruction inst1(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  272. {{SPV_OPERAND_TYPE_ID, {1u}},
  273. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
  274. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  275. // OpMemberDecorate %2 0 Constant
  276. Instruction inst2(&ir_context, SpvOpMemberDecorate, 0u, 0u,
  277. {{SPV_OPERAND_TYPE_ID, {2u}},
  278. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
  279. {SPV_OPERAND_TYPE_DECORATION, {SpvDecorationConstant}}});
  280. DecorationManager* decoManager = ir_context.get_decoration_mgr();
  281. EXPECT_THAT(GetErrorMessage(), "");
  282. EXPECT_FALSE(decoManager->AreDecorationsTheSame(&inst1, &inst2, false));
  283. }
  284. TEST_F(DecorationManagerTest, RemoveDecorationFromVariable) {
  285. const std::string spirv = R"(
  286. OpCapability Shader
  287. OpCapability Linkage
  288. OpMemoryModel Logical GLSL450
  289. OpDecorate %1 Constant
  290. OpDecorate %2 Restrict
  291. %2 = OpDecorationGroup
  292. OpGroupDecorate %2 %1 %3
  293. %4 = OpTypeInt 32 0
  294. %1 = OpVariable %4 Uniform
  295. %3 = OpVariable %4 Uniform
  296. )";
  297. DecorationManager* decoManager = GetDecorationManager(spirv);
  298. EXPECT_THAT(GetErrorMessage(), "");
  299. decoManager->RemoveDecorationsFrom(1u);
  300. auto decorations = decoManager->GetDecorationsFor(1u, false);
  301. EXPECT_THAT(GetErrorMessage(), "");
  302. EXPECT_TRUE(decorations.empty());
  303. decorations = decoManager->GetDecorationsFor(3u, false);
  304. EXPECT_THAT(GetErrorMessage(), "");
  305. const std::string expected_decorations = R"(OpDecorate %2 Restrict
  306. )";
  307. EXPECT_THAT(ToText(decorations), expected_decorations);
  308. const std::string expected_binary = R"(OpCapability Shader
  309. OpCapability Linkage
  310. OpMemoryModel Logical GLSL450
  311. OpDecorate %2 Restrict
  312. %2 = OpDecorationGroup
  313. OpGroupDecorate %2 %3
  314. %4 = OpTypeInt 32 0
  315. %1 = OpVariable %4 Uniform
  316. %3 = OpVariable %4 Uniform
  317. )";
  318. EXPECT_THAT(ModuleToText(), expected_binary);
  319. }
  320. TEST_F(DecorationManagerTest, RemoveDecorationStringFromVariable) {
  321. const std::string spirv = R"(
  322. OpCapability Shader
  323. OpCapability Linkage
  324. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  325. OpExtension "SPV_GOOGLE_decorate_string"
  326. OpMemoryModel Logical GLSL450
  327. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello world"
  328. OpDecorate %2 Restrict
  329. %2 = OpDecorationGroup
  330. OpGroupDecorate %2 %1 %3
  331. %4 = OpTypeInt 32 0
  332. %1 = OpVariable %4 Uniform
  333. %3 = OpVariable %4 Uniform
  334. )";
  335. DecorationManager* decoManager = GetDecorationManager(spirv);
  336. EXPECT_THAT(GetErrorMessage(), "");
  337. decoManager->RemoveDecorationsFrom(1u);
  338. auto decorations = decoManager->GetDecorationsFor(1u, false);
  339. EXPECT_THAT(GetErrorMessage(), "");
  340. EXPECT_TRUE(decorations.empty());
  341. decorations = decoManager->GetDecorationsFor(3u, false);
  342. EXPECT_THAT(GetErrorMessage(), "");
  343. const std::string expected_decorations = R"(OpDecorate %2 Restrict
  344. )";
  345. EXPECT_THAT(ToText(decorations), expected_decorations);
  346. const std::string expected_binary = R"(OpCapability Shader
  347. OpCapability Linkage
  348. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  349. OpExtension "SPV_GOOGLE_decorate_string"
  350. OpMemoryModel Logical GLSL450
  351. OpDecorate %2 Restrict
  352. %2 = OpDecorationGroup
  353. OpGroupDecorate %2 %3
  354. %4 = OpTypeInt 32 0
  355. %1 = OpVariable %4 Uniform
  356. %3 = OpVariable %4 Uniform
  357. )";
  358. EXPECT_THAT(ModuleToText(), expected_binary);
  359. }
  360. TEST_F(DecorationManagerTest, RemoveDecorationFromDecorationGroup) {
  361. const std::string spirv = R"(
  362. OpCapability Shader
  363. OpCapability Linkage
  364. OpMemoryModel Logical GLSL450
  365. OpDecorate %1 Constant
  366. OpDecorate %2 Restrict
  367. %2 = OpDecorationGroup
  368. OpGroupDecorate %2 %1 %3
  369. %4 = OpTypeInt 32 0
  370. %1 = OpVariable %4 Uniform
  371. %3 = OpVariable %4 Uniform
  372. )";
  373. DecorationManager* decoManager = GetDecorationManager(spirv);
  374. EXPECT_THAT(GetErrorMessage(), "");
  375. decoManager->RemoveDecorationsFrom(2u);
  376. auto decorations = decoManager->GetDecorationsFor(2u, false);
  377. EXPECT_THAT(GetErrorMessage(), "");
  378. EXPECT_TRUE(decorations.empty());
  379. decorations = decoManager->GetDecorationsFor(1u, false);
  380. EXPECT_THAT(GetErrorMessage(), "");
  381. const std::string expected_decorations = R"(OpDecorate %1 Constant
  382. )";
  383. EXPECT_THAT(ToText(decorations), expected_decorations);
  384. decorations = decoManager->GetDecorationsFor(3u, false);
  385. EXPECT_THAT(GetErrorMessage(), "");
  386. EXPECT_THAT(ToText(decorations), "");
  387. const std::string expected_binary = R"(OpCapability Shader
  388. OpCapability Linkage
  389. OpMemoryModel Logical GLSL450
  390. OpDecorate %1 Constant
  391. %2 = OpDecorationGroup
  392. %4 = OpTypeInt 32 0
  393. %1 = OpVariable %4 Uniform
  394. %3 = OpVariable %4 Uniform
  395. )";
  396. EXPECT_THAT(ModuleToText(), expected_binary);
  397. }
  398. TEST_F(DecorationManagerTest,
  399. RemoveDecorationFromDecorationGroupKeepDeadDecorations) {
  400. const std::string spirv = R"(
  401. OpCapability Shader
  402. OpCapability Linkage
  403. OpMemoryModel Logical GLSL450
  404. OpDecorate %1 Constant
  405. OpDecorate %2 Restrict
  406. %2 = OpDecorationGroup
  407. OpGroupDecorate %2 %1
  408. %3 = OpTypeInt 32 0
  409. %1 = OpVariable %3 Uniform
  410. )";
  411. DecorationManager* decoManager = GetDecorationManager(spirv);
  412. EXPECT_THAT(GetErrorMessage(), "");
  413. decoManager->RemoveDecorationsFrom(1u);
  414. auto decorations = decoManager->GetDecorationsFor(1u, false);
  415. EXPECT_THAT(GetErrorMessage(), "");
  416. EXPECT_TRUE(decorations.empty());
  417. decorations = decoManager->GetDecorationsFor(2u, false);
  418. EXPECT_THAT(GetErrorMessage(), "");
  419. const std::string expected_decorations = R"(OpDecorate %2 Restrict
  420. )";
  421. EXPECT_THAT(ToText(decorations), expected_decorations);
  422. const std::string expected_binary = R"(OpCapability Shader
  423. OpCapability Linkage
  424. OpMemoryModel Logical GLSL450
  425. OpDecorate %2 Restrict
  426. %2 = OpDecorationGroup
  427. %3 = OpTypeInt 32 0
  428. %1 = OpVariable %3 Uniform
  429. )";
  430. EXPECT_THAT(ModuleToText(), expected_binary);
  431. }
  432. TEST_F(DecorationManagerTest, RemoveAllDecorationsAppliedByGroup) {
  433. const std::string spirv = R"(
  434. OpCapability Shader
  435. OpCapability Linkage
  436. OpMemoryModel Logical GLSL450
  437. OpDecorate %1 Constant
  438. OpDecorate %2 Restrict
  439. %2 = OpDecorationGroup
  440. OpGroupDecorate %2 %1
  441. OpDecorate %3 BuiltIn VertexId
  442. %3 = OpDecorationGroup
  443. OpGroupDecorate %3 %1
  444. %4 = OpTypeInt 32 0
  445. %1 = OpVariable %4 Input
  446. )";
  447. DecorationManager* decoManager = GetDecorationManager(spirv);
  448. EXPECT_THAT(GetErrorMessage(), "");
  449. decoManager->RemoveDecorationsFrom(1u, [](const Instruction& inst) {
  450. return inst.opcode() == SpvOpDecorate &&
  451. inst.GetSingleWordInOperand(0u) == 3u;
  452. });
  453. auto decorations = decoManager->GetDecorationsFor(1u, false);
  454. EXPECT_THAT(GetErrorMessage(), "");
  455. std::string expected_decorations = R"(OpDecorate %1 Constant
  456. OpDecorate %2 Restrict
  457. )";
  458. EXPECT_THAT(ToText(decorations), expected_decorations);
  459. decorations = decoManager->GetDecorationsFor(2u, false);
  460. EXPECT_THAT(GetErrorMessage(), "");
  461. expected_decorations = R"(OpDecorate %2 Restrict
  462. )";
  463. EXPECT_THAT(ToText(decorations), expected_decorations);
  464. const std::string expected_binary = R"(OpCapability Shader
  465. OpCapability Linkage
  466. OpMemoryModel Logical GLSL450
  467. OpDecorate %1 Constant
  468. OpDecorate %2 Restrict
  469. %2 = OpDecorationGroup
  470. OpGroupDecorate %2 %1
  471. OpDecorate %3 BuiltIn VertexId
  472. %3 = OpDecorationGroup
  473. %4 = OpTypeInt 32 0
  474. %1 = OpVariable %4 Input
  475. )";
  476. EXPECT_THAT(ModuleToText(), expected_binary);
  477. }
  478. TEST_F(DecorationManagerTest, RemoveSomeDecorationsAppliedByGroup) {
  479. const std::string spirv = R"(
  480. OpCapability Shader
  481. OpCapability Linkage
  482. OpMemoryModel Logical GLSL450
  483. OpDecorate %1 Constant
  484. OpDecorate %2 Restrict
  485. %2 = OpDecorationGroup
  486. OpGroupDecorate %2 %1
  487. OpDecorate %3 BuiltIn VertexId
  488. OpDecorate %3 Invariant
  489. %3 = OpDecorationGroup
  490. OpGroupDecorate %3 %1
  491. %uint = OpTypeInt 32 0
  492. %1 = OpVariable %uint Input
  493. )";
  494. DecorationManager* decoManager = GetDecorationManager(spirv);
  495. EXPECT_THAT(GetErrorMessage(), "");
  496. decoManager->RemoveDecorationsFrom(1u, [](const Instruction& inst) {
  497. return inst.opcode() == SpvOpDecorate &&
  498. inst.GetSingleWordInOperand(0u) == 3u &&
  499. inst.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn;
  500. });
  501. auto decorations = decoManager->GetDecorationsFor(1u, false);
  502. EXPECT_THAT(GetErrorMessage(), "");
  503. std::string expected_decorations = R"(OpDecorate %1 Constant
  504. OpDecorate %1 Invariant
  505. OpDecorate %2 Restrict
  506. )";
  507. EXPECT_THAT(ToText(decorations), expected_decorations);
  508. decorations = decoManager->GetDecorationsFor(2u, false);
  509. EXPECT_THAT(GetErrorMessage(), "");
  510. expected_decorations = R"(OpDecorate %2 Restrict
  511. )";
  512. EXPECT_THAT(ToText(decorations), expected_decorations);
  513. const std::string expected_binary = R"(OpCapability Shader
  514. OpCapability Linkage
  515. OpMemoryModel Logical GLSL450
  516. OpDecorate %1 Constant
  517. OpDecorate %2 Restrict
  518. %2 = OpDecorationGroup
  519. OpGroupDecorate %2 %1
  520. OpDecorate %3 BuiltIn VertexId
  521. OpDecorate %3 Invariant
  522. %3 = OpDecorationGroup
  523. OpDecorate %1 Invariant
  524. %4 = OpTypeInt 32 0
  525. %1 = OpVariable %4 Input
  526. )";
  527. EXPECT_THAT(ModuleToText(), expected_binary);
  528. }
  529. TEST_F(DecorationManagerTest, RemoveDecorationDecorate) {
  530. const std::string spirv = R"(
  531. OpCapability Shader
  532. OpCapability Linkage
  533. OpMemoryModel Logical GLSL450
  534. OpDecorate %1 Constant
  535. OpDecorate %1 Restrict
  536. %2 = OpTypeInt 32 0
  537. %1 = OpVariable %2 Uniform
  538. )";
  539. DecorationManager* decoManager = GetDecorationManager(spirv);
  540. EXPECT_THAT(GetErrorMessage(), "");
  541. auto decorations = decoManager->GetDecorationsFor(1u, false);
  542. decoManager->RemoveDecoration(decorations.front());
  543. decorations = decoManager->GetDecorationsFor(1u, false);
  544. EXPECT_THAT(GetErrorMessage(), "");
  545. const std::string expected_decorations = R"(OpDecorate %1 Restrict
  546. )";
  547. EXPECT_THAT(ToText(decorations), expected_decorations);
  548. }
  549. TEST_F(DecorationManagerTest, RemoveDecorationStringDecorate) {
  550. const std::string spirv = R"(
  551. OpCapability Shader
  552. OpCapability Linkage
  553. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  554. OpExtension "SPV_GOOGLE_decorate_string"
  555. OpMemoryModel Logical GLSL450
  556. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "foobar"
  557. OpDecorate %1 Restrict
  558. %2 = OpTypeInt 32 0
  559. %1 = OpVariable %2 Uniform
  560. )";
  561. DecorationManager* decoManager = GetDecorationManager(spirv);
  562. EXPECT_THAT(GetErrorMessage(), "");
  563. auto decorations = decoManager->GetDecorationsFor(1u, false);
  564. decoManager->RemoveDecoration(decorations.front());
  565. decorations = decoManager->GetDecorationsFor(1u, false);
  566. EXPECT_THAT(GetErrorMessage(), "");
  567. const std::string expected_decorations = R"(OpDecorate %1 Restrict
  568. )";
  569. EXPECT_THAT(ToText(decorations), expected_decorations);
  570. }
  571. TEST_F(DecorationManagerTest, CloneDecorations) {
  572. const std::string spirv = R"(
  573. 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. OpGroupDecorate %3 %1
  584. %4 = OpTypeInt 32 0
  585. %1 = OpVariable %4 Input
  586. %5 = OpVariable %4 Input
  587. )";
  588. DecorationManager* decoManager = GetDecorationManager(spirv);
  589. EXPECT_THAT(GetErrorMessage(), "");
  590. // Check cloning OpDecorate including group decorations.
  591. auto decorations = decoManager->GetDecorationsFor(5u, false);
  592. EXPECT_THAT(GetErrorMessage(), "");
  593. EXPECT_TRUE(decorations.empty());
  594. decoManager->CloneDecorations(1u, 5u);
  595. decorations = decoManager->GetDecorationsFor(5u, false);
  596. EXPECT_THAT(GetErrorMessage(), "");
  597. std::string expected_decorations = R"(OpDecorate %5 Constant
  598. OpDecorate %2 Restrict
  599. OpDecorate %3 BuiltIn VertexId
  600. OpDecorate %3 Invariant
  601. )";
  602. EXPECT_THAT(ToText(decorations), expected_decorations);
  603. // Check that bookkeeping for ID 2 remains the same.
  604. decorations = decoManager->GetDecorationsFor(2u, false);
  605. EXPECT_THAT(GetErrorMessage(), "");
  606. expected_decorations = R"(OpDecorate %2 Restrict
  607. )";
  608. EXPECT_THAT(ToText(decorations), expected_decorations);
  609. const std::string expected_binary = R"(OpCapability Shader
  610. OpCapability Linkage
  611. OpMemoryModel Logical GLSL450
  612. OpDecorate %1 Constant
  613. OpDecorate %2 Restrict
  614. %2 = OpDecorationGroup
  615. OpGroupDecorate %2 %1 %5
  616. OpDecorate %3 BuiltIn VertexId
  617. OpDecorate %3 Invariant
  618. %3 = OpDecorationGroup
  619. OpGroupDecorate %3 %1 %5
  620. OpDecorate %5 Constant
  621. %4 = OpTypeInt 32 0
  622. %1 = OpVariable %4 Input
  623. %5 = OpVariable %4 Input
  624. )";
  625. EXPECT_THAT(ModuleToText(), expected_binary);
  626. }
  627. TEST_F(DecorationManagerTest, CloneDecorationsStringAndId) {
  628. const std::string spirv = R"(OpCapability Shader
  629. OpCapability Linkage
  630. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  631. OpExtension "SPV_GOOGLE_decorate_string"
  632. OpMemoryModel Logical GLSL450
  633. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "blah"
  634. OpDecorateId %1 HlslCounterBufferGOOGLE %2
  635. OpDecorate %1 Aliased
  636. %3 = OpTypeInt 32 0
  637. %4 = OpTypePointer Uniform %3
  638. %1 = OpVariable %4 Uniform
  639. %2 = OpVariable %4 Uniform
  640. %5 = OpVariable %4 Uniform
  641. )";
  642. DecorationManager* decoManager = GetDecorationManager(spirv);
  643. EXPECT_THAT(GetErrorMessage(), "");
  644. // Check cloning OpDecorate including group decorations.
  645. auto decorations = decoManager->GetDecorationsFor(5u, false);
  646. EXPECT_THAT(GetErrorMessage(), "");
  647. EXPECT_TRUE(decorations.empty());
  648. decoManager->CloneDecorations(1u, 5u);
  649. decorations = decoManager->GetDecorationsFor(5u, false);
  650. EXPECT_THAT(GetErrorMessage(), "");
  651. std::string expected_decorations =
  652. R"(OpDecorateStringGOOGLE %5 HlslSemanticGOOGLE "blah"
  653. OpDecorateId %5 HlslCounterBufferGOOGLE %2
  654. OpDecorate %5 Aliased
  655. )";
  656. EXPECT_THAT(ToText(decorations), expected_decorations);
  657. const std::string expected_binary = R"(OpCapability Shader
  658. OpCapability Linkage
  659. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  660. OpExtension "SPV_GOOGLE_decorate_string"
  661. OpMemoryModel Logical GLSL450
  662. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "blah"
  663. OpDecorateId %1 HlslCounterBufferGOOGLE %2
  664. OpDecorate %1 Aliased
  665. OpDecorateStringGOOGLE %5 HlslSemanticGOOGLE "blah"
  666. OpDecorateId %5 HlslCounterBufferGOOGLE %2
  667. OpDecorate %5 Aliased
  668. %3 = OpTypeInt 32 0
  669. %4 = OpTypePointer Uniform %3
  670. %1 = OpVariable %4 Uniform
  671. %2 = OpVariable %4 Uniform
  672. %5 = OpVariable %4 Uniform
  673. )";
  674. EXPECT_THAT(ModuleToText(), expected_binary);
  675. }
  676. TEST_F(DecorationManagerTest, CloneSomeDecorations) {
  677. const std::string spirv = R"(OpCapability Shader
  678. OpCapability Linkage
  679. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  680. OpExtension "SPV_GOOGLE_decorate_string"
  681. OpMemoryModel Logical GLSL450
  682. OpDecorate %1 RelaxedPrecision
  683. OpDecorate %1 Restrict
  684. %2 = OpTypeInt 32 0
  685. %3 = OpTypePointer Function %2
  686. %4 = OpTypeVoid
  687. %5 = OpTypeFunction %4
  688. %6 = OpFunction %4 None %5
  689. %7 = OpLabel
  690. %1 = OpVariable %3 Function
  691. %8 = OpUndef %2
  692. OpReturn
  693. OpFunctionEnd
  694. )";
  695. DecorationManager* decoManager = GetDecorationManager(spirv);
  696. EXPECT_EQ(GetErrorMessage(), "");
  697. // Check cloning OpDecorate including group decorations.
  698. auto decorations = decoManager->GetDecorationsFor(8u, false);
  699. EXPECT_EQ(GetErrorMessage(), "");
  700. EXPECT_TRUE(decorations.empty());
  701. decoManager->CloneDecorations(1u, 8u, {SpvDecorationRelaxedPrecision});
  702. decorations = decoManager->GetDecorationsFor(8u, false);
  703. EXPECT_THAT(GetErrorMessage(), "");
  704. std::string expected_decorations =
  705. R"(OpDecorate %8 RelaxedPrecision
  706. )";
  707. EXPECT_EQ(ToText(decorations), expected_decorations);
  708. const std::string expected_binary = R"(OpCapability Shader
  709. OpCapability Linkage
  710. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  711. OpExtension "SPV_GOOGLE_decorate_string"
  712. OpMemoryModel Logical GLSL450
  713. OpDecorate %1 RelaxedPrecision
  714. OpDecorate %1 Restrict
  715. OpDecorate %8 RelaxedPrecision
  716. %2 = OpTypeInt 32 0
  717. %3 = OpTypePointer Function %2
  718. %4 = OpTypeVoid
  719. %5 = OpTypeFunction %4
  720. %6 = OpFunction %4 None %5
  721. %7 = OpLabel
  722. %1 = OpVariable %3 Function
  723. %8 = OpUndef %2
  724. OpReturn
  725. OpFunctionEnd
  726. )";
  727. EXPECT_EQ(ModuleToText(), expected_binary);
  728. }
  729. // Test cloning decoration for an id that is decorated via a group decoration.
  730. TEST_F(DecorationManagerTest, CloneSomeGroupDecorations) {
  731. const std::string spirv = R"(OpCapability Shader
  732. OpCapability Linkage
  733. OpMemoryModel Logical GLSL450
  734. OpDecorate %1 RelaxedPrecision
  735. OpDecorate %1 Restrict
  736. %1 = OpDecorationGroup
  737. OpGroupDecorate %1 %2
  738. %3 = OpTypeInt 32 0
  739. %4 = OpTypePointer Function %3
  740. %5 = OpTypeVoid
  741. %6 = OpTypeFunction %5
  742. %7 = OpFunction %5 None %6
  743. %8 = OpLabel
  744. %2 = OpVariable %4 Function
  745. %9 = OpUndef %3
  746. OpReturn
  747. OpFunctionEnd
  748. )";
  749. DecorationManager* decoManager = GetDecorationManager(spirv);
  750. EXPECT_EQ(GetErrorMessage(), "");
  751. // Check cloning OpDecorate including group decorations.
  752. auto decorations = decoManager->GetDecorationsFor(9u, false);
  753. EXPECT_EQ(GetErrorMessage(), "");
  754. EXPECT_TRUE(decorations.empty());
  755. decoManager->CloneDecorations(2u, 9u, {SpvDecorationRelaxedPrecision});
  756. decorations = decoManager->GetDecorationsFor(9u, false);
  757. EXPECT_THAT(GetErrorMessage(), "");
  758. std::string expected_decorations =
  759. R"(OpDecorate %9 RelaxedPrecision
  760. )";
  761. EXPECT_EQ(ToText(decorations), expected_decorations);
  762. const std::string expected_binary = R"(OpCapability Shader
  763. OpCapability Linkage
  764. OpMemoryModel Logical GLSL450
  765. OpDecorate %1 RelaxedPrecision
  766. OpDecorate %1 Restrict
  767. %1 = OpDecorationGroup
  768. OpGroupDecorate %1 %2
  769. OpDecorate %9 RelaxedPrecision
  770. %3 = OpTypeInt 32 0
  771. %4 = OpTypePointer Function %3
  772. %5 = OpTypeVoid
  773. %6 = OpTypeFunction %5
  774. %7 = OpFunction %5 None %6
  775. %8 = OpLabel
  776. %2 = OpVariable %4 Function
  777. %9 = OpUndef %3
  778. OpReturn
  779. OpFunctionEnd
  780. )";
  781. EXPECT_EQ(ModuleToText(), expected_binary);
  782. }
  783. TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithoutGroupsTrue) {
  784. const std::string spirv = R"(
  785. OpCapability Shader
  786. OpCapability Linkage
  787. OpMemoryModel Logical GLSL450
  788. OpDecorate %1 Restrict
  789. OpDecorate %2 Constant
  790. OpDecorate %2 Restrict
  791. OpDecorate %1 Constant
  792. %u32 = OpTypeInt 32 0
  793. %1 = OpVariable %u32 Uniform
  794. %2 = OpVariable %u32 Uniform
  795. )";
  796. DecorationManager* decoManager = GetDecorationManager(spirv);
  797. EXPECT_THAT(GetErrorMessage(), "");
  798. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  799. }
  800. TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithoutGroupsFalse) {
  801. const std::string spirv = R"(
  802. OpCapability Shader
  803. OpCapability Linkage
  804. OpMemoryModel Logical GLSL450
  805. OpDecorate %1 Restrict
  806. OpDecorate %2 Constant
  807. OpDecorate %2 Restrict
  808. %u32 = OpTypeInt 32 0
  809. %1 = OpVariable %u32 Uniform
  810. %2 = OpVariable %u32 Uniform
  811. )";
  812. DecorationManager* decoManager = GetDecorationManager(spirv);
  813. EXPECT_THAT(GetErrorMessage(), "");
  814. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  815. }
  816. TEST_F(DecorationManagerTest, HaveTheSameDecorationsIdWithoutGroupsTrue) {
  817. const std::string spirv = R"(
  818. OpCapability Kernel
  819. OpCapability Linkage
  820. OpMemoryModel Logical GLSL450
  821. OpDecorateId %1 AlignmentId %nine
  822. OpDecorateId %3 MaxByteOffsetId %nine
  823. OpDecorateId %3 AlignmentId %nine
  824. OpDecorateId %1 MaxByteOffsetId %nine
  825. %u32 = OpTypeInt 32 0
  826. %nine = OpConstant %u32 9
  827. %1 = OpVariable %u32 Uniform
  828. %3 = OpVariable %u32 Uniform
  829. )";
  830. DecorationManager* decoManager = GetDecorationManager(spirv);
  831. EXPECT_THAT(GetErrorMessage(), "");
  832. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 3u));
  833. }
  834. TEST_F(DecorationManagerTest, HaveTheSameDecorationsIdWithoutGroupsFalse) {
  835. const std::string spirv = R"(
  836. OpCapability Kernel
  837. OpCapability Linkage
  838. OpMemoryModel Logical GLSL450
  839. OpDecorateId %1 AlignmentId %nine
  840. OpDecorateId %2 MaxByteOffsetId %nine
  841. OpDecorateId %2 AlignmentId %nine
  842. %u32 = OpTypeInt 32 0
  843. %nine = OpConstant %u32 9
  844. %1 = OpVariable %u32 Uniform
  845. %2 = OpVariable %u32 Uniform
  846. )";
  847. DecorationManager* decoManager = GetDecorationManager(spirv);
  848. EXPECT_THAT(GetErrorMessage(), "");
  849. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  850. }
  851. TEST_F(DecorationManagerTest, HaveTheSameDecorationsStringWithoutGroupsTrue) {
  852. const std::string spirv = R"(
  853. OpCapability Kernel
  854. OpCapability Linkage
  855. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  856. OpExtension "SPV_GOOGLE_decorate_string"
  857. OpMemoryModel Logical GLSL450
  858. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  859. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
  860. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  861. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "world"
  862. %u32 = OpTypeInt 32 0
  863. %1 = OpVariable %u32 Uniform
  864. %2 = OpVariable %u32 Uniform
  865. )";
  866. DecorationManager* decoManager = GetDecorationManager(spirv);
  867. EXPECT_THAT(GetErrorMessage(), "");
  868. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  869. }
  870. TEST_F(DecorationManagerTest, HaveTheSameDecorationsStringWithoutGroupsFalse) {
  871. const std::string spirv = R"(
  872. OpCapability Kernel
  873. OpCapability Linkage
  874. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  875. OpExtension "SPV_GOOGLE_decorate_string"
  876. OpMemoryModel Logical GLSL450
  877. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  878. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
  879. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  880. %u32 = OpTypeInt 32 0
  881. %1 = OpVariable %u32 Uniform
  882. %2 = OpVariable %u32 Uniform
  883. )";
  884. DecorationManager* decoManager = GetDecorationManager(spirv);
  885. EXPECT_THAT(GetErrorMessage(), "");
  886. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  887. }
  888. TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithGroupsTrue) {
  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 %1 Constant
  896. OpDecorate %3 Restrict
  897. %3 = OpDecorationGroup
  898. OpGroupDecorate %3 %2
  899. OpDecorate %4 Invariant
  900. %4 = OpDecorationGroup
  901. OpGroupDecorate %4 %1 %2
  902. %u32 = OpTypeInt 32 0
  903. %1 = OpVariable %u32 Uniform
  904. %2 = OpVariable %u32 Uniform
  905. )";
  906. DecorationManager* decoManager = GetDecorationManager(spirv);
  907. EXPECT_THAT(GetErrorMessage(), "");
  908. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  909. }
  910. TEST_F(DecorationManagerTest, HaveTheSameDecorationsWithGroupsFalse) {
  911. const std::string spirv = R"(
  912. OpCapability Shader
  913. OpCapability Linkage
  914. OpMemoryModel Logical GLSL450
  915. OpDecorate %1 Restrict
  916. OpDecorate %2 Constant
  917. OpDecorate %1 Constant
  918. OpDecorate %4 Invariant
  919. %4 = OpDecorationGroup
  920. OpGroupDecorate %4 %1 %2
  921. %u32 = OpTypeInt 32 0
  922. %1 = OpVariable %u32 Uniform
  923. %2 = OpVariable %u32 Uniform
  924. )";
  925. DecorationManager* decoManager = GetDecorationManager(spirv);
  926. EXPECT_THAT(GetErrorMessage(), "");
  927. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  928. }
  929. TEST_F(DecorationManagerTest, HaveTheSameDecorationsDuplicateDecorations) {
  930. const std::string spirv = R"(
  931. OpCapability Shader
  932. OpCapability Linkage
  933. OpMemoryModel Logical GLSL450
  934. OpDecorate %1 Constant
  935. OpDecorate %2 Constant
  936. OpDecorate %2 Constant
  937. %u32 = OpTypeInt 32 0
  938. %1 = OpVariable %u32 Uniform
  939. %2 = OpVariable %u32 Uniform
  940. )";
  941. DecorationManager* decoManager = GetDecorationManager(spirv);
  942. EXPECT_THAT(GetErrorMessage(), "");
  943. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  944. }
  945. TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentVariations) {
  946. const std::string spirv = R"(
  947. OpCapability Shader
  948. OpCapability Linkage
  949. OpMemoryModel Logical GLSL450
  950. OpDecorate %1 Location 0
  951. OpDecorate %2 Location 1
  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_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  959. }
  960. TEST_F(DecorationManagerTest,
  961. HaveTheSameDecorationsDuplicateMemberDecorations) {
  962. const std::string spirv = R"(
  963. OpCapability Shader
  964. OpCapability Linkage
  965. OpMemoryModel Logical GLSL450
  966. OpMemberDecorate %1 0 Location 0
  967. OpMemberDecorate %2 0 Location 0
  968. OpMemberDecorate %2 0 Location 0
  969. %u32 = OpTypeInt 32 0
  970. %1 = OpTypeStruct %u32 %u32
  971. %2 = OpTypeStruct %u32 %u32
  972. )";
  973. DecorationManager* decoManager = GetDecorationManager(spirv);
  974. EXPECT_THAT(GetErrorMessage(), "");
  975. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  976. }
  977. TEST_F(DecorationManagerTest,
  978. HaveTheSameDecorationsDifferentMemberSameDecoration) {
  979. const std::string spirv = R"(
  980. OpCapability Shader
  981. OpCapability Linkage
  982. OpMemoryModel Logical GLSL450
  983. OpMemberDecorate %1 0 Location 0
  984. OpMemberDecorate %2 1 Location 0
  985. %u32 = OpTypeInt 32 0
  986. %1 = OpTypeStruct %u32 %u32
  987. %2 = OpTypeStruct %u32 %u32
  988. )";
  989. DecorationManager* decoManager = GetDecorationManager(spirv);
  990. EXPECT_THAT(GetErrorMessage(), "");
  991. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  992. }
  993. TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentMemberVariations) {
  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 0 Location 1
  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, HaveTheSameDecorationsDuplicateIdDecorations) {
  1009. const std::string spirv = R"(
  1010. OpCapability Shader
  1011. OpCapability Linkage
  1012. OpMemoryModel Logical GLSL450
  1013. OpDecorateId %1 AlignmentId %2
  1014. OpDecorateId %3 AlignmentId %2
  1015. OpDecorateId %3 AlignmentId %2
  1016. %u32 = OpTypeInt 32 0
  1017. %1 = OpVariable %u32 Uniform
  1018. %3 = OpVariable %u32 Uniform
  1019. %2 = OpSpecConstant %u32 0
  1020. )";
  1021. DecorationManager* decoManager = GetDecorationManager(spirv);
  1022. EXPECT_THAT(GetErrorMessage(), "");
  1023. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 3u));
  1024. }
  1025. TEST_F(DecorationManagerTest,
  1026. HaveTheSameDecorationsDuplicateStringDecorations) {
  1027. const std::string spirv = R"(
  1028. OpCapability Shader
  1029. OpCapability Linkage
  1030. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  1031. OpExtension "SPV_GOOGLE_decorate_string"
  1032. OpMemoryModel Logical GLSL450
  1033. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  1034. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  1035. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  1036. %u32 = OpTypeInt 32 0
  1037. %1 = OpVariable %u32 Uniform
  1038. %2 = OpVariable %u32 Uniform
  1039. )";
  1040. DecorationManager* decoManager = GetDecorationManager(spirv);
  1041. EXPECT_THAT(GetErrorMessage(), "");
  1042. EXPECT_TRUE(decoManager->HaveTheSameDecorations(1u, 2u));
  1043. }
  1044. TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentIdVariations) {
  1045. const std::string spirv = R"(
  1046. OpCapability Shader
  1047. OpCapability Linkage
  1048. OpMemoryModel Logical GLSL450
  1049. OpDecorateId %1 AlignmentId %2
  1050. OpDecorateId %3 AlignmentId %4
  1051. %u32 = OpTypeInt 32 0
  1052. %1 = OpVariable %u32 Uniform
  1053. %3 = OpVariable %u32 Uniform
  1054. %2 = OpSpecConstant %u32 0
  1055. %4 = OpSpecConstant %u32 0
  1056. )";
  1057. DecorationManager* decoManager = GetDecorationManager(spirv);
  1058. EXPECT_THAT(GetErrorMessage(), "");
  1059. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1060. }
  1061. TEST_F(DecorationManagerTest, HaveTheSameDecorationsDifferentStringVariations) {
  1062. const std::string spirv = R"(
  1063. OpCapability Shader
  1064. OpCapability Linkage
  1065. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  1066. OpExtension "SPV_GOOGLE_decorate_string"
  1067. OpMemoryModel Logical GLSL450
  1068. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  1069. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
  1070. )";
  1071. DecorationManager* decoManager = GetDecorationManager(spirv);
  1072. EXPECT_THAT(GetErrorMessage(), "");
  1073. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1074. }
  1075. TEST_F(DecorationManagerTest, HaveTheSameDecorationsLeftSymmetry) {
  1076. // Left being a subset of right is not enough.
  1077. const std::string spirv = R"(
  1078. OpCapability Shader
  1079. OpCapability Linkage
  1080. OpMemoryModel Logical GLSL450
  1081. OpDecorate %1 Constant
  1082. OpDecorate %1 Constant
  1083. OpDecorate %2 Constant
  1084. OpDecorate %2 Restrict
  1085. %u32 = OpTypeInt 32 0
  1086. %1 = OpVariable %u32 Uniform
  1087. %2 = OpVariable %u32 Uniform
  1088. )";
  1089. DecorationManager* decoManager = GetDecorationManager(spirv);
  1090. EXPECT_THAT(GetErrorMessage(), "");
  1091. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1092. }
  1093. TEST_F(DecorationManagerTest, HaveTheSameDecorationsRightSymmetry) {
  1094. // Right being a subset of left is not enough.
  1095. const std::string spirv = R"(
  1096. OpCapability Shader
  1097. OpCapability Linkage
  1098. OpMemoryModel Logical GLSL450
  1099. OpDecorate %1 Constant
  1100. OpDecorate %1 Restrict
  1101. OpDecorate %2 Constant
  1102. OpDecorate %2 Constant
  1103. %u32 = OpTypeInt 32 0
  1104. %1 = OpVariable %u32 Uniform
  1105. %2 = OpVariable %u32 Uniform
  1106. )";
  1107. DecorationManager* decoManager = GetDecorationManager(spirv);
  1108. EXPECT_THAT(GetErrorMessage(), "");
  1109. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1110. }
  1111. TEST_F(DecorationManagerTest, HaveTheSameDecorationIdsLeftSymmetry) {
  1112. const std::string spirv = R"(
  1113. OpCapability Kernel
  1114. OpCapability Linkage
  1115. OpMemoryModel Logical GLSL450
  1116. OpDecorateId %1 AlignmentId %nine
  1117. OpDecorateId %1 AlignmentId %nine
  1118. OpDecorateId %2 AlignmentId %nine
  1119. OpDecorateId %2 MaxByteOffsetId %nine
  1120. %u32 = OpTypeInt 32 0
  1121. %nine = OpConstant %u32 9
  1122. %1 = OpVariable %u32 Uniform
  1123. %2 = OpVariable %u32 Uniform
  1124. )";
  1125. DecorationManager* decoManager = GetDecorationManager(spirv);
  1126. EXPECT_THAT(GetErrorMessage(), "");
  1127. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1128. }
  1129. TEST_F(DecorationManagerTest, HaveTheSameDecorationIdsRightSymmetry) {
  1130. const std::string spirv = R"(
  1131. OpCapability Kernel
  1132. OpCapability Linkage
  1133. OpMemoryModel Logical GLSL450
  1134. OpDecorateId %1 AlignmentId %nine
  1135. OpDecorateId %1 MaxByteOffsetId %nine
  1136. OpDecorateId %2 AlignmentId %nine
  1137. OpDecorateId %2 AlignmentId %nine
  1138. %u32 = OpTypeInt 32 0
  1139. %nine = OpConstant %u32 9
  1140. %1 = OpVariable %u32 Uniform
  1141. %2 = OpVariable %u32 Uniform
  1142. )";
  1143. DecorationManager* decoManager = GetDecorationManager(spirv);
  1144. EXPECT_THAT(GetErrorMessage(), "");
  1145. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1146. }
  1147. TEST_F(DecorationManagerTest, HaveTheSameDecorationStringsLeftSymmetry) {
  1148. const std::string spirv = R"(
  1149. OpCapability Kernel
  1150. OpCapability Linkage
  1151. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  1152. OpExtension "SPV_GOOGLE_decorate_string"
  1153. OpMemoryModel Logical GLSL450
  1154. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  1155. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  1156. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  1157. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "world"
  1158. %u32 = OpTypeInt 32 0
  1159. %1 = OpVariable %u32 Uniform
  1160. %2 = OpVariable %u32 Uniform
  1161. )";
  1162. DecorationManager* decoManager = GetDecorationManager(spirv);
  1163. EXPECT_THAT(GetErrorMessage(), "");
  1164. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1165. }
  1166. TEST_F(DecorationManagerTest, HaveTheSameDecorationStringsRightSymmetry) {
  1167. const std::string spirv = R"(
  1168. OpCapability Kernel
  1169. OpCapability Linkage
  1170. OpExtension "SPV_GOOGLE_hlsl_functionality1"
  1171. OpExtension "SPV_GOOGLE_decorate_string"
  1172. OpMemoryModel Logical GLSL450
  1173. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "hello"
  1174. OpDecorateStringGOOGLE %1 HlslSemanticGOOGLE "world"
  1175. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  1176. OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "hello"
  1177. %u32 = OpTypeInt 32 0
  1178. %1 = OpVariable %u32 Uniform
  1179. %2 = OpVariable %u32 Uniform
  1180. )";
  1181. DecorationManager* decoManager = GetDecorationManager(spirv);
  1182. EXPECT_THAT(GetErrorMessage(), "");
  1183. EXPECT_FALSE(decoManager->HaveTheSameDecorations(1u, 2u));
  1184. }
  1185. } // namespace
  1186. } // namespace analysis
  1187. } // namespace opt
  1188. } // namespace spvtools