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 15 #ifndef SOURCE_FUZZ_TRANSFORMATION_WRAP_VECTOR_SYNONYM_H_ 16 #define SOURCE_FUZZ_TRANSFORMATION_WRAP_VECTOR_SYNONYM_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 class TransformationWrapVectorSynonym : public Transformation { 27 public: 28 explicit TransformationWrapVectorSynonym( 29 protobufs::TransformationWrapVectorSynonym message); 30 31 TransformationWrapVectorSynonym(uint32_t instruction_id, 32 uint32_t vector_operand1, 33 uint32_t vector_operand2, uint32_t fresh_id, 34 uint32_t pos); 35 // - |instruction_id| must be the id of a supported arithmetic operation 36 // and must be relevant. 37 // - |vector_operand1| and |vector_operand2| represents the result ids of the 38 // two vector operands. 39 // - |fresh_id| is an unused id that will be used as a result id of the 40 // created instruction. 41 // - |vector_operand1| and |vector_operand2| must have compatible vector types 42 // that are supported by this transformation. 43 // - |pos| is an index of the operands of |instruction_id| in the 44 // |vector_operand1| and |vector_operand2|. It must be less than the size 45 // of those vector operands. 46 // - A vector type with the same width as the types of the vector operands, 47 // and element type matching the type of |instruction_id|, must exist in the 48 // module. 49 bool IsApplicable( 50 opt::IRContext* ir_context, 51 const TransformationContext& transformation_context) const override; 52 53 // Adds a new instruction before the |instruction_id| with |fresh_id| 54 // result id and |instruction_id|'s opcode. The added instruction has 55 // two operands: |vector_operand1| and |vector_operand2| and its type 56 // id is equal to the type ids of those operands. A new fact is added 57 // to the fact manager specifying that |fresh_id[pos]| is synonymous 58 // to |instruction_id|. 59 void Apply(opt::IRContext* ir_context, 60 TransformationContext* transformation_context) const override; 61 62 std::unordered_set<uint32_t> GetFreshIds() const override; 63 protobufs::Transformation ToMessage() const override; 64 65 // Checks whether the instruction given is supported by the transformation. 66 // A valid instruction must: 67 // - has both result id and type id. 68 // - is a supported scalar operation instruction. 69 // - has a supported type that is either int or float. 70 static bool IsInstructionSupported(opt::IRContext* ir_context, 71 const opt::Instruction& instruction); 72 73 private: 74 protobufs::TransformationWrapVectorSynonym message_; 75 }; 76 77 } // namespace fuzz 78 } // namespace spvtools 79 80 #endif // SOURCE_FUZZ_TRANSFORMATION_WRAP_VECTOR_SYNONYM_H_ 81