transformation_set_function_control_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_function_control.h"
  15. #include "test/fuzz/fuzz_test_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. namespace {
  19. TEST(TransformationSetFunctionControlTest, VariousScenarios) {
  20. // This is a simple transformation; this test captures the important things
  21. // to check for.
  22. std::string shader = R"(
  23. OpCapability Shader
  24. %1 = OpExtInstImport "GLSL.std.450"
  25. OpMemoryModel Logical GLSL450
  26. OpEntryPoint Fragment %4 "main" %54
  27. OpExecutionMode %4 OriginUpperLeft
  28. OpSource ESSL 310
  29. OpName %4 "main"
  30. OpName %11 "foo(i1;i1;"
  31. OpName %9 "a"
  32. OpName %10 "b"
  33. OpName %13 "bar("
  34. OpName %17 "baz(i1;"
  35. OpName %16 "x"
  36. OpName %21 "boo(i1;i1;"
  37. OpName %19 "a"
  38. OpName %20 "b"
  39. OpName %29 "g"
  40. OpName %42 "param"
  41. OpName %44 "param"
  42. OpName %45 "param"
  43. OpName %48 "param"
  44. OpName %49 "param"
  45. OpName %54 "color"
  46. OpDecorate %54 Location 0
  47. %2 = OpTypeVoid
  48. %3 = OpTypeFunction %2
  49. %6 = OpTypeInt 32 1
  50. %7 = OpTypePointer Function %6
  51. %8 = OpTypeFunction %6 %7 %7
  52. %15 = OpTypeFunction %6 %7
  53. %28 = OpTypePointer Private %6
  54. %29 = OpVariable %28 Private
  55. %30 = OpConstant %6 2
  56. %31 = OpConstant %6 5
  57. %51 = OpTypeFloat 32
  58. %52 = OpTypeVector %51 4
  59. %53 = OpTypePointer Output %52
  60. %54 = OpVariable %53 Output
  61. %4 = OpFunction %2 None %3
  62. %5 = OpLabel
  63. %42 = OpVariable %7 Function
  64. %44 = OpVariable %7 Function
  65. %45 = OpVariable %7 Function
  66. %48 = OpVariable %7 Function
  67. %49 = OpVariable %7 Function
  68. %41 = OpFunctionCall %2 %13
  69. OpStore %42 %30
  70. %43 = OpFunctionCall %6 %17 %42
  71. OpStore %44 %31
  72. %46 = OpLoad %6 %29
  73. OpStore %45 %46
  74. %47 = OpFunctionCall %6 %21 %44 %45
  75. OpStore %48 %43
  76. OpStore %49 %47
  77. %50 = OpFunctionCall %6 %11 %48 %49
  78. OpReturn
  79. OpFunctionEnd
  80. %11 = OpFunction %6 Const %8
  81. %9 = OpFunctionParameter %7
  82. %10 = OpFunctionParameter %7
  83. %12 = OpLabel
  84. %23 = OpLoad %6 %9
  85. %24 = OpLoad %6 %10
  86. %25 = OpIAdd %6 %23 %24
  87. OpReturnValue %25
  88. OpFunctionEnd
  89. %13 = OpFunction %2 Inline %3
  90. %14 = OpLabel
  91. OpStore %29 %30
  92. OpReturn
  93. OpFunctionEnd
  94. %17 = OpFunction %6 Pure|DontInline %15
  95. %16 = OpFunctionParameter %7
  96. %18 = OpLabel
  97. %32 = OpLoad %6 %16
  98. %33 = OpIAdd %6 %31 %32
  99. OpReturnValue %33
  100. OpFunctionEnd
  101. %21 = OpFunction %6 DontInline %8
  102. %19 = OpFunctionParameter %7
  103. %20 = OpFunctionParameter %7
  104. %22 = OpLabel
  105. %36 = OpLoad %6 %19
  106. %37 = OpLoad %6 %20
  107. %38 = OpIMul %6 %36 %37
  108. OpReturnValue %38
  109. OpFunctionEnd
  110. )";
  111. const auto env = SPV_ENV_UNIVERSAL_1_3;
  112. const auto consumer = nullptr;
  113. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  114. FactManager fact_manager;
  115. // %36 is not a function
  116. ASSERT_FALSE(TransformationSetFunctionControl(36, SpvFunctionControlMaskNone)
  117. .IsApplicable(context.get(), fact_manager));
  118. // Cannot add the Pure function control to %4 as it did not already have it
  119. ASSERT_FALSE(TransformationSetFunctionControl(4, SpvFunctionControlPureMask)
  120. .IsApplicable(context.get(), fact_manager));
  121. // Cannot add the Const function control to %21 as it did not already
  122. // have it
  123. ASSERT_FALSE(TransformationSetFunctionControl(21, SpvFunctionControlConstMask)
  124. .IsApplicable(context.get(), fact_manager));
  125. // Set to None, removing Const
  126. TransformationSetFunctionControl transformation1(11,
  127. SpvFunctionControlMaskNone);
  128. ASSERT_TRUE(transformation1.IsApplicable(context.get(), fact_manager));
  129. transformation1.Apply(context.get(), &fact_manager);
  130. // Set to Inline; silly to do it on an entry point, but it is allowed
  131. TransformationSetFunctionControl transformation2(
  132. 4, SpvFunctionControlInlineMask);
  133. ASSERT_TRUE(transformation2.IsApplicable(context.get(), fact_manager));
  134. transformation2.Apply(context.get(), &fact_manager);
  135. // Set to Pure, removing DontInline
  136. TransformationSetFunctionControl transformation3(17,
  137. SpvFunctionControlPureMask);
  138. ASSERT_TRUE(transformation3.IsApplicable(context.get(), fact_manager));
  139. transformation3.Apply(context.get(), &fact_manager);
  140. // Change from Inline to DontInline
  141. TransformationSetFunctionControl transformation4(
  142. 13, SpvFunctionControlDontInlineMask);
  143. ASSERT_TRUE(transformation4.IsApplicable(context.get(), fact_manager));
  144. transformation4.Apply(context.get(), &fact_manager);
  145. std::string after_transformation = R"(
  146. OpCapability Shader
  147. %1 = OpExtInstImport "GLSL.std.450"
  148. OpMemoryModel Logical GLSL450
  149. OpEntryPoint Fragment %4 "main" %54
  150. OpExecutionMode %4 OriginUpperLeft
  151. OpSource ESSL 310
  152. OpName %4 "main"
  153. OpName %11 "foo(i1;i1;"
  154. OpName %9 "a"
  155. OpName %10 "b"
  156. OpName %13 "bar("
  157. OpName %17 "baz(i1;"
  158. OpName %16 "x"
  159. OpName %21 "boo(i1;i1;"
  160. OpName %19 "a"
  161. OpName %20 "b"
  162. OpName %29 "g"
  163. OpName %42 "param"
  164. OpName %44 "param"
  165. OpName %45 "param"
  166. OpName %48 "param"
  167. OpName %49 "param"
  168. OpName %54 "color"
  169. OpDecorate %54 Location 0
  170. %2 = OpTypeVoid
  171. %3 = OpTypeFunction %2
  172. %6 = OpTypeInt 32 1
  173. %7 = OpTypePointer Function %6
  174. %8 = OpTypeFunction %6 %7 %7
  175. %15 = OpTypeFunction %6 %7
  176. %28 = OpTypePointer Private %6
  177. %29 = OpVariable %28 Private
  178. %30 = OpConstant %6 2
  179. %31 = OpConstant %6 5
  180. %51 = OpTypeFloat 32
  181. %52 = OpTypeVector %51 4
  182. %53 = OpTypePointer Output %52
  183. %54 = OpVariable %53 Output
  184. %4 = OpFunction %2 Inline %3
  185. %5 = OpLabel
  186. %42 = OpVariable %7 Function
  187. %44 = OpVariable %7 Function
  188. %45 = OpVariable %7 Function
  189. %48 = OpVariable %7 Function
  190. %49 = OpVariable %7 Function
  191. %41 = OpFunctionCall %2 %13
  192. OpStore %42 %30
  193. %43 = OpFunctionCall %6 %17 %42
  194. OpStore %44 %31
  195. %46 = OpLoad %6 %29
  196. OpStore %45 %46
  197. %47 = OpFunctionCall %6 %21 %44 %45
  198. OpStore %48 %43
  199. OpStore %49 %47
  200. %50 = OpFunctionCall %6 %11 %48 %49
  201. OpReturn
  202. OpFunctionEnd
  203. %11 = OpFunction %6 None %8
  204. %9 = OpFunctionParameter %7
  205. %10 = OpFunctionParameter %7
  206. %12 = OpLabel
  207. %23 = OpLoad %6 %9
  208. %24 = OpLoad %6 %10
  209. %25 = OpIAdd %6 %23 %24
  210. OpReturnValue %25
  211. OpFunctionEnd
  212. %13 = OpFunction %2 DontInline %3
  213. %14 = OpLabel
  214. OpStore %29 %30
  215. OpReturn
  216. OpFunctionEnd
  217. %17 = OpFunction %6 Pure %15
  218. %16 = OpFunctionParameter %7
  219. %18 = OpLabel
  220. %32 = OpLoad %6 %16
  221. %33 = OpIAdd %6 %31 %32
  222. OpReturnValue %33
  223. OpFunctionEnd
  224. %21 = OpFunction %6 DontInline %8
  225. %19 = OpFunctionParameter %7
  226. %20 = OpFunctionParameter %7
  227. %22 = OpLabel
  228. %36 = OpLoad %6 %19
  229. %37 = OpLoad %6 %20
  230. %38 = OpIMul %6 %36 %37
  231. OpReturnValue %38
  232. OpFunctionEnd
  233. )";
  234. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  235. }
  236. } // namespace
  237. } // namespace fuzz
  238. } // namespace spvtools