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_ADD_CONSTANT_COMPOSITE_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_CONSTANT_COMPOSITE_H_
17 
18 #include <vector>
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 TransformationAddConstantComposite : public Transformation {
29  public:
30   explicit TransformationAddConstantComposite(
31       const protobufs::TransformationAddConstantComposite& message);
32 
33   TransformationAddConstantComposite(
34       uint32_t fresh_id, uint32_t type_id,
35       const std::vector<uint32_t>& constituent_ids, bool is_irrelevant);
36 
37   // - |message_.fresh_id| must be a fresh id
38   // - |message_.type_id| must be the id of a composite type
39   // - |message_.constituent_id| must refer to ids that match the constituent
40   //   types of this composite type
41   // - If |message_.type_id| is a struct type, it must not have the Block or
42   //   BufferBlock decoration
43   bool IsApplicable(
44       opt::IRContext* ir_context,
45       const TransformationContext& transformation_context) const override;
46 
47   // - Adds an OpConstantComposite instruction defining a constant of type
48   //   |message_.type_id|, using |message_.constituent_id| as constituents, with
49   //   result id |message_.fresh_id|.
50   // - Creates an IdIsIrrelevant fact about |fresh_id| if |is_irrelevant| is
51   //   true.
52   void Apply(opt::IRContext* ir_context,
53              TransformationContext* transformation_context) const override;
54 
55   std::unordered_set<uint32_t> GetFreshIds() const override;
56 
57   protobufs::Transformation ToMessage() const override;
58 
59  private:
60   protobufs::TransformationAddConstantComposite message_;
61 };
62 
63 }  // namespace fuzz
64 }  // namespace spvtools
65 
66 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_CONSTANT_COMPOSITE_H_
67