transformation_add_loop_preheader_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright (c) 2020 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_loop_preheader.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(TransformationAddLoopPreheaderTest, SimpleTest) {
  22. std::string shader = R"(
  23. OpCapability Shader
  24. %1 = OpExtInstImport "GLSL.std.450"
  25. OpMemoryModel Logical GLSL450
  26. OpEntryPoint Fragment %4 "main"
  27. OpExecutionMode %4 OriginUpperLeft
  28. OpSource ESSL 310
  29. OpName %4 "main"
  30. %2 = OpTypeVoid
  31. %3 = OpTypeFunction %2
  32. %6 = OpTypeBool
  33. %7 = OpConstantFalse %6
  34. %4 = OpFunction %2 None %3
  35. %5 = OpLabel
  36. OpSelectionMerge %10 None
  37. OpBranchConditional %7 %8 %9
  38. %8 = OpLabel
  39. OpBranch %10
  40. %9 = OpLabel
  41. OpBranch %10
  42. %10 = OpLabel
  43. OpLoopMerge %12 %11 None
  44. OpBranch %11
  45. %11 = OpLabel
  46. OpBranchConditional %7 %10 %12
  47. %12 = OpLabel
  48. OpLoopMerge %14 %13 None
  49. OpBranch %13
  50. %13 = OpLabel
  51. OpBranchConditional %7 %14 %12
  52. %15 = OpLabel
  53. OpLoopMerge %17 %16 None
  54. OpBranch %16
  55. %16 = OpLabel
  56. OpBranchConditional %7 %15 %17
  57. %17 = OpLabel
  58. OpBranch %14
  59. %14 = OpLabel
  60. OpReturn
  61. OpFunctionEnd
  62. )";
  63. const auto env = SPV_ENV_UNIVERSAL_1_5;
  64. const auto consumer = nullptr;
  65. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  66. spvtools::ValidatorOptions validator_options;
  67. TransformationContext transformation_context(
  68. MakeUnique<FactManager>(context.get()), validator_options);
  69. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  70. kConsoleMessageConsumer));
  71. // %9 is not a loop header
  72. ASSERT_FALSE(TransformationAddLoopPreheader(9, 15, {}).IsApplicable(
  73. context.get(), transformation_context));
  74. // The id %12 is not fresh
  75. ASSERT_FALSE(TransformationAddLoopPreheader(10, 12, {})
  76. .IsApplicable(context.get(), transformation_context));
  77. // Loop header %15 is not reachable (the only predecessor is the back-edge
  78. // block)
  79. ASSERT_FALSE(TransformationAddLoopPreheader(15, 100, {})
  80. .IsApplicable(context.get(), transformation_context));
  81. auto transformation1 = TransformationAddLoopPreheader(10, 20, {});
  82. ASSERT_TRUE(
  83. transformation1.IsApplicable(context.get(), transformation_context));
  84. ApplyAndCheckFreshIds(transformation1, context.get(),
  85. &transformation_context);
  86. auto transformation2 = TransformationAddLoopPreheader(12, 21, {});
  87. ASSERT_TRUE(
  88. transformation2.IsApplicable(context.get(), transformation_context));
  89. ApplyAndCheckFreshIds(transformation2, context.get(),
  90. &transformation_context);
  91. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  92. kConsoleMessageConsumer));
  93. std::string after_transformations = R"(
  94. OpCapability Shader
  95. %1 = OpExtInstImport "GLSL.std.450"
  96. OpMemoryModel Logical GLSL450
  97. OpEntryPoint Fragment %4 "main"
  98. OpExecutionMode %4 OriginUpperLeft
  99. OpSource ESSL 310
  100. OpName %4 "main"
  101. %2 = OpTypeVoid
  102. %3 = OpTypeFunction %2
  103. %6 = OpTypeBool
  104. %7 = OpConstantFalse %6
  105. %4 = OpFunction %2 None %3
  106. %5 = OpLabel
  107. OpSelectionMerge %20 None
  108. OpBranchConditional %7 %8 %9
  109. %8 = OpLabel
  110. OpBranch %20
  111. %9 = OpLabel
  112. OpBranch %20
  113. %20 = OpLabel
  114. OpBranch %10
  115. %10 = OpLabel
  116. OpLoopMerge %21 %11 None
  117. OpBranch %11
  118. %11 = OpLabel
  119. OpBranchConditional %7 %10 %21
  120. %21 = OpLabel
  121. OpBranch %12
  122. %12 = OpLabel
  123. OpLoopMerge %14 %13 None
  124. OpBranch %13
  125. %13 = OpLabel
  126. OpBranchConditional %7 %14 %12
  127. %15 = OpLabel
  128. OpLoopMerge %17 %16 None
  129. OpBranch %16
  130. %16 = OpLabel
  131. OpBranchConditional %7 %15 %17
  132. %17 = OpLabel
  133. OpBranch %14
  134. %14 = OpLabel
  135. OpReturn
  136. OpFunctionEnd
  137. )";
  138. ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
  139. }
  140. TEST(TransformationAddLoopPreheaderTest, OpPhi) {
  141. std::string shader = R"(
  142. OpCapability Shader
  143. %1 = OpExtInstImport "GLSL.std.450"
  144. OpMemoryModel Logical GLSL450
  145. OpEntryPoint Fragment %4 "main"
  146. OpExecutionMode %4 OriginUpperLeft
  147. OpSource ESSL 310
  148. OpName %4 "main"
  149. %2 = OpTypeVoid
  150. %3 = OpTypeFunction %2
  151. %6 = OpTypeBool
  152. %7 = OpConstantFalse %6
  153. %4 = OpFunction %2 None %3
  154. %5 = OpLabel
  155. %20 = OpCopyObject %6 %7
  156. OpBranch %8
  157. %8 = OpLabel
  158. %31 = OpPhi %6 %20 %5 %21 %9
  159. OpLoopMerge %10 %9 None
  160. OpBranch %9
  161. %9 = OpLabel
  162. %21 = OpCopyObject %6 %7
  163. OpBranchConditional %7 %8 %10
  164. %10 = OpLabel
  165. OpSelectionMerge %13 None
  166. OpBranchConditional %7 %11 %12
  167. %11 = OpLabel
  168. %22 = OpCopyObject %6 %7
  169. OpBranch %13
  170. %12 = OpLabel
  171. %23 = OpCopyObject %6 %7
  172. OpBranch %13
  173. %13 = OpLabel
  174. %32 = OpPhi %6 %22 %11 %23 %12 %24 %14
  175. %33 = OpPhi %6 %7 %11 %7 %12 %24 %14
  176. OpLoopMerge %15 %14 None
  177. OpBranch %14
  178. %14 = OpLabel
  179. %24 = OpCopyObject %6 %7
  180. OpBranchConditional %7 %13 %15
  181. %15 = OpLabel
  182. OpReturn
  183. OpFunctionEnd
  184. )";
  185. const auto env = SPV_ENV_UNIVERSAL_1_5;
  186. const auto consumer = nullptr;
  187. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  188. spvtools::ValidatorOptions validator_options;
  189. TransformationContext transformation_context(
  190. MakeUnique<FactManager>(context.get()), validator_options);
  191. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  192. kConsoleMessageConsumer));
  193. auto transformation1 = TransformationAddLoopPreheader(8, 40, {});
  194. ASSERT_TRUE(
  195. transformation1.IsApplicable(context.get(), transformation_context));
  196. ApplyAndCheckFreshIds(transformation1, context.get(),
  197. &transformation_context);
  198. // Not enough ids for the OpPhi instructions are given
  199. ASSERT_FALSE(TransformationAddLoopPreheader(13, 41, {})
  200. .IsApplicable(context.get(), transformation_context));
  201. // Not enough ids for the OpPhi instructions are given
  202. ASSERT_FALSE(TransformationAddLoopPreheader(13, 41, {42})
  203. .IsApplicable(context.get(), transformation_context));
  204. // One of the ids is not fresh
  205. ASSERT_FALSE(TransformationAddLoopPreheader(13, 41, {31, 42})
  206. .IsApplicable(context.get(), transformation_context));
  207. // One of the ids is repeated
  208. ASSERT_FALSE(TransformationAddLoopPreheader(13, 41, {41, 42})
  209. .IsApplicable(context.get(), transformation_context));
  210. auto transformation2 = TransformationAddLoopPreheader(13, 41, {42, 43});
  211. ASSERT_TRUE(
  212. transformation2.IsApplicable(context.get(), transformation_context));
  213. ApplyAndCheckFreshIds(transformation2, context.get(),
  214. &transformation_context);
  215. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  216. kConsoleMessageConsumer));
  217. std::string after_transformations = R"(
  218. OpCapability Shader
  219. %1 = OpExtInstImport "GLSL.std.450"
  220. OpMemoryModel Logical GLSL450
  221. OpEntryPoint Fragment %4 "main"
  222. OpExecutionMode %4 OriginUpperLeft
  223. OpSource ESSL 310
  224. OpName %4 "main"
  225. %2 = OpTypeVoid
  226. %3 = OpTypeFunction %2
  227. %6 = OpTypeBool
  228. %7 = OpConstantFalse %6
  229. %4 = OpFunction %2 None %3
  230. %5 = OpLabel
  231. %20 = OpCopyObject %6 %7
  232. OpBranch %40
  233. %40 = OpLabel
  234. OpBranch %8
  235. %8 = OpLabel
  236. %31 = OpPhi %6 %20 %40 %21 %9
  237. OpLoopMerge %10 %9 None
  238. OpBranch %9
  239. %9 = OpLabel
  240. %21 = OpCopyObject %6 %7
  241. OpBranchConditional %7 %8 %10
  242. %10 = OpLabel
  243. OpSelectionMerge %41 None
  244. OpBranchConditional %7 %11 %12
  245. %11 = OpLabel
  246. %22 = OpCopyObject %6 %7
  247. OpBranch %41
  248. %12 = OpLabel
  249. %23 = OpCopyObject %6 %7
  250. OpBranch %41
  251. %41 = OpLabel
  252. %42 = OpPhi %6 %22 %11 %23 %12
  253. %43 = OpPhi %6 %7 %11 %7 %12
  254. OpBranch %13
  255. %13 = OpLabel
  256. %32 = OpPhi %6 %42 %41 %24 %14
  257. %33 = OpPhi %6 %43 %41 %24 %14
  258. OpLoopMerge %15 %14 None
  259. OpBranch %14
  260. %14 = OpLabel
  261. %24 = OpCopyObject %6 %7
  262. OpBranchConditional %7 %13 %15
  263. %15 = OpLabel
  264. OpReturn
  265. OpFunctionEnd
  266. )";
  267. ASSERT_TRUE(IsEqual(env, after_transformations, context.get()));
  268. }
  269. } // namespace
  270. } // namespace fuzz
  271. } // namespace spvtools