2
0

transformation_swap_two_functions_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright (c) 2021 Shiyu Liu
  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_swap_two_functions.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(TransformationSwapTwoFunctionsTest, SimpleTest) {
  22. // float multiplyBy2(in float value) {
  23. // return value*2.0;
  24. // }
  25. // float multiplyBy4(in float value) {
  26. // return multiplyBy2(value)*2.0;
  27. // }
  28. // float multiplyBy8(in float value) {
  29. // return multiplyBy2(value)*multiplyBy4(value);
  30. // }
  31. // layout(location=0) in float value;
  32. // void main() { //4
  33. // multiplyBy2(3.7); //10
  34. // multiplyBy4(3.9); //13
  35. // multiplyBy8(5.0); //16
  36. // }
  37. std::string shader = R"(
  38. OpCapability Shader
  39. %1 = OpExtInstImport "GLSL.std.450"
  40. OpMemoryModel Logical GLSL450
  41. OpEntryPoint Fragment %4 "main" %48
  42. OpExecutionMode %4 OriginUpperLeft
  43. OpSource ESSL 310
  44. OpName %4 "main"
  45. OpName %10 "multiplyBy2(f1;"
  46. OpName %9 "value"
  47. OpName %13 "multiplyBy4(f1;"
  48. OpName %12 "value"
  49. OpName %16 "multiplyBy8(f1;"
  50. OpName %15 "value"
  51. OpName %23 "param"
  52. OpName %29 "param"
  53. OpName %32 "param"
  54. OpName %39 "param"
  55. OpName %42 "param"
  56. OpName %45 "param"
  57. OpName %48 "value"
  58. OpDecorate %48 Location 0
  59. %2 = OpTypeVoid
  60. %3 = OpTypeFunction %2
  61. %6 = OpTypeFloat 32
  62. %7 = OpTypePointer Function %6
  63. %8 = OpTypeFunction %6 %7
  64. %19 = OpConstant %6 2
  65. %38 = OpConstant %6 3.70000005
  66. %41 = OpConstant %6 3.9000001
  67. %44 = OpConstant %6 5
  68. %47 = OpTypePointer Input %6
  69. %48 = OpVariable %47 Input
  70. %4 = OpFunction %2 None %3
  71. %5 = OpLabel
  72. %39 = OpVariable %7 Function
  73. %42 = OpVariable %7 Function
  74. %45 = OpVariable %7 Function
  75. OpStore %39 %38
  76. %40 = OpFunctionCall %6 %10 %39
  77. OpStore %42 %41
  78. %43 = OpFunctionCall %6 %13 %42
  79. OpStore %45 %44
  80. %46 = OpFunctionCall %6 %16 %45
  81. OpReturn
  82. OpFunctionEnd
  83. %10 = OpFunction %6 None %8
  84. %9 = OpFunctionParameter %7
  85. %11 = OpLabel
  86. %18 = OpLoad %6 %9
  87. %20 = OpFMul %6 %18 %19
  88. OpReturnValue %20
  89. OpFunctionEnd
  90. %13 = OpFunction %6 None %8
  91. %12 = OpFunctionParameter %7
  92. %14 = OpLabel
  93. %23 = OpVariable %7 Function
  94. %24 = OpLoad %6 %12
  95. OpStore %23 %24
  96. %25 = OpFunctionCall %6 %10 %23
  97. %26 = OpFMul %6 %25 %19
  98. OpReturnValue %26
  99. OpFunctionEnd
  100. %16 = OpFunction %6 None %8
  101. %15 = OpFunctionParameter %7
  102. %17 = OpLabel
  103. %29 = OpVariable %7 Function
  104. %32 = OpVariable %7 Function
  105. %30 = OpLoad %6 %15
  106. OpStore %29 %30
  107. %31 = OpFunctionCall %6 %10 %29
  108. %33 = OpLoad %6 %15
  109. OpStore %32 %33
  110. %34 = OpFunctionCall %6 %13 %32
  111. %35 = OpFMul %6 %31 %34
  112. OpReturnValue %35
  113. OpFunctionEnd
  114. )";
  115. const auto env = SPV_ENV_UNIVERSAL_1_3;
  116. const auto consumer = nullptr;
  117. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  118. spvtools::ValidatorOptions validator_options;
  119. // Check context validity.
  120. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  121. kConsoleMessageConsumer));
  122. TransformationContext transformation_context(
  123. MakeUnique<FactManager>(context.get()), validator_options);
  124. #ifndef NDEBUG
  125. // Function should not swap with itself.
  126. ASSERT_DEATH(TransformationSwapTwoFunctions(4, 4).IsApplicable(
  127. context.get(), transformation_context),
  128. "The two function ids cannot be the same.");
  129. #endif
  130. // Function with id 29 does not exist.
  131. ASSERT_FALSE(TransformationSwapTwoFunctions(10, 29).IsApplicable(
  132. context.get(), transformation_context));
  133. // Function with id 30 does not exist.
  134. ASSERT_FALSE(TransformationSwapTwoFunctions(30, 13).IsApplicable(
  135. context.get(), transformation_context));
  136. // Both functions with id 5 and 6 do not exist.
  137. ASSERT_FALSE(TransformationSwapTwoFunctions(5, 6).IsApplicable(
  138. context.get(), transformation_context));
  139. // Function with result_id 10 and 13 should swap successfully.
  140. auto swap_test5 = TransformationSwapTwoFunctions(10, 13);
  141. ASSERT_TRUE(swap_test5.IsApplicable(context.get(), transformation_context));
  142. // Get the definitions of functions 10 and 13, as recorded by the def-use
  143. // manager.
  144. auto def_use_manager = context->get_def_use_mgr();
  145. auto function_10_inst = def_use_manager->GetDef(10);
  146. auto function_13_inst = def_use_manager->GetDef(13);
  147. ApplyAndCheckFreshIds(swap_test5, context.get(), &transformation_context);
  148. // Check that def-use information for functions 10 and 13 has been preserved
  149. // by the transformation.
  150. ASSERT_EQ(function_10_inst, def_use_manager->GetDef(10));
  151. ASSERT_EQ(function_13_inst, def_use_manager->GetDef(13));
  152. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  153. kConsoleMessageConsumer));
  154. std::string after_transformation = R"(
  155. OpCapability Shader
  156. %1 = OpExtInstImport "GLSL.std.450"
  157. OpMemoryModel Logical GLSL450
  158. OpEntryPoint Fragment %4 "main" %48
  159. OpExecutionMode %4 OriginUpperLeft
  160. OpSource ESSL 310
  161. OpName %4 "main"
  162. OpName %10 "multiplyBy2(f1;"
  163. OpName %9 "value"
  164. OpName %13 "multiplyBy4(f1;"
  165. OpName %12 "value"
  166. OpName %16 "multiplyBy8(f1;"
  167. OpName %15 "value"
  168. OpName %23 "param"
  169. OpName %29 "param"
  170. OpName %32 "param"
  171. OpName %39 "param"
  172. OpName %42 "param"
  173. OpName %45 "param"
  174. OpName %48 "value"
  175. OpDecorate %48 Location 0
  176. %2 = OpTypeVoid
  177. %3 = OpTypeFunction %2
  178. %6 = OpTypeFloat 32
  179. %7 = OpTypePointer Function %6
  180. %8 = OpTypeFunction %6 %7
  181. %19 = OpConstant %6 2
  182. %38 = OpConstant %6 3.70000005
  183. %41 = OpConstant %6 3.9000001
  184. %44 = OpConstant %6 5
  185. %47 = OpTypePointer Input %6
  186. %48 = OpVariable %47 Input
  187. %4 = OpFunction %2 None %3
  188. %5 = OpLabel
  189. %39 = OpVariable %7 Function
  190. %42 = OpVariable %7 Function
  191. %45 = OpVariable %7 Function
  192. OpStore %39 %38
  193. %40 = OpFunctionCall %6 %10 %39
  194. OpStore %42 %41
  195. %43 = OpFunctionCall %6 %13 %42
  196. OpStore %45 %44
  197. %46 = OpFunctionCall %6 %16 %45
  198. OpReturn
  199. OpFunctionEnd
  200. %13 = OpFunction %6 None %8
  201. %12 = OpFunctionParameter %7
  202. %14 = OpLabel
  203. %23 = OpVariable %7 Function
  204. %24 = OpLoad %6 %12
  205. OpStore %23 %24
  206. %25 = OpFunctionCall %6 %10 %23
  207. %26 = OpFMul %6 %25 %19
  208. OpReturnValue %26
  209. OpFunctionEnd
  210. %10 = OpFunction %6 None %8
  211. %9 = OpFunctionParameter %7
  212. %11 = OpLabel
  213. %18 = OpLoad %6 %9
  214. %20 = OpFMul %6 %18 %19
  215. OpReturnValue %20
  216. OpFunctionEnd
  217. %16 = OpFunction %6 None %8
  218. %15 = OpFunctionParameter %7
  219. %17 = OpLabel
  220. %29 = OpVariable %7 Function
  221. %32 = OpVariable %7 Function
  222. %30 = OpLoad %6 %15
  223. OpStore %29 %30
  224. %31 = OpFunctionCall %6 %10 %29
  225. %33 = OpLoad %6 %15
  226. OpStore %32 %33
  227. %34 = OpFunctionCall %6 %13 %32
  228. %35 = OpFMul %6 %31 %34
  229. OpReturnValue %35
  230. OpFunctionEnd
  231. )";
  232. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  233. }
  234. } // namespace
  235. } // namespace fuzz
  236. } // namespace spvtools