1 // Copyright (c) 2020 Vasyl Teliman
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_MOVE_INSTRUCTION_DOWN_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_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 TransformationMoveInstructionDown : public Transformation {
27  public:
28   explicit TransformationMoveInstructionDown(
29       const protobufs::TransformationMoveInstructionDown& message);
30 
31   explicit TransformationMoveInstructionDown(
32       const protobufs::InstructionDescriptor& instruction);
33 
34   // - |instruction| should be a descriptor of a valid instruction in the module
35   // - |instruction|'s opcode should be supported by this transformation
36   // - neither |instruction| nor its successor may be the last instruction in
37   //   the block
38   // - |instruction|'s successor may not be dependent on the |instruction|
39   // - it should be possible to insert |instruction|'s opcode after its
40   //   successor
41   bool IsApplicable(
42       opt::IRContext* ir_context,
43       const TransformationContext& transformation_context) const override;
44 
45   // Swaps |instruction| with its successor.
46   void Apply(opt::IRContext* ir_context,
47              TransformationContext* transformation_context) const override;
48 
49   std::unordered_set<uint32_t> GetFreshIds() const override;
50 
51   protobufs::Transformation ToMessage() const override;
52 
53  private:
54   // Returns true if the |inst| is supported by this transformation.
55   static bool IsInstructionSupported(opt::IRContext* ir_context,
56                                      const opt::Instruction& inst);
57 
58   // Returns true if |inst| represents a "simple" instruction. That is, it
59   // neither reads from nor writes to the memory and is not a barrier.
60   static bool IsSimpleInstruction(opt::IRContext* ir_context,
61                                   const opt::Instruction& inst);
62 
63   // Returns true if |inst| reads from memory.
64   static bool IsMemoryReadInstruction(opt::IRContext* ir_context,
65                                       const opt::Instruction& inst);
66 
67   // Returns id being used by |inst| to read from. |inst| must be a memory read
68   // instruction (see IsMemoryReadInstruction). Returned id is not guaranteed to
69   // have pointer type.
70   static uint32_t GetMemoryReadTarget(opt::IRContext* ir_context,
71                                       const opt::Instruction& inst);
72 
73   // Returns true if |inst| that writes to the memory.
74   static bool IsMemoryWriteInstruction(opt::IRContext* ir_context,
75                                        const opt::Instruction& inst);
76 
77   // Returns id being used by |inst| to write into. |inst| must be a memory
78   // write instruction (see IsMemoryWriteInstruction). Returned id is not
79   // guaranteed to have pointer type.
80   static uint32_t GetMemoryWriteTarget(opt::IRContext* ir_context,
81                                        const opt::Instruction& inst);
82 
83   // Returns true if |inst| either reads from or writes to the memory
84   // (see IsMemoryReadInstruction and IsMemoryWriteInstruction accordingly).
85   static bool IsMemoryInstruction(opt::IRContext* ir_context,
86                                   const opt::Instruction& inst);
87 
88   // Returns true if |inst| is a barrier instruction.
89   static bool IsBarrierInstruction(const opt::Instruction& inst);
90 
91   // Returns true if it is possible to swap |a| and |b| without changing the
92   // module's semantics. |a| and |b| are required to be supported instructions
93   // (see IsInstructionSupported). In particular, if either |a| or |b| are
94   // memory or barrier instructions, some checks are used to only say that they
95   // can be swapped if the swap is definitely semantics-preserving.
96   static bool CanSafelySwapInstructions(opt::IRContext* ir_context,
97                                         const opt::Instruction& a,
98                                         const opt::Instruction& b,
99                                         const FactManager& fact_manager);
100 
101   protobufs::TransformationMoveInstructionDown message_;
102 };
103 
104 }  // namespace fuzz
105 }  // namespace spvtools
106 
107 #endif  // SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_H_
108