Debian Codegraph @

third_party/spirv-tools/source/fuzz/transformation_add_bit_instruction_synonym.cpp

raw
1 // Copyright (c) 2020 André Perez Maselco
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 #include "source/fuzz/transformation_add_bit_instruction_synonym.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
23 TransformationAddBitInstructionSynonym::TransformationAddBitInstructionSynonym(
24 const spvtools::fuzz::protobufs::TransformationAddBitInstructionSynonym&
25 message)
26 : message_(message) {}
27
28 TransformationAddBitInstructionSynonym::TransformationAddBitInstructionSynonym(
29 const uint32_t instruction_result_id,
30 const std::vector<uint32_t>& fresh_ids) {
31 message_.set_instruction_result_id(instruction_result_id);
32 *message_.mutable_fresh_ids() =
33 google::protobuf::RepeatedField<google::protobuf::uint32>(
34 fresh_ids.begin(), fresh_ids.end());
35 }
36
37 bool TransformationAddBitInstructionSynonym::IsApplicable(
38 opt::IRContext* ir_context,
39 const TransformationContext& transformation_context) const {
40 auto instruction =
41 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
42
43 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3557):
44 // Right now we only support certain operations. When this issue is addressed
45 // the following conditional can use the function |spvOpcodeIsBit|.
46 // |instruction| must be defined and must be a supported bit instruction.
47 if (!instruction || (instruction->opcode() != SpvOpBitwiseOr &&
48 instruction->opcode() != SpvOpBitwiseXor &&
49 instruction->opcode() != SpvOpBitwiseAnd &&
50 instruction->opcode() != SpvOpNot)) {
51 return false;
52 }
53
54 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3792):
55 // Right now, only integer operands are supported.
56 if (ir_context->get_type_mgr()->GetType(instruction->type_id())->AsVector()) {
57 return false;
58 }
59
60 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3791):
61 // This condition could be relaxed if the index exists as another integer
62 // type.
63 // All bit indexes must be defined as 32-bit unsigned integers.
64 uint32_t width = ir_context->get_type_mgr()
65 ->GetType(instruction->type_id())
66 ->AsInteger()
67 ->width();
68 for (uint32_t i = 0; i < width; i++) {
69 if (!fuzzerutil::MaybeGetIntegerConstant(ir_context, transformation_context,
70 {i}, 32, false, false)) {
71 return false;
72 }
73 }
74
75 // |message_.fresh_ids.size| must have the exact number of fresh ids required
76 // to apply the transformation.
77 if (static_cast<uint32_t>(message_.fresh_ids().size()) !=
78 GetRequiredFreshIdCount(ir_context, instruction)) {
79 return false;
80 }
81
82 // All ids in |message_.fresh_ids| must be fresh.
83 for (uint32_t fresh_id : message_.fresh_ids()) {
84 if (!fuzzerutil::IsFreshId(ir_context, fresh_id)) {
85 return false;
86 }
87 }
88
89 return true;
90 }
91
92 void TransformationAddBitInstructionSynonym::Apply(
93 opt::IRContext* ir_context,
94 TransformationContext* transformation_context) const {
95 auto bit_instruction =
96 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
97
98 // Use an appropriate helper function to add the new instruction and new
99 // synonym fact. The helper function should take care of invalidating
100 // analyses before adding facts.
101 switch (bit_instruction->opcode()) {
102 case SpvOpBitwiseOr:
103 case SpvOpBitwiseXor:
104 case SpvOpBitwiseAnd:
105 case SpvOpNot:
106 AddOpBitwiseOrOpNotSynonym(ir_context, transformation_context,
107 bit_instruction);
108 break;
109 default:
110 assert(false && "Should be unreachable.");
111 break;
112 }
113 }
114
115 protobufs::Transformation TransformationAddBitInstructionSynonym::ToMessage()
116 const {
117 protobufs::Transformation result;
118 *result.mutable_add_bit_instruction_synonym() = message_;
119 return result;
120 }
121
122 uint32_t TransformationAddBitInstructionSynonym::GetRequiredFreshIdCount(
123 opt::IRContext* ir_context, opt::Instruction* bit_instruction) {
124 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3557):
125 // Right now, only certain operations are supported.
126 switch (bit_instruction->opcode()) {
127 case SpvOpBitwiseOr:
128 case SpvOpBitwiseXor:
129 case SpvOpBitwiseAnd:
130 case SpvOpNot:
131 return (2 + bit_instruction->NumInOperands()) *
132 ir_context->get_type_mgr()
133 ->GetType(bit_instruction->type_id())
134 ->AsInteger()
135 ->width() -
136 1;
137 default:
138 assert(false && "Unsupported bit instruction.");
139 return 0;
140 }
141 }
142
143 void TransformationAddBitInstructionSynonym::AddOpBitwiseOrOpNotSynonym(
144 opt::IRContext* ir_context, TransformationContext* transformation_context,
145 opt::Instruction* bit_instruction) const {
146 // Fresh id iterator.
147 auto fresh_id = message_.fresh_ids().begin();
148
149 // |width| is the bit width of operands (8, 16, 32 or 64).
150 const uint32_t width = ir_context->get_type_mgr()
151 ->GetType(bit_instruction->type_id())
152 ->AsInteger()
153 ->width();
154
155 // |count| is the number of bits to be extracted and inserted at a time.
156 const uint32_t count = fuzzerutil::MaybeGetIntegerConstant(
157 ir_context, *transformation_context, {1}, 32, false, false);
158
159 // |extracted_bit_instructions| is the collection of OpBiwise* or OpNot
160 // instructions that evaluate the extracted bits. Those ids will be used to
161 // insert the result bits.
162 std::vector<uint32_t> extracted_bit_instructions(width);
163
164 for (uint32_t i = 0; i < width; i++) {
165 // |offset| is the current bit index.
166 uint32_t offset = fuzzerutil::MaybeGetIntegerConstant(
167 ir_context, *transformation_context, {i}, 32, false, false);
168
169 // |bit_extract_ids| are the two extracted bits from the operands.
170 opt::Instruction::OperandList bit_extract_ids;
171
172 // Extracts the i-th bit from operands.
173 for (auto operand = bit_instruction->begin() + 2;
174 operand != bit_instruction->end(); operand++) {
175 auto bit_extract =
176 opt::Instruction(ir_context, SpvOpBitFieldUExtract,
177 bit_instruction->type_id(), *fresh_id++,
178 {{SPV_OPERAND_TYPE_ID, operand->words},
179 {SPV_OPERAND_TYPE_ID, {offset}},
180 {SPV_OPERAND_TYPE_ID, {count}}});
181 bit_instruction->InsertBefore(MakeUnique<opt::Instruction>(bit_extract));
182 fuzzerutil::UpdateModuleIdBound(ir_context, bit_extract.result_id());
183 bit_extract_ids.push_back(
184 {SPV_OPERAND_TYPE_ID, {bit_extract.result_id()}});
185 }
186
187 // Applies |bit_instruction| to the extracted bits.
188 auto extracted_bit_instruction = opt::Instruction(
189 ir_context, bit_instruction->opcode(), bit_instruction->type_id(),
190 *fresh_id++, bit_extract_ids);
191 bit_instruction->InsertBefore(
192 MakeUnique<opt::Instruction>(extracted_bit_instruction));
193 fuzzerutil::UpdateModuleIdBound(ir_context,
194 extracted_bit_instruction.result_id());
195 extracted_bit_instructions[i] = extracted_bit_instruction.result_id();
196 }
197
198 // The first two ids in |extracted_bit_instructions| are used to insert the
199 // first two bits of the result.
200 uint32_t offset = fuzzerutil::MaybeGetIntegerConstant(
201 ir_context, *transformation_context, {1}, 32, false, false);
202 auto bit_insert = opt::Instruction(
203 ir_context, SpvOpBitFieldInsert, bit_instruction->type_id(), *fresh_id++,
204 {{SPV_OPERAND_TYPE_ID, {extracted_bit_instructions[0]}},
205 {SPV_OPERAND_TYPE_ID, {extracted_bit_instructions[1]}},
206 {SPV_OPERAND_TYPE_ID, {offset}},
207 {SPV_OPERAND_TYPE_ID, {count}}});
208 bit_instruction->InsertBefore(MakeUnique<opt::Instruction>(bit_insert));
209 fuzzerutil::UpdateModuleIdBound(ir_context, bit_insert.result_id());
210
211 // Inserts the remaining bits.
212 for (uint32_t i = 2; i < width; i++) {
213 offset = fuzzerutil::MaybeGetIntegerConstant(
214 ir_context, *transformation_context, {i}, 32, false, false);
215 bit_insert = opt::Instruction(
216 ir_context, SpvOpBitFieldInsert, bit_instruction->type_id(),
217 *fresh_id++,
218 {{SPV_OPERAND_TYPE_ID, {bit_insert.result_id()}},
219 {SPV_OPERAND_TYPE_ID, {extracted_bit_instructions[i]}},
220 {SPV_OPERAND_TYPE_ID, {offset}},
221 {SPV_OPERAND_TYPE_ID, {count}}});
222 bit_instruction->InsertBefore(MakeUnique<opt::Instruction>(bit_insert));
223 fuzzerutil::UpdateModuleIdBound(ir_context, bit_insert.result_id());
224 }
225
226 ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
227
228 // We only add a synonym fact if the bit instruction is not irrelevant, and if
229 // the new result id we would make it synonymous with is not irrelevant. (It
230 // could be irrelevant if we are in a dead block.)
231 if (!transformation_context->GetFactManager()->IdIsIrrelevant(
232 bit_instruction->result_id()) &&
233 !transformation_context->GetFactManager()->IdIsIrrelevant(
234 bit_insert.result_id())) {
235 // Adds the fact that the last |bit_insert| instruction is synonymous of
236 // |bit_instruction|.
237 transformation_context->GetFactManager()->AddFactDataSynonym(
238 MakeDataDescriptor(bit_insert.result_id(), {}),
239 MakeDataDescriptor(bit_instruction->result_id(), {}));
240 }
241 }
242
243 std::unordered_set<uint32_t>
244 TransformationAddBitInstructionSynonym::GetFreshIds() const {
245 std::unordered_set<uint32_t> result;
246 for (auto id : message_.fresh_ids()) {
247 result.insert(id);
248 }
249 return result;
250 }
251
252 } // namespace fuzz
253 } // namespace spvtools