transformation_add_local_variable_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_local_variable.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(TransformationAddLocalVariableTest, BasicTest) {
  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. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeInt 32 1
  32. %7 = OpTypeStruct %6 %6
  33. %8 = OpTypePointer Function %7
  34. %10 = OpConstant %6 1
  35. %11 = OpConstant %6 2
  36. %12 = OpConstantComposite %7 %10 %11
  37. %13 = OpTypeFloat 32
  38. %14 = OpTypeInt 32 0
  39. %15 = OpConstant %14 3
  40. %16 = OpTypeArray %13 %15
  41. %17 = OpTypeBool
  42. %18 = OpTypeStruct %16 %7 %17
  43. %19 = OpTypePointer Function %18
  44. %21 = OpConstant %13 1
  45. %22 = OpConstant %13 2
  46. %23 = OpConstant %13 4
  47. %24 = OpConstantComposite %16 %21 %22 %23
  48. %25 = OpConstant %6 5
  49. %26 = OpConstant %6 6
  50. %27 = OpConstantComposite %7 %25 %26
  51. %28 = OpConstantFalse %17
  52. %29 = OpConstantComposite %18 %24 %27 %28
  53. %30 = OpTypeVector %13 2
  54. %31 = OpTypePointer Function %30
  55. %33 = OpConstantComposite %30 %21 %21
  56. %34 = OpTypeVector %17 3
  57. %35 = OpTypePointer Function %34
  58. %37 = OpConstantTrue %17
  59. %38 = OpConstantComposite %34 %37 %28 %28
  60. %39 = OpTypeVector %13 4
  61. %40 = OpTypeMatrix %39 3
  62. %41 = OpTypePointer Function %40
  63. %43 = OpConstantComposite %39 %21 %22 %23 %21
  64. %44 = OpConstantComposite %39 %22 %23 %21 %22
  65. %45 = OpConstantComposite %39 %23 %21 %22 %23
  66. %46 = OpConstantComposite %40 %43 %44 %45
  67. %50 = OpTypePointer Function %14
  68. %51 = OpConstantNull %14
  69. %4 = OpFunction %2 None %3
  70. %5 = OpLabel
  71. OpReturn
  72. OpFunctionEnd
  73. )";
  74. const auto env = SPV_ENV_UNIVERSAL_1_4;
  75. const auto consumer = nullptr;
  76. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  77. spvtools::ValidatorOptions validator_options;
  78. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  79. kConsoleMessageConsumer));
  80. TransformationContext transformation_context(
  81. MakeUnique<FactManager>(context.get()), validator_options);
  82. // A few cases of inapplicable transformations:
  83. // Id 4 is already in use
  84. ASSERT_FALSE(TransformationAddLocalVariable(4, 50, 4, 51, true)
  85. .IsApplicable(context.get(), transformation_context));
  86. // Type mismatch between initializer and pointer
  87. ASSERT_FALSE(TransformationAddLocalVariable(105, 46, 4, 51, true)
  88. .IsApplicable(context.get(), transformation_context));
  89. // Id 5 is not a function
  90. ASSERT_FALSE(TransformationAddLocalVariable(105, 50, 5, 51, true)
  91. .IsApplicable(context.get(), transformation_context));
  92. // %105 = OpVariable %50 Function %51
  93. {
  94. TransformationAddLocalVariable transformation(105, 50, 4, 51, true);
  95. ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(105));
  96. ASSERT_EQ(nullptr, context->get_instr_block(105));
  97. ASSERT_TRUE(
  98. transformation.IsApplicable(context.get(), transformation_context));
  99. ApplyAndCheckFreshIds(transformation, context.get(),
  100. &transformation_context);
  101. ASSERT_EQ(spv::Op::OpVariable,
  102. context->get_def_use_mgr()->GetDef(105)->opcode());
  103. ASSERT_EQ(5, context->get_instr_block(105)->id());
  104. }
  105. // %104 = OpVariable %41 Function %46
  106. {
  107. TransformationAddLocalVariable transformation(104, 41, 4, 46, false);
  108. ASSERT_TRUE(
  109. transformation.IsApplicable(context.get(), transformation_context));
  110. ApplyAndCheckFreshIds(transformation, context.get(),
  111. &transformation_context);
  112. }
  113. // %103 = OpVariable %35 Function %38
  114. {
  115. TransformationAddLocalVariable transformation(103, 35, 4, 38, true);
  116. ASSERT_TRUE(
  117. transformation.IsApplicable(context.get(), transformation_context));
  118. ApplyAndCheckFreshIds(transformation, context.get(),
  119. &transformation_context);
  120. }
  121. // %102 = OpVariable %31 Function %33
  122. {
  123. TransformationAddLocalVariable transformation(102, 31, 4, 33, false);
  124. ASSERT_TRUE(
  125. transformation.IsApplicable(context.get(), transformation_context));
  126. ApplyAndCheckFreshIds(transformation, context.get(),
  127. &transformation_context);
  128. }
  129. // %101 = OpVariable %19 Function %29
  130. {
  131. TransformationAddLocalVariable transformation(101, 19, 4, 29, true);
  132. ASSERT_TRUE(
  133. transformation.IsApplicable(context.get(), transformation_context));
  134. ApplyAndCheckFreshIds(transformation, context.get(),
  135. &transformation_context);
  136. }
  137. // %100 = OpVariable %8 Function %12
  138. {
  139. TransformationAddLocalVariable transformation(100, 8, 4, 12, false);
  140. ASSERT_TRUE(
  141. transformation.IsApplicable(context.get(), transformation_context));
  142. ApplyAndCheckFreshIds(transformation, context.get(),
  143. &transformation_context);
  144. }
  145. ASSERT_FALSE(
  146. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(100));
  147. ASSERT_TRUE(
  148. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(101));
  149. ASSERT_FALSE(
  150. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(102));
  151. ASSERT_TRUE(
  152. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(103));
  153. ASSERT_FALSE(
  154. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(104));
  155. ASSERT_TRUE(
  156. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(105));
  157. std::string after_transformation = R"(
  158. OpCapability Shader
  159. %1 = OpExtInstImport "GLSL.std.450"
  160. OpMemoryModel Logical GLSL450
  161. OpEntryPoint Fragment %4 "main"
  162. OpExecutionMode %4 OriginUpperLeft
  163. OpSource ESSL 310
  164. %2 = OpTypeVoid
  165. %3 = OpTypeFunction %2
  166. %6 = OpTypeInt 32 1
  167. %7 = OpTypeStruct %6 %6
  168. %8 = OpTypePointer Function %7
  169. %10 = OpConstant %6 1
  170. %11 = OpConstant %6 2
  171. %12 = OpConstantComposite %7 %10 %11
  172. %13 = OpTypeFloat 32
  173. %14 = OpTypeInt 32 0
  174. %15 = OpConstant %14 3
  175. %16 = OpTypeArray %13 %15
  176. %17 = OpTypeBool
  177. %18 = OpTypeStruct %16 %7 %17
  178. %19 = OpTypePointer Function %18
  179. %21 = OpConstant %13 1
  180. %22 = OpConstant %13 2
  181. %23 = OpConstant %13 4
  182. %24 = OpConstantComposite %16 %21 %22 %23
  183. %25 = OpConstant %6 5
  184. %26 = OpConstant %6 6
  185. %27 = OpConstantComposite %7 %25 %26
  186. %28 = OpConstantFalse %17
  187. %29 = OpConstantComposite %18 %24 %27 %28
  188. %30 = OpTypeVector %13 2
  189. %31 = OpTypePointer Function %30
  190. %33 = OpConstantComposite %30 %21 %21
  191. %34 = OpTypeVector %17 3
  192. %35 = OpTypePointer Function %34
  193. %37 = OpConstantTrue %17
  194. %38 = OpConstantComposite %34 %37 %28 %28
  195. %39 = OpTypeVector %13 4
  196. %40 = OpTypeMatrix %39 3
  197. %41 = OpTypePointer Function %40
  198. %43 = OpConstantComposite %39 %21 %22 %23 %21
  199. %44 = OpConstantComposite %39 %22 %23 %21 %22
  200. %45 = OpConstantComposite %39 %23 %21 %22 %23
  201. %46 = OpConstantComposite %40 %43 %44 %45
  202. %50 = OpTypePointer Function %14
  203. %51 = OpConstantNull %14
  204. %4 = OpFunction %2 None %3
  205. %5 = OpLabel
  206. %100 = OpVariable %8 Function %12
  207. %101 = OpVariable %19 Function %29
  208. %102 = OpVariable %31 Function %33
  209. %103 = OpVariable %35 Function %38
  210. %104 = OpVariable %41 Function %46
  211. %105 = OpVariable %50 Function %51
  212. OpReturn
  213. OpFunctionEnd
  214. )";
  215. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  216. }
  217. } // namespace
  218. } // namespace fuzz
  219. } // namespace spvtools