| 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 |
// Returns true if and only if the module does not define the given id. |
| 42 |
bool IsFreshId(opt::IRContext* context, uint32_t id); |
| 43 |
|
| 44 |
// Updates the module's id bound if needed so that it is large enough to |
| 45 |
// account for the given id. |
| 46 |
void UpdateModuleIdBound(opt::IRContext* context, uint32_t id); |
| 47 |
|
| 48 |
// Return the block with id |maybe_block_id| if it exists, and nullptr |
| 49 |
// otherwise. |
| 50 |
opt::BasicBlock* MaybeFindBlock(opt::IRContext* context, |
| 51 |
uint32_t maybe_block_id); |
| 52 |
|
| 53 |
// When adding an edge from |bb_from| to |bb_to| (which are assumed to be blocks |
| 54 |
// in the same function), it is important to supply |bb_to| with ids that can be |
| 55 |
// used to augment OpPhi instructions in the case that there is not already such |
| 56 |
// an edge. This function returns true if and only if the ids provided in |
| 57 |
// |phi_ids| suffice for this purpose, |
| 58 |
bool PhiIdsOkForNewEdge( |
| 59 |
opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to, |
| 60 |
const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids); |
| 61 |
|
| 62 |
// Requires that |bool_id| is a valid result id of either OpConstantTrue or |
| 63 |
// OpConstantFalse, that PhiIdsOkForNewEdge(context, bb_from, bb_to, phi_ids) |
| 64 |
// holds, and that bb_from ends with "OpBranch %some_block". Turns OpBranch |
| 65 |
// into "OpBranchConditional |condition_value| ...", such that control will |
| 66 |
// branch to %some_block, with |bb_to| being the unreachable alternative. |
| 67 |
// Updates OpPhi instructions in |bb_to| using |phi_ids| so that the new edge is |
| 68 |
// valid. |condition_value| above is equal to |true| if |bool_id| is a result id |
| 69 |
// of an OpConstantTrue instruction. |
| 70 |
void AddUnreachableEdgeAndUpdateOpPhis( |
| 71 |
opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to, |
| 72 |
uint32_t bool_id, |
| 73 |
const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids); |
| 74 |
|
| 75 |
// Returns true if and only if |loop_header_id| is a loop header and |
| 76 |
// |block_id| is a reachable block branching to and dominated by |
| 77 |
// |loop_header_id|. |
| 78 |
bool BlockIsBackEdge(opt::IRContext* context, uint32_t block_id, |
| 79 |
uint32_t loop_header_id); |
| 80 |
|
| 81 |
// Returns true if and only if |maybe_loop_header_id| is a loop header and |
| 82 |
// |block_id| is in the continue construct of the associated loop. |
| 83 |
bool BlockIsInLoopContinueConstruct(opt::IRContext* context, uint32_t block_id, |
| 84 |
uint32_t maybe_loop_header_id); |
| 85 |
|
| 86 |
// If |block| contains |inst|, an iterator for |inst| is returned. |
| 87 |
// Otherwise |block|->end() is returned. |
| 88 |
opt::BasicBlock::iterator GetIteratorForInstruction( |
| 89 |
opt::BasicBlock* block, const opt::Instruction* inst); |
| 90 |
|
| 91 |
// Returns true if and only if there is a path to |bb| from the entry block of |
| 92 |
// the function that contains |bb|. |
| 93 |
bool BlockIsReachableInItsFunction(opt::IRContext* context, |
| 94 |
opt::BasicBlock* bb); |
| 95 |
|
| 96 |
// Determines whether it is OK to insert an instruction with opcode |opcode| |
| 97 |
// before |instruction_in_block|. |
| 98 |
bool CanInsertOpcodeBeforeInstruction( |
| 99 |
SpvOp opcode, const opt::BasicBlock::iterator& instruction_in_block); |
| 100 |
|
| 101 |
// Determines whether it is OK to make a synonym of |inst|. |
| 102 |
// |transformation_context| is used to verify that the result id of |inst| |
| 103 |
// does not participate in IdIsIrrelevant fact. |
| 104 |
bool CanMakeSynonymOf(opt::IRContext* ir_context, |
| 105 |
const TransformationContext& transformation_context, |
| 106 |
opt::Instruction* inst); |
| 107 |
|
| 108 |
// Determines whether the given type is a composite; that is: an array, matrix, |
| 109 |
// struct or vector. |
| 110 |
bool IsCompositeType(const opt::analysis::Type* type); |
| 111 |
|
| 112 |
// Returns a vector containing the same elements as |repeated_field|. |
| 113 |
std::vector<uint32_t> RepeatedFieldToVector( |
| 114 |
const google::protobuf::RepeatedField<uint32_t>& repeated_field); |
| 115 |
|
| 116 |
// Given a type id, |base_object_type_id|, returns 0 if the type is not a |
| 117 |
// composite type or if |index| is too large to be used as an index into the |
| 118 |
// composite. Otherwise returns the type id of the type associated with the |
| 119 |
// composite's index. |
| 120 |
// |
| 121 |
// Example: if |base_object_type_id| is 10, and we have: |
| 122 |
// |
| 123 |
// %10 = OpTypeStruct %3 %4 %5 |
| 124 |
// |
| 125 |
// then 3 will be returned if |index| is 0, 5 if |index| is 2, and 0 if index |
| 126 |
// is 3 or larger. |
| 127 |
uint32_t WalkOneCompositeTypeIndex(opt::IRContext* context, |
| 128 |
uint32_t base_object_type_id, |
| 129 |
uint32_t index); |
| 130 |
|
| 131 |
// Given a type id, |base_object_type_id|, checks that the given sequence of |
| 132 |
// |indices| is suitable for indexing into this type. Returns the id of the |
| 133 |
// type of the final sub-object reached via the indices if they are valid, and |
| 134 |
// 0 otherwise. |
| 135 |
uint32_t WalkCompositeTypeIndices( |
| 136 |
opt::IRContext* context, uint32_t base_object_type_id, |
| 137 |
const google::protobuf::RepeatedField<google::protobuf::uint32>& indices); |
| 138 |
|
| 139 |
// Returns the number of members associated with |struct_type_instruction|, |
| 140 |
// which must be an OpStructType instruction. |
| 141 |
uint32_t GetNumberOfStructMembers( |
| 142 |
const opt::Instruction& struct_type_instruction); |
| 143 |
|
| 144 |
// Returns the constant size of the array associated with |
| 145 |
// |array_type_instruction|, which must be an OpArrayType instruction. Returns |
| 146 |
// 0 if there is not a static size. |
| 147 |
uint32_t GetArraySize(const opt::Instruction& array_type_instruction, |
| 148 |
opt::IRContext* context); |
| 149 |
|
| 150 |
// Returns the bound for indexing into a composite of type |
| 151 |
// |composite_type_inst|, i.e. the number of fields of a struct, the size of an |
| 152 |
// array, the number of components of a vector, or the number of columns of a |
| 153 |
// matrix. |composite_type_inst| must be the type of a composite. |
| 154 |
uint32_t GetBoundForCompositeIndex(const opt::Instruction& composite_type_inst, |
| 155 |
opt::IRContext* ir_context); |
| 156 |
|
| 157 |
// Returns true if and only if |context| is valid, according to the validator |
| 158 |
// instantiated with |validator_options|. |consumer| is used for error |
| 159 |
// reporting. |
| 160 |
bool IsValid(const opt::IRContext* context, |
| 161 |
spv_validator_options validator_options, MessageConsumer consumer); |
| 162 |
|
| 163 |
// Returns true if and only if IsValid(|context|, |validator_options|) holds, |
| 164 |
// and furthermore every basic block in |context| has its enclosing function as |
| 165 |
// its parent, and every instruction in |context| has a distinct unique id. |
| 166 |
// |consumer| is used for error reporting. |
| 167 |
bool IsValidAndWellFormed(const opt::IRContext* context, |
| 168 |
spv_validator_options validator_options, |
| 169 |
MessageConsumer consumer); |
| 170 |
|
| 171 |
// Returns a clone of |context|, by writing |context| to a binary and then |
| 172 |
// parsing it again. |
| 173 |
std::unique_ptr<opt::IRContext> CloneIRContext(opt::IRContext* context); |
| 174 |
|
| 175 |
// Returns true if and only if |id| is the id of a type that is not a function |
| 176 |
// type. |
| 177 |
bool IsNonFunctionTypeId(opt::IRContext* ir_context, uint32_t id); |
| 178 |
|
| 179 |
// Returns true if and only if |block_id| is a merge block or continue target |
| 180 |
bool IsMergeOrContinue(opt::IRContext* ir_context, uint32_t block_id); |
| 181 |
|
| 182 |
// Returns the id of the header of the loop corresponding to the given loop |
| 183 |
// merge block. Returns 0 if |merge_block_id| is not a loop merge block. |
| 184 |
uint32_t GetLoopFromMergeBlock(opt::IRContext* ir_context, |
| 185 |
uint32_t merge_block_id); |
| 186 |
|
| 187 |
// Returns the result id of an instruction of the form: |
| 188 |
// %id = OpTypeFunction |type_ids| |
| 189 |
// or 0 if no such instruction exists. |
| 190 |
uint32_t FindFunctionType(opt::IRContext* ir_context, |
| 191 |
const std::vector<uint32_t>& type_ids); |
| 192 |
|
| 193 |
// Returns a type instruction (OpTypeFunction) for |function|. |
| 194 |
// Returns |nullptr| if type is not found. |
| 195 |
opt::Instruction* GetFunctionType(opt::IRContext* context, |
| 196 |
const opt::Function* function); |
| 197 |
|
| 198 |
// Returns the function with result id |function_id|, or |nullptr| if no such |
| 199 |
// function exists. |
| 200 |
opt::Function* FindFunction(opt::IRContext* ir_context, uint32_t function_id); |
| 201 |
|
| 202 |
// Returns true if |function| has a block that the termination instruction is |
| 203 |
// OpKill or OpUnreachable. |
| 204 |
bool FunctionContainsOpKillOrUnreachable(const opt::Function& function); |
| 205 |
|
| 206 |
// Returns |true| if one of entry points has function id |function_id|. |
| 207 |
bool FunctionIsEntryPoint(opt::IRContext* context, uint32_t function_id); |
| 208 |
|
| 209 |
// Checks whether |id| is available (according to dominance rules) at the use |
| 210 |
// point defined by input operand |use_input_operand_index| of |
| 211 |
// |use_instruction|. |use_instruction| must be a in some basic block. |
| 212 |
bool IdIsAvailableAtUse(opt::IRContext* context, |
| 213 |
opt::Instruction* use_instruction, |
| 214 |
uint32_t use_input_operand_index, uint32_t id); |
| 215 |
|
| 216 |
// Checks whether |id| is available (according to dominance rules) at the |
| 217 |
// program point directly before |instruction|. |instruction| must be in some |
| 218 |
// basic block. |
| 219 |
bool IdIsAvailableBeforeInstruction(opt::IRContext* context, |
| 220 |
opt::Instruction* instruction, uint32_t id); |
| 221 |
|
| 222 |
// Returns true if and only if |instruction| is an OpFunctionParameter |
| 223 |
// associated with |function|. |
| 224 |
bool InstructionIsFunctionParameter(opt::Instruction* instruction, |
| 225 |
opt::Function* function); |
| 226 |
|
| 227 |
// Returns the type id of the instruction defined by |result_id|, or 0 if there |
| 228 |
// is no such result id. |
| 229 |
uint32_t GetTypeId(opt::IRContext* context, uint32_t result_id); |
| 230 |
|
| 231 |
// Given |pointer_type_inst|, which must be an OpTypePointer instruction, |
| 232 |
// returns the id of the associated pointee type. |
| 233 |
uint32_t GetPointeeTypeIdFromPointerType(opt::Instruction* pointer_type_inst); |
| 234 |
|
| 235 |
// Given |pointer_type_id|, which must be the id of a pointer type, returns the |
| 236 |
// id of the associated pointee type. |
| 237 |
uint32_t GetPointeeTypeIdFromPointerType(opt::IRContext* context, |
| 238 |
uint32_t pointer_type_id); |
| 239 |
|
| 240 |
// Given |pointer_type_inst|, which must be an OpTypePointer instruction, |
| 241 |
// returns the associated storage class. |
| 242 |
SpvStorageClass GetStorageClassFromPointerType( |
| 243 |
opt::Instruction* pointer_type_inst); |
| 244 |
|
| 245 |
// Given |pointer_type_id|, which must be the id of a pointer type, returns the |
| 246 |
// associated storage class. |
| 247 |
SpvStorageClass GetStorageClassFromPointerType(opt::IRContext* context, |
| 248 |
uint32_t pointer_type_id); |
| 249 |
|
| 250 |
// Returns the id of a pointer with pointee type |pointee_type_id| and storage |
| 251 |
// class |storage_class|, if it exists, and 0 otherwise. |
| 252 |
uint32_t MaybeGetPointerType(opt::IRContext* context, uint32_t pointee_type_id, |
| 253 |
SpvStorageClass storage_class); |
| 254 |
|
| 255 |
// Given an instruction |inst| and an operand absolute index |absolute_index|, |
| 256 |
// returns the index of the operand restricted to the input operands. |
| 257 |
uint32_t InOperandIndexFromOperandIndex(const opt::Instruction& inst, |
| 258 |
uint32_t absolute_index); |
| 259 |
|
| 260 |
// Returns true if and only if |type| is one of the types for which it is legal |
| 261 |
// to have an OpConstantNull value. |
| 262 |
bool IsNullConstantSupported(const opt::analysis::Type& type); |
| 263 |
|
| 264 |
// Returns true if and only if the SPIR-V version being used requires that |
| 265 |
// global variables accessed in the static call graph of an entry point need |
| 266 |
// to be listed in that entry point's interface. |
| 267 |
bool GlobalVariablesMustBeDeclaredInEntryPointInterfaces( |
| 268 |
const opt::IRContext* context); |
| 269 |
|
| 270 |
// Adds |id| into the interface of every entry point of the shader. |
| 271 |
// Does nothing if SPIR-V doesn't require global variables, that are accessed |
| 272 |
// from an entry point function, to be listed in that function's interface. |
| 273 |
void AddVariableIdToEntryPointInterfaces(opt::IRContext* context, uint32_t id); |
| 274 |
|
| 275 |
// Adds a global variable with storage class |storage_class| to the module, with |
| 276 |
// type |type_id| and either no initializer or |initializer_id| as an |
| 277 |
// initializer, depending on whether |initializer_id| is 0. The global variable |
| 278 |
// has result id |result_id|. Updates module's id bound to accommodate for |
| 279 |
// |result_id|. |
| 280 |
// |
| 281 |
// - |type_id| must be the id of a pointer type with the same storage class as |
| 282 |
// |storage_class|. |
| 283 |
// - |storage_class| must be Private or Workgroup. |
| 284 |
// - |initializer_id| must be 0 if |storage_class| is Workgroup, and otherwise |
| 285 |
// may either be 0 or the id of a constant whose type is the pointee type of |
| 286 |
// |type_id|. |
| 287 |
void AddGlobalVariable(opt::IRContext* context, uint32_t result_id, |
| 288 |
uint32_t type_id, SpvStorageClass storage_class, |
| 289 |
uint32_t initializer_id); |
| 290 |
|
| 291 |
// Adds an instruction to the start of |function_id|, of the form: |
| 292 |
// |result_id| = OpVariable |type_id| Function |initializer_id|. |
| 293 |
// Updates module's id bound to accommodate for |result_id|. |
| 294 |
// |
| 295 |
// - |type_id| must be the id of a pointer type with Function storage class. |
| 296 |
// - |initializer_id| must be the id of a constant with the same type as the |
| 297 |
// pointer's pointee type. |
| 298 |
// - |function_id| must be the id of a function. |
| 299 |
void AddLocalVariable(opt::IRContext* context, uint32_t result_id, |
| 300 |
uint32_t type_id, uint32_t function_id, |
| 301 |
uint32_t initializer_id); |
| 302 |
|
| 303 |
// Returns true if the vector |arr| has duplicates. |
| 304 |
bool HasDuplicates(const std::vector<uint32_t>& arr); |
| 305 |
|
| 306 |
// Checks that the given vector |arr| contains a permutation of a range |
| 307 |
// [lo, hi]. That being said, all elements in the range are present without |
| 308 |
// duplicates. If |arr| is empty, returns true iff |lo > hi|. |
| 309 |
bool IsPermutationOfRange(const std::vector<uint32_t>& arr, uint32_t lo, |
| 310 |
uint32_t hi); |
| 311 |
|
| 312 |
// Returns OpFunctionParameter instructions corresponding to the function |
| 313 |
// with result id |function_id|. |
| 314 |
std::vector<opt::Instruction*> GetParameters(opt::IRContext* ir_context, |
| 315 |
uint32_t function_id); |
| 316 |
|
| 317 |
// Removes an OpFunctionParameter instruction with result id |parameter_id| |
| 318 |
// from the its function. Parameter's function must not be an entry-point |
| 319 |
// function. The function must have a parameter with result id |parameter_id|. |
| 320 |
// |
| 321 |
// Prefer using this function to opt::Function::RemoveParameter since |
| 322 |
// this function also guarantees that |ir_context| has no invalid pointers |
| 323 |
// to the removed parameter. |
| 324 |
void RemoveParameter(opt::IRContext* ir_context, uint32_t parameter_id); |
| 325 |
|
| 326 |
// Returns all OpFunctionCall instructions that call a function with result id |
| 327 |
// |function_id|. |
| 328 |
std::vector<opt::Instruction*> GetCallers(opt::IRContext* ir_context, |
| 329 |
uint32_t function_id); |
| 330 |
|
| 331 |
// Returns a function that contains OpFunctionParameter instruction with result |
| 332 |
// id |param_id|. Returns nullptr if the module has no such function. |
| 333 |
opt::Function* GetFunctionFromParameterId(opt::IRContext* ir_context, |
| 334 |
uint32_t param_id); |
| 335 |
|
| 336 |
// Changes the type of function |function_id| so that its return type is |
| 337 |
// |return_type_id| and its parameters' types are |parameter_type_ids|. If a |
| 338 |
// suitable function type already exists in the module, it is used, otherwise |
| 339 |
// |new_function_type_result_id| is used as the result id of a suitable new |
| 340 |
// function type instruction. If the old type of the function doesn't have any |
| 341 |
// more users, it is removed from the module. Returns the result id of the |
| 342 |
// OpTypeFunction instruction that is used as a type of the function with |
| 343 |
// |function_id|. |
| 344 |
// |
| 345 |
// CAUTION: When the old type of the function is removed from the module, its |
| 346 |
// memory is deallocated. Be sure not to use any pointers to the old |
| 347 |
// type when this function returns. |
| 348 |
uint32_t UpdateFunctionType(opt::IRContext* ir_context, uint32_t function_id, |
| 349 |
uint32_t new_function_type_result_id, |
| 350 |
uint32_t return_type_id, |
| 351 |
const std::vector<uint32_t>& parameter_type_ids); |
| 352 |
|
| 353 |
// Creates new OpTypeFunction instruction in the module. |type_ids| may not be |
| 354 |
// empty. It may not contain result ids of OpTypeFunction instructions. |
| 355 |
// |type_ids[i]| may not be a result id of OpTypeVoid instruction for |i >= 1|. |
| 356 |
// |result_id| may not equal to 0. Updates module's id bound to accommodate for |
| 357 |
// |result_id|. |
| 358 |
void AddFunctionType(opt::IRContext* ir_context, uint32_t result_id, |
| 359 |
const std::vector<uint32_t>& type_ids); |
| 360 |
|
| 361 |
// Returns a result id of an OpTypeFunction instruction in the module. Creates a |
| 362 |
// new instruction if required and returns |result_id|. type_ids| may not be |
| 363 |
// empty. It may not contain result ids of OpTypeFunction instructions. |
| 364 |
// |type_ids[i]| may not be a result id of OpTypeVoid instruction for |i >= 1|. |
| 365 |
// |result_id| must not be equal to 0. Updates module's id bound to accommodate |
| 366 |
// for |result_id|. |
| 367 |
uint32_t FindOrCreateFunctionType(opt::IRContext* ir_context, |
| 368 |
uint32_t result_id, |
| 369 |
const std::vector<uint32_t>& type_ids); |
| 370 |
|
| 371 |
// Returns a result id of an OpTypeInt instruction if present. Returns 0 |
| 372 |
// otherwise. |
| 373 |
uint32_t MaybeGetIntegerType(opt::IRContext* ir_context, uint32_t width, |
| 374 |
bool is_signed); |
| 375 |
|
| 376 |
// Returns a result id of an OpTypeFloat instruction if present. Returns 0 |
| 377 |
// otherwise. |
| 378 |
uint32_t MaybeGetFloatType(opt::IRContext* ir_context, uint32_t width); |
| 379 |
|
| 380 |
// Returns a result id of an OpTypeBool instruction if present. Returns 0 |
| 381 |
// otherwise. |
| 382 |
uint32_t MaybeGetBoolType(opt::IRContext* ir_context); |
| 383 |
|
| 384 |
// Returns a result id of an OpTypeVector instruction if present. Returns 0 |
| 385 |
// otherwise. |component_type_id| must be a valid result id of an OpTypeInt, |
| 386 |
// OpTypeFloat or OpTypeBool instruction in the module. |element_count| must be |
| 387 |
// in the range [2, 4]. |
| 388 |
uint32_t MaybeGetVectorType(opt::IRContext* ir_context, |
| 389 |
uint32_t component_type_id, uint32_t element_count); |
| 390 |
|
| 391 |
// Returns a result id of an OpTypeStruct instruction whose field types exactly |
| 392 |
// match |component_type_ids| if such an instruction is present. Returns 0 |
| 393 |
// otherwise. |component_type_ids| may not contain a result id of an |
| 394 |
// OpTypeFunction. |
| 395 |
uint32_t MaybeGetStructType(opt::IRContext* ir_context, |
| 396 |
const std::vector<uint32_t>& component_type_ids); |
| 397 |
|
| 398 |
// Returns a result id of an OpTypeVoid instruction if present. Returns 0 |
| 399 |
// otherwise. |
| 400 |
uint32_t MaybeGetVoidType(opt::IRContext* ir_context); |
| 401 |
|
| 402 |
// Recursive definition is the following: |
| 403 |
// - if |scalar_or_composite_type_id| is a result id of a scalar type - returns |
| 404 |
// a result id of the following constants (depending on the type): int -> 0, |
| 405 |
// float -> 0.0, bool -> false. |
| 406 |
// - otherwise, returns a result id of an OpConstantComposite instruction. |
| 407 |
// Every component of the composite constant is looked up by calling this |
| 408 |
// function with the type id of that component. |
| 409 |
// Returns 0 if no such instruction is present in the module. |
| 410 |
// The returned id either participates in IdIsIrrelevant fact or not, depending |
| 411 |
// on the |is_irrelevant| parameter. |
| 412 |
uint32_t MaybeGetZeroConstant( |
| 413 |
opt::IRContext* ir_context, |
| 414 |
const TransformationContext& transformation_context, |
| 415 |
uint32_t scalar_or_composite_type_id, bool is_irrelevant); |
| 416 |
|
| 417 |
// Returns true if it is possible to create an OpConstant or an |
| 418 |
// OpConstantComposite instruction of type |type_id|. That is, returns true if |
| 419 |
// the type associated with |type_id| and all its constituents are either scalar |
| 420 |
// or composite. |
| 421 |
bool CanCreateConstant(opt::IRContext* ir_context, uint32_t type_id); |
| 422 |
|
| 423 |
// Returns the result id of an OpConstant instruction. |scalar_type_id| must be |
| 424 |
// a result id of a scalar type (i.e. int, float or bool). Returns 0 if no such |
| 425 |
// instruction is present in the module. The returned id either participates in |
| 426 |
// IdIsIrrelevant fact or not, depending on the |is_irrelevant| parameter. |
| 427 |
uint32_t MaybeGetScalarConstant( |
| 428 |
opt::IRContext* ir_context, |
| 429 |
const TransformationContext& transformation_context, |
| 430 |
const std::vector<uint32_t>& words, uint32_t scalar_type_id, |
| 431 |
bool is_irrelevant); |
| 432 |
|
| 433 |
// Returns the result id of an OpConstantComposite instruction. |
| 434 |
// |composite_type_id| must be a result id of a composite type (i.e. vector, |
| 435 |
// matrix, struct or array). Returns 0 if no such instruction is present in the |
| 436 |
// module. The returned id either participates in IdIsIrrelevant fact or not, |
| 437 |
// depending on the |is_irrelevant| parameter. |
| 438 |
uint32_t MaybeGetCompositeConstant( |
| 439 |
opt::IRContext* ir_context, |
| 440 |
const TransformationContext& transformation_context, |
| 441 |
const std::vector<uint32_t>& component_ids, uint32_t composite_type_id, |
| 442 |
bool is_irrelevant); |
| 443 |
|
| 444 |
// Returns the result id of an OpConstant instruction of integral type. |
| 445 |
// Returns 0 if no such instruction or type is present in the module. |
| 446 |
// The returned id either participates in IdIsIrrelevant fact or not, depending |
| 447 |
// on the |is_irrelevant| parameter. |
| 448 |
uint32_t MaybeGetIntegerConstant( |
| 449 |
opt::IRContext* ir_context, |
| 450 |
const TransformationContext& transformation_context, |
| 451 |
const std::vector<uint32_t>& words, uint32_t width, bool is_signed, |
| 452 |
bool is_irrelevant); |
| 453 |
|
| 454 |
// Returns the id of a 32-bit integer constant in the module with type |
| 455 |
// |int_type_id| and value |value|, or 0 if no such constant exists in the |
| 456 |
// module. |int_type_id| must exist in the module and it must correspond to a |
| 457 |
// 32-bit integer type. |
| 458 |
uint32_t MaybeGetIntegerConstantFromValueAndType(opt::IRContext* ir_context, |
| 459 |
uint32_t value, |
| 460 |
uint32_t int_type_id); |
| 461 |
|
| 462 |
// Returns the result id of an OpConstant instruction of floating-point type. |
| 463 |
// Returns 0 if no such instruction or type is present in the module. |
| 464 |
// The returned id either participates in IdIsIrrelevant fact or not, depending |
| 465 |
// on the |is_irrelevant| parameter. |
| 466 |
uint32_t MaybeGetFloatConstant( |
| 467 |
opt::IRContext* ir_context, |
| 468 |
const TransformationContext& transformation_context, |
| 469 |
const std::vector<uint32_t>& words, uint32_t width, bool is_irrelevant); |
| 470 |
|
| 471 |
// Returns the id of a boolean constant with value |value| if it exists in the |
| 472 |
// module, or 0 otherwise. The returned id either participates in IdIsIrrelevant |
| 473 |
// fact or not, depending on the |is_irrelevant| parameter. |
| 474 |
uint32_t MaybeGetBoolConstant( |
| 475 |
opt::IRContext* context, |
| 476 |
const TransformationContext& transformation_context, bool value, |
| 477 |
bool is_irrelevant); |
| 478 |
|
| 479 |
// Creates a new OpTypeInt instruction in the module. Updates module's id bound |
| 480 |
// to accommodate for |result_id|. |
| 481 |
void AddIntegerType(opt::IRContext* ir_context, uint32_t result_id, |
| 482 |
uint32_t width, bool is_signed); |
| 483 |
|
| 484 |
// Creates a new OpTypeFloat instruction in the module. Updates module's id |
| 485 |
// bound to accommodate for |result_id|. |
| 486 |
void AddFloatType(opt::IRContext* ir_context, uint32_t result_id, |
| 487 |
uint32_t width); |
| 488 |
|
| 489 |
// Creates a new OpTypeVector instruction in the module. |component_type_id| |
| 490 |
// must be a valid result id of an OpTypeInt, OpTypeFloat or OpTypeBool |
| 491 |
// instruction in the module. |element_count| must be in the range [2, 4]. |
| 492 |
// Updates module's id bound to accommodate for |result_id|. |
| 493 |
void AddVectorType(opt::IRContext* ir_context, uint32_t result_id, |
| 494 |
uint32_t component_type_id, uint32_t element_count); |
| 495 |
|
| 496 |
// Creates a new OpTypeStruct instruction in the module. Updates module's id |
| 497 |
// bound to accommodate for |result_id|. |component_type_ids| may not contain |
| 498 |
// a result id of an OpTypeFunction. if |component_type_ids| contains a result |
| 499 |
// of an OpTypeStruct instruction, that struct may not have BuiltIn members. |
| 500 |
void AddStructType(opt::IRContext* ir_context, uint32_t result_id, |
| 501 |
const std::vector<uint32_t>& component_type_ids); |
| 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|. |
| 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 |
} // namespace fuzzerutil |
| 592 |
} // namespace fuzz |
| 593 |
} // namespace spvtools |
| 594 |
|
| 595 |
#endif // SOURCE_FUZZ_FUZZER_UTIL_H_ |