ir_context_test.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // Copyright (c) 2017 Google Inc.
  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 <algorithm>
  15. #include <memory>
  16. #include <string>
  17. #include <utility>
  18. #include "gmock/gmock.h"
  19. #include "gtest/gtest.h"
  20. #include "source/opt/ir_context.h"
  21. #include "source/opt/pass.h"
  22. #include "test/opt/pass_fixture.h"
  23. #include "test/opt/pass_utils.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace {
  27. using Analysis = IRContext::Analysis;
  28. using ::testing::Each;
  29. using ::testing::UnorderedElementsAre;
  30. class DummyPassPreservesNothing : public Pass {
  31. public:
  32. DummyPassPreservesNothing(Status s) : Pass(), status_to_return_(s) {}
  33. const char* name() const override { return "dummy-pass"; }
  34. Status Process() override { return status_to_return_; }
  35. private:
  36. Status status_to_return_;
  37. };
  38. class DummyPassPreservesAll : public Pass {
  39. public:
  40. DummyPassPreservesAll(Status s) : Pass(), status_to_return_(s) {}
  41. const char* name() const override { return "dummy-pass"; }
  42. Status Process() override { return status_to_return_; }
  43. Analysis GetPreservedAnalyses() override {
  44. return Analysis(IRContext::kAnalysisEnd - 1);
  45. }
  46. private:
  47. Status status_to_return_;
  48. };
  49. class DummyPassPreservesFirst : public Pass {
  50. public:
  51. DummyPassPreservesFirst(Status s) : Pass(), status_to_return_(s) {}
  52. const char* name() const override { return "dummy-pass"; }
  53. Status Process() override { return status_to_return_; }
  54. Analysis GetPreservedAnalyses() override { return IRContext::kAnalysisBegin; }
  55. private:
  56. Status status_to_return_;
  57. };
  58. using IRContextTest = PassTest<::testing::Test>;
  59. TEST_F(IRContextTest, IndividualValidAfterBuild) {
  60. std::unique_ptr<Module> module(new Module());
  61. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  62. spvtools::MessageConsumer());
  63. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  64. i <<= 1) {
  65. localContext.BuildInvalidAnalyses(i);
  66. EXPECT_TRUE(localContext.AreAnalysesValid(i));
  67. }
  68. }
  69. TEST_F(IRContextTest, AllValidAfterBuild) {
  70. std::unique_ptr<Module> module = MakeUnique<Module>();
  71. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  72. spvtools::MessageConsumer());
  73. Analysis built_analyses = IRContext::kAnalysisNone;
  74. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  75. i <<= 1) {
  76. localContext.BuildInvalidAnalyses(i);
  77. built_analyses |= i;
  78. }
  79. EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
  80. }
  81. TEST_F(IRContextTest, AllValidAfterPassNoChange) {
  82. std::unique_ptr<Module> module = MakeUnique<Module>();
  83. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  84. spvtools::MessageConsumer());
  85. Analysis built_analyses = IRContext::kAnalysisNone;
  86. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  87. i <<= 1) {
  88. localContext.BuildInvalidAnalyses(i);
  89. built_analyses |= i;
  90. }
  91. DummyPassPreservesNothing pass(Pass::Status::SuccessWithoutChange);
  92. Pass::Status s = pass.Run(&localContext);
  93. EXPECT_EQ(s, Pass::Status::SuccessWithoutChange);
  94. EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
  95. }
  96. TEST_F(IRContextTest, NoneValidAfterPassWithChange) {
  97. std::unique_ptr<Module> module = MakeUnique<Module>();
  98. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  99. spvtools::MessageConsumer());
  100. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  101. i <<= 1) {
  102. localContext.BuildInvalidAnalyses(i);
  103. }
  104. DummyPassPreservesNothing pass(Pass::Status::SuccessWithChange);
  105. Pass::Status s = pass.Run(&localContext);
  106. EXPECT_EQ(s, Pass::Status::SuccessWithChange);
  107. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  108. i <<= 1) {
  109. EXPECT_FALSE(localContext.AreAnalysesValid(i));
  110. }
  111. }
  112. TEST_F(IRContextTest, AllPreservedAfterPassWithChange) {
  113. std::unique_ptr<Module> module = MakeUnique<Module>();
  114. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  115. spvtools::MessageConsumer());
  116. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  117. i <<= 1) {
  118. localContext.BuildInvalidAnalyses(i);
  119. }
  120. DummyPassPreservesAll pass(Pass::Status::SuccessWithChange);
  121. Pass::Status s = pass.Run(&localContext);
  122. EXPECT_EQ(s, Pass::Status::SuccessWithChange);
  123. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  124. i <<= 1) {
  125. EXPECT_TRUE(localContext.AreAnalysesValid(i));
  126. }
  127. }
  128. TEST_F(IRContextTest, PreserveFirstOnlyAfterPassWithChange) {
  129. std::unique_ptr<Module> module = MakeUnique<Module>();
  130. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
  131. spvtools::MessageConsumer());
  132. for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
  133. i <<= 1) {
  134. localContext.BuildInvalidAnalyses(i);
  135. }
  136. DummyPassPreservesFirst pass(Pass::Status::SuccessWithChange);
  137. Pass::Status s = pass.Run(&localContext);
  138. EXPECT_EQ(s, Pass::Status::SuccessWithChange);
  139. EXPECT_TRUE(localContext.AreAnalysesValid(IRContext::kAnalysisBegin));
  140. for (Analysis i = IRContext::kAnalysisBegin << 1; i < IRContext::kAnalysisEnd;
  141. i <<= 1) {
  142. EXPECT_FALSE(localContext.AreAnalysesValid(i));
  143. }
  144. }
  145. TEST_F(IRContextTest, KillMemberName) {
  146. const std::string text = R"(
  147. OpCapability Shader
  148. %1 = OpExtInstImport "GLSL.std.450"
  149. OpMemoryModel Logical GLSL450
  150. OpEntryPoint Fragment %2 "main"
  151. OpExecutionMode %2 OriginUpperLeft
  152. OpSource GLSL 430
  153. OpName %3 "stuff"
  154. OpMemberName %3 0 "refZ"
  155. OpMemberDecorate %3 0 Offset 0
  156. OpDecorate %3 Block
  157. %4 = OpTypeFloat 32
  158. %3 = OpTypeStruct %4
  159. %5 = OpTypeVoid
  160. %6 = OpTypeFunction %5
  161. %2 = OpFunction %5 None %6
  162. %7 = OpLabel
  163. OpReturn
  164. OpFunctionEnd
  165. )";
  166. std::unique_ptr<IRContext> context =
  167. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  168. // Build the decoration manager.
  169. context->get_decoration_mgr();
  170. // Delete the OpTypeStruct. Should delete the OpName, OpMemberName, and
  171. // OpMemberDecorate associated with it.
  172. context->KillDef(3);
  173. // Make sure all of the name are removed.
  174. for (auto& inst : context->debugs2()) {
  175. EXPECT_EQ(inst.opcode(), SpvOpNop);
  176. }
  177. // Make sure all of the decorations are removed.
  178. for (auto& inst : context->annotations()) {
  179. EXPECT_EQ(inst.opcode(), SpvOpNop);
  180. }
  181. }
  182. TEST_F(IRContextTest, KillGroupDecoration) {
  183. const std::string text = R"(
  184. OpCapability Shader
  185. %1 = OpExtInstImport "GLSL.std.450"
  186. OpMemoryModel Logical GLSL450
  187. OpEntryPoint Fragment %2 "main"
  188. OpExecutionMode %2 OriginUpperLeft
  189. OpSource GLSL 430
  190. OpDecorate %3 Restrict
  191. %3 = OpDecorationGroup
  192. OpGroupDecorate %3 %4 %5
  193. %6 = OpTypeFloat 32
  194. %7 = OpTypePointer Function %6
  195. %8 = OpTypeStruct %6
  196. %9 = OpTypeVoid
  197. %10 = OpTypeFunction %9
  198. %2 = OpFunction %9 None %10
  199. %11 = OpLabel
  200. %4 = OpVariable %7 Function
  201. %5 = OpVariable %7 Function
  202. OpReturn
  203. OpFunctionEnd
  204. )";
  205. std::unique_ptr<IRContext> context =
  206. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  207. // Build the decoration manager.
  208. context->get_decoration_mgr();
  209. // Delete the second variable.
  210. context->KillDef(5);
  211. // The three decorations instructions should still be there. The first two
  212. // should be the same, but the third should have %5 removed.
  213. // Check the OpDecorate instruction
  214. auto inst = context->annotation_begin();
  215. EXPECT_EQ(inst->opcode(), SpvOpDecorate);
  216. EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
  217. // Check the OpDecorationGroup Instruction
  218. ++inst;
  219. EXPECT_EQ(inst->opcode(), SpvOpDecorationGroup);
  220. EXPECT_EQ(inst->result_id(), 3);
  221. // Check that %5 is no longer part of the group.
  222. ++inst;
  223. EXPECT_EQ(inst->opcode(), SpvOpGroupDecorate);
  224. EXPECT_EQ(inst->NumInOperands(), 2);
  225. EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
  226. EXPECT_EQ(inst->GetSingleWordInOperand(1), 4);
  227. // Check that we are at the end.
  228. ++inst;
  229. EXPECT_EQ(inst, context->annotation_end());
  230. }
  231. TEST_F(IRContextTest, TakeNextUniqueIdIncrementing) {
  232. const uint32_t NUM_TESTS = 1000;
  233. IRContext localContext(SPV_ENV_UNIVERSAL_1_2, nullptr);
  234. for (uint32_t i = 1; i < NUM_TESTS; ++i)
  235. EXPECT_EQ(i, localContext.TakeNextUniqueId());
  236. }
  237. TEST_F(IRContextTest, KillGroupDecorationWitNoDecorations) {
  238. const std::string text = R"(
  239. OpCapability Shader
  240. %1 = OpExtInstImport "GLSL.std.450"
  241. OpMemoryModel Logical GLSL450
  242. OpEntryPoint Fragment %2 "main"
  243. OpExecutionMode %2 OriginUpperLeft
  244. OpSource GLSL 430
  245. %3 = OpDecorationGroup
  246. OpGroupDecorate %3 %4 %5
  247. %6 = OpTypeFloat 32
  248. %7 = OpTypePointer Function %6
  249. %8 = OpTypeStruct %6
  250. %9 = OpTypeVoid
  251. %10 = OpTypeFunction %9
  252. %2 = OpFunction %9 None %10
  253. %11 = OpLabel
  254. %4 = OpVariable %7 Function
  255. %5 = OpVariable %7 Function
  256. OpReturn
  257. OpFunctionEnd
  258. )";
  259. std::unique_ptr<IRContext> context =
  260. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  261. // Build the decoration manager.
  262. context->get_decoration_mgr();
  263. // Delete the second variable.
  264. context->KillDef(5);
  265. // The two decoration instructions should still be there. The first one
  266. // should be the same, but the second should have %5 removed.
  267. // Check the OpDecorationGroup Instruction
  268. auto inst = context->annotation_begin();
  269. EXPECT_EQ(inst->opcode(), SpvOpDecorationGroup);
  270. EXPECT_EQ(inst->result_id(), 3);
  271. // Check that %5 is no longer part of the group.
  272. ++inst;
  273. EXPECT_EQ(inst->opcode(), SpvOpGroupDecorate);
  274. EXPECT_EQ(inst->NumInOperands(), 2);
  275. EXPECT_EQ(inst->GetSingleWordInOperand(0), 3);
  276. EXPECT_EQ(inst->GetSingleWordInOperand(1), 4);
  277. // Check that we are at the end.
  278. ++inst;
  279. EXPECT_EQ(inst, context->annotation_end());
  280. }
  281. TEST_F(IRContextTest, KillDecorationGroup) {
  282. const std::string text = R"(
  283. OpCapability Shader
  284. %1 = OpExtInstImport "GLSL.std.450"
  285. OpMemoryModel Logical GLSL450
  286. OpEntryPoint Fragment %2 "main"
  287. OpExecutionMode %2 OriginUpperLeft
  288. OpSource GLSL 430
  289. %3 = OpDecorationGroup
  290. OpGroupDecorate %3 %4 %5
  291. %6 = OpTypeFloat 32
  292. %7 = OpTypePointer Function %6
  293. %8 = OpTypeStruct %6
  294. %9 = OpTypeVoid
  295. %10 = OpTypeFunction %9
  296. %2 = OpFunction %9 None %10
  297. %11 = OpLabel
  298. %4 = OpVariable %7 Function
  299. %5 = OpVariable %7 Function
  300. OpReturn
  301. OpFunctionEnd
  302. )";
  303. std::unique_ptr<IRContext> context =
  304. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  305. // Build the decoration manager.
  306. context->get_decoration_mgr();
  307. // Delete the second variable.
  308. context->KillDef(3);
  309. // Check the OpDecorationGroup Instruction is still there.
  310. EXPECT_TRUE(context->annotations().empty());
  311. }
  312. TEST_F(IRContextTest, BasicVisitFromEntryPoint) {
  313. // Make sure we visit the entry point, and the function it calls.
  314. // Do not visit Dead or Exported.
  315. const std::string text = R"(
  316. OpCapability Shader
  317. OpMemoryModel Logical GLSL450
  318. OpEntryPoint Fragment %10 "main"
  319. OpName %10 "main"
  320. OpName %Dead "Dead"
  321. OpName %11 "Constant"
  322. OpName %ExportedFunc "ExportedFunc"
  323. OpDecorate %ExportedFunc LinkageAttributes "ExportedFunc" Export
  324. %void = OpTypeVoid
  325. %6 = OpTypeFunction %void
  326. %10 = OpFunction %void None %6
  327. %14 = OpLabel
  328. %15 = OpFunctionCall %void %11
  329. %16 = OpFunctionCall %void %11
  330. OpReturn
  331. OpFunctionEnd
  332. %11 = OpFunction %void None %6
  333. %18 = OpLabel
  334. OpReturn
  335. OpFunctionEnd
  336. %Dead = OpFunction %void None %6
  337. %19 = OpLabel
  338. OpReturn
  339. OpFunctionEnd
  340. %ExportedFunc = OpFunction %void None %7
  341. %20 = OpLabel
  342. %21 = OpFunctionCall %void %11
  343. OpReturn
  344. OpFunctionEnd
  345. )";
  346. // clang-format on
  347. std::unique_ptr<IRContext> localContext =
  348. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  349. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  350. EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
  351. << text << std::endl;
  352. std::vector<uint32_t> processed;
  353. Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
  354. processed.push_back(fp->result_id());
  355. return false;
  356. };
  357. localContext->ProcessEntryPointCallTree(mark_visited);
  358. EXPECT_THAT(processed, UnorderedElementsAre(10, 11));
  359. }
  360. TEST_F(IRContextTest, BasicVisitReachable) {
  361. // Make sure we visit the entry point, exported function, and the function
  362. // they call. Do not visit Dead.
  363. const std::string text = R"(
  364. OpCapability Shader
  365. OpMemoryModel Logical GLSL450
  366. OpEntryPoint Fragment %10 "main"
  367. OpName %10 "main"
  368. OpName %Dead "Dead"
  369. OpName %11 "Constant"
  370. OpName %12 "ExportedFunc"
  371. OpName %13 "Constant2"
  372. OpDecorate %12 LinkageAttributes "ExportedFunc" Export
  373. %void = OpTypeVoid
  374. %6 = OpTypeFunction %void
  375. %10 = OpFunction %void None %6
  376. %14 = OpLabel
  377. %15 = OpFunctionCall %void %11
  378. %16 = OpFunctionCall %void %11
  379. OpReturn
  380. OpFunctionEnd
  381. %11 = OpFunction %void None %6
  382. %18 = OpLabel
  383. OpReturn
  384. OpFunctionEnd
  385. %Dead = OpFunction %void None %6
  386. %19 = OpLabel
  387. OpReturn
  388. OpFunctionEnd
  389. %12 = OpFunction %void None %6
  390. %20 = OpLabel
  391. %21 = OpFunctionCall %void %13
  392. OpReturn
  393. OpFunctionEnd
  394. %13 = OpFunction %void None %6
  395. %22 = OpLabel
  396. OpReturn
  397. OpFunctionEnd
  398. )";
  399. // clang-format on
  400. std::unique_ptr<IRContext> localContext =
  401. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  402. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  403. EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
  404. << text << std::endl;
  405. std::vector<uint32_t> processed;
  406. Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
  407. processed.push_back(fp->result_id());
  408. return false;
  409. };
  410. localContext->ProcessReachableCallTree(mark_visited);
  411. EXPECT_THAT(processed, UnorderedElementsAre(10, 11, 12, 13));
  412. }
  413. TEST_F(IRContextTest, BasicVisitOnlyOnce) {
  414. // Make sure we visit %12 only once, even if it is called from two different
  415. // functions.
  416. const std::string text = R"(
  417. OpCapability Shader
  418. OpMemoryModel Logical GLSL450
  419. OpEntryPoint Fragment %10 "main"
  420. OpName %10 "main"
  421. OpName %Dead "Dead"
  422. OpName %11 "Constant"
  423. OpName %12 "ExportedFunc"
  424. OpDecorate %12 LinkageAttributes "ExportedFunc" Export
  425. %void = OpTypeVoid
  426. %6 = OpTypeFunction %void
  427. %10 = OpFunction %void None %6
  428. %14 = OpLabel
  429. %15 = OpFunctionCall %void %11
  430. %16 = OpFunctionCall %void %12
  431. OpReturn
  432. OpFunctionEnd
  433. %11 = OpFunction %void None %6
  434. %18 = OpLabel
  435. %19 = OpFunctionCall %void %12
  436. OpReturn
  437. OpFunctionEnd
  438. %Dead = OpFunction %void None %6
  439. %20 = OpLabel
  440. OpReturn
  441. OpFunctionEnd
  442. %12 = OpFunction %void None %6
  443. %21 = OpLabel
  444. OpReturn
  445. OpFunctionEnd
  446. )";
  447. // clang-format on
  448. std::unique_ptr<IRContext> localContext =
  449. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  450. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  451. EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
  452. << text << std::endl;
  453. std::vector<uint32_t> processed;
  454. Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
  455. processed.push_back(fp->result_id());
  456. return false;
  457. };
  458. localContext->ProcessReachableCallTree(mark_visited);
  459. EXPECT_THAT(processed, UnorderedElementsAre(10, 11, 12));
  460. }
  461. TEST_F(IRContextTest, BasicDontVisitExportedVariable) {
  462. // Make sure we only visit functions and not exported variables.
  463. const std::string text = R"(
  464. OpCapability Shader
  465. OpMemoryModel Logical GLSL450
  466. OpEntryPoint Fragment %10 "main"
  467. OpExecutionMode %10 OriginUpperLeft
  468. OpSource GLSL 150
  469. OpName %10 "main"
  470. OpName %12 "export_var"
  471. OpDecorate %12 LinkageAttributes "export_var" Export
  472. %void = OpTypeVoid
  473. %6 = OpTypeFunction %void
  474. %float = OpTypeFloat 32
  475. %float_1 = OpConstant %float 1
  476. %12 = OpVariable %float Output
  477. %10 = OpFunction %void None %6
  478. %14 = OpLabel
  479. OpStore %12 %float_1
  480. OpReturn
  481. OpFunctionEnd
  482. )";
  483. // clang-format on
  484. std::unique_ptr<IRContext> localContext =
  485. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  486. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  487. EXPECT_NE(nullptr, localContext) << "Assembling failed for shader:\n"
  488. << text << std::endl;
  489. std::vector<uint32_t> processed;
  490. Pass::ProcessFunction mark_visited = [&processed](Function* fp) {
  491. processed.push_back(fp->result_id());
  492. return false;
  493. };
  494. localContext->ProcessReachableCallTree(mark_visited);
  495. EXPECT_THAT(processed, UnorderedElementsAre(10));
  496. }
  497. TEST_F(IRContextTest, IdBoundTestAtLimit) {
  498. const std::string text = R"(
  499. OpCapability Shader
  500. OpCapability Linkage
  501. OpMemoryModel Logical GLSL450
  502. %1 = OpTypeVoid
  503. %2 = OpTypeFunction %1
  504. %3 = OpFunction %1 None %2
  505. %4 = OpLabel
  506. OpReturn
  507. OpFunctionEnd)";
  508. std::unique_ptr<IRContext> context =
  509. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  510. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  511. uint32_t current_bound = context->module()->id_bound();
  512. context->set_max_id_bound(current_bound);
  513. uint32_t next_id_bound = context->TakeNextId();
  514. EXPECT_EQ(next_id_bound, 0);
  515. EXPECT_EQ(current_bound, context->module()->id_bound());
  516. next_id_bound = context->TakeNextId();
  517. EXPECT_EQ(next_id_bound, 0);
  518. }
  519. TEST_F(IRContextTest, IdBoundTestBelowLimit) {
  520. const std::string text = R"(
  521. OpCapability Shader
  522. OpCapability Linkage
  523. OpMemoryModel Logical GLSL450
  524. %1 = OpTypeVoid
  525. %2 = OpTypeFunction %1
  526. %3 = OpFunction %1 None %2
  527. %4 = OpLabel
  528. OpReturn
  529. OpFunctionEnd)";
  530. std::unique_ptr<IRContext> context =
  531. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  532. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  533. uint32_t current_bound = context->module()->id_bound();
  534. context->set_max_id_bound(current_bound + 100);
  535. uint32_t next_id_bound = context->TakeNextId();
  536. EXPECT_EQ(next_id_bound, current_bound);
  537. EXPECT_EQ(current_bound + 1, context->module()->id_bound());
  538. next_id_bound = context->TakeNextId();
  539. EXPECT_EQ(next_id_bound, current_bound + 1);
  540. }
  541. TEST_F(IRContextTest, IdBoundTestNearLimit) {
  542. const std::string text = R"(
  543. OpCapability Shader
  544. OpCapability Linkage
  545. OpMemoryModel Logical GLSL450
  546. %1 = OpTypeVoid
  547. %2 = OpTypeFunction %1
  548. %3 = OpFunction %1 None %2
  549. %4 = OpLabel
  550. OpReturn
  551. OpFunctionEnd)";
  552. std::unique_ptr<IRContext> context =
  553. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  554. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  555. uint32_t current_bound = context->module()->id_bound();
  556. context->set_max_id_bound(current_bound + 1);
  557. uint32_t next_id_bound = context->TakeNextId();
  558. EXPECT_EQ(next_id_bound, current_bound);
  559. EXPECT_EQ(current_bound + 1, context->module()->id_bound());
  560. next_id_bound = context->TakeNextId();
  561. EXPECT_EQ(next_id_bound, 0);
  562. }
  563. TEST_F(IRContextTest, IdBoundTestUIntMax) {
  564. const std::string text = R"(
  565. OpCapability Shader
  566. OpCapability Linkage
  567. OpMemoryModel Logical GLSL450
  568. %1 = OpTypeVoid
  569. %2 = OpTypeFunction %1
  570. %3 = OpFunction %1 None %2
  571. %4294967294 = OpLabel ; ID is UINT_MAX-1
  572. OpReturn
  573. OpFunctionEnd)";
  574. std::unique_ptr<IRContext> context =
  575. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  576. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  577. uint32_t current_bound = context->module()->id_bound();
  578. // Expecting |BuildModule| to preserve the numeric ids.
  579. EXPECT_EQ(current_bound, std::numeric_limits<uint32_t>::max());
  580. context->set_max_id_bound(current_bound);
  581. uint32_t next_id_bound = context->TakeNextId();
  582. EXPECT_EQ(next_id_bound, 0);
  583. EXPECT_EQ(current_bound, context->module()->id_bound());
  584. }
  585. } // namespace
  586. } // namespace opt
  587. } // namespace spvtools