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_TRANSFORMATION_ACCESS_CHAIN_H_ 16 #define SOURCE_FUZZ_TRANSFORMATION_ACCESS_CHAIN_H_ 17 18 #include <utility> 19 20 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h" 21 #include "source/fuzz/transformation.h" 22 #include "source/fuzz/transformation_context.h" 23 #include "source/opt/ir_context.h" 24 25 namespace spvtools { 26 namespace fuzz { 27 28 class TransformationAccessChain : public Transformation { 29 public: 30 explicit TransformationAccessChain( 31 const protobufs::TransformationAccessChain& message); 32 33 TransformationAccessChain( 34 uint32_t fresh_id, uint32_t pointer_id, 35 const std::vector<uint32_t>& index_id, 36 const protobufs::InstructionDescriptor& instruction_to_insert_before); 37 38 // - |message_.fresh_id| must be fresh 39 // - |message_.instruction_to_insert_before| must identify an instruction 40 // before which it is legitimate to insert an OpAccessChain instruction 41 // - |message_.pointer_id| must be a result id with pointer type that is 42 // available (according to dominance rules) at the insertion point. 43 // - The pointer must not be OpConstantNull or OpUndef 44 // - |message_.index_id| must be a sequence of ids of 32-bit integer constants 45 // such that it is possible to walk the pointee type of 46 // |message_.pointer_id| using these indices, remaining in-bounds. 47 // - If type t is the final type reached by walking these indices, the module 48 // must include an instruction "OpTypePointer SC %t" where SC is the storage 49 // class associated with |message_.pointer_id| 50 bool IsApplicable( 51 opt::IRContext* ir_context, 52 const TransformationContext& transformation_context) const override; 53 54 // Adds an instruction of the form: 55 // |message_.fresh_id| = OpAccessChain %ptr |message_.index_id| 56 // where %ptr is the result if of an instruction declaring a pointer to the 57 // type reached by walking the pointee type of |message_.pointer_id| using 58 // the indices in |message_.index_id|, and with the same storage class as 59 // |message_.pointer_id|. 60 // 61 // If the fact manager in |transformation_context| reports that 62 // |message_.pointer_id| has an irrelevant pointee value, then the fact that 63 // |message_.fresh_id| (the result of the access chain) also has an irrelevant 64 // pointee value is also recorded. 65 void Apply(opt::IRContext* ir_context, 66 TransformationContext* transformation_context) const override; 67 68 protobufs::Transformation ToMessage() const override; 69 70 private: 71 // Returns {false, 0} if |index_id| does not correspond to a 32-bit integer 72 // constant. Otherwise, returns {true, value}, where value is the value of 73 // the 32-bit integer constant to which |index_id| corresponds. 74 std::pair<bool, uint32_t> GetIndexValue(opt::IRContext* ir_context, 75 uint32_t index_id) const; 76 77 protobufs::TransformationAccessChain message_; 78 }; 79 80 } // namespace fuzz 81 } // namespace spvtools 82 83 #endif // SOURCE_FUZZ_TRANSFORMATION_ACCESS_CHAIN_H_ 84