• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VECTOR_SHUFFLE_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_VECTOR_SHUFFLE_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 #include "source/opt/types.h"
23 
24 namespace spvtools {
25 namespace fuzz {
26 
27 class TransformationVectorShuffle : public Transformation {
28  public:
29   explicit TransformationVectorShuffle(
30       protobufs::TransformationVectorShuffle message);
31 
32   TransformationVectorShuffle(
33       const protobufs::InstructionDescriptor& instruction_to_insert_before,
34       uint32_t fresh_id, uint32_t vector1, uint32_t vector2,
35       const std::vector<uint32_t>& component);
36 
37   // - |message_.fresh_id| must not be in use
38   // - |message_.instruction_to_insert_before| must identify an instruction
39   //   before which it is legitimate to insert an OpVectorShuffle
40   // - |message_.vector1| and |message_.vector2| must be instructions of vector
41   //   type, and the element types of these vectors must be the same
42   // - Each element of |message_.component| must either be 0xFFFFFFFF
43   //   (representing an undefined component), or must be less than the combined
44   //   sizes of the input vectors
45   // - The module must already contain a vector type with the same element type
46   //   as |message_.vector1| and |message_.vector2|, and with the size of
47   //   |message_component| as its element count
48   bool IsApplicable(
49       opt::IRContext* ir_context,
50       const TransformationContext& transformation_context) const override;
51 
52   // Inserts an OpVectorShuffle instruction before
53   // |message_.instruction_to_insert_before|, shuffles vectors
54   // |message_.vector1| and |message_.vector2| using the indices provided by
55   // |message_.component|, into |message_.fresh_id|.
56   //
57   // If |message_.fresh_id| is irrelevant (e.g. due to being in a dead block)
58   // of if one of |message_.vector1| or |message_.vector2| is irrelevant and the
59   // shuffle reads components from the irrelevant vector then no synonym facts
60   // are added.
61   //
62   // Otherwise, a fact is added recording that element of |message_.fresh_id| is
63   // synonymous with the element of |message_.vector1| or |message_.vector2|
64   // from which it came (with undefined components being ignored).
65   void Apply(opt::IRContext* ir_context,
66              TransformationContext* transformation_context) const override;
67 
68   std::unordered_set<uint32_t> GetFreshIds() const override;
69 
70   protobufs::Transformation ToMessage() const override;
71 
72  private:
73   // Returns a type id that already exists in |ir_context| suitable for
74   // representing the result of the shuffle, where |element_type| is known to
75   // be the common element type of the vectors to which the shuffle is being
76   // applied.  Returns 0 if no such id exists.
77   uint32_t GetResultTypeId(opt::IRContext* ir_context,
78                            const opt::analysis::Type& element_type) const;
79 
80   // Returns the type associated with |id_of_vector| in |ir_context|.
81   static opt::analysis::Vector* GetVectorType(opt::IRContext* ir_context,
82                                               uint32_t id_of_vector);
83 
84   // Helper method for adding data synonym facts when applying the
85   // transformation to |ir_context| and |transformation_context|.
86   void AddDataSynonymFacts(opt::IRContext* ir_context,
87                            TransformationContext* transformation_context) const;
88 
89   protobufs::TransformationVectorShuffle message_;
90 };
91 
92 }  // namespace fuzz
93 }  // namespace spvtools
94 
95 #endif  // SOURCE_FUZZ_TRANSFORMATION_VECTOR_SHUFFLE_H_
96