1 // Copyright (c) 2020 Vasyl Teliman
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_H_
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"
26 class TransformationMoveInstructionDown
: public Transformation
{
28 explicit TransformationMoveInstructionDown(
29 protobufs::TransformationMoveInstructionDown message
);
31 explicit TransformationMoveInstructionDown(
32 const protobufs::InstructionDescriptor
& instruction
);
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
38 // - |instruction|'s successor may not be dependent on the |instruction|
39 // - it should be possible to insert |instruction|'s opcode after its
42 opt::IRContext
* ir_context
,
43 const TransformationContext
& transformation_context
) const override
;
45 // Swaps |instruction| with its successor.
46 void Apply(opt::IRContext
* ir_context
,
47 TransformationContext
* transformation_context
) const override
;
49 std::unordered_set
<uint32_t> GetFreshIds() const override
;
51 protobufs::Transformation
ToMessage() const override
;
54 // Returns true if the |inst| is supported by this transformation.
55 static bool IsInstructionSupported(opt::IRContext
* ir_context
,
56 const opt::Instruction
& inst
);
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
);
63 // Returns true if |inst| reads from memory.
64 static bool IsMemoryReadInstruction(opt::IRContext
* ir_context
,
65 const opt::Instruction
& inst
);
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
70 static uint32_t GetMemoryReadTarget(opt::IRContext
* ir_context
,
71 const opt::Instruction
& inst
);
73 // Returns true if |inst| that writes to the memory.
74 static bool IsMemoryWriteInstruction(opt::IRContext
* ir_context
,
75 const opt::Instruction
& inst
);
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
);
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
);
88 // Returns true if |inst| is a barrier instruction.
89 static bool IsBarrierInstruction(const opt::Instruction
& inst
);
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
);
101 protobufs::TransformationMoveInstructionDown message_
;
105 } // namespace spvtools
107 #endif // SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_H_