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_FUZZER_UTIL_H_
16 #define SOURCE_FUZZ_FUZZER_UTIL_H_
17
18 #include <iostream>
19 #include <map>
20 #include <vector>
21
22 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
23 #include "source/fuzz/transformation_context.h"
24 #include "source/opt/basic_block.h"
25 #include "source/opt/instruction.h"
26 #include "source/opt/ir_context.h"
27 #include "spirv-tools/libspirv.hpp"
28
29 namespace spvtools {
30 namespace fuzz {
31
32 // Provides types and global utility methods for use by the fuzzer
33 namespace fuzzerutil {
34
35 // A silent message consumer.
36 extern const spvtools::MessageConsumer kSilentMessageConsumer;
37
38 // Function type that produces a SPIR-V module.
39 using ModuleSupplier = std::function<std::unique_ptr<opt::IRContext>()>;
40
41 // Builds a new opt::IRContext object. Returns true if successful and changes
42 // the |ir_context| parameter. Otherwise (if any errors occur), returns false
43 // and |ir_context| remains unchanged.
44 bool BuildIRContext(spv_target_env target_env,
45 const spvtools::MessageConsumer& message_consumer,
46 const std::vector<uint32_t>& binary_in,
47 spv_validator_options validator_options,
48 std::unique_ptr<spvtools::opt::IRContext>* ir_context);
49
50 // Returns true if and only if the module does not define the given id.
51 bool IsFreshId(opt::IRContext* context, uint32_t id);
52
53 // Updates the module's id bound if needed so that it is large enough to
54 // account for the given id.
55 void UpdateModuleIdBound(opt::IRContext* context, uint32_t id);
56
57 // Return the block with id |maybe_block_id| if it exists, and nullptr
58 // otherwise.
59 opt::BasicBlock* MaybeFindBlock(opt::IRContext* context,
60 uint32_t maybe_block_id);
61
62 // When adding an edge from |bb_from| to |bb_to| (which are assumed to be blocks
63 // in the same function), it is important to supply |bb_to| with ids that can be
64 // used to augment OpPhi instructions in the case that there is not already such
65 // an edge. This function returns true if and only if the ids provided in
66 // |phi_ids| suffice for this purpose,
67 bool PhiIdsOkForNewEdge(
68 opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to,
69 const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids);
70
71 // Returns an OpBranchConditional instruction that will create an unreachable
72 // branch from |bb_from_id| to |bb_to_id|. |bool_id| must be a result id of
73 // either OpConstantTrue or OpConstantFalse. Based on the opcode of |bool_id|,
74 // operands of the returned instruction will be positioned in a way that the
75 // branch from |bb_from_id| to |bb_to_id| is always unreachable.
76 opt::Instruction CreateUnreachableEdgeInstruction(opt::IRContext* ir_context,
77 uint32_t bb_from_id,
78 uint32_t bb_to_id,
79 uint32_t bool_id);
80
81 // Requires that |bool_id| is a valid result id of either OpConstantTrue or
82 // OpConstantFalse, that PhiIdsOkForNewEdge(context, bb_from, bb_to, phi_ids)
83 // holds, and that bb_from ends with "OpBranch %some_block". Turns OpBranch
84 // into "OpBranchConditional |condition_value| ...", such that control will
85 // branch to %some_block, with |bb_to| being the unreachable alternative.
86 // Updates OpPhi instructions in |bb_to| using |phi_ids| so that the new edge is
87 // valid. |condition_value| above is equal to |true| if |bool_id| is a result id
88 // of an OpConstantTrue instruction.
89 void AddUnreachableEdgeAndUpdateOpPhis(
90 opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to,
91 uint32_t bool_id,
92 const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids);
93
94 // Returns true if and only if |loop_header_id| is a loop header and
95 // |block_id| is a reachable block branching to and dominated by
96 // |loop_header_id|.
97 bool BlockIsBackEdge(opt::IRContext* context, uint32_t block_id,
98 uint32_t loop_header_id);
99
100 // Returns true if and only if |maybe_loop_header_id| is a loop header and
101 // |block_id| is in the continue construct of the associated loop.
102 bool BlockIsInLoopContinueConstruct(opt::IRContext* context, uint32_t block_id,
103 uint32_t maybe_loop_header_id);
104
105 // If |block| contains |inst|, an iterator for |inst| is returned.
106 // Otherwise |block|->end() is returned.
107 opt::BasicBlock::iterator GetIteratorForInstruction(
108 opt::BasicBlock* block, const opt::Instruction* inst);
109
110 // Returns true if and only if there is a path to |bb| from the entry block of
111 // the function that contains |bb|.
112 bool BlockIsReachableInItsFunction(opt::IRContext* context,
113 opt::BasicBlock* bb);
114
115 // Determines whether it is OK to insert an instruction with opcode |opcode|
116 // before |instruction_in_block|.
117 bool CanInsertOpcodeBeforeInstruction(
118 SpvOp opcode, const opt::BasicBlock::iterator& instruction_in_block);
119
120 // Determines whether it is OK to make a synonym of |inst|.
121 // |transformation_context| is used to verify that the result id of |inst|
122 // does not participate in IdIsIrrelevant fact.
123 bool CanMakeSynonymOf(opt::IRContext* ir_context,
124 const TransformationContext& transformation_context,
125 opt::Instruction* inst);
126
127 // Determines whether the given type is a composite; that is: an array, matrix,
128 // struct or vector.
129 bool IsCompositeType(const opt::analysis::Type* type);
130
131 // Returns a vector containing the same elements as |repeated_field|.
132 std::vector<uint32_t> RepeatedFieldToVector(
133 const google::protobuf::RepeatedField<uint32_t>& repeated_field);
134
135 // Given a type id, |base_object_type_id|, returns 0 if the type is not a
136 // composite type or if |index| is too large to be used as an index into the
137 // composite. Otherwise returns the type id of the type associated with the
138 // composite's index.
139 //
140 // Example: if |base_object_type_id| is 10, and we have:
141 //
142 // %10 = OpTypeStruct %3 %4 %5
143 //
144 // then 3 will be returned if |index| is 0, 5 if |index| is 2, and 0 if index
145 // is 3 or larger.
146 uint32_t WalkOneCompositeTypeIndex(opt::IRContext* context,
147 uint32_t base_object_type_id,
148 uint32_t index);
149
150 // Given a type id, |base_object_type_id|, checks that the given sequence of
151 // |indices| is suitable for indexing into this type. Returns the id of the
152 // type of the final sub-object reached via the indices if they are valid, and
153 // 0 otherwise.
154 uint32_t WalkCompositeTypeIndices(
155 opt::IRContext* context, uint32_t base_object_type_id,
156 const google::protobuf::RepeatedField<google::protobuf::uint32>& indices);
157
158 // Returns the number of members associated with |struct_type_instruction|,
159 // which must be an OpStructType instruction.
160 uint32_t GetNumberOfStructMembers(
161 const opt::Instruction& struct_type_instruction);
162
163 // Returns the constant size of the array associated with
164 // |array_type_instruction|, which must be an OpArrayType instruction. Returns
165 // 0 if there is not a static size.
166 uint32_t GetArraySize(const opt::Instruction& array_type_instruction,
167 opt::IRContext* context);
168
169 // Returns the bound for indexing into a composite of type
170 // |composite_type_inst|, i.e. the number of fields of a struct, the size of an
171 // array, the number of components of a vector, or the number of columns of a
172 // matrix. |composite_type_inst| must be the type of a composite.
173 uint32_t GetBoundForCompositeIndex(const opt::Instruction& composite_type_inst,
174 opt::IRContext* ir_context);
175
176 // Returns true if and only if |context| is valid, according to the validator
177 // instantiated with |validator_options|. |consumer| is used for error
178 // reporting.
179 bool IsValid(const opt::IRContext* context,
180 spv_validator_options validator_options, MessageConsumer consumer);
181
182 // Returns true if and only if IsValid(|context|, |validator_options|) holds,
183 // and furthermore every basic block in |context| has its enclosing function as
184 // its parent, and every instruction in |context| has a distinct unique id.
185 // |consumer| is used for error reporting.
186 bool IsValidAndWellFormed(const opt::IRContext* context,
187 spv_validator_options validator_options,
188 MessageConsumer consumer);
189
190 // Returns a clone of |context|, by writing |context| to a binary and then
191 // parsing it again.
192 std::unique_ptr<opt::IRContext> CloneIRContext(opt::IRContext* context);
193
194 // Returns true if and only if |id| is the id of a type that is not a function
195 // type.
196 bool IsNonFunctionTypeId(opt::IRContext* ir_context, uint32_t id);
197
198 // Returns true if and only if |block_id| is a merge block or continue target
199 bool IsMergeOrContinue(opt::IRContext* ir_context, uint32_t block_id);
200
201 // Returns the id of the header of the loop corresponding to the given loop
202 // merge block. Returns 0 if |merge_block_id| is not a loop merge block.
203 uint32_t GetLoopFromMergeBlock(opt::IRContext* ir_context,
204 uint32_t merge_block_id);
205
206 // Returns the result id of an instruction of the form:
207 // %id = OpTypeFunction |type_ids|
208 // or 0 if no such instruction exists.
209 uint32_t FindFunctionType(opt::IRContext* ir_context,
210 const std::vector<uint32_t>& type_ids);
211
212 // Returns a type instruction (OpTypeFunction) for |function|.
213 // Returns |nullptr| if type is not found.
214 opt::Instruction* GetFunctionType(opt::IRContext* context,
215 const opt::Function* function);
216
217 // Returns the function with result id |function_id|, or |nullptr| if no such
218 // function exists.
219 opt::Function* FindFunction(opt::IRContext* ir_context, uint32_t function_id);
220
221 // Returns true if |function| has a block that the termination instruction is
222 // OpKill or OpUnreachable.
223 bool FunctionContainsOpKillOrUnreachable(const opt::Function& function);
224
225 // Returns |true| if one of entry points has function id |function_id|.
226 bool FunctionIsEntryPoint(opt::IRContext* context, uint32_t function_id);
227
228 // Checks whether |id| is available (according to dominance rules) at the use
229 // point defined by input operand |use_input_operand_index| of
230 // |use_instruction|. |use_instruction| must be a in some basic block.
231 bool IdIsAvailableAtUse(opt::IRContext* context,
232 opt::Instruction* use_instruction,
233 uint32_t use_input_operand_index, uint32_t id);
234
235 // Checks whether |id| is available (according to dominance rules) at the
236 // program point directly before |instruction|. |instruction| must be in some
237 // basic block.
238 bool IdIsAvailableBeforeInstruction(opt::IRContext* context,
239 opt::Instruction* instruction, uint32_t id);
240
241 // Returns true if and only if |instruction| is an OpFunctionParameter
242 // associated with |function|.
243 bool InstructionIsFunctionParameter(opt::Instruction* instruction,
244 opt::Function* function);
245
246 // Returns the type id of the instruction defined by |result_id|, or 0 if there
247 // is no such result id.
248 uint32_t GetTypeId(opt::IRContext* context, uint32_t result_id);
249
250 // Given |pointer_type_inst|, which must be an OpTypePointer instruction,
251 // returns the id of the associated pointee type.
252 uint32_t GetPointeeTypeIdFromPointerType(opt::Instruction* pointer_type_inst);
253
254 // Given |pointer_type_id|, which must be the id of a pointer type, returns the
255 // id of the associated pointee type.
256 uint32_t GetPointeeTypeIdFromPointerType(opt::IRContext* context,
257 uint32_t pointer_type_id);
258
259 // Given |pointer_type_inst|, which must be an OpTypePointer instruction,
260 // returns the associated storage class.
261 SpvStorageClass GetStorageClassFromPointerType(
262 opt::Instruction* pointer_type_inst);
263
264 // Given |pointer_type_id|, which must be the id of a pointer type, returns the
265 // associated storage class.
266 SpvStorageClass GetStorageClassFromPointerType(opt::IRContext* context,
267 uint32_t pointer_type_id);
268
269 // Returns the id of a pointer with pointee type |pointee_type_id| and storage
270 // class |storage_class|, if it exists, and 0 otherwise.
271 uint32_t MaybeGetPointerType(opt::IRContext* context, uint32_t pointee_type_id,
272 SpvStorageClass storage_class);
273
274 // Given an instruction |inst| and an operand absolute index |absolute_index|,
275 // returns the index of the operand restricted to the input operands.
276 uint32_t InOperandIndexFromOperandIndex(const opt::Instruction& inst,
277 uint32_t absolute_index);
278
279 // Returns true if and only if |type| is one of the types for which it is legal
280 // to have an OpConstantNull value.
281 bool IsNullConstantSupported(const opt::analysis::Type& type);
282
283 // Returns true if and only if the SPIR-V version being used requires that
284 // global variables accessed in the static call graph of an entry point need
285 // to be listed in that entry point's interface.
286 bool GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
287 const opt::IRContext* context);
288
289 // Adds |id| into the interface of every entry point of the shader.
290 // Does nothing if SPIR-V doesn't require global variables, that are accessed
291 // from an entry point function, to be listed in that function's interface.
292 void AddVariableIdToEntryPointInterfaces(opt::IRContext* context, uint32_t id);
293
294 // Adds a global variable with storage class |storage_class| to the module, with
295 // type |type_id| and either no initializer or |initializer_id| as an
296 // initializer, depending on whether |initializer_id| is 0. The global variable
297 // has result id |result_id|. Updates module's id bound to accommodate for
298 // |result_id|.
299 //
300 // - |type_id| must be the id of a pointer type with the same storage class as
301 // |storage_class|.
302 // - |storage_class| must be Private or Workgroup.
303 // - |initializer_id| must be 0 if |storage_class| is Workgroup, and otherwise
304 // may either be 0 or the id of a constant whose type is the pointee type of
305 // |type_id|.
306 //
307 // Returns a pointer to the new global variable instruction.
308 opt::Instruction* AddGlobalVariable(opt::IRContext* context, uint32_t result_id,
309 uint32_t type_id,
310 SpvStorageClass storage_class,
311 uint32_t initializer_id);
312
313 // Adds an instruction to the start of |function_id|, of the form:
314 // |result_id| = OpVariable |type_id| Function |initializer_id|.
315 // Updates module's id bound to accommodate for |result_id|.
316 //
317 // - |type_id| must be the id of a pointer type with Function storage class.
318 // - |initializer_id| must be the id of a constant with the same type as the
319 // pointer's pointee type.
320 // - |function_id| must be the id of a function.
321 //
322 // Returns a pointer to the new local variable instruction.
323 opt::Instruction* AddLocalVariable(opt::IRContext* context, uint32_t result_id,
324 uint32_t type_id, uint32_t function_id,
325 uint32_t initializer_id);
326
327 // Returns true if the vector |arr| has duplicates.
328 bool HasDuplicates(const std::vector<uint32_t>& arr);
329
330 // Checks that the given vector |arr| contains a permutation of a range
331 // [lo, hi]. That being said, all elements in the range are present without
332 // duplicates. If |arr| is empty, returns true iff |lo > hi|.
333 bool IsPermutationOfRange(const std::vector<uint32_t>& arr, uint32_t lo,
334 uint32_t hi);
335
336 // Returns OpFunctionParameter instructions corresponding to the function
337 // with result id |function_id|.
338 std::vector<opt::Instruction*> GetParameters(opt::IRContext* ir_context,
339 uint32_t function_id);
340
341 // Removes an OpFunctionParameter instruction with result id |parameter_id|
342 // from the its function. Parameter's function must not be an entry-point
343 // function. The function must have a parameter with result id |parameter_id|.
344 //
345 // Prefer using this function to opt::Function::RemoveParameter since
346 // this function also guarantees that |ir_context| has no invalid pointers
347 // to the removed parameter.
348 void RemoveParameter(opt::IRContext* ir_context, uint32_t parameter_id);
349
350 // Returns all OpFunctionCall instructions that call a function with result id
351 // |function_id|.
352 std::vector<opt::Instruction*> GetCallers(opt::IRContext* ir_context,
353 uint32_t function_id);
354
355 // Returns a function that contains OpFunctionParameter instruction with result
356 // id |param_id|. Returns nullptr if the module has no such function.
357 opt::Function* GetFunctionFromParameterId(opt::IRContext* ir_context,
358 uint32_t param_id);
359
360 // Changes the type of function |function_id| so that its return type is
361 // |return_type_id| and its parameters' types are |parameter_type_ids|. If a
362 // suitable function type already exists in the module, it is used, otherwise
363 // |new_function_type_result_id| is used as the result id of a suitable new
364 // function type instruction. If the old type of the function doesn't have any
365 // more users, it is removed from the module. Returns the result id of the
366 // OpTypeFunction instruction that is used as a type of the function with
367 // |function_id|.
368 //
369 // CAUTION: When the old type of the function is removed from the module, its
370 // memory is deallocated. Be sure not to use any pointers to the old
371 // type when this function returns.
372 uint32_t UpdateFunctionType(opt::IRContext* ir_context, uint32_t function_id,
373 uint32_t new_function_type_result_id,
374 uint32_t return_type_id,
375 const std::vector<uint32_t>& parameter_type_ids);
376
377 // Creates new OpTypeFunction instruction in the module. |type_ids| may not be
378 // empty. It may not contain result ids of OpTypeFunction instructions.
379 // |type_ids[i]| may not be a result id of OpTypeVoid instruction for |i >= 1|.
380 // |result_id| may not equal to 0. Updates module's id bound to accommodate for
381 // |result_id|.
382 void AddFunctionType(opt::IRContext* ir_context, uint32_t result_id,
383 const std::vector<uint32_t>& type_ids);
384
385 // Returns a result id of an OpTypeFunction instruction in the module. Creates a
386 // new instruction if required and returns |result_id|. type_ids| may not be
387 // empty. It may not contain result ids of OpTypeFunction instructions.
388 // |type_ids[i]| may not be a result id of OpTypeVoid instruction for |i >= 1|.
389 // |result_id| must not be equal to 0. Updates module's id bound to accommodate
390 // for |result_id|.
391 uint32_t FindOrCreateFunctionType(opt::IRContext* ir_context,
392 uint32_t result_id,
393 const std::vector<uint32_t>& type_ids);
394
395 // Returns a result id of an OpTypeInt instruction if present. Returns 0
396 // otherwise.
397 uint32_t MaybeGetIntegerType(opt::IRContext* ir_context, uint32_t width,
398 bool is_signed);
399
400 // Returns a result id of an OpTypeFloat instruction if present. Returns 0
401 // otherwise.
402 uint32_t MaybeGetFloatType(opt::IRContext* ir_context, uint32_t width);
403
404 // Returns a result id of an OpTypeBool instruction if present. Returns 0
405 // otherwise.
406 uint32_t MaybeGetBoolType(opt::IRContext* ir_context);
407
408 // Returns a result id of an OpTypeVector instruction if present. Returns 0
409 // otherwise. |component_type_id| must be a valid result id of an OpTypeInt,
410 // OpTypeFloat or OpTypeBool instruction in the module. |element_count| must be
411 // in the range [2, 4].
412 uint32_t MaybeGetVectorType(opt::IRContext* ir_context,
413 uint32_t component_type_id, uint32_t element_count);
414
415 // Returns a result id of an OpTypeStruct instruction whose field types exactly
416 // match |component_type_ids| if such an instruction is present. Returns 0
417 // otherwise. |component_type_ids| may not contain a result id of an
418 // OpTypeFunction.
419 uint32_t MaybeGetStructType(opt::IRContext* ir_context,
420 const std::vector<uint32_t>& component_type_ids);
421
422 // Returns a result id of an OpTypeVoid instruction if present. Returns 0
423 // otherwise.
424 uint32_t MaybeGetVoidType(opt::IRContext* ir_context);
425
426 // Recursive definition is the following:
427 // - if |scalar_or_composite_type_id| is a result id of a scalar type - returns
428 // a result id of the following constants (depending on the type): int -> 0,
429 // float -> 0.0, bool -> false.
430 // - otherwise, returns a result id of an OpConstantComposite instruction.
431 // Every component of the composite constant is looked up by calling this
432 // function with the type id of that component.
433 // Returns 0 if no such instruction is present in the module.
434 // The returned id either participates in IdIsIrrelevant fact or not, depending
435 // on the |is_irrelevant| parameter.
436 uint32_t MaybeGetZeroConstant(
437 opt::IRContext* ir_context,
438 const TransformationContext& transformation_context,
439 uint32_t scalar_or_composite_type_id, bool is_irrelevant);
440
441 // Returns true if it is possible to create an OpConstant or an
442 // OpConstantComposite instruction of type |type_id|. That is, returns true if
443 // the type associated with |type_id| and all its constituents are either scalar
444 // or composite.
445 bool CanCreateConstant(opt::IRContext* ir_context, uint32_t type_id);
446
447 // Returns the result id of an OpConstant instruction. |scalar_type_id| must be
448 // a result id of a scalar type (i.e. int, float or bool). Returns 0 if no such
449 // instruction is present in the module. The returned id either participates in
450 // IdIsIrrelevant fact or not, depending on the |is_irrelevant| parameter.
451 uint32_t MaybeGetScalarConstant(
452 opt::IRContext* ir_context,
453 const TransformationContext& transformation_context,
454 const std::vector<uint32_t>& words, uint32_t scalar_type_id,
455 bool is_irrelevant);
456
457 // Returns the result id of an OpConstantComposite instruction.
458 // |composite_type_id| must be a result id of a composite type (i.e. vector,
459 // matrix, struct or array). Returns 0 if no such instruction is present in the
460 // module. The returned id either participates in IdIsIrrelevant fact or not,
461 // depending on the |is_irrelevant| parameter.
462 uint32_t MaybeGetCompositeConstant(
463 opt::IRContext* ir_context,
464 const TransformationContext& transformation_context,
465 const std::vector<uint32_t>& component_ids, uint32_t composite_type_id,
466 bool is_irrelevant);
467
468 // Returns the result id of an OpConstant instruction of integral type.
469 // Returns 0 if no such instruction or type is present in the module.
470 // The returned id either participates in IdIsIrrelevant fact or not, depending
471 // on the |is_irrelevant| parameter.
472 uint32_t MaybeGetIntegerConstant(
473 opt::IRContext* ir_context,
474 const TransformationContext& transformation_context,
475 const std::vector<uint32_t>& words, uint32_t width, bool is_signed,
476 bool is_irrelevant);
477
478 // Returns the id of a 32-bit integer constant in the module with type
479 // |int_type_id| and value |value|, or 0 if no such constant exists in the
480 // module. |int_type_id| must exist in the module and it must correspond to a
481 // 32-bit integer type.
482 uint32_t MaybeGetIntegerConstantFromValueAndType(opt::IRContext* ir_context,
483 uint32_t value,
484 uint32_t int_type_id);
485
486 // Returns the result id of an OpConstant instruction of floating-point type.
487 // Returns 0 if no such instruction or type is present in the module.
488 // The returned id either participates in IdIsIrrelevant fact or not, depending
489 // on the |is_irrelevant| parameter.
490 uint32_t MaybeGetFloatConstant(
491 opt::IRContext* ir_context,
492 const TransformationContext& transformation_context,
493 const std::vector<uint32_t>& words, uint32_t width, bool is_irrelevant);
494
495 // Returns the id of a boolean constant with value |value| if it exists in the
496 // module, or 0 otherwise. The returned id either participates in IdIsIrrelevant
497 // fact or not, depending on the |is_irrelevant| parameter.
498 uint32_t MaybeGetBoolConstant(
499 opt::IRContext* context,
500 const TransformationContext& transformation_context, bool value,
501 bool is_irrelevant);
502
503 // Returns a vector of words representing the integer |value|, only considering
504 // the last |width| bits. The last |width| bits are sign-extended if the value
505 // is signed, zero-extended if it is unsigned.
506 // |width| must be <= 64.
507 // If |width| <= 32, returns a vector containing one value. If |width| > 64,
508 // returns a vector containing two values, with the first one representing the
509 // lower-order word of the value and the second one representing the
510 // higher-order word.
511 std::vector<uint32_t> IntToWords(uint64_t value, uint32_t width,
512 bool is_signed);
513
514 // Returns a bit pattern that represents a floating-point |value|.
FloatToWord(float value)515 inline uint32_t FloatToWord(float value) {
516 uint32_t result;
517 memcpy(&result, &value, sizeof(uint32_t));
518 return result;
519 }
520
521 // Returns true if any of the following is true:
522 // - |type1_id| and |type2_id| are the same id
523 // - |type1_id| and |type2_id| refer to integer scalar or vector types, only
524 // differing by their signedness.
525 bool TypesAreEqualUpToSign(opt::IRContext* ir_context, uint32_t type1_id,
526 uint32_t type2_id);
527
528 // Converts repeated field of UInt32Pair to a map. If two or more equal values
529 // of |UInt32Pair::first()| are available in |data|, the last value of
530 // |UInt32Pair::second()| is used.
531 std::map<uint32_t, uint32_t> RepeatedUInt32PairToMap(
532 const google::protobuf::RepeatedPtrField<protobufs::UInt32Pair>& data);
533
534 // Converts a map into a repeated field of UInt32Pair.
535 google::protobuf::RepeatedPtrField<protobufs::UInt32Pair>
536 MapToRepeatedUInt32Pair(const std::map<uint32_t, uint32_t>& data);
537
538 // Returns the last instruction in |block_id| before which an instruction with
539 // opcode |opcode| can be inserted, or nullptr if there is no such instruction.
540 opt::Instruction* GetLastInsertBeforeInstruction(opt::IRContext* ir_context,
541 uint32_t block_id,
542 SpvOp opcode);
543
544 // Checks whether various conditions hold related to the acceptability of
545 // replacing the id use at |use_in_operand_index| of |use_instruction| with a
546 // synonym or another id of appropriate type if the original id is irrelevant.
547 // In particular, this checks that:
548 // - If id use is an index of an irrelevant id (|use_in_operand_index > 0|)
549 // in OpAccessChain - it can't be replaced.
550 // - The id use is not an index into a struct field in an OpAccessChain - such
551 // indices must be constants, so it is dangerous to replace them.
552 // - The id use is not a pointer function call argument, on which there are
553 // restrictions that make replacement problematic.
554 // - The id use is not the Sample parameter of an OpImageTexelPointer
555 // instruction, as this must satisfy particular requirements.
556 bool IdUseCanBeReplaced(opt::IRContext* ir_context,
557 const TransformationContext& transformation_context,
558 opt::Instruction* use_instruction,
559 uint32_t use_in_operand_index);
560
561 // Requires that |struct_type_id| is the id of a struct type, and (as per the
562 // SPIR-V spec) that either all or none of the members of |struct_type_id| have
563 // the BuiltIn decoration. Returns true if and only if all members have the
564 // BuiltIn decoration.
565 bool MembersHaveBuiltInDecoration(opt::IRContext* ir_context,
566 uint32_t struct_type_id);
567
568 // Returns true if and only if |id| is decorated with either Block or
569 // BufferBlock. Even though these decorations are only allowed on struct types,
570 // for convenience |id| can be any result id so that it is possible to call this
571 // method on something that *might* be a struct type.
572 bool HasBlockOrBufferBlockDecoration(opt::IRContext* ir_context, uint32_t id);
573
574 // Returns true iff splitting block |block_to_split| just before the instruction
575 // |split_before| would separate an OpSampledImage instruction from its usage.
576 bool SplittingBeforeInstructionSeparatesOpSampledImageDefinitionFromUse(
577 opt::BasicBlock* block_to_split, opt::Instruction* split_before);
578
579 // Returns true if the instruction given has no side effects.
580 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3758): Add any
581 // missing instructions to the list. In particular, GLSL extended instructions
582 // (called using OpExtInst) have not been considered.
583 bool InstructionHasNoSideEffects(const opt::Instruction& instruction);
584
585 // Returns a set of the ids of all the return blocks that are reachable from
586 // the entry block of |function_id|.
587 // Assumes that the function exists in the module.
588 std::set<uint32_t> GetReachableReturnBlocks(opt::IRContext* ir_context,
589 uint32_t function_id);
590
591 // Returns true if changing terminator instruction to |new_terminator| in the
592 // basic block with id |block_id| preserves domination rules and valid block
593 // order (i.e. dominator must always appear before dominated in the CFG).
594 // Returns false otherwise.
595 bool NewTerminatorPreservesDominationRules(opt::IRContext* ir_context,
596 uint32_t block_id,
597 opt::Instruction new_terminator);
598
599 } // namespace fuzzerutil
600 } // namespace fuzz
601 } // namespace spvtools
602
603 #endif // SOURCE_FUZZ_FUZZER_UTIL_H_
604