transformation_permute_phi_operands_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (c) 2020 Vasyl Teliman
  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_permute_phi_operands.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(TransformationPermutePhiOperandsTest, 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 = OpTypePointer Function %6
  33. %9 = OpConstant %6 0
  34. %11 = OpConstant %6 1
  35. %14 = OpTypeBool
  36. %4 = OpFunction %2 None %3
  37. %5 = OpLabel
  38. %8 = OpVariable %7 Function
  39. %10 = OpVariable %7 Function
  40. OpStore %8 %9
  41. OpStore %10 %11
  42. %12 = OpLoad %6 %8
  43. %13 = OpLoad %6 %10
  44. %15 = OpSLessThan %14 %12 %13
  45. OpSelectionMerge %17 None
  46. OpBranchConditional %15 %16 %21
  47. %16 = OpLabel
  48. %18 = OpLoad %6 %10
  49. %19 = OpLoad %6 %8
  50. %20 = OpIAdd %6 %19 %18
  51. OpBranch %17
  52. %21 = OpLabel
  53. %22 = OpLoad %6 %10
  54. %23 = OpLoad %6 %8
  55. %24 = OpISub %6 %23 %22
  56. OpBranch %17
  57. %17 = OpLabel
  58. %25 = OpPhi %6 %20 %16 %24 %21
  59. %30 = OpIAdd %6 %25 %25
  60. OpStore %8 %25
  61. OpReturn
  62. OpFunctionEnd
  63. )";
  64. const auto env = SPV_ENV_UNIVERSAL_1_3;
  65. const auto consumer = nullptr;
  66. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  67. spvtools::ValidatorOptions validator_options;
  68. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  69. kConsoleMessageConsumer));
  70. TransformationContext transformation_context(
  71. MakeUnique<FactManager>(context.get()), validator_options);
  72. // Result id is invalid.
  73. ASSERT_FALSE(TransformationPermutePhiOperands(26, {}).IsApplicable(
  74. context.get(), transformation_context));
  75. // Result id is not of an OpPhi instruction.
  76. ASSERT_FALSE(TransformationPermutePhiOperands(24, {}).IsApplicable(
  77. context.get(), transformation_context));
  78. // Result id is not of an OpPhi instruction.
  79. ASSERT_FALSE(TransformationPermutePhiOperands(24, {}).IsApplicable(
  80. context.get(), transformation_context));
  81. // Permutation has invalid size.
  82. ASSERT_FALSE(TransformationPermutePhiOperands(25, {0, 1, 2})
  83. .IsApplicable(context.get(), transformation_context));
  84. #ifndef NDEBUG
  85. // Permutation has duplicates.
  86. ASSERT_DEATH(TransformationPermutePhiOperands(25, {0, 0})
  87. .IsApplicable(context.get(), transformation_context),
  88. "Permutation has duplicates");
  89. #endif
  90. // Permutation's values are not in range.
  91. ASSERT_FALSE(TransformationPermutePhiOperands(25, {1, 2})
  92. .IsApplicable(context.get(), transformation_context));
  93. TransformationPermutePhiOperands transformation(25, {1, 0});
  94. ASSERT_TRUE(
  95. transformation.IsApplicable(context.get(), transformation_context));
  96. ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
  97. // Check that the def-use manager knows that the phi instruction's ids have
  98. // been permuted.
  99. std::vector<std::pair<uint32_t, uint32_t>> phi_operand_to_new_operand_index =
  100. {{20, 4}, {16, 5}, {24, 2}, {21, 3}};
  101. for (std::pair<uint32_t, uint32_t>& entry :
  102. phi_operand_to_new_operand_index) {
  103. context->get_def_use_mgr()->WhileEachUse(
  104. entry.first,
  105. [&entry](opt::Instruction* inst, uint32_t operand_index) -> bool {
  106. if (inst->result_id() == 25) {
  107. EXPECT_EQ(entry.second, operand_index);
  108. return false;
  109. }
  110. return true;
  111. });
  112. }
  113. bool found_use_in_store = false;
  114. bool found_use_in_add_lhs = false;
  115. bool found_use_in_add_rhs = false;
  116. context->get_def_use_mgr()->ForEachUse(
  117. 25, [&found_use_in_store, &found_use_in_add_lhs, &found_use_in_add_rhs](
  118. opt::Instruction* inst, uint32_t operand_index) {
  119. if (inst->opcode() == spv::Op::OpStore) {
  120. ASSERT_FALSE(found_use_in_store);
  121. found_use_in_store = true;
  122. } else {
  123. ASSERT_EQ(spv::Op::OpIAdd, inst->opcode());
  124. if (operand_index == 2) {
  125. ASSERT_FALSE(found_use_in_add_lhs);
  126. found_use_in_add_lhs = true;
  127. } else {
  128. ASSERT_EQ(3, operand_index);
  129. ASSERT_FALSE(found_use_in_add_rhs);
  130. found_use_in_add_rhs = true;
  131. }
  132. }
  133. });
  134. ASSERT_TRUE(found_use_in_store);
  135. ASSERT_TRUE(found_use_in_add_lhs);
  136. ASSERT_TRUE(found_use_in_add_rhs);
  137. std::string after_transformation = R"(
  138. OpCapability Shader
  139. %1 = OpExtInstImport "GLSL.std.450"
  140. OpMemoryModel Logical GLSL450
  141. OpEntryPoint Fragment %4 "main"
  142. OpExecutionMode %4 OriginUpperLeft
  143. OpSource ESSL 310
  144. %2 = OpTypeVoid
  145. %3 = OpTypeFunction %2
  146. %6 = OpTypeInt 32 1
  147. %7 = OpTypePointer Function %6
  148. %9 = OpConstant %6 0
  149. %11 = OpConstant %6 1
  150. %14 = OpTypeBool
  151. %4 = OpFunction %2 None %3
  152. %5 = OpLabel
  153. %8 = OpVariable %7 Function
  154. %10 = OpVariable %7 Function
  155. OpStore %8 %9
  156. OpStore %10 %11
  157. %12 = OpLoad %6 %8
  158. %13 = OpLoad %6 %10
  159. %15 = OpSLessThan %14 %12 %13
  160. OpSelectionMerge %17 None
  161. OpBranchConditional %15 %16 %21
  162. %16 = OpLabel
  163. %18 = OpLoad %6 %10
  164. %19 = OpLoad %6 %8
  165. %20 = OpIAdd %6 %19 %18
  166. OpBranch %17
  167. %21 = OpLabel
  168. %22 = OpLoad %6 %10
  169. %23 = OpLoad %6 %8
  170. %24 = OpISub %6 %23 %22
  171. OpBranch %17
  172. %17 = OpLabel
  173. %25 = OpPhi %6 %24 %21 %20 %16
  174. %30 = OpIAdd %6 %25 %25
  175. OpStore %8 %25
  176. OpReturn
  177. OpFunctionEnd
  178. )";
  179. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  180. }
  181. } // namespace
  182. } // namespace fuzz
  183. } // namespace spvtools