• 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_SET_MEMORY_OPERANDS_MASK_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_SET_MEMORY_OPERANDS_MASK_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 TransformationSetMemoryOperandsMask : public Transformation {
27  public:
28   explicit TransformationSetMemoryOperandsMask(
29       protobufs::TransformationSetMemoryOperandsMask message);
30 
31   TransformationSetMemoryOperandsMask(
32       const protobufs::InstructionDescriptor& memory_access_instruction,
33       uint32_t memory_operands_mask, uint32_t memory_operands_mask_index);
34 
35   // - |message_.memory_access_instruction| must describe a memory access
36   //   instruction.
37   // - |message_.memory_operands_mask_index| must be suitable for this memory
38   //   access instruction, e.g. it must be 0 in the case of OpLoad, and may be
39   //   1 in the case of OpCopyMemory if the SPIR-V version is 1.4 or higher.
40   // - |message_.memory_operands_mask| must be identical to the original memory
41   //   operands mask, except that Volatile may be added, and Nontemporal may be
42   //   toggled.
43   bool IsApplicable(
44       opt::IRContext* ir_context,
45       const TransformationContext& transformation_context) const override;
46 
47   // Replaces the operands mask identified by
48   // |message_.memory_operands_mask_index| in the instruction described by
49   // |message_.memory_access_instruction| with |message_.memory_operands_mask|,
50   // creating an input operand for the mask if no such operand was present.
51   void Apply(opt::IRContext* ir_context,
52              TransformationContext* transformation_context) const override;
53 
54   std::unordered_set<uint32_t> GetFreshIds() const override;
55 
56   protobufs::Transformation ToMessage() const override;
57 
58   // Helper function that determines whether |instruction| is a memory
59   // instruction (e.g. OpLoad).
60   static bool IsMemoryAccess(const opt::Instruction& instruction);
61 
62   // Does the version of SPIR-V being used support multiple memory operand
63   // masks on relevant memory access instructions?
64   static bool MultipleMemoryOperandMasksAreSupported(
65       opt::IRContext* ir_context);
66 
67   // Helper function to get the input operand index associated with mask number
68   // |mask_index|. This is a bit tricky if there are multiple masks, because the
69   // index associated with the second mask depends on whether the first mask
70   // includes any flags such as Aligned that have corresponding operands.
71   static uint32_t GetInOperandIndexForMask(const opt::Instruction& instruction,
72                                            uint32_t mask_index);
73 
74  private:
75   protobufs::TransformationSetMemoryOperandsMask message_;
76 };
77 
78 }  // namespace fuzz
79 }  // namespace spvtools
80 
81 #endif  // SOURCE_FUZZ_TRANSFORMATION_SET_MEMORY_OPERANDS_MASK_H_
82