• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       const std::vector<std::pair<uint32_t, uint32_t>>& fresh_ids_for_clamping =
38           {});
39 
40   // - |message_.fresh_id| must be fresh.
41   // - |message_.instruction_to_insert_before| must identify an instruction
42   //   before which it is legitimate to insert an OpAccessChain instruction.
43   // - |message_.pointer_id| must be a result id with pointer type that is
44   //   available (according to dominance rules) at the insertion point.
45   // - The pointer must not be OpConstantNull or OpUndef.
46   // - |message_.index_id| must be a sequence of ids of 32-bit integers
47   //   such that it is possible to walk the pointee type of
48   //   |message_.pointer_id| using these indices.
49   // - All indices used to access a struct must be OpConstant.
50   // - The indices used to index non-struct composites will be clamped to be
51   //   in bound. Enough fresh ids must be given in
52   //   |message_.fresh_id_for_clamping| to perform clamping (2 for
53   //   each index accessing a non-struct). This requires the bool type and
54   //   a constant of value (bound - 1) to be declared in the module.
55   // - If type t is the final type reached by walking these indices, the module
56   //   must include an instruction "OpTypePointer SC %t" where SC is the storage
57   //   class associated with |message_.pointer_id|.
58   bool IsApplicable(
59       opt::IRContext* ir_context,
60       const TransformationContext& transformation_context) const override;
61 
62   // Adds an instruction of the form:
63   //   |message_.fresh_id| = OpAccessChain %ptr |message_.index_id|
64   // where %ptr is the result if of an instruction declaring a pointer to the
65   // type reached by walking the pointee type of |message_.pointer_id| using
66   // the indices in |message_.index_id|, and with the same storage class as
67   // |message_.pointer_id|.
68   //
69   // For each of the indices traversing non-struct composites, two clamping
70   // instructions are added using ids in |message_.fresh_id_for_clamping|.
71   //
72   // If the fact manager in |transformation_context| reports that
73   // |message_.pointer_id| has an irrelevant pointee value, then the fact that
74   // |message_.fresh_id| (the result of the access chain) also has an irrelevant
75   // pointee value is also recorded.
76   void Apply(opt::IRContext* ir_context,
77              TransformationContext* transformation_context) const override;
78 
79   std::unordered_set<uint32_t> GetFreshIds() const override;
80 
81   protobufs::Transformation ToMessage() const override;
82 
83  private:
84   // Returns {false, 0} in each of the following cases:
85   // - |index_id| does not correspond to a 32-bit integer constant
86   // - the object being indexed is not a composite type
87   // - the constant at |index_id| is out of bounds.
88   // Otherwise, returns {true, value}, where value is the value of the constant
89   // at |index_id|.
90   std::pair<bool, uint32_t> GetIndexValue(opt::IRContext* ir_context,
91                                           uint32_t index_id,
92                                           uint32_t object_type_id) const;
93 
94   // Returns true if |index_id| corresponds, in the given context, to a 32-bit
95   // integer which can be used to index an object of the type specified by
96   // |object_type_id|. Returns false otherwise.
97   static bool ValidIndexToComposite(opt::IRContext* ir_context,
98                                     uint32_t index_id, uint32_t object_type_id);
99 
100   protobufs::TransformationAccessChain message_;
101 };
102 
103 }  // namespace fuzz
104 }  // namespace spvtools
105 
106 #endif  // SOURCE_FUZZ_TRANSFORMATION_ACCESS_CHAIN_H_
107