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_ADD_COPY_MEMORY_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_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 TransformationAddCopyMemory : public Transformation {
27  public:
28   explicit TransformationAddCopyMemory(
29       const protobufs::TransformationAddCopyMemory& message);
30 
31   TransformationAddCopyMemory(
32       const protobufs::InstructionDescriptor& instruction_descriptor,
33       uint32_t fresh_id, uint32_t source_id, SpvStorageClass storage_class,
34       uint32_t initializer_id);
35 
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
39   //   insertion).
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
43   //   OpCopyMemory.
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|
48   //   must be the same.
49   bool IsApplicable(
50       opt::IRContext* ir_context,
51       const TransformationContext& transformation_context) const override;
52 
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;
58 
59   std::unordered_set<uint32_t> GetFreshIds() const override;
60 
61   protobufs::Transformation ToMessage() const override;
62 
63   // Returns true if we can copy memory from |instruction| using OpCopyMemory.
64   static bool IsInstructionSupported(opt::IRContext* ir_context,
65                                      opt::Instruction* inst);
66 
67  private:
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);
71 
72   protobufs::TransformationAddCopyMemory message_;
73 };
74 
75 }  // namespace fuzz
76 }  // namespace spvtools
77 
78 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_H_
79