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 15 #ifndef SOURCE_FUZZ_TRANSFORMATION_MUTATE_POINTER_H_ 16 #define SOURCE_FUZZ_TRANSFORMATION_MUTATE_POINTER_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 TransformationMutatePointer : public Transformation { 27 public: 28 explicit TransformationMutatePointer( 29 const protobufs::TransformationMutatePointer& message); 30 31 explicit TransformationMutatePointer( 32 uint32_t pointer_id, uint32_t fresh_id, 33 const protobufs::InstructionDescriptor& insert_before); 34 35 // - |fresh_id| must be fresh. 36 // - |insert_before| must be a valid instruction descriptor of some 37 // instruction in the module. 38 // - It should be possible to insert OpLoad and OpStore before 39 // |insert_before|. 40 // - |pointer_id| must be a result id of some instruction in the module. 41 // - Instruction with result id |pointer_id| must be valid (see 42 // IsValidPointerInstruction method). 43 // - There must exist an irrelevant constant in the module. Type of the 44 // constant must be equal to the type of the |pointer_id|'s pointee. 45 // - |pointer_id| must be available (according to the dominance rules) before 46 // |insert_before|. 47 bool IsApplicable( 48 opt::IRContext* ir_context, 49 const TransformationContext& transformation_context) const override; 50 51 // Inserts the following instructions before |insert_before|: 52 // %fresh_id = OpLoad %pointee_type_id %pointer_id 53 // OpStore %pointer_id %constant_id 54 // OpStore %pointer_id %fresh_id 55 void Apply(opt::IRContext* ir_context, 56 TransformationContext* transformation_context) const override; 57 58 std::unordered_set<uint32_t> GetFreshIds() const override; 59 60 protobufs::Transformation ToMessage() const override; 61 62 // Returns true if |inst| valid pointer according to the following: 63 // - |inst| has result id and type id. 64 // - |inst| is neither OpUndef nor OpConstantNull. 65 // - |inst| has a pointer type. 66 // - |inst|'s storage class is either Private, Function or Workgroup. 67 // - |inst|'s pointee type and all its constituents are either scalar or 68 // composite. 69 static bool IsValidPointerInstruction(opt::IRContext* ir_context, 70 const opt::Instruction& inst); 71 72 private: 73 protobufs::TransformationMutatePointer message_; 74 }; 75 76 } // namespace fuzz 77 } // namespace spvtools 78 79 #endif // SOURCE_FUZZ_TRANSFORMATION_MUTATE_POINTER_H_ 80