pass_remove_duplicates_test.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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/ir_context.h"
  21. #include "source/opt/pass_manager.h"
  22. #include "source/opt/remove_duplicates_pass.h"
  23. #include "source/spirv_constant.h"
  24. #include "test/unit_spirv.h"
  25. namespace spvtools {
  26. namespace opt {
  27. namespace {
  28. class RemoveDuplicatesTest : public ::testing::Test {
  29. public:
  30. RemoveDuplicatesTest()
  31. : tools_(SPV_ENV_UNIVERSAL_1_2),
  32. context_(),
  33. consumer_([this](spv_message_level_t level, const char*,
  34. const spv_position_t& position, const char* message) {
  35. if (!error_message_.empty()) error_message_ += "\n";
  36. switch (level) {
  37. case SPV_MSG_FATAL:
  38. case SPV_MSG_INTERNAL_ERROR:
  39. case SPV_MSG_ERROR:
  40. error_message_ += "ERROR";
  41. break;
  42. case SPV_MSG_WARNING:
  43. error_message_ += "WARNING";
  44. break;
  45. case SPV_MSG_INFO:
  46. error_message_ += "INFO";
  47. break;
  48. case SPV_MSG_DEBUG:
  49. error_message_ += "DEBUG";
  50. break;
  51. }
  52. error_message_ +=
  53. ": " + std::to_string(position.index) + ": " + message;
  54. }),
  55. disassemble_options_(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER),
  56. error_message_() {
  57. tools_.SetMessageConsumer(consumer_);
  58. }
  59. void TearDown() override { error_message_.clear(); }
  60. std::string RunPass(const std::string& text) {
  61. context_ = spvtools::BuildModule(SPV_ENV_UNIVERSAL_1_2, consumer_, text);
  62. if (!context_.get()) return std::string();
  63. PassManager manager;
  64. manager.SetMessageConsumer(consumer_);
  65. manager.AddPass<RemoveDuplicatesPass>();
  66. Pass::Status pass_res = manager.Run(context_.get());
  67. if (pass_res == Pass::Status::Failure) return std::string();
  68. return ModuleToText();
  69. }
  70. // Disassembles |binary| and outputs the result in |text|. If |text| is a
  71. // null pointer, SPV_ERROR_INVALID_POINTER is returned.
  72. spv_result_t Disassemble(const std::vector<uint32_t>& binary,
  73. std::string* text) {
  74. if (!text) return SPV_ERROR_INVALID_POINTER;
  75. return tools_.Disassemble(binary, text, disassemble_options_)
  76. ? SPV_SUCCESS
  77. : SPV_ERROR_INVALID_BINARY;
  78. }
  79. // Returns the accumulated error messages for the test.
  80. std::string GetErrorMessage() const { return error_message_; }
  81. std::string ToText(const std::vector<Instruction*>& inst) {
  82. std::vector<uint32_t> binary = {SpvMagicNumber, 0x10200, 0u, 2u, 0u};
  83. for (const Instruction* i : inst)
  84. i->ToBinaryWithoutAttachedDebugInsts(&binary);
  85. std::string text;
  86. Disassemble(binary, &text);
  87. return text;
  88. }
  89. std::string ModuleToText() {
  90. std::vector<uint32_t> binary;
  91. context_->module()->ToBinary(&binary, false);
  92. std::string text;
  93. Disassemble(binary, &text);
  94. return text;
  95. }
  96. private:
  97. spvtools::SpirvTools
  98. tools_; // An instance for calling SPIRV-Tools functionalities.
  99. std::unique_ptr<IRContext> context_;
  100. spvtools::MessageConsumer consumer_;
  101. uint32_t disassemble_options_;
  102. std::string error_message_;
  103. };
  104. TEST_F(RemoveDuplicatesTest, DuplicateCapabilities) {
  105. const std::string spirv = R"(
  106. OpCapability Shader
  107. OpCapability Linkage
  108. OpCapability Shader
  109. OpMemoryModel Logical GLSL450
  110. )";
  111. const std::string after = R"(OpCapability Shader
  112. OpCapability Linkage
  113. OpMemoryModel Logical GLSL450
  114. )";
  115. EXPECT_EQ(RunPass(spirv), after);
  116. EXPECT_EQ(GetErrorMessage(), "");
  117. }
  118. TEST_F(RemoveDuplicatesTest, DuplicateExtInstImports) {
  119. const std::string spirv = R"(
  120. OpCapability Shader
  121. OpCapability Linkage
  122. %1 = OpExtInstImport "OpenCL.std"
  123. %2 = OpExtInstImport "OpenCL.std"
  124. %3 = OpExtInstImport "GLSL.std.450"
  125. OpMemoryModel Logical GLSL450
  126. )";
  127. const std::string after = R"(OpCapability Shader
  128. OpCapability Linkage
  129. %1 = OpExtInstImport "OpenCL.std"
  130. %3 = OpExtInstImport "GLSL.std.450"
  131. OpMemoryModel Logical GLSL450
  132. )";
  133. EXPECT_EQ(RunPass(spirv), after);
  134. EXPECT_EQ(GetErrorMessage(), "");
  135. }
  136. TEST_F(RemoveDuplicatesTest, DuplicateTypes) {
  137. const std::string spirv = R"(
  138. OpCapability Shader
  139. OpCapability Linkage
  140. OpMemoryModel Logical GLSL450
  141. %1 = OpTypeInt 32 0
  142. %2 = OpTypeInt 32 0
  143. %3 = OpTypeStruct %1 %2
  144. )";
  145. const std::string after = R"(OpCapability Shader
  146. OpCapability Linkage
  147. OpMemoryModel Logical GLSL450
  148. %1 = OpTypeInt 32 0
  149. %3 = OpTypeStruct %1 %1
  150. )";
  151. EXPECT_EQ(RunPass(spirv), after);
  152. EXPECT_EQ(GetErrorMessage(), "");
  153. }
  154. TEST_F(RemoveDuplicatesTest, SameTypeDifferentMemberDecoration) {
  155. const std::string spirv = R"(
  156. OpCapability Shader
  157. OpCapability Linkage
  158. OpMemoryModel Logical GLSL450
  159. OpDecorate %1 GLSLPacked
  160. %2 = OpTypeInt 32 0
  161. %1 = OpTypeStruct %2 %2
  162. %3 = OpTypeStruct %2 %2
  163. )";
  164. const std::string after = R"(OpCapability Shader
  165. OpCapability Linkage
  166. OpMemoryModel Logical GLSL450
  167. OpDecorate %1 GLSLPacked
  168. %2 = OpTypeInt 32 0
  169. %1 = OpTypeStruct %2 %2
  170. %3 = OpTypeStruct %2 %2
  171. )";
  172. EXPECT_EQ(RunPass(spirv), after);
  173. EXPECT_EQ(GetErrorMessage(), "");
  174. }
  175. TEST_F(RemoveDuplicatesTest, SameTypeAndMemberDecoration) {
  176. const std::string spirv = R"(
  177. OpCapability Shader
  178. OpCapability Linkage
  179. OpMemoryModel Logical GLSL450
  180. OpDecorate %1 GLSLPacked
  181. OpDecorate %2 GLSLPacked
  182. %3 = OpTypeInt 32 0
  183. %1 = OpTypeStruct %3 %3
  184. %2 = OpTypeStruct %3 %3
  185. )";
  186. const std::string after = R"(OpCapability Shader
  187. OpCapability Linkage
  188. OpMemoryModel Logical GLSL450
  189. OpDecorate %1 GLSLPacked
  190. %3 = OpTypeInt 32 0
  191. %1 = OpTypeStruct %3 %3
  192. )";
  193. EXPECT_EQ(RunPass(spirv), after);
  194. EXPECT_EQ(GetErrorMessage(), "");
  195. }
  196. TEST_F(RemoveDuplicatesTest, SameTypeAndDifferentName) {
  197. const std::string spirv = R"(
  198. OpCapability Shader
  199. OpCapability Linkage
  200. OpMemoryModel Logical GLSL450
  201. OpName %1 "Type1"
  202. OpName %2 "Type2"
  203. %3 = OpTypeInt 32 0
  204. %1 = OpTypeStruct %3 %3
  205. %2 = OpTypeStruct %3 %3
  206. )";
  207. const std::string after = R"(OpCapability Shader
  208. OpCapability Linkage
  209. OpMemoryModel Logical GLSL450
  210. OpName %1 "Type1"
  211. %3 = OpTypeInt 32 0
  212. %1 = OpTypeStruct %3 %3
  213. )";
  214. EXPECT_EQ(RunPass(spirv), after);
  215. EXPECT_EQ(GetErrorMessage(), "");
  216. }
  217. // Check that #1033 has been fixed.
  218. TEST_F(RemoveDuplicatesTest, DoNotRemoveDifferentOpDecorationGroup) {
  219. const std::string spirv = R"(
  220. OpCapability Shader
  221. OpCapability Linkage
  222. OpMemoryModel Logical GLSL450
  223. OpDecorate %1 Constant
  224. %1 = OpDecorationGroup
  225. OpDecorate %2 Restrict
  226. %2 = OpDecorationGroup
  227. OpGroupDecorate %3 %1 %2
  228. %4 = OpTypeInt 32 0
  229. %3 = OpVariable %4 Uniform
  230. )";
  231. const std::string after = R"(OpCapability Shader
  232. OpCapability Linkage
  233. OpMemoryModel Logical GLSL450
  234. OpDecorate %1 Constant
  235. %1 = OpDecorationGroup
  236. OpDecorate %2 Restrict
  237. %2 = OpDecorationGroup
  238. OpGroupDecorate %3 %1 %2
  239. %4 = OpTypeInt 32 0
  240. %3 = OpVariable %4 Uniform
  241. )";
  242. EXPECT_EQ(RunPass(spirv), after);
  243. EXPECT_EQ(GetErrorMessage(), "");
  244. }
  245. TEST_F(RemoveDuplicatesTest, DifferentDecorationGroup) {
  246. const std::string spirv = R"(
  247. OpCapability Shader
  248. OpCapability Linkage
  249. OpMemoryModel Logical GLSL450
  250. OpDecorate %1 Constant
  251. OpDecorate %1 Restrict
  252. %1 = OpDecorationGroup
  253. OpDecorate %2 Constant
  254. %2 = OpDecorationGroup
  255. OpGroupDecorate %1 %3
  256. OpGroupDecorate %2 %4
  257. %5 = OpTypeInt 32 0
  258. %3 = OpVariable %5 Uniform
  259. %4 = OpVariable %5 Uniform
  260. )";
  261. const std::string after = R"(OpCapability Shader
  262. OpCapability Linkage
  263. OpMemoryModel Logical GLSL450
  264. OpDecorate %1 Constant
  265. OpDecorate %1 Restrict
  266. %1 = OpDecorationGroup
  267. OpDecorate %2 Constant
  268. %2 = OpDecorationGroup
  269. OpGroupDecorate %1 %3
  270. OpGroupDecorate %2 %4
  271. %5 = OpTypeInt 32 0
  272. %3 = OpVariable %5 Uniform
  273. %4 = OpVariable %5 Uniform
  274. )";
  275. EXPECT_EQ(RunPass(spirv), after);
  276. EXPECT_EQ(GetErrorMessage(), "");
  277. }
  278. // Test what happens when a type is a resource type. For now we are merging
  279. // them, but, if we want to merge types and make reflection work (issue #1372),
  280. // we will not be able to merge %2 and %3 below.
  281. TEST_F(RemoveDuplicatesTest, DontMergeNestedResourceTypes) {
  282. const std::string spirv = R"(OpCapability Shader
  283. OpMemoryModel Logical GLSL450
  284. OpSource HLSL 600
  285. OpName %1 "PositionAdjust"
  286. OpMemberName %1 0 "XAdjust"
  287. OpName %2 "NormalAdjust"
  288. OpMemberName %2 0 "XDir"
  289. OpMemberName %3 0 "AdjustXYZ"
  290. OpMemberName %3 1 "AdjustDir"
  291. OpName %4 "Constants"
  292. OpMemberDecorate %1 0 Offset 0
  293. OpMemberDecorate %2 0 Offset 0
  294. OpMemberDecorate %3 0 Offset 0
  295. OpMemberDecorate %3 1 Offset 16
  296. OpDecorate %3 Block
  297. OpDecorate %4 DescriptorSet 0
  298. OpDecorate %4 Binding 0
  299. %5 = OpTypeFloat 32
  300. %6 = OpTypeVector %5 3
  301. %1 = OpTypeStruct %6
  302. %2 = OpTypeStruct %6
  303. %3 = OpTypeStruct %1 %2
  304. %7 = OpTypePointer Uniform %3
  305. %4 = OpVariable %7 Uniform
  306. )";
  307. const std::string result = R"(OpCapability Shader
  308. OpMemoryModel Logical GLSL450
  309. OpSource HLSL 600
  310. OpName %1 "PositionAdjust"
  311. OpMemberName %1 0 "XAdjust"
  312. OpMemberName %3 0 "AdjustXYZ"
  313. OpMemberName %3 1 "AdjustDir"
  314. OpName %4 "Constants"
  315. OpMemberDecorate %1 0 Offset 0
  316. OpMemberDecorate %3 0 Offset 0
  317. OpMemberDecorate %3 1 Offset 16
  318. OpDecorate %3 Block
  319. OpDecorate %4 DescriptorSet 0
  320. OpDecorate %4 Binding 0
  321. %5 = OpTypeFloat 32
  322. %6 = OpTypeVector %5 3
  323. %1 = OpTypeStruct %6
  324. %3 = OpTypeStruct %1 %1
  325. %7 = OpTypePointer Uniform %3
  326. %4 = OpVariable %7 Uniform
  327. )";
  328. EXPECT_EQ(RunPass(spirv), result);
  329. EXPECT_EQ(GetErrorMessage(), "");
  330. }
  331. // See comment for DontMergeNestedResourceTypes.
  332. TEST_F(RemoveDuplicatesTest, DontMergeResourceTypes) {
  333. const std::string spirv = R"(OpCapability Shader
  334. OpMemoryModel Logical GLSL450
  335. OpSource HLSL 600
  336. OpName %1 "PositionAdjust"
  337. OpMemberName %1 0 "XAdjust"
  338. OpName %2 "NormalAdjust"
  339. OpMemberName %2 0 "XDir"
  340. OpName %3 "Constants"
  341. OpMemberDecorate %1 0 Offset 0
  342. OpMemberDecorate %2 0 Offset 0
  343. OpDecorate %3 DescriptorSet 0
  344. OpDecorate %3 Binding 0
  345. OpDecorate %4 DescriptorSet 1
  346. OpDecorate %4 Binding 0
  347. %5 = OpTypeFloat 32
  348. %6 = OpTypeVector %5 3
  349. %1 = OpTypeStruct %6
  350. %2 = OpTypeStruct %6
  351. %7 = OpTypePointer Uniform %1
  352. %8 = OpTypePointer Uniform %2
  353. %3 = OpVariable %7 Uniform
  354. %4 = OpVariable %8 Uniform
  355. )";
  356. const std::string result = R"(OpCapability Shader
  357. OpMemoryModel Logical GLSL450
  358. OpSource HLSL 600
  359. OpName %1 "PositionAdjust"
  360. OpMemberName %1 0 "XAdjust"
  361. OpName %3 "Constants"
  362. OpMemberDecorate %1 0 Offset 0
  363. OpDecorate %3 DescriptorSet 0
  364. OpDecorate %3 Binding 0
  365. OpDecorate %4 DescriptorSet 1
  366. OpDecorate %4 Binding 0
  367. %5 = OpTypeFloat 32
  368. %6 = OpTypeVector %5 3
  369. %1 = OpTypeStruct %6
  370. %7 = OpTypePointer Uniform %1
  371. %3 = OpVariable %7 Uniform
  372. %4 = OpVariable %7 Uniform
  373. )";
  374. EXPECT_EQ(RunPass(spirv), result);
  375. EXPECT_EQ(GetErrorMessage(), "");
  376. }
  377. // See comment for DontMergeNestedResourceTypes.
  378. TEST_F(RemoveDuplicatesTest, DontMergeResourceTypesContainingArray) {
  379. const std::string spirv = R"(OpCapability Shader
  380. OpMemoryModel Logical GLSL450
  381. OpSource HLSL 600
  382. OpName %1 "PositionAdjust"
  383. OpMemberName %1 0 "XAdjust"
  384. OpName %2 "NormalAdjust"
  385. OpMemberName %2 0 "XDir"
  386. OpName %3 "Constants"
  387. OpMemberDecorate %1 0 Offset 0
  388. OpMemberDecorate %2 0 Offset 0
  389. OpDecorate %3 DescriptorSet 0
  390. OpDecorate %3 Binding 0
  391. OpDecorate %4 DescriptorSet 1
  392. OpDecorate %4 Binding 0
  393. %5 = OpTypeFloat 32
  394. %6 = OpTypeVector %5 3
  395. %1 = OpTypeStruct %6
  396. %2 = OpTypeStruct %6
  397. %7 = OpTypeInt 32 0
  398. %8 = OpConstant %7 4
  399. %9 = OpTypeArray %1 %8
  400. %10 = OpTypeArray %2 %8
  401. %11 = OpTypePointer Uniform %9
  402. %12 = OpTypePointer Uniform %10
  403. %3 = OpVariable %11 Uniform
  404. %4 = OpVariable %12 Uniform
  405. )";
  406. const std::string result = R"(OpCapability Shader
  407. OpMemoryModel Logical GLSL450
  408. OpSource HLSL 600
  409. OpName %1 "PositionAdjust"
  410. OpMemberName %1 0 "XAdjust"
  411. OpName %3 "Constants"
  412. OpMemberDecorate %1 0 Offset 0
  413. OpDecorate %3 DescriptorSet 0
  414. OpDecorate %3 Binding 0
  415. OpDecorate %4 DescriptorSet 1
  416. OpDecorate %4 Binding 0
  417. %5 = OpTypeFloat 32
  418. %6 = OpTypeVector %5 3
  419. %1 = OpTypeStruct %6
  420. %7 = OpTypeInt 32 0
  421. %8 = OpConstant %7 4
  422. %9 = OpTypeArray %1 %8
  423. %11 = OpTypePointer Uniform %9
  424. %3 = OpVariable %11 Uniform
  425. %4 = OpVariable %11 Uniform
  426. )";
  427. EXPECT_EQ(RunPass(spirv), result);
  428. EXPECT_EQ(GetErrorMessage(), "");
  429. }
  430. // Test that we merge the type of a resource with a type that is not the type
  431. // a resource. The resource type appears first in this case. We must keep
  432. // the resource type.
  433. TEST_F(RemoveDuplicatesTest, MergeResourceTypeWithNonresourceType1) {
  434. const std::string spirv = R"(OpCapability Shader
  435. OpMemoryModel Logical GLSL450
  436. OpSource HLSL 600
  437. OpName %1 "PositionAdjust"
  438. OpMemberName %1 0 "XAdjust"
  439. OpName %2 "NormalAdjust"
  440. OpMemberName %2 0 "XDir"
  441. OpName %3 "Constants"
  442. OpMemberDecorate %1 0 Offset 0
  443. OpMemberDecorate %2 0 Offset 0
  444. OpDecorate %3 DescriptorSet 0
  445. OpDecorate %3 Binding 0
  446. %4 = OpTypeFloat 32
  447. %5 = OpTypeVector %4 3
  448. %1 = OpTypeStruct %5
  449. %2 = OpTypeStruct %5
  450. %6 = OpTypePointer Uniform %1
  451. %7 = OpTypePointer Uniform %2
  452. %3 = OpVariable %6 Uniform
  453. %8 = OpVariable %7 Uniform
  454. )";
  455. const std::string result = R"(OpCapability Shader
  456. OpMemoryModel Logical GLSL450
  457. OpSource HLSL 600
  458. OpName %1 "PositionAdjust"
  459. OpMemberName %1 0 "XAdjust"
  460. OpName %3 "Constants"
  461. OpMemberDecorate %1 0 Offset 0
  462. OpDecorate %3 DescriptorSet 0
  463. OpDecorate %3 Binding 0
  464. %4 = OpTypeFloat 32
  465. %5 = OpTypeVector %4 3
  466. %1 = OpTypeStruct %5
  467. %6 = OpTypePointer Uniform %1
  468. %3 = OpVariable %6 Uniform
  469. %8 = OpVariable %6 Uniform
  470. )";
  471. EXPECT_EQ(RunPass(spirv), result);
  472. EXPECT_EQ(GetErrorMessage(), "");
  473. }
  474. // Test that we merge the type of a resource with a type that is not the type
  475. // a resource. The resource type appears second in this case. We must keep
  476. // the resource type.
  477. //
  478. // See comment for DontMergeNestedResourceTypes.
  479. TEST_F(RemoveDuplicatesTest, MergeResourceTypeWithNonresourceType2) {
  480. const std::string spirv = R"(OpCapability Shader
  481. OpMemoryModel Logical GLSL450
  482. OpSource HLSL 600
  483. OpName %1 "PositionAdjust"
  484. OpMemberName %1 0 "XAdjust"
  485. OpName %2 "NormalAdjust"
  486. OpMemberName %2 0 "XDir"
  487. OpName %3 "Constants"
  488. OpMemberDecorate %1 0 Offset 0
  489. OpMemberDecorate %2 0 Offset 0
  490. OpDecorate %3 DescriptorSet 0
  491. OpDecorate %3 Binding 0
  492. %4 = OpTypeFloat 32
  493. %5 = OpTypeVector %4 3
  494. %1 = OpTypeStruct %5
  495. %2 = OpTypeStruct %5
  496. %6 = OpTypePointer Uniform %1
  497. %7 = OpTypePointer Uniform %2
  498. %8 = OpVariable %6 Uniform
  499. %3 = OpVariable %7 Uniform
  500. )";
  501. const std::string result = R"(OpCapability Shader
  502. OpMemoryModel Logical GLSL450
  503. OpSource HLSL 600
  504. OpName %1 "PositionAdjust"
  505. OpMemberName %1 0 "XAdjust"
  506. OpName %3 "Constants"
  507. OpMemberDecorate %1 0 Offset 0
  508. OpDecorate %3 DescriptorSet 0
  509. OpDecorate %3 Binding 0
  510. %4 = OpTypeFloat 32
  511. %5 = OpTypeVector %4 3
  512. %1 = OpTypeStruct %5
  513. %6 = OpTypePointer Uniform %1
  514. %8 = OpVariable %6 Uniform
  515. %3 = OpVariable %6 Uniform
  516. )";
  517. EXPECT_EQ(RunPass(spirv), result);
  518. EXPECT_EQ(GetErrorMessage(), "");
  519. }
  520. // In this test, %8 and %9 are the same and only %9 is used in a resource.
  521. // However, we cannot merge them unless we also merge %2 and %3, which cannot
  522. // happen because both are used in resources.
  523. //
  524. // If we try to avoid replaces resource types, then remove duplicates should
  525. // have not change in this case. That is not currently implemented.
  526. TEST_F(RemoveDuplicatesTest, MergeResourceTypeWithNonresourceType3) {
  527. const std::string spirv = R"(OpCapability Shader
  528. OpMemoryModel Logical GLSL450
  529. OpEntryPoint GLCompute %1 "main"
  530. OpSource HLSL 600
  531. OpName %2 "PositionAdjust"
  532. OpMemberName %2 0 "XAdjust"
  533. OpName %3 "NormalAdjust"
  534. OpMemberName %3 0 "XDir"
  535. OpName %4 "Constants"
  536. OpMemberDecorate %2 0 Offset 0
  537. OpMemberDecorate %3 0 Offset 0
  538. OpDecorate %4 DescriptorSet 0
  539. OpDecorate %4 Binding 0
  540. OpDecorate %5 DescriptorSet 1
  541. OpDecorate %5 Binding 0
  542. %6 = OpTypeFloat 32
  543. %7 = OpTypeVector %6 3
  544. %2 = OpTypeStruct %7
  545. %3 = OpTypeStruct %7
  546. %8 = OpTypePointer Uniform %3
  547. %9 = OpTypePointer Uniform %2
  548. %10 = OpTypeStruct %3
  549. %11 = OpTypePointer Uniform %10
  550. %5 = OpVariable %9 Uniform
  551. %4 = OpVariable %11 Uniform
  552. %12 = OpTypeVoid
  553. %13 = OpTypeFunction %12
  554. %14 = OpTypeInt 32 0
  555. %15 = OpConstant %14 0
  556. %1 = OpFunction %12 None %13
  557. %16 = OpLabel
  558. %17 = OpAccessChain %8 %4 %15
  559. OpReturn
  560. OpFunctionEnd
  561. )";
  562. const std::string result = R"(OpCapability Shader
  563. OpMemoryModel Logical GLSL450
  564. OpEntryPoint GLCompute %1 "main"
  565. OpSource HLSL 600
  566. OpName %2 "PositionAdjust"
  567. OpMemberName %2 0 "XAdjust"
  568. OpName %4 "Constants"
  569. OpMemberDecorate %2 0 Offset 0
  570. OpDecorate %4 DescriptorSet 0
  571. OpDecorate %4 Binding 0
  572. OpDecorate %5 DescriptorSet 1
  573. OpDecorate %5 Binding 0
  574. %6 = OpTypeFloat 32
  575. %7 = OpTypeVector %6 3
  576. %2 = OpTypeStruct %7
  577. %8 = OpTypePointer Uniform %2
  578. %10 = OpTypeStruct %2
  579. %11 = OpTypePointer Uniform %10
  580. %5 = OpVariable %8 Uniform
  581. %4 = OpVariable %11 Uniform
  582. %12 = OpTypeVoid
  583. %13 = OpTypeFunction %12
  584. %14 = OpTypeInt 32 0
  585. %15 = OpConstant %14 0
  586. %1 = OpFunction %12 None %13
  587. %16 = OpLabel
  588. %17 = OpAccessChain %8 %4 %15
  589. OpReturn
  590. OpFunctionEnd
  591. )";
  592. EXPECT_EQ(RunPass(spirv), result);
  593. EXPECT_EQ(GetErrorMessage(), "");
  594. }
  595. } // namespace
  596. } // namespace opt
  597. } // namespace spvtools