transformation_set_selection_control_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_set_selection_control.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(TransformationSetSelectionControlTest, VariousScenarios) {
  22. // This is a simple transformation; this test captures the important things
  23. // to check for.
  24. std::string shader = R"(
  25. OpCapability Shader
  26. %1 = OpExtInstImport "GLSL.std.450"
  27. OpMemoryModel Logical GLSL450
  28. OpEntryPoint Fragment %4 "main"
  29. OpExecutionMode %4 OriginUpperLeft
  30. OpSource ESSL 310
  31. OpName %4 "main"
  32. OpName %8 "i"
  33. %2 = OpTypeVoid
  34. %3 = OpTypeFunction %2
  35. %6 = OpTypeInt 32 1
  36. %7 = OpTypePointer Function %6
  37. %9 = OpConstant %6 0
  38. %16 = OpConstant %6 10
  39. %17 = OpTypeBool
  40. %20 = OpConstant %6 3
  41. %25 = OpConstant %6 1
  42. %28 = OpConstant %6 2
  43. %38 = OpConstant %6 4
  44. %4 = OpFunction %2 None %3
  45. %5 = OpLabel
  46. %8 = OpVariable %7 Function
  47. OpStore %8 %9
  48. OpBranch %10
  49. %10 = OpLabel
  50. OpLoopMerge %12 %13 None
  51. OpBranch %14
  52. %14 = OpLabel
  53. %15 = OpLoad %6 %8
  54. %18 = OpSLessThan %17 %15 %16
  55. OpBranchConditional %18 %11 %12
  56. %11 = OpLabel
  57. %19 = OpLoad %6 %8
  58. %21 = OpSGreaterThan %17 %19 %20
  59. OpSelectionMerge %23 Flatten
  60. OpBranchConditional %21 %22 %23
  61. %22 = OpLabel
  62. %24 = OpLoad %6 %8
  63. %26 = OpIAdd %6 %24 %25
  64. OpStore %8 %26
  65. OpBranch %23
  66. %23 = OpLabel
  67. %27 = OpLoad %6 %8
  68. %29 = OpSLessThan %17 %27 %28
  69. OpSelectionMerge %31 DontFlatten
  70. OpBranchConditional %29 %30 %31
  71. %30 = OpLabel
  72. %32 = OpLoad %6 %8
  73. %33 = OpISub %6 %32 %25
  74. OpStore %8 %33
  75. OpBranch %31
  76. %31 = OpLabel
  77. %34 = OpLoad %6 %8
  78. OpSelectionMerge %37 None
  79. OpSwitch %34 %36 0 %35
  80. %36 = OpLabel
  81. OpBranch %37
  82. %35 = OpLabel
  83. %39 = OpLoad %6 %8
  84. %40 = OpIAdd %6 %39 %38
  85. OpStore %8 %40
  86. OpBranch %36
  87. %37 = OpLabel
  88. OpBranch %13
  89. %13 = OpLabel
  90. %43 = OpLoad %6 %8
  91. %44 = OpIAdd %6 %43 %25
  92. OpStore %8 %44
  93. OpBranch %10
  94. %12 = OpLabel
  95. OpReturn
  96. OpFunctionEnd
  97. )";
  98. const auto env = SPV_ENV_UNIVERSAL_1_3;
  99. const auto consumer = nullptr;
  100. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  101. spvtools::ValidatorOptions validator_options;
  102. TransformationContext transformation_context(
  103. MakeUnique<FactManager>(context.get()), validator_options);
  104. // %44 is not a block
  105. ASSERT_FALSE(TransformationSetSelectionControl(
  106. 44, uint32_t(spv::SelectionControlMask::Flatten))
  107. .IsApplicable(context.get(), transformation_context));
  108. // %13 does not end with OpSelectionMerge
  109. ASSERT_FALSE(TransformationSetSelectionControl(
  110. 13, uint32_t(spv::SelectionControlMask::MaskNone))
  111. .IsApplicable(context.get(), transformation_context));
  112. // %10 ends in OpLoopMerge, not OpSelectionMerge
  113. ASSERT_FALSE(TransformationSetSelectionControl(
  114. 10, uint32_t(spv::SelectionControlMask::MaskNone))
  115. .IsApplicable(context.get(), transformation_context));
  116. TransformationSetSelectionControl transformation1(
  117. 11, uint32_t(spv::SelectionControlMask::DontFlatten));
  118. ASSERT_TRUE(
  119. transformation1.IsApplicable(context.get(), transformation_context));
  120. ApplyAndCheckFreshIds(transformation1, context.get(),
  121. &transformation_context);
  122. TransformationSetSelectionControl transformation2(
  123. 23, uint32_t(spv::SelectionControlMask::Flatten));
  124. ASSERT_TRUE(
  125. transformation2.IsApplicable(context.get(), transformation_context));
  126. ApplyAndCheckFreshIds(transformation2, context.get(),
  127. &transformation_context);
  128. TransformationSetSelectionControl transformation3(
  129. 31, uint32_t(spv::SelectionControlMask::MaskNone));
  130. ASSERT_TRUE(
  131. transformation3.IsApplicable(context.get(), transformation_context));
  132. ApplyAndCheckFreshIds(transformation3, context.get(),
  133. &transformation_context);
  134. TransformationSetSelectionControl transformation4(
  135. 31, uint32_t(spv::SelectionControlMask::Flatten));
  136. ASSERT_TRUE(
  137. transformation4.IsApplicable(context.get(), transformation_context));
  138. ApplyAndCheckFreshIds(transformation4, context.get(),
  139. &transformation_context);
  140. std::string after_transformation = R"(
  141. OpCapability Shader
  142. %1 = OpExtInstImport "GLSL.std.450"
  143. OpMemoryModel Logical GLSL450
  144. OpEntryPoint Fragment %4 "main"
  145. OpExecutionMode %4 OriginUpperLeft
  146. OpSource ESSL 310
  147. OpName %4 "main"
  148. OpName %8 "i"
  149. %2 = OpTypeVoid
  150. %3 = OpTypeFunction %2
  151. %6 = OpTypeInt 32 1
  152. %7 = OpTypePointer Function %6
  153. %9 = OpConstant %6 0
  154. %16 = OpConstant %6 10
  155. %17 = OpTypeBool
  156. %20 = OpConstant %6 3
  157. %25 = OpConstant %6 1
  158. %28 = OpConstant %6 2
  159. %38 = OpConstant %6 4
  160. %4 = OpFunction %2 None %3
  161. %5 = OpLabel
  162. %8 = OpVariable %7 Function
  163. OpStore %8 %9
  164. OpBranch %10
  165. %10 = OpLabel
  166. OpLoopMerge %12 %13 None
  167. OpBranch %14
  168. %14 = OpLabel
  169. %15 = OpLoad %6 %8
  170. %18 = OpSLessThan %17 %15 %16
  171. OpBranchConditional %18 %11 %12
  172. %11 = OpLabel
  173. %19 = OpLoad %6 %8
  174. %21 = OpSGreaterThan %17 %19 %20
  175. OpSelectionMerge %23 DontFlatten
  176. OpBranchConditional %21 %22 %23
  177. %22 = OpLabel
  178. %24 = OpLoad %6 %8
  179. %26 = OpIAdd %6 %24 %25
  180. OpStore %8 %26
  181. OpBranch %23
  182. %23 = OpLabel
  183. %27 = OpLoad %6 %8
  184. %29 = OpSLessThan %17 %27 %28
  185. OpSelectionMerge %31 Flatten
  186. OpBranchConditional %29 %30 %31
  187. %30 = OpLabel
  188. %32 = OpLoad %6 %8
  189. %33 = OpISub %6 %32 %25
  190. OpStore %8 %33
  191. OpBranch %31
  192. %31 = OpLabel
  193. %34 = OpLoad %6 %8
  194. OpSelectionMerge %37 Flatten
  195. OpSwitch %34 %36 0 %35
  196. %36 = OpLabel
  197. OpBranch %37
  198. %35 = OpLabel
  199. %39 = OpLoad %6 %8
  200. %40 = OpIAdd %6 %39 %38
  201. OpStore %8 %40
  202. OpBranch %36
  203. %37 = OpLabel
  204. OpBranch %13
  205. %13 = OpLabel
  206. %43 = OpLoad %6 %8
  207. %44 = OpIAdd %6 %43 %25
  208. OpStore %8 %44
  209. OpBranch %10
  210. %12 = OpLabel
  211. OpReturn
  212. OpFunctionEnd
  213. )";
  214. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  215. }
  216. } // namespace
  217. } // namespace fuzz
  218. } // namespace spvtools