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