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 15 #ifndef SOURCE_FUZZ_FUZZER_PASS_ADD_EQUATION_INSTRUCTIONS_H_ 16 #define SOURCE_FUZZ_FUZZER_PASS_ADD_EQUATION_INSTRUCTIONS_H_ 17 18 #include <vector> 19 20 #include "source/fuzz/fuzzer_pass.h" 21 22 namespace spvtools { 23 namespace fuzz { 24 25 // Fuzzer pass that sprinkles instructions through the module that define 26 // equations using various arithmetic and logical operators. 27 class FuzzerPassAddEquationInstructions : public FuzzerPass { 28 public: 29 FuzzerPassAddEquationInstructions( 30 opt::IRContext* ir_context, TransformationContext* transformation_context, 31 FuzzerContext* fuzzer_context, 32 protobufs::TransformationSequence* transformations); 33 34 void Apply() override; 35 36 private: 37 // Yields those instructions in |instructions| that have integer scalar or 38 // vector result type. 39 std::vector<opt::Instruction*> GetIntegerInstructions( 40 const std::vector<opt::Instruction*>& instructions) const; 41 42 // Returns only instructions, that have either a scalar floating-point or a 43 // vector type. 44 std::vector<opt::Instruction*> GetFloatInstructions( 45 const std::vector<opt::Instruction*>& instructions) const; 46 47 // Yields those instructions in |instructions| that have boolean scalar or 48 // vector result type. 49 std::vector<opt::Instruction*> GetBooleanInstructions( 50 const std::vector<opt::Instruction*>& instructions) const; 51 52 // Yields those instructions in |instructions| that have a scalar numerical or 53 // a vector of numerical components type. Only 16, 32 and 64-bit numericals 54 // are supported if both OpTypeInt and OpTypeFloat instructions can be created 55 // with the specified width (e.g. for 16-bit types both Float16 and Int16 56 // capabilities must be present). 57 std::vector<opt::Instruction*> GetNumericalInstructions( 58 const std::vector<opt::Instruction*>& instructions) const; 59 60 // Requires that |instructions| are scalars or vectors of some type. Returns 61 // only those instructions whose width is |width|. If |width| is 1 this means 62 // the scalars. 63 std::vector<opt::Instruction*> RestrictToVectorWidth( 64 const std::vector<opt::Instruction*>& instructions, 65 uint32_t vector_width) const; 66 67 // Requires that |instructions| are integer or float scalars or vectors. 68 // Returns only those instructions for which the bit-width of the underlying 69 // integer or floating-point type is |bit_width|. 70 std::vector<opt::Instruction*> RestrictToElementBitWidth( 71 const std::vector<opt::Instruction*>& instructions, 72 uint32_t bit_width) const; 73 }; 74 75 } // namespace fuzz 76 } // namespace spvtools 77 78 #endif // SOURCE_FUZZ_FUZZER_PASS_ADD_EQUATION_INSTRUCTIONS_H_ 79