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_ADD_COPY_MEMORY_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_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 TransformationAddCopyMemory
: public Transformation
{
28 explicit TransformationAddCopyMemory(
29 protobufs::TransformationAddCopyMemory message
);
31 TransformationAddCopyMemory(
32 const protobufs::InstructionDescriptor
& instruction_descriptor
,
33 uint32_t fresh_id
, uint32_t source_id
, spv::StorageClass storage_class
,
34 uint32_t initializer_id
);
36 // - |instruction_descriptor| must point to a valid instruction in the module.
37 // - it should be possible to insert OpCopyMemory before
38 // |instruction_descriptor| (i.e. the module remains valid after the
40 // - |source_id| must be a result id for some valid instruction in the module.
41 // - |fresh_id| must be a fresh id to copy memory into.
42 // - type of |source_id| must be OpTypePointer where pointee can be used with
44 // - If the pointee type of |source_id| is a struct type, it must not have the
45 // Block or BufferBlock decoration.
46 // - |storage_class| must be either Private or Function.
47 // - type ids of instructions with result ids |source_id| and |initialize_id|
50 opt::IRContext
* ir_context
,
51 const TransformationContext
& transformation_context
) const override
;
53 // A global or local variable with id |target_id| and |storage_class| class is
54 // created. An 'OpCopyMemory %fresh_id %source_id' instruction is inserted
55 // before the |instruction_descriptor|.
56 void Apply(opt::IRContext
* ir_context
,
57 TransformationContext
* transformation_context
) const override
;
59 std::unordered_set
<uint32_t> GetFreshIds() const override
;
61 protobufs::Transformation
ToMessage() const override
;
63 // Returns true if we can copy memory from |instruction| using OpCopyMemory.
64 static bool IsInstructionSupported(opt::IRContext
* ir_context
,
65 opt::Instruction
* inst
);
68 // Returns whether the type, pointed to by some OpTypePointer, can be used
69 // with OpCopyMemory instruction.
70 static bool CanUsePointeeWithCopyMemory(const opt::analysis::Type
& type
);
72 protobufs::TransformationAddCopyMemory message_
;
76 } // namespace spvtools
78 #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_H_