transformation_expand_vector_reduction.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2020 André Perez Maselco
  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. #ifndef SOURCE_FUZZ_TRANSFORMATION_EXPAND_VECTOR_REDUCTION_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_EXPAND_VECTOR_REDUCTION_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. // clang-format off
  23. // SPIR-V code to help understand the transformation.
  24. //
  25. // -------------------------------------------------------------------------------
  26. // | Reference shader | Variant shader |
  27. // -------------------------------------------------------------------------------
  28. // | OpCapability Shader | OpCapability Shader |
  29. // | %1 = OpExtInstImport "GLSL.std.450" | %1 = OpExtInstImport "GLSL.std.450" |
  30. // | OpMemoryModel Logical GLSL450 | OpMemoryModel Logical GLSL450 |
  31. // | OpEntryPoint Vertex %9 "main" | OpEntryPoint Vertex %9 "main" |
  32. // | | |
  33. // | ; Types | ; Types |
  34. // | %2 = OpTypeBool | %2 = OpTypeBool |
  35. // | %3 = OpTypeVector %2 2 | %3 = OpTypeVector %2 2 |
  36. // | %4 = OpTypeVoid | %4 = OpTypeVoid |
  37. // | %5 = OpTypeFunction %4 | %5 = OpTypeFunction %4 |
  38. // | | |
  39. // | ; Constants | ; Constants |
  40. // | %6 = OpConstantTrue %2 | %6 = OpConstantTrue %2 |
  41. // | %7 = OpConstantFalse %2 | %7 = OpConstantFalse %2 |
  42. // | %8 = OpConstantComposite %3 %6 %7 | %8 = OpConstantComposite %3 %6 %7 |
  43. // | | |
  44. // | ; main function | ; main function |
  45. // | %9 = OpFunction %4 None %5 | %9 = OpFunction %4 None %5 |
  46. // | %10 = OpLabel | %10 = OpLabel |
  47. // | %11 = OpAny %2 %8 | |
  48. // | %12 = OpAll %2 %8 | ; Add OpAny synonym |
  49. // | OpReturn | %13 = OpCompositeExtract %2 %8 0 |
  50. // | OpFunctionEnd | %14 = OpCompositeExtract %2 %8 1 |
  51. // | | %15 = OpLogicalOr %2 %13 %14 |
  52. // | | %11 = OpAny %2 %8 |
  53. // | | |
  54. // | | ; Add OpAll synonym |
  55. // | | %16 = OpCompositeExtract %2 %8 0 |
  56. // | | %17 = OpCompositeExtract %2 %8 1 |
  57. // | | %18 = OpLogicalAnd %2 %16 %17 |
  58. // | | %12 = OpAll %2 %8 |
  59. // | | OpReturn |
  60. // | | OpFunctionEnd |
  61. // -------------------------------------------------------------------------------
  62. //
  63. // %11 and %15 are synonymous
  64. // %12 and %18 are synonymous
  65. // clang-format on
  66. class TransformationExpandVectorReduction : public Transformation {
  67. public:
  68. explicit TransformationExpandVectorReduction(
  69. protobufs::TransformationExpandVectorReduction message);
  70. TransformationExpandVectorReduction(const uint32_t instruction_result_id,
  71. const std::vector<uint32_t>& fresh_ids);
  72. // - |message_.instruction_result_id| must be OpAny or OpAll.
  73. // - |message_.fresh_ids| must be fresh ids needed to apply the
  74. // transformation.
  75. bool IsApplicable(
  76. opt::IRContext* ir_context,
  77. const TransformationContext& transformation_context) const override;
  78. // Adds synonyms for OpAny and OpAll instructions by evaluating each vector
  79. // component with the corresponding logical operation.
  80. void Apply(opt::IRContext* ir_context,
  81. TransformationContext* transformation_context) const override;
  82. std::unordered_set<uint32_t> GetFreshIds() const override;
  83. protobufs::Transformation ToMessage() const override;
  84. // Returns the number of fresh ids required to apply the transformation.
  85. static uint32_t GetRequiredFreshIdCount(opt::IRContext* ir_context,
  86. opt::Instruction* instruction);
  87. private:
  88. protobufs::TransformationExpandVectorReduction message_;
  89. };
  90. } // namespace fuzz
  91. } // namespace spvtools
  92. #endif // SOURCE_FUZZ_TRANSFORMATION_EXPAND_VECTOR_REDUCTION_H_