validation_during_reduction_test.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // Copyright (c) 2018 Google LLC
  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 "source/reduce/reducer.h"
  15. #include "source/reduce/reduction_opportunity.h"
  16. #include "source/reduce/remove_instruction_reduction_opportunity.h"
  17. #include "test/reduce/reduce_test_util.h"
  18. namespace spvtools {
  19. namespace reduce {
  20. namespace {
  21. using opt::Function;
  22. using opt::IRContext;
  23. using opt::Instruction;
  24. // A reduction opportunity finder that finds opportunities to remove global
  25. // values regardless of whether they are referenced. This is very likely to make
  26. // the resulting module invalid. We use this to test the reducer's behavior in
  27. // the scenario where a bad reduction pass leads to an invalid module.
  28. class BlindlyRemoveGlobalValuesReductionOpportunityFinder
  29. : public ReductionOpportunityFinder {
  30. public:
  31. BlindlyRemoveGlobalValuesReductionOpportunityFinder() = default;
  32. ~BlindlyRemoveGlobalValuesReductionOpportunityFinder() override = default;
  33. // The name of this pass.
  34. std::string GetName() const final { return "BlindlyRemoveGlobalValuesPass"; }
  35. // Finds opportunities to remove all global values. Assuming they are all
  36. // referenced (directly or indirectly) from elsewhere in the module, each such
  37. // opportunity will make the module invalid.
  38. std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities(
  39. IRContext* context, uint32_t /*unused*/) const final {
  40. std::vector<std::unique_ptr<ReductionOpportunity>> result;
  41. for (auto& inst : context->module()->types_values()) {
  42. if (inst.HasResultId()) {
  43. result.push_back(
  44. MakeUnique<RemoveInstructionReductionOpportunity>(&inst));
  45. }
  46. }
  47. return result;
  48. }
  49. };
  50. // A reduction opportunity that exists at the start of every function whose
  51. // first instruction is an OpVariable instruction. When applied, the OpVariable
  52. // instruction is duplicated (with a fresh result id). This allows each
  53. // reduction step to increase the number of variables to check if the validator
  54. // limits are enforced.
  55. class OpVariableDuplicatorReductionOpportunity : public ReductionOpportunity {
  56. public:
  57. OpVariableDuplicatorReductionOpportunity(Function* function)
  58. : function_(function) {}
  59. bool PreconditionHolds() override {
  60. Instruction* first_instruction = &*function_->begin()[0].begin();
  61. return first_instruction->opcode() == spv::Op::OpVariable;
  62. }
  63. protected:
  64. void Apply() override {
  65. // Duplicate the first OpVariable instruction.
  66. Instruction* first_instruction = &*function_->begin()[0].begin();
  67. assert(first_instruction->opcode() == spv::Op::OpVariable &&
  68. "Expected first instruction to be OpVariable");
  69. IRContext* context = first_instruction->context();
  70. Instruction* cloned_instruction = first_instruction->Clone(context);
  71. cloned_instruction->SetResultId(context->TakeNextId());
  72. cloned_instruction->InsertBefore(first_instruction);
  73. }
  74. private:
  75. Function* function_;
  76. };
  77. // A reduction opportunity finder that finds
  78. // OpVariableDuplicatorReductionOpportunity.
  79. class OpVariableDuplicatorReductionOpportunityFinder
  80. : public ReductionOpportunityFinder {
  81. public:
  82. OpVariableDuplicatorReductionOpportunityFinder() = default;
  83. ~OpVariableDuplicatorReductionOpportunityFinder() override = default;
  84. std::string GetName() const final {
  85. return "LocalVariableAdderReductionOpportunityFinder";
  86. }
  87. std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities(
  88. IRContext* context, uint32_t /*unused*/) const final {
  89. std::vector<std::unique_ptr<ReductionOpportunity>> result;
  90. for (auto& function : *context->module()) {
  91. Instruction* first_instruction = &*function.begin()[0].begin();
  92. if (first_instruction->opcode() == spv::Op::OpVariable) {
  93. result.push_back(
  94. MakeUnique<OpVariableDuplicatorReductionOpportunity>(&function));
  95. }
  96. }
  97. return result;
  98. }
  99. };
  100. TEST(ValidationDuringReductionTest, CheckInvalidPassMakesNoProgress) {
  101. // A module whose global values are all referenced, so that any application of
  102. // MakeModuleInvalidPass will make the module invalid. Check that the reducer
  103. // makes no progress, as every step will be invalid and treated as
  104. // uninteresting.
  105. std::string original = R"(
  106. OpCapability Shader
  107. %1 = OpExtInstImport "GLSL.std.450"
  108. OpMemoryModel Logical GLSL450
  109. OpEntryPoint Fragment %4 "main" %60
  110. OpExecutionMode %4 OriginUpperLeft
  111. OpSource ESSL 310
  112. OpName %4 "main"
  113. OpName %16 "buf2"
  114. OpMemberName %16 0 "i"
  115. OpName %18 ""
  116. OpName %25 "buf1"
  117. OpMemberName %25 0 "f"
  118. OpName %27 ""
  119. OpName %60 "_GLF_color"
  120. OpMemberDecorate %16 0 Offset 0
  121. OpDecorate %16 Block
  122. OpDecorate %18 DescriptorSet 0
  123. OpDecorate %18 Binding 2
  124. OpMemberDecorate %25 0 Offset 0
  125. OpDecorate %25 Block
  126. OpDecorate %27 DescriptorSet 0
  127. OpDecorate %27 Binding 1
  128. OpDecorate %60 Location 0
  129. %2 = OpTypeVoid
  130. %3 = OpTypeFunction %2
  131. %6 = OpTypeInt 32 1
  132. %9 = OpConstant %6 0
  133. %16 = OpTypeStruct %6
  134. %17 = OpTypePointer Uniform %16
  135. %18 = OpVariable %17 Uniform
  136. %19 = OpTypePointer Uniform %6
  137. %22 = OpTypeBool
  138. %24 = OpTypeFloat 32
  139. %25 = OpTypeStruct %24
  140. %26 = OpTypePointer Uniform %25
  141. %27 = OpVariable %26 Uniform
  142. %28 = OpTypePointer Uniform %24
  143. %31 = OpConstant %24 2
  144. %56 = OpConstant %6 1
  145. %58 = OpTypeVector %24 4
  146. %59 = OpTypePointer Output %58
  147. %60 = OpVariable %59 Output
  148. %72 = OpUndef %24
  149. %74 = OpUndef %6
  150. %4 = OpFunction %2 None %3
  151. %5 = OpLabel
  152. OpBranch %10
  153. %10 = OpLabel
  154. %73 = OpPhi %6 %74 %5 %77 %34
  155. %71 = OpPhi %24 %72 %5 %76 %34
  156. %70 = OpPhi %6 %9 %5 %57 %34
  157. %20 = OpAccessChain %19 %18 %9
  158. %21 = OpLoad %6 %20
  159. %23 = OpSLessThan %22 %70 %21
  160. OpLoopMerge %12 %34 None
  161. OpBranchConditional %23 %11 %12
  162. %11 = OpLabel
  163. %29 = OpAccessChain %28 %27 %9
  164. %30 = OpLoad %24 %29
  165. %32 = OpFOrdGreaterThan %22 %30 %31
  166. OpSelectionMerge %90 None
  167. OpBranchConditional %32 %33 %46
  168. %33 = OpLabel
  169. %40 = OpFAdd %24 %71 %30
  170. %45 = OpISub %6 %73 %21
  171. OpBranch %90
  172. %46 = OpLabel
  173. %50 = OpFMul %24 %71 %30
  174. %54 = OpSDiv %6 %73 %21
  175. OpBranch %90
  176. %90 = OpLabel
  177. %77 = OpPhi %6 %45 %33 %54 %46
  178. %76 = OpPhi %24 %40 %33 %50 %46
  179. OpBranch %34
  180. %34 = OpLabel
  181. %57 = OpIAdd %6 %70 %56
  182. OpBranch %10
  183. %12 = OpLabel
  184. %61 = OpAccessChain %28 %27 %9
  185. %62 = OpLoad %24 %61
  186. %66 = OpConvertSToF %24 %21
  187. %68 = OpConvertSToF %24 %73
  188. %69 = OpCompositeConstruct %58 %62 %71 %66 %68
  189. OpStore %60 %69
  190. OpReturn
  191. OpFunctionEnd
  192. )";
  193. spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
  194. Reducer reducer(env);
  195. reducer.SetMessageConsumer(NopDiagnostic);
  196. // Say that every module is interesting.
  197. reducer.SetInterestingnessFunction(
  198. [](const std::vector<uint32_t>&, uint32_t) -> bool { return true; });
  199. reducer.AddReductionPass(
  200. MakeUnique<BlindlyRemoveGlobalValuesReductionOpportunityFinder>());
  201. std::vector<uint32_t> binary_in;
  202. SpirvTools t(env);
  203. ASSERT_TRUE(t.Assemble(original, &binary_in, kReduceAssembleOption));
  204. std::vector<uint32_t> binary_out;
  205. spvtools::ReducerOptions reducer_options;
  206. reducer_options.set_step_limit(500);
  207. // Don't fail on a validation error; just treat it as uninteresting.
  208. reducer_options.set_fail_on_validation_error(false);
  209. spvtools::ValidatorOptions validator_options;
  210. Reducer::ReductionResultStatus status = reducer.Run(
  211. std::move(binary_in), &binary_out, reducer_options, validator_options);
  212. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  213. // The reducer should have no impact.
  214. CheckEqual(env, original, binary_out);
  215. }
  216. TEST(ValidationDuringReductionTest, CheckNotAlwaysInvalidCanMakeProgress) {
  217. // A module with just one unreferenced global value. All but one application
  218. // of MakeModuleInvalidPass will make the module invalid.
  219. std::string original = R"(
  220. OpCapability Shader
  221. %1 = OpExtInstImport "GLSL.std.450"
  222. OpMemoryModel Logical GLSL450
  223. OpEntryPoint Fragment %4 "main" %60
  224. OpExecutionMode %4 OriginUpperLeft
  225. OpSource ESSL 310
  226. OpName %4 "main"
  227. OpName %16 "buf2"
  228. OpMemberName %16 0 "i"
  229. OpName %18 ""
  230. OpName %25 "buf1"
  231. OpMemberName %25 0 "f"
  232. OpName %27 ""
  233. OpName %60 "_GLF_color"
  234. OpMemberDecorate %16 0 Offset 0
  235. OpDecorate %16 Block
  236. OpDecorate %18 DescriptorSet 0
  237. OpDecorate %18 Binding 2
  238. OpMemberDecorate %25 0 Offset 0
  239. OpDecorate %25 Block
  240. OpDecorate %27 DescriptorSet 0
  241. OpDecorate %27 Binding 1
  242. OpDecorate %60 Location 0
  243. %2 = OpTypeVoid
  244. %3 = OpTypeFunction %2
  245. %6 = OpTypeInt 32 1
  246. %9 = OpConstant %6 0
  247. %16 = OpTypeStruct %6
  248. %17 = OpTypePointer Uniform %16
  249. %18 = OpVariable %17 Uniform
  250. %19 = OpTypePointer Uniform %6
  251. %22 = OpTypeBool
  252. %24 = OpTypeFloat 32
  253. %25 = OpTypeStruct %24
  254. %26 = OpTypePointer Uniform %25
  255. %27 = OpVariable %26 Uniform
  256. %28 = OpTypePointer Uniform %24
  257. %31 = OpConstant %24 2
  258. %56 = OpConstant %6 1
  259. %1000 = OpConstant %6 1000 ; It should be possible to remove this instruction without making the module invalid.
  260. %58 = OpTypeVector %24 4
  261. %59 = OpTypePointer Output %58
  262. %60 = OpVariable %59 Output
  263. %72 = OpUndef %24
  264. %74 = OpUndef %6
  265. %4 = OpFunction %2 None %3
  266. %5 = OpLabel
  267. OpBranch %10
  268. %10 = OpLabel
  269. %73 = OpPhi %6 %74 %5 %77 %34
  270. %71 = OpPhi %24 %72 %5 %76 %34
  271. %70 = OpPhi %6 %9 %5 %57 %34
  272. %20 = OpAccessChain %19 %18 %9
  273. %21 = OpLoad %6 %20
  274. %23 = OpSLessThan %22 %70 %21
  275. OpLoopMerge %12 %34 None
  276. OpBranchConditional %23 %11 %12
  277. %11 = OpLabel
  278. %29 = OpAccessChain %28 %27 %9
  279. %30 = OpLoad %24 %29
  280. %32 = OpFOrdGreaterThan %22 %30 %31
  281. OpSelectionMerge %90 None
  282. OpBranchConditional %32 %33 %46
  283. %33 = OpLabel
  284. %40 = OpFAdd %24 %71 %30
  285. %45 = OpISub %6 %73 %21
  286. OpBranch %90
  287. %46 = OpLabel
  288. %50 = OpFMul %24 %71 %30
  289. %54 = OpSDiv %6 %73 %21
  290. OpBranch %90
  291. %90 = OpLabel
  292. %77 = OpPhi %6 %45 %33 %54 %46
  293. %76 = OpPhi %24 %40 %33 %50 %46
  294. OpBranch %34
  295. %34 = OpLabel
  296. %57 = OpIAdd %6 %70 %56
  297. OpBranch %10
  298. %12 = OpLabel
  299. %61 = OpAccessChain %28 %27 %9
  300. %62 = OpLoad %24 %61
  301. %66 = OpConvertSToF %24 %21
  302. %68 = OpConvertSToF %24 %73
  303. %69 = OpCompositeConstruct %58 %62 %71 %66 %68
  304. OpStore %60 %69
  305. OpReturn
  306. OpFunctionEnd
  307. )";
  308. // This is the same as the original, except that the constant declaration of
  309. // 1000 is gone.
  310. std::string expected = R"(
  311. OpCapability Shader
  312. %1 = OpExtInstImport "GLSL.std.450"
  313. OpMemoryModel Logical GLSL450
  314. OpEntryPoint Fragment %4 "main" %60
  315. OpExecutionMode %4 OriginUpperLeft
  316. OpSource ESSL 310
  317. OpName %4 "main"
  318. OpName %16 "buf2"
  319. OpMemberName %16 0 "i"
  320. OpName %18 ""
  321. OpName %25 "buf1"
  322. OpMemberName %25 0 "f"
  323. OpName %27 ""
  324. OpName %60 "_GLF_color"
  325. OpMemberDecorate %16 0 Offset 0
  326. OpDecorate %16 Block
  327. OpDecorate %18 DescriptorSet 0
  328. OpDecorate %18 Binding 2
  329. OpMemberDecorate %25 0 Offset 0
  330. OpDecorate %25 Block
  331. OpDecorate %27 DescriptorSet 0
  332. OpDecorate %27 Binding 1
  333. OpDecorate %60 Location 0
  334. %2 = OpTypeVoid
  335. %3 = OpTypeFunction %2
  336. %6 = OpTypeInt 32 1
  337. %9 = OpConstant %6 0
  338. %16 = OpTypeStruct %6
  339. %17 = OpTypePointer Uniform %16
  340. %18 = OpVariable %17 Uniform
  341. %19 = OpTypePointer Uniform %6
  342. %22 = OpTypeBool
  343. %24 = OpTypeFloat 32
  344. %25 = OpTypeStruct %24
  345. %26 = OpTypePointer Uniform %25
  346. %27 = OpVariable %26 Uniform
  347. %28 = OpTypePointer Uniform %24
  348. %31 = OpConstant %24 2
  349. %56 = OpConstant %6 1
  350. %58 = OpTypeVector %24 4
  351. %59 = OpTypePointer Output %58
  352. %60 = OpVariable %59 Output
  353. %72 = OpUndef %24
  354. %74 = OpUndef %6
  355. %4 = OpFunction %2 None %3
  356. %5 = OpLabel
  357. OpBranch %10
  358. %10 = OpLabel
  359. %73 = OpPhi %6 %74 %5 %77 %34
  360. %71 = OpPhi %24 %72 %5 %76 %34
  361. %70 = OpPhi %6 %9 %5 %57 %34
  362. %20 = OpAccessChain %19 %18 %9
  363. %21 = OpLoad %6 %20
  364. %23 = OpSLessThan %22 %70 %21
  365. OpLoopMerge %12 %34 None
  366. OpBranchConditional %23 %11 %12
  367. %11 = OpLabel
  368. %29 = OpAccessChain %28 %27 %9
  369. %30 = OpLoad %24 %29
  370. %32 = OpFOrdGreaterThan %22 %30 %31
  371. OpSelectionMerge %90 None
  372. OpBranchConditional %32 %33 %46
  373. %33 = OpLabel
  374. %40 = OpFAdd %24 %71 %30
  375. %45 = OpISub %6 %73 %21
  376. OpBranch %90
  377. %46 = OpLabel
  378. %50 = OpFMul %24 %71 %30
  379. %54 = OpSDiv %6 %73 %21
  380. OpBranch %90
  381. %90 = OpLabel
  382. %77 = OpPhi %6 %45 %33 %54 %46
  383. %76 = OpPhi %24 %40 %33 %50 %46
  384. OpBranch %34
  385. %34 = OpLabel
  386. %57 = OpIAdd %6 %70 %56
  387. OpBranch %10
  388. %12 = OpLabel
  389. %61 = OpAccessChain %28 %27 %9
  390. %62 = OpLoad %24 %61
  391. %66 = OpConvertSToF %24 %21
  392. %68 = OpConvertSToF %24 %73
  393. %69 = OpCompositeConstruct %58 %62 %71 %66 %68
  394. OpStore %60 %69
  395. OpReturn
  396. OpFunctionEnd
  397. )";
  398. spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
  399. Reducer reducer(env);
  400. reducer.SetMessageConsumer(NopDiagnostic);
  401. // Say that every module is interesting.
  402. reducer.SetInterestingnessFunction(
  403. [](const std::vector<uint32_t>&, uint32_t) -> bool { return true; });
  404. reducer.AddReductionPass(
  405. MakeUnique<BlindlyRemoveGlobalValuesReductionOpportunityFinder>());
  406. std::vector<uint32_t> binary_in;
  407. SpirvTools t(env);
  408. ASSERT_TRUE(t.Assemble(original, &binary_in, kReduceAssembleOption));
  409. std::vector<uint32_t> binary_out;
  410. spvtools::ReducerOptions reducer_options;
  411. reducer_options.set_step_limit(500);
  412. // Don't fail on a validation error; just treat it as uninteresting.
  413. reducer_options.set_fail_on_validation_error(false);
  414. spvtools::ValidatorOptions validator_options;
  415. Reducer::ReductionResultStatus status = reducer.Run(
  416. std::move(binary_in), &binary_out, reducer_options, validator_options);
  417. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  418. CheckEqual(env, expected, binary_out);
  419. }
  420. // Sets up a Reducer for use in the CheckValidationOptions test; avoids
  421. // repetition.
  422. void SetupReducerForCheckValidationOptions(Reducer* reducer) {
  423. reducer->SetMessageConsumer(NopDiagnostic);
  424. // Say that every module is interesting.
  425. reducer->SetInterestingnessFunction(
  426. [](const std::vector<uint32_t>&, uint32_t) -> bool { return true; });
  427. // Each "reduction" step will duplicate the first OpVariable instruction in
  428. // the function.
  429. reducer->AddReductionPass(
  430. MakeUnique<OpVariableDuplicatorReductionOpportunityFinder>());
  431. }
  432. TEST(ValidationDuringReductionTest, CheckValidationOptions) {
  433. // A module that only validates when the "skip-block-layout" validator option
  434. // is used. Also, the entry point's first instruction creates a local
  435. // variable; this instruction will be duplicated on each reduction step.
  436. std::string original = R"(
  437. OpCapability Shader
  438. %1 = OpExtInstImport "GLSL.std.450"
  439. OpMemoryModel Logical GLSL450
  440. OpEntryPoint Vertex %2 "Main" %3
  441. OpSource HLSL 600
  442. OpDecorate %3 BuiltIn Position
  443. OpDecorate %4 DescriptorSet 0
  444. OpDecorate %4 Binding 99
  445. OpDecorate %5 ArrayStride 16
  446. OpMemberDecorate %6 0 Offset 0
  447. OpMemberDecorate %6 1 Offset 32
  448. OpMemberDecorate %6 1 MatrixStride 16
  449. OpMemberDecorate %6 1 ColMajor
  450. OpMemberDecorate %6 2 Offset 96
  451. OpMemberDecorate %6 3 Offset 100
  452. OpMemberDecorate %6 4 Offset 112
  453. OpMemberDecorate %6 4 MatrixStride 16
  454. OpMemberDecorate %6 4 ColMajor
  455. OpMemberDecorate %6 5 Offset 176
  456. OpDecorate %6 Block
  457. %7 = OpTypeFloat 32
  458. %8 = OpTypeVector %7 4
  459. %9 = OpTypeMatrix %8 4
  460. %10 = OpTypeVector %7 2
  461. %11 = OpTypeInt 32 1
  462. %12 = OpTypeInt 32 0
  463. %13 = OpConstant %12 2
  464. %14 = OpConstant %11 1
  465. %15 = OpConstant %11 5
  466. %5 = OpTypeArray %8 %13
  467. %6 = OpTypeStruct %5 %9 %12 %10 %9 %7
  468. %16 = OpTypePointer Uniform %6
  469. %17 = OpTypePointer Output %8
  470. %18 = OpTypeVoid
  471. %19 = OpTypeFunction %18
  472. %20 = OpTypePointer Uniform %7
  473. %4 = OpVariable %16 Uniform
  474. %3 = OpVariable %17 Output
  475. %21 = OpTypePointer Function %11
  476. %2 = OpFunction %18 None %19
  477. %22 = OpLabel
  478. %23 = OpVariable %21 Function
  479. %24 = OpAccessChain %20 %4 %15
  480. %25 = OpLoad %7 %24
  481. %26 = OpCompositeConstruct %8 %25 %25 %25 %25
  482. OpStore %3 %26
  483. OpReturn
  484. OpFunctionEnd
  485. )";
  486. spv_target_env env = SPV_ENV_VULKAN_1_0;
  487. std::vector<uint32_t> binary_in;
  488. SpirvTools t(env);
  489. ASSERT_TRUE(t.Assemble(original, &binary_in, kReduceAssembleOption));
  490. std::vector<uint32_t> binary_out;
  491. spvtools::ReducerOptions reducer_options;
  492. spvtools::ValidatorOptions validator_options;
  493. reducer_options.set_step_limit(3);
  494. reducer_options.set_fail_on_validation_error(true);
  495. // Reduction should fail because the initial state is invalid without the
  496. // "skip-block-layout" validator option. Note that the interestingness test
  497. // always returns true.
  498. {
  499. Reducer reducer(env);
  500. SetupReducerForCheckValidationOptions(&reducer);
  501. Reducer::ReductionResultStatus status =
  502. reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,
  503. reducer_options, validator_options);
  504. ASSERT_EQ(status, Reducer::ReductionResultStatus::kInitialStateInvalid);
  505. }
  506. // Try again with validator option.
  507. validator_options.SetSkipBlockLayout(true);
  508. // Reduction should hit step limit; module is seen as valid, interestingness
  509. // test always succeeds, and the finder yields infinite opportunities.
  510. {
  511. Reducer reducer(env);
  512. SetupReducerForCheckValidationOptions(&reducer);
  513. Reducer::ReductionResultStatus status =
  514. reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,
  515. reducer_options, validator_options);
  516. ASSERT_EQ(status, Reducer::ReductionResultStatus::kReachedStepLimit);
  517. }
  518. // Now set a limit on the number of local variables.
  519. validator_options.SetUniversalLimit(spv_validator_limit_max_local_variables,
  520. 2);
  521. // Reduction should now fail due to reaching an invalid state; after one step,
  522. // a local variable is added and the module becomes "invalid" given the
  523. // validator limits.
  524. {
  525. Reducer reducer(env);
  526. SetupReducerForCheckValidationOptions(&reducer);
  527. Reducer::ReductionResultStatus status =
  528. reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,
  529. reducer_options, validator_options);
  530. ASSERT_EQ(status, Reducer::ReductionResultStatus::kStateInvalid);
  531. }
  532. }
  533. } // namespace
  534. } // namespace reduce
  535. } // namespace spvtools