reducer_test.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <unordered_map>
  16. #include "source/opt/build_module.h"
  17. #include "source/reduce/operand_to_const_reduction_opportunity_finder.h"
  18. #include "source/reduce/remove_unused_instruction_reduction_opportunity_finder.h"
  19. #include "test/reduce/reduce_test_util.h"
  20. namespace spvtools {
  21. namespace reduce {
  22. namespace {
  23. const spv_target_env kEnv = SPV_ENV_UNIVERSAL_1_3;
  24. const MessageConsumer kMessageConsumer = NopDiagnostic;
  25. // This changes its mind each time IsInteresting is invoked as to whether the
  26. // binary is interesting, until some limit is reached after which the binary is
  27. // always deemed interesting. This is useful to test that reduction passes
  28. // interleave in interesting ways for a while, and then always succeed after
  29. // some point; the latter is important to end up with a predictable final
  30. // reduced binary for tests.
  31. class PingPongInteresting {
  32. public:
  33. explicit PingPongInteresting(uint32_t always_interesting_after)
  34. : is_interesting_(true),
  35. always_interesting_after_(always_interesting_after),
  36. count_(0) {}
  37. bool IsInteresting() {
  38. bool result;
  39. if (count_ > always_interesting_after_) {
  40. result = true;
  41. } else {
  42. result = is_interesting_;
  43. is_interesting_ = !is_interesting_;
  44. }
  45. count_++;
  46. return result;
  47. }
  48. private:
  49. bool is_interesting_;
  50. const uint32_t always_interesting_after_;
  51. uint32_t count_;
  52. };
  53. TEST(ReducerTest, ExprToConstantAndRemoveUnreferenced) {
  54. // Check that ExprToConstant and RemoveUnreferenced work together; once some
  55. // ID uses have been changed to constants, those IDs can be removed.
  56. std::string original = R"(
  57. OpCapability Shader
  58. %1 = OpExtInstImport "GLSL.std.450"
  59. OpMemoryModel Logical GLSL450
  60. OpEntryPoint Fragment %4 "main" %60
  61. OpExecutionMode %4 OriginUpperLeft
  62. OpSource ESSL 310
  63. OpName %4 "main"
  64. OpName %16 "buf2"
  65. OpMemberName %16 0 "i"
  66. OpName %18 ""
  67. OpName %25 "buf1"
  68. OpMemberName %25 0 "f"
  69. OpName %27 ""
  70. OpName %60 "_GLF_color"
  71. OpMemberDecorate %16 0 Offset 0
  72. OpDecorate %16 Block
  73. OpDecorate %18 DescriptorSet 0
  74. OpDecorate %18 Binding 2
  75. OpMemberDecorate %25 0 Offset 0
  76. OpDecorate %25 Block
  77. OpDecorate %27 DescriptorSet 0
  78. OpDecorate %27 Binding 1
  79. OpDecorate %60 Location 0
  80. %2 = OpTypeVoid
  81. %3 = OpTypeFunction %2
  82. %6 = OpTypeInt 32 1
  83. %9 = OpConstant %6 0
  84. %16 = OpTypeStruct %6
  85. %17 = OpTypePointer Uniform %16
  86. %18 = OpVariable %17 Uniform
  87. %19 = OpTypePointer Uniform %6
  88. %22 = OpTypeBool
  89. %100 = OpConstantTrue %22
  90. %24 = OpTypeFloat 32
  91. %25 = OpTypeStruct %24
  92. %26 = OpTypePointer Uniform %25
  93. %27 = OpVariable %26 Uniform
  94. %28 = OpTypePointer Uniform %24
  95. %31 = OpConstant %24 2
  96. %56 = OpConstant %6 1
  97. %58 = OpTypeVector %24 4
  98. %59 = OpTypePointer Output %58
  99. %60 = OpVariable %59 Output
  100. %72 = OpUndef %24
  101. %74 = OpUndef %6
  102. %4 = OpFunction %2 None %3
  103. %5 = OpLabel
  104. OpBranch %10
  105. %10 = OpLabel
  106. %73 = OpPhi %6 %74 %5 %77 %34
  107. %71 = OpPhi %24 %72 %5 %76 %34
  108. %70 = OpPhi %6 %9 %5 %57 %34
  109. %20 = OpAccessChain %19 %18 %9
  110. %21 = OpLoad %6 %20
  111. %23 = OpSLessThan %22 %70 %21
  112. OpLoopMerge %12 %34 None
  113. OpBranchConditional %23 %11 %12
  114. %11 = OpLabel
  115. %29 = OpAccessChain %28 %27 %9
  116. %30 = OpLoad %24 %29
  117. %32 = OpFOrdGreaterThan %22 %30 %31
  118. OpSelectionMerge %90 None
  119. OpBranchConditional %32 %33 %46
  120. %33 = OpLabel
  121. %40 = OpFAdd %24 %71 %30
  122. %45 = OpISub %6 %73 %21
  123. OpBranch %90
  124. %46 = OpLabel
  125. %50 = OpFMul %24 %71 %30
  126. %54 = OpSDiv %6 %73 %21
  127. OpBranch %90
  128. %90 = OpLabel
  129. %77 = OpPhi %6 %45 %33 %54 %46
  130. %76 = OpPhi %24 %40 %33 %50 %46
  131. OpBranch %34
  132. %34 = OpLabel
  133. %57 = OpIAdd %6 %70 %56
  134. OpBranch %10
  135. %12 = OpLabel
  136. %61 = OpAccessChain %28 %27 %9
  137. %62 = OpLoad %24 %61
  138. %66 = OpConvertSToF %24 %21
  139. %68 = OpConvertSToF %24 %73
  140. %69 = OpCompositeConstruct %58 %62 %71 %66 %68
  141. OpStore %60 %69
  142. OpReturn
  143. OpFunctionEnd
  144. )";
  145. std::string expected = R"(
  146. OpCapability Shader
  147. %1 = OpExtInstImport "GLSL.std.450"
  148. OpMemoryModel Logical GLSL450
  149. OpEntryPoint Fragment %4 "main"
  150. OpExecutionMode %4 OriginUpperLeft
  151. %2 = OpTypeVoid
  152. %3 = OpTypeFunction %2
  153. %6 = OpTypeInt 32 1
  154. %9 = OpConstant %6 0
  155. %22 = OpTypeBool
  156. %100 = OpConstantTrue %22
  157. %24 = OpTypeFloat 32
  158. %31 = OpConstant %24 2
  159. %56 = OpConstant %6 1
  160. %72 = OpUndef %24
  161. %74 = OpUndef %6
  162. %4 = OpFunction %2 None %3
  163. %5 = OpLabel
  164. OpBranch %10
  165. %10 = OpLabel
  166. OpLoopMerge %12 %34 None
  167. OpBranchConditional %100 %11 %12
  168. %11 = OpLabel
  169. OpSelectionMerge %90 None
  170. OpBranchConditional %100 %33 %46
  171. %33 = OpLabel
  172. OpBranch %90
  173. %46 = OpLabel
  174. OpBranch %90
  175. %90 = OpLabel
  176. OpBranch %34
  177. %34 = OpLabel
  178. OpBranch %10
  179. %12 = OpLabel
  180. OpReturn
  181. OpFunctionEnd
  182. )";
  183. Reducer reducer(kEnv);
  184. PingPongInteresting ping_pong_interesting(10);
  185. reducer.SetMessageConsumer(kMessageConsumer);
  186. reducer.SetInterestingnessFunction(
  187. [&ping_pong_interesting](const std::vector<uint32_t>&, uint32_t) -> bool {
  188. return ping_pong_interesting.IsInteresting();
  189. });
  190. reducer.AddReductionPass(
  191. MakeUnique<RemoveUnusedInstructionReductionOpportunityFinder>(false));
  192. reducer.AddReductionPass(
  193. MakeUnique<OperandToConstReductionOpportunityFinder>());
  194. std::vector<uint32_t> binary_in;
  195. SpirvTools t(kEnv);
  196. ASSERT_TRUE(t.Assemble(original, &binary_in, kReduceAssembleOption));
  197. std::vector<uint32_t> binary_out;
  198. spvtools::ReducerOptions reducer_options;
  199. reducer_options.set_step_limit(500);
  200. reducer_options.set_fail_on_validation_error(true);
  201. spvtools::ValidatorOptions validator_options;
  202. Reducer::ReductionResultStatus status = reducer.Run(
  203. std::move(binary_in), &binary_out, reducer_options, validator_options);
  204. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  205. CheckEqual(kEnv, expected, binary_out);
  206. }
  207. bool InterestingWhileOpcodeExists(const std::vector<uint32_t>& binary,
  208. spv::Op opcode, uint32_t count, bool dump) {
  209. if (dump) {
  210. std::stringstream ss;
  211. ss << "temp_" << count << ".spv";
  212. DumpShader(binary, ss.str().c_str());
  213. }
  214. std::unique_ptr<opt::IRContext> context =
  215. BuildModule(kEnv, kMessageConsumer, binary.data(), binary.size());
  216. assert(context);
  217. bool interesting = false;
  218. for (auto& function : *context->module()) {
  219. context->cfg()->ForEachBlockInPostOrder(
  220. &*function.begin(),
  221. [opcode, &interesting](opt::BasicBlock* block) -> void {
  222. for (auto& inst : *block) {
  223. if (inst.opcode() == spv::Op(opcode)) {
  224. interesting = true;
  225. break;
  226. }
  227. }
  228. });
  229. if (interesting) {
  230. break;
  231. }
  232. }
  233. return interesting;
  234. }
  235. bool InterestingWhileIMulReachable(const std::vector<uint32_t>& binary,
  236. uint32_t count) {
  237. return InterestingWhileOpcodeExists(binary, spv::Op::OpIMul, count, false);
  238. }
  239. bool InterestingWhileSDivReachable(const std::vector<uint32_t>& binary,
  240. uint32_t count) {
  241. return InterestingWhileOpcodeExists(binary, spv::Op::OpSDiv, count, false);
  242. }
  243. // The shader below was derived from the following GLSL, and optimized.
  244. // #version 310 es
  245. // precision highp float;
  246. // layout(location = 0) out vec4 _GLF_color;
  247. // int foo() {
  248. // int x = 1;
  249. // int y;
  250. // x = y / x; // SDiv
  251. // return x;
  252. // }
  253. // void main() {
  254. // int c;
  255. // while (bool(c)) {
  256. // do {
  257. // if (bool(c)) {
  258. // if (bool(c)) {
  259. // ++c;
  260. // } else {
  261. // _GLF_color.x = float(c*c); // IMul
  262. // }
  263. // return;
  264. // }
  265. // } while(bool(foo()));
  266. // return;
  267. // }
  268. // }
  269. const std::string kShaderWithLoopsDivAndMul = R"(
  270. OpCapability Shader
  271. %1 = OpExtInstImport "GLSL.std.450"
  272. OpMemoryModel Logical GLSL450
  273. OpEntryPoint Fragment %4 "main" %49
  274. OpExecutionMode %4 OriginUpperLeft
  275. OpSource ESSL 310
  276. OpName %4 "main"
  277. OpName %49 "_GLF_color"
  278. OpDecorate %49 Location 0
  279. OpDecorate %52 RelaxedPrecision
  280. OpDecorate %77 RelaxedPrecision
  281. %2 = OpTypeVoid
  282. %3 = OpTypeFunction %2
  283. %6 = OpTypeInt 32 1
  284. %12 = OpConstant %6 1
  285. %27 = OpTypeBool
  286. %28 = OpTypeInt 32 0
  287. %29 = OpConstant %28 0
  288. %46 = OpTypeFloat 32
  289. %47 = OpTypeVector %46 4
  290. %48 = OpTypePointer Output %47
  291. %49 = OpVariable %48 Output
  292. %54 = OpTypePointer Output %46
  293. %64 = OpConstantFalse %27
  294. %67 = OpConstantTrue %27
  295. %81 = OpUndef %6
  296. %4 = OpFunction %2 None %3
  297. %5 = OpLabel
  298. OpBranch %61
  299. %61 = OpLabel
  300. OpLoopMerge %60 %63 None
  301. OpBranch %20
  302. %20 = OpLabel
  303. %30 = OpINotEqual %27 %81 %29
  304. OpLoopMerge %22 %23 None
  305. OpBranchConditional %30 %21 %22
  306. %21 = OpLabel
  307. OpBranch %31
  308. %31 = OpLabel
  309. OpLoopMerge %33 %38 None
  310. OpBranch %32
  311. %32 = OpLabel
  312. OpBranchConditional %30 %37 %38
  313. %37 = OpLabel
  314. OpSelectionMerge %42 None
  315. OpBranchConditional %30 %41 %45
  316. %41 = OpLabel
  317. OpBranch %42
  318. %45 = OpLabel
  319. %52 = OpIMul %6 %81 %81
  320. %53 = OpConvertSToF %46 %52
  321. %55 = OpAccessChain %54 %49 %29
  322. OpStore %55 %53
  323. OpBranch %42
  324. %42 = OpLabel
  325. OpBranch %33
  326. %38 = OpLabel
  327. %77 = OpSDiv %6 %81 %12
  328. %58 = OpINotEqual %27 %77 %29
  329. OpBranchConditional %58 %31 %33
  330. %33 = OpLabel
  331. %86 = OpPhi %27 %67 %42 %64 %38
  332. OpSelectionMerge %68 None
  333. OpBranchConditional %86 %22 %68
  334. %68 = OpLabel
  335. OpBranch %22
  336. %23 = OpLabel
  337. OpBranch %20
  338. %22 = OpLabel
  339. %90 = OpPhi %27 %64 %20 %86 %33 %67 %68
  340. OpSelectionMerge %70 None
  341. OpBranchConditional %90 %60 %70
  342. %70 = OpLabel
  343. OpBranch %60
  344. %63 = OpLabel
  345. OpBranch %61
  346. %60 = OpLabel
  347. OpReturn
  348. OpFunctionEnd
  349. )";
  350. // The shader below comes from the following GLSL.
  351. // #version 320 es
  352. //
  353. // int baz(int x) {
  354. // int y = x + 1;
  355. // y = y + 2;
  356. // if (y > 0) {
  357. // return x;
  358. // }
  359. // return x + 1;
  360. // }
  361. //
  362. // int bar(int a) {
  363. // if (a == 3) {
  364. // return baz(2*a);
  365. // }
  366. // a = a + 1;
  367. // for (int i = 0; i < 10; i++) {
  368. // a += baz(a);
  369. // }
  370. // return a;
  371. // }
  372. //
  373. // void main() {
  374. // int x;
  375. // x = 3;
  376. // x += 1;
  377. // x += bar(x);
  378. // x += baz(x);
  379. // }
  380. const std::string kShaderWithMultipleFunctions = R"(
  381. OpCapability Shader
  382. %1 = OpExtInstImport "GLSL.std.450"
  383. OpMemoryModel Logical GLSL450
  384. OpEntryPoint Fragment %4 "main"
  385. OpExecutionMode %4 OriginUpperLeft
  386. OpSource ESSL 320
  387. %2 = OpTypeVoid
  388. %3 = OpTypeFunction %2
  389. %6 = OpTypeInt 32 1
  390. %7 = OpTypePointer Function %6
  391. %8 = OpTypeFunction %6 %7
  392. %17 = OpConstant %6 1
  393. %20 = OpConstant %6 2
  394. %23 = OpConstant %6 0
  395. %24 = OpTypeBool
  396. %35 = OpConstant %6 3
  397. %53 = OpConstant %6 10
  398. %4 = OpFunction %2 None %3
  399. %5 = OpLabel
  400. %65 = OpVariable %7 Function
  401. %68 = OpVariable %7 Function
  402. %73 = OpVariable %7 Function
  403. OpStore %65 %35
  404. %66 = OpLoad %6 %65
  405. %67 = OpIAdd %6 %66 %17
  406. OpStore %65 %67
  407. %69 = OpLoad %6 %65
  408. OpStore %68 %69
  409. %70 = OpFunctionCall %6 %13 %68
  410. %71 = OpLoad %6 %65
  411. %72 = OpIAdd %6 %71 %70
  412. OpStore %65 %72
  413. %74 = OpLoad %6 %65
  414. OpStore %73 %74
  415. %75 = OpFunctionCall %6 %10 %73
  416. %76 = OpLoad %6 %65
  417. %77 = OpIAdd %6 %76 %75
  418. OpStore %65 %77
  419. OpReturn
  420. OpFunctionEnd
  421. %10 = OpFunction %6 None %8
  422. %9 = OpFunctionParameter %7
  423. %11 = OpLabel
  424. %15 = OpVariable %7 Function
  425. %16 = OpLoad %6 %9
  426. %18 = OpIAdd %6 %16 %17
  427. OpStore %15 %18
  428. %19 = OpLoad %6 %15
  429. %21 = OpIAdd %6 %19 %20
  430. OpStore %15 %21
  431. %22 = OpLoad %6 %15
  432. %25 = OpSGreaterThan %24 %22 %23
  433. OpSelectionMerge %27 None
  434. OpBranchConditional %25 %26 %27
  435. %26 = OpLabel
  436. %28 = OpLoad %6 %9
  437. OpReturnValue %28
  438. %27 = OpLabel
  439. %30 = OpLoad %6 %9
  440. %31 = OpIAdd %6 %30 %17
  441. OpReturnValue %31
  442. OpFunctionEnd
  443. %13 = OpFunction %6 None %8
  444. %12 = OpFunctionParameter %7
  445. %14 = OpLabel
  446. %41 = OpVariable %7 Function
  447. %46 = OpVariable %7 Function
  448. %55 = OpVariable %7 Function
  449. %34 = OpLoad %6 %12
  450. %36 = OpIEqual %24 %34 %35
  451. OpSelectionMerge %38 None
  452. OpBranchConditional %36 %37 %38
  453. %37 = OpLabel
  454. %39 = OpLoad %6 %12
  455. %40 = OpIMul %6 %20 %39
  456. OpStore %41 %40
  457. %42 = OpFunctionCall %6 %10 %41
  458. OpReturnValue %42
  459. %38 = OpLabel
  460. %44 = OpLoad %6 %12
  461. %45 = OpIAdd %6 %44 %17
  462. OpStore %12 %45
  463. OpStore %46 %23
  464. OpBranch %47
  465. %47 = OpLabel
  466. OpLoopMerge %49 %50 None
  467. OpBranch %51
  468. %51 = OpLabel
  469. %52 = OpLoad %6 %46
  470. %54 = OpSLessThan %24 %52 %53
  471. OpBranchConditional %54 %48 %49
  472. %48 = OpLabel
  473. %56 = OpLoad %6 %12
  474. OpStore %55 %56
  475. %57 = OpFunctionCall %6 %10 %55
  476. %58 = OpLoad %6 %12
  477. %59 = OpIAdd %6 %58 %57
  478. OpStore %12 %59
  479. OpBranch %50
  480. %50 = OpLabel
  481. %60 = OpLoad %6 %46
  482. %61 = OpIAdd %6 %60 %17
  483. OpStore %46 %61
  484. OpBranch %47
  485. %49 = OpLabel
  486. %62 = OpLoad %6 %12
  487. OpReturnValue %62
  488. OpFunctionEnd
  489. )";
  490. TEST(ReducerTest, ShaderReduceWhileMulReachable) {
  491. Reducer reducer(kEnv);
  492. reducer.SetInterestingnessFunction(InterestingWhileIMulReachable);
  493. reducer.AddDefaultReductionPasses();
  494. reducer.SetMessageConsumer(kMessageConsumer);
  495. std::vector<uint32_t> binary_in;
  496. SpirvTools t(kEnv);
  497. ASSERT_TRUE(
  498. t.Assemble(kShaderWithLoopsDivAndMul, &binary_in, kReduceAssembleOption));
  499. std::vector<uint32_t> binary_out;
  500. spvtools::ReducerOptions reducer_options;
  501. reducer_options.set_step_limit(500);
  502. reducer_options.set_fail_on_validation_error(true);
  503. spvtools::ValidatorOptions validator_options;
  504. Reducer::ReductionResultStatus status = reducer.Run(
  505. std::move(binary_in), &binary_out, reducer_options, validator_options);
  506. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  507. }
  508. TEST(ReducerTest, ShaderReduceWhileDivReachable) {
  509. Reducer reducer(kEnv);
  510. reducer.SetInterestingnessFunction(InterestingWhileSDivReachable);
  511. reducer.AddDefaultReductionPasses();
  512. reducer.SetMessageConsumer(kMessageConsumer);
  513. std::vector<uint32_t> binary_in;
  514. SpirvTools t(kEnv);
  515. ASSERT_TRUE(
  516. t.Assemble(kShaderWithLoopsDivAndMul, &binary_in, kReduceAssembleOption));
  517. std::vector<uint32_t> binary_out;
  518. spvtools::ReducerOptions reducer_options;
  519. reducer_options.set_step_limit(500);
  520. reducer_options.set_fail_on_validation_error(true);
  521. spvtools::ValidatorOptions validator_options;
  522. Reducer::ReductionResultStatus status = reducer.Run(
  523. std::move(binary_in), &binary_out, reducer_options, validator_options);
  524. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  525. }
  526. // Computes an instruction count for each function in the module represented by
  527. // |binary|.
  528. std::unordered_map<uint32_t, uint32_t> GetFunctionInstructionCount(
  529. const std::vector<uint32_t>& binary) {
  530. std::unique_ptr<opt::IRContext> context =
  531. BuildModule(kEnv, kMessageConsumer, binary.data(), binary.size());
  532. assert(context != nullptr && "Failed to build module.");
  533. std::unordered_map<uint32_t, uint32_t> result;
  534. for (auto& function : *context->module()) {
  535. uint32_t& count = result[function.result_id()] = 0;
  536. function.ForEachInst([&count](opt::Instruction*) { count++; });
  537. }
  538. return result;
  539. }
  540. TEST(ReducerTest, SingleFunctionReduction) {
  541. Reducer reducer(kEnv);
  542. PingPongInteresting ping_pong_interesting(4);
  543. reducer.SetInterestingnessFunction(
  544. [&ping_pong_interesting](const std::vector<uint32_t>&, uint32_t) -> bool {
  545. return ping_pong_interesting.IsInteresting();
  546. });
  547. reducer.AddDefaultReductionPasses();
  548. reducer.SetMessageConsumer(kMessageConsumer);
  549. std::vector<uint32_t> binary_in;
  550. SpirvTools t(kEnv);
  551. ASSERT_TRUE(t.Assemble(kShaderWithMultipleFunctions, &binary_in,
  552. kReduceAssembleOption));
  553. auto original_instruction_count = GetFunctionInstructionCount(binary_in);
  554. std::vector<uint32_t> binary_out;
  555. spvtools::ReducerOptions reducer_options;
  556. reducer_options.set_step_limit(500);
  557. reducer_options.set_fail_on_validation_error(true);
  558. // Instruct the reducer to only target function 13.
  559. reducer_options.set_target_function(13);
  560. spvtools::ValidatorOptions validator_options;
  561. Reducer::ReductionResultStatus status = reducer.Run(
  562. std::move(binary_in), &binary_out, reducer_options, validator_options);
  563. ASSERT_EQ(status, Reducer::ReductionResultStatus::kComplete);
  564. auto final_instruction_count = GetFunctionInstructionCount(binary_out);
  565. // Nothing should have been removed from these functions.
  566. ASSERT_EQ(original_instruction_count.at(4), final_instruction_count.at(4));
  567. ASSERT_EQ(original_instruction_count.at(10), final_instruction_count.at(10));
  568. // Function 13 should have been reduced to these five instructions:
  569. // OpFunction
  570. // OpFunctionParameter
  571. // OpLabel
  572. // OpReturnValue
  573. // OpFunctionEnd
  574. ASSERT_EQ(5, final_instruction_count.at(13));
  575. }
  576. } // namespace
  577. } // namespace reduce
  578. } // namespace spvtools