1 // Copyright (c) 2019 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_COMPOSITE_EXTRACT_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_EXTRACT_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 TransformationCompositeExtract : public Transformation {
27  public:
28   explicit TransformationCompositeExtract(
29       const protobufs::TransformationCompositeExtract& message);
30 
31   TransformationCompositeExtract(
32       const protobufs::InstructionDescriptor& instruction_to_insert_before,
33       uint32_t fresh_id, uint32_t composite_id,
34       const std::vector<uint32_t>& index);
35 
36   // - |message_.fresh_id| must be available
37   // - |message_.instruction_to_insert_before| must identify an instruction
38   //   before which it is valid to place an OpCompositeExtract
39   // - |message_.composite_id| must be the id of an instruction that defines
40   //   a composite object, and this id must be available at the instruction
41   //   identified by |message_.instruction_to_insert_before|
42   // - |message_.index| must be a suitable set of indices for
43   //   |message_.composite_id|, i.e. it must be possible to follow this chain
44   //   of indices to reach a sub-object of |message_.composite_id|
45   bool IsApplicable(
46       opt::IRContext* ir_context,
47       const TransformationContext& transformation_context) const override;
48 
49   // Adds an OpCompositeConstruct instruction before the instruction identified
50   // by |message_.instruction_to_insert_before|, that extracts from
51   // |message_.composite_id| via indices |message_.index| into
52   // |message_.fresh_id|.
53   //
54   // Adds a synonym fact associating |message_.fresh_id| with the relevant
55   // element of |message_.composite_id|, as long as these ids support synonym
56   // creation.
57   void Apply(opt::IRContext* ir_context,
58              TransformationContext* transformation_context) const override;
59 
60   std::unordered_set<uint32_t> GetFreshIds() const override;
61 
62   protobufs::Transformation ToMessage() const override;
63 
64  private:
65   // Helper method for adding data synonym facts when applying the
66   // transformation to |ir_context| and |transformation_context|.
67   void AddDataSynonymFacts(opt::IRContext* ir_context,
68                            TransformationContext* transformation_context) const;
69 
70   protobufs::TransformationCompositeExtract message_;
71 };
72 
73 }  // namespace fuzz
74 }  // namespace spvtools
75 
76 #endif  // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_EXTRACT_H_
77