transformation_add_dead_block_test.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Copyright (c) 2019 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/fuzz/transformation_add_dead_block.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "test/fuzz/fuzz_test_util.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. namespace {
  21. TEST(TransformationAddDeadBlockTest, BasicTest) {
  22. std::string reference_shader = R"(
  23. OpCapability Shader
  24. %1 = OpExtInstImport "GLSL.std.450"
  25. OpMemoryModel Logical GLSL450
  26. OpEntryPoint Fragment %6 "main"
  27. OpExecutionMode %6 OriginUpperLeft
  28. ; Types
  29. %2 = OpTypeBool
  30. %3 = OpTypeVoid
  31. %4 = OpTypeFunction %3
  32. ; Constants
  33. %5 = OpConstantTrue %2
  34. ; main function
  35. %6 = OpFunction %3 None %4
  36. %7 = OpLabel
  37. OpSelectionMerge %11 None
  38. OpBranchConditional %5 %8 %9
  39. %8 = OpLabel
  40. OpBranch %10
  41. %9 = OpLabel
  42. OpBranch %10
  43. %10 = OpLabel
  44. OpBranch %11
  45. %11 = OpLabel
  46. OpBranch %13
  47. %12 = OpLabel
  48. OpBranch %13
  49. %13 = OpLabel
  50. OpReturn
  51. OpFunctionEnd
  52. )";
  53. const auto env = SPV_ENV_UNIVERSAL_1_4;
  54. const auto consumer = nullptr;
  55. const auto context =
  56. BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
  57. spvtools::ValidatorOptions validator_options;
  58. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  59. kConsoleMessageConsumer));
  60. TransformationContext transformation_context(
  61. MakeUnique<FactManager>(context.get()), validator_options);
  62. // Id 4 is already in use
  63. auto transformation = TransformationAddDeadBlock(4, 11, true);
  64. ASSERT_FALSE(
  65. transformation.IsApplicable(context.get(), transformation_context));
  66. // Id 5 is not a block
  67. transformation = TransformationAddDeadBlock(14, 5, true);
  68. ASSERT_FALSE(
  69. transformation.IsApplicable(context.get(), transformation_context));
  70. // Tests existing block not dominating its successor block.
  71. transformation = TransformationAddDeadBlock(14, 8, true);
  72. ASSERT_FALSE(
  73. transformation.IsApplicable(context.get(), transformation_context));
  74. transformation = TransformationAddDeadBlock(14, 9, true);
  75. ASSERT_FALSE(
  76. transformation.IsApplicable(context.get(), transformation_context));
  77. // Tests existing block being an unreachable block.
  78. transformation = TransformationAddDeadBlock(14, 12, true);
  79. ASSERT_FALSE(
  80. transformation.IsApplicable(context.get(), transformation_context));
  81. // Tests applicable case.
  82. transformation = TransformationAddDeadBlock(14, 11, true);
  83. ASSERT_TRUE(
  84. transformation.IsApplicable(context.get(), transformation_context));
  85. ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
  86. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(14));
  87. std::string variant_shader = R"(
  88. OpCapability Shader
  89. %1 = OpExtInstImport "GLSL.std.450"
  90. OpMemoryModel Logical GLSL450
  91. OpEntryPoint Fragment %6 "main"
  92. OpExecutionMode %6 OriginUpperLeft
  93. ; Types
  94. %2 = OpTypeBool
  95. %3 = OpTypeVoid
  96. %4 = OpTypeFunction %3
  97. ; Constants
  98. %5 = OpConstantTrue %2
  99. ; main function
  100. %6 = OpFunction %3 None %4
  101. %7 = OpLabel
  102. OpSelectionMerge %11 None
  103. OpBranchConditional %5 %8 %9
  104. %8 = OpLabel
  105. OpBranch %10
  106. %9 = OpLabel
  107. OpBranch %10
  108. %10 = OpLabel
  109. OpBranch %11
  110. %11 = OpLabel
  111. OpSelectionMerge %13 None
  112. OpBranchConditional %5 %13 %14
  113. %14 = OpLabel
  114. OpBranch %13
  115. %12 = OpLabel
  116. OpBranch %13
  117. %13 = OpLabel
  118. OpReturn
  119. OpFunctionEnd
  120. )";
  121. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  122. kConsoleMessageConsumer));
  123. ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
  124. }
  125. TEST(TransformationAddDeadBlockTest, ApplicableWithFalseCondition) {
  126. std::string reference_shader = R"(
  127. OpCapability Shader
  128. %1 = OpExtInstImport "GLSL.std.450"
  129. OpMemoryModel Logical GLSL450
  130. OpEntryPoint Fragment %6 "main"
  131. OpExecutionMode %6 OriginUpperLeft
  132. ; Types
  133. %2 = OpTypeBool
  134. %3 = OpTypeVoid
  135. %4 = OpTypeFunction %3
  136. ; Constants
  137. %5 = OpConstantFalse %2
  138. ; main function
  139. %6 = OpFunction %3 None %4
  140. %7 = OpLabel
  141. OpSelectionMerge %11 None
  142. OpBranchConditional %5 %8 %9
  143. %8 = OpLabel
  144. OpBranch %10
  145. %9 = OpLabel
  146. OpBranch %10
  147. %10 = OpLabel
  148. OpBranch %11
  149. %11 = OpLabel
  150. OpBranch %13
  151. %12 = OpLabel
  152. OpBranch %13
  153. %13 = OpLabel
  154. OpReturn
  155. OpFunctionEnd
  156. )";
  157. const auto env = SPV_ENV_UNIVERSAL_1_4;
  158. const auto consumer = nullptr;
  159. const auto context =
  160. BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
  161. spvtools::ValidatorOptions validator_options;
  162. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  163. kConsoleMessageConsumer));
  164. TransformationContext transformation_context(
  165. MakeUnique<FactManager>(context.get()), validator_options);
  166. auto transformation = TransformationAddDeadBlock(14, 11, false);
  167. ASSERT_TRUE(
  168. transformation.IsApplicable(context.get(), transformation_context));
  169. ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
  170. std::string variant_shader = R"(
  171. OpCapability Shader
  172. %1 = OpExtInstImport "GLSL.std.450"
  173. OpMemoryModel Logical GLSL450
  174. OpEntryPoint Fragment %6 "main"
  175. OpExecutionMode %6 OriginUpperLeft
  176. ; Types
  177. %2 = OpTypeBool
  178. %3 = OpTypeVoid
  179. %4 = OpTypeFunction %3
  180. ; Constants
  181. %5 = OpConstantFalse %2
  182. ; main function
  183. %6 = OpFunction %3 None %4
  184. %7 = OpLabel
  185. OpSelectionMerge %11 None
  186. OpBranchConditional %5 %8 %9
  187. %8 = OpLabel
  188. OpBranch %10
  189. %9 = OpLabel
  190. OpBranch %10
  191. %10 = OpLabel
  192. OpBranch %11
  193. %11 = OpLabel
  194. OpSelectionMerge %13 None
  195. OpBranchConditional %5 %14 %13
  196. %14 = OpLabel
  197. OpBranch %13
  198. %12 = OpLabel
  199. OpBranch %13
  200. %13 = OpLabel
  201. OpReturn
  202. OpFunctionEnd
  203. )";
  204. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  205. kConsoleMessageConsumer));
  206. ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
  207. }
  208. TEST(TransformationAddDeadBlockTest, TargetBlockMustNotBeSelectionMerge) {
  209. std::string shader = R"(
  210. OpCapability Shader
  211. %1 = OpExtInstImport "GLSL.std.450"
  212. OpMemoryModel Logical GLSL450
  213. OpEntryPoint Fragment %4 "main"
  214. OpExecutionMode %4 OriginUpperLeft
  215. OpSource ESSL 310
  216. OpName %4 "main"
  217. %2 = OpTypeVoid
  218. %3 = OpTypeFunction %2
  219. %6 = OpTypeBool
  220. %7 = OpConstantTrue %6
  221. %4 = OpFunction %2 None %3
  222. %5 = OpLabel
  223. OpSelectionMerge %10 None
  224. OpBranchConditional %7 %8 %9
  225. %8 = OpLabel
  226. OpBranch %10
  227. %9 = OpLabel
  228. OpBranch %10
  229. %10 = OpLabel
  230. OpReturn
  231. OpFunctionEnd
  232. )";
  233. const auto env = SPV_ENV_UNIVERSAL_1_4;
  234. const auto consumer = nullptr;
  235. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  236. spvtools::ValidatorOptions validator_options;
  237. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  238. kConsoleMessageConsumer));
  239. TransformationContext transformation_context(
  240. MakeUnique<FactManager>(context.get()), validator_options);
  241. ASSERT_FALSE(TransformationAddDeadBlock(100, 9, true)
  242. .IsApplicable(context.get(), transformation_context));
  243. }
  244. TEST(TransformationAddDeadBlockTest, TargetBlockMustNotBeLoopMergeOrContinue) {
  245. std::string shader = R"(
  246. OpCapability Shader
  247. %1 = OpExtInstImport "GLSL.std.450"
  248. OpMemoryModel Logical GLSL450
  249. OpEntryPoint Fragment %6 "main"
  250. OpExecutionMode %6 OriginUpperLeft
  251. ; Types
  252. %2 = OpTypeBool
  253. %3 = OpTypeVoid
  254. %4 = OpTypeFunction %3
  255. ; Constants
  256. %5 = OpConstantTrue %2
  257. ; main function
  258. %6 = OpFunction %3 None %4
  259. %7 = OpLabel
  260. OpBranch %8
  261. %8 = OpLabel
  262. OpLoopMerge %12 %11 None
  263. OpBranch %13
  264. %13 = OpLabel
  265. OpSelectionMerge %14 None
  266. OpBranchConditional %5 %9 %10
  267. %9 = OpLabel
  268. OpBranch %11
  269. %10 = OpLabel
  270. OpBranch %12
  271. %14 = OpLabel
  272. OpUnreachable
  273. %11 = OpLabel
  274. OpBranch %8
  275. %12 = OpLabel
  276. OpReturn
  277. OpFunctionEnd
  278. )";
  279. const auto env = SPV_ENV_UNIVERSAL_1_4;
  280. const auto consumer = nullptr;
  281. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  282. spvtools::ValidatorOptions validator_options;
  283. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  284. kConsoleMessageConsumer));
  285. TransformationContext transformation_context(
  286. MakeUnique<FactManager>(context.get()), validator_options);
  287. // Bad because 9's successor is the loop continue target.
  288. ASSERT_FALSE(TransformationAddDeadBlock(100, 9, true)
  289. .IsApplicable(context.get(), transformation_context));
  290. // Bad because 10's successor is the loop merge.
  291. ASSERT_FALSE(TransformationAddDeadBlock(100, 10, true)
  292. .IsApplicable(context.get(), transformation_context));
  293. }
  294. TEST(TransformationAddDeadBlockTest, SourceBlockMustNotBeLoopHead) {
  295. std::string shader = R"(
  296. OpCapability Shader
  297. %1 = OpExtInstImport "GLSL.std.450"
  298. OpMemoryModel Logical GLSL450
  299. OpEntryPoint Fragment %4 "main"
  300. OpExecutionMode %4 OriginUpperLeft
  301. OpSource ESSL 310
  302. OpName %4 "main"
  303. %2 = OpTypeVoid
  304. %3 = OpTypeFunction %2
  305. %6 = OpTypeBool
  306. %7 = OpConstantTrue %6
  307. %4 = OpFunction %2 None %3
  308. %5 = OpLabel
  309. OpBranch %8
  310. %8 = OpLabel
  311. OpLoopMerge %11 %12 None
  312. OpBranch %9
  313. %9 = OpLabel
  314. OpBranchConditional %7 %11 %12
  315. %12 = OpLabel
  316. OpBranch %8
  317. %11 = OpLabel
  318. OpReturn
  319. OpFunctionEnd
  320. )";
  321. const auto env = SPV_ENV_UNIVERSAL_1_4;
  322. const auto consumer = nullptr;
  323. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  324. spvtools::ValidatorOptions validator_options;
  325. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  326. kConsoleMessageConsumer));
  327. TransformationContext transformation_context(
  328. MakeUnique<FactManager>(context.get()), validator_options);
  329. // Bad because 8 is a loop head.
  330. ASSERT_FALSE(TransformationAddDeadBlock(100, 8, true)
  331. .IsApplicable(context.get(), transformation_context));
  332. }
  333. TEST(TransformationAddDeadBlockTest, OpPhiInTarget) {
  334. std::string shader = R"(
  335. OpCapability Shader
  336. %1 = OpExtInstImport "GLSL.std.450"
  337. OpMemoryModel Logical GLSL450
  338. OpEntryPoint Fragment %4 "main"
  339. OpExecutionMode %4 OriginUpperLeft
  340. OpSource ESSL 310
  341. OpName %4 "main"
  342. %2 = OpTypeVoid
  343. %3 = OpTypeFunction %2
  344. %6 = OpTypeBool
  345. %7 = OpConstantTrue %6
  346. %9 = OpTypeInt 32 0
  347. %10 = OpConstant %9 1
  348. %4 = OpFunction %2 None %3
  349. %5 = OpLabel
  350. OpBranch %8
  351. %8 = OpLabel
  352. %12 = OpPhi %6 %7 %5
  353. %13 = OpPhi %9 %10 %5
  354. OpReturn
  355. OpFunctionEnd
  356. )";
  357. const auto env = SPV_ENV_UNIVERSAL_1_4;
  358. const auto consumer = nullptr;
  359. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  360. spvtools::ValidatorOptions validator_options;
  361. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  362. kConsoleMessageConsumer));
  363. TransformationContext transformation_context(
  364. MakeUnique<FactManager>(context.get()), validator_options);
  365. TransformationAddDeadBlock transformation(100, 5, true);
  366. ASSERT_TRUE(
  367. transformation.IsApplicable(context.get(), transformation_context));
  368. ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
  369. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  370. kConsoleMessageConsumer));
  371. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(100));
  372. std::string after_transformation = R"(
  373. OpCapability Shader
  374. %1 = OpExtInstImport "GLSL.std.450"
  375. OpMemoryModel Logical GLSL450
  376. OpEntryPoint Fragment %4 "main"
  377. OpExecutionMode %4 OriginUpperLeft
  378. OpSource ESSL 310
  379. OpName %4 "main"
  380. %2 = OpTypeVoid
  381. %3 = OpTypeFunction %2
  382. %6 = OpTypeBool
  383. %7 = OpConstantTrue %6
  384. %9 = OpTypeInt 32 0
  385. %10 = OpConstant %9 1
  386. %4 = OpFunction %2 None %3
  387. %5 = OpLabel
  388. OpSelectionMerge %8 None
  389. OpBranchConditional %7 %8 %100
  390. %100 = OpLabel
  391. OpBranch %8
  392. %8 = OpLabel
  393. %12 = OpPhi %6 %7 %5 %7 %100
  394. %13 = OpPhi %9 %10 %5 %10 %100
  395. OpReturn
  396. OpFunctionEnd
  397. )";
  398. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  399. }
  400. TEST(TransformationAddDeadBlockTest, BackEdge) {
  401. std::string shader = R"(
  402. OpCapability Shader
  403. %1 = OpExtInstImport "GLSL.std.450"
  404. OpMemoryModel Logical GLSL450
  405. OpEntryPoint Fragment %4 "main"
  406. OpExecutionMode %4 OriginUpperLeft
  407. OpSource ESSL 310
  408. OpName %4 "main"
  409. %2 = OpTypeVoid
  410. %3 = OpTypeFunction %2
  411. %6 = OpTypeBool
  412. %7 = OpConstantTrue %6
  413. %4 = OpFunction %2 None %3
  414. %5 = OpLabel
  415. OpBranch %8
  416. %8 = OpLabel
  417. OpLoopMerge %10 %9 None
  418. OpBranchConditional %7 %9 %10
  419. %9 = OpLabel
  420. OpBranch %8
  421. %10 = OpLabel
  422. OpReturn
  423. OpFunctionEnd
  424. )";
  425. const auto env = SPV_ENV_UNIVERSAL_1_4;
  426. const auto consumer = nullptr;
  427. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  428. spvtools::ValidatorOptions validator_options;
  429. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  430. kConsoleMessageConsumer));
  431. TransformationContext transformation_context(
  432. MakeUnique<FactManager>(context.get()), validator_options);
  433. // 9 is a back edge block, so it would not be OK to add a dead block here,
  434. // as then both 9 and the dead block would branch to the loop header, 8.
  435. ASSERT_FALSE(TransformationAddDeadBlock(100, 9, true)
  436. .IsApplicable(context.get(), transformation_context));
  437. }
  438. } // namespace
  439. } // namespace fuzz
  440. } // namespace spvtools