1 // Copyright (c) 2018 The Khronos Group Inc. 2 // Copyright (c) 2018 Valve Corporation 3 // Copyright (c) 2018 LunarG Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #ifndef LIBSPIRV_OPT_INSTRUMENT_PASS_H_ 18 #define LIBSPIRV_OPT_INSTRUMENT_PASS_H_ 19 20 #include <list> 21 #include <memory> 22 #include <vector> 23 24 #include "source/opt/ir_builder.h" 25 #include "source/opt/pass.h" 26 #include "spirv-tools/instrument.hpp" 27 28 // This is a base class to assist in the creation of passes which instrument 29 // shader modules. More specifically, passes which replace instructions with a 30 // larger and more capable set of instructions. Commonly, these new 31 // instructions will add testing of operands and execute different 32 // instructions depending on the outcome, including outputting of debug 33 // information into a buffer created especially for that purpose. 34 // 35 // This class contains helper functions to create an InstProcessFunction, 36 // which is the heart of any derived class implementing a specific 37 // instrumentation pass. It takes an instruction as an argument, decides 38 // if it should be instrumented, and generates code to replace it. This class 39 // also supplies function InstProcessEntryPointCallTree which applies the 40 // InstProcessFunction to every reachable instruction in a module and replaces 41 // the instruction with new instructions if generated. 42 // 43 // Chief among the helper functions are output code generation functions, 44 // used to generate code in the shader which writes data to output buffers 45 // associated with that validation. Currently one such function, 46 // GenDebugStreamWrite, exists. Other such functions may be added in the 47 // future. Each is accompanied by documentation describing the format of 48 // its output buffer. 49 // 50 // A validation pass may read or write multiple buffers. All such buffers 51 // are located in a single debug descriptor set whose index is passed at the 52 // creation of the instrumentation pass. The bindings of the buffers used by 53 // a validation pass are permanently assigned and fixed and documented by 54 // the kDebugOutput* static consts. 55 56 namespace spvtools { 57 namespace opt { 58 59 // Validation Ids 60 // These are used to identify the general validation being done and map to 61 // its output buffers. 62 static const uint32_t kInstValidationIdBindless = 0; 63 static const uint32_t kInstValidationIdBuffAddr = 1; 64 static const uint32_t kInstValidationIdDebugPrintf = 2; 65 66 class InstrumentPass : public Pass { 67 using cbb_ptr = const BasicBlock*; 68 69 public: 70 using InstProcessFunction = 71 std::function<void(BasicBlock::iterator, UptrVectorIterator<BasicBlock>, 72 uint32_t, std::vector<std::unique_ptr<BasicBlock>>*)>; 73 74 ~InstrumentPass() override = default; 75 GetPreservedAnalyses()76 IRContext::Analysis GetPreservedAnalyses() override { 77 return IRContext::kAnalysisDefUse | IRContext::kAnalysisDecorations | 78 IRContext::kAnalysisCombinators | IRContext::kAnalysisNameMap | 79 IRContext::kAnalysisBuiltinVarId | IRContext::kAnalysisConstants; 80 } 81 82 protected: 83 // Create instrumentation pass for |validation_id| which utilizes descriptor 84 // set |desc_set| for debug input and output buffers and writes |shader_id| 85 // into debug output records. |opt_direct_reads| indicates that the pass 86 // will see direct input buffer reads and should prepare to optimize them. 87 InstrumentPass(uint32_t desc_set, uint32_t shader_id, uint32_t validation_id, 88 bool opt_direct_reads = false) Pass()89 : Pass(), 90 desc_set_(desc_set), 91 shader_id_(shader_id), 92 validation_id_(validation_id), 93 opt_direct_reads_(opt_direct_reads) {} 94 95 // Initialize state for instrumentation of module. 96 void InitializeInstrument(); 97 98 // Call |pfn| on all instructions in all functions in the call tree of the 99 // entry points in |module|. If code is generated for an instruction, replace 100 // the instruction's block with the new blocks that are generated. Continue 101 // processing at the top of the last new block. 102 bool InstProcessEntryPointCallTree(InstProcessFunction& pfn); 103 104 // Move all code in |ref_block_itr| preceding the instruction |ref_inst_itr| 105 // to be instrumented into block |new_blk_ptr|. 106 void MovePreludeCode(BasicBlock::iterator ref_inst_itr, 107 UptrVectorIterator<BasicBlock> ref_block_itr, 108 std::unique_ptr<BasicBlock>* new_blk_ptr); 109 110 // Move all code in |ref_block_itr| succeeding the instruction |ref_inst_itr| 111 // to be instrumented into block |new_blk_ptr|. 112 void MovePostludeCode(UptrVectorIterator<BasicBlock> ref_block_itr, 113 BasicBlock* new_blk_ptr); 114 115 // Generate instructions in |builder| which will atomically fetch and 116 // increment the size of the debug output buffer stream of the current 117 // validation and write a record to the end of the stream, if enough space 118 // in the buffer remains. The record will contain the index of the function 119 // and instruction within that function |func_idx, instruction_idx| which 120 // generated the record. It will also contain additional information to 121 // identify the instance of the shader, depending on the stage |stage_idx| 122 // of the shader. Finally, the record will contain validation-specific 123 // data contained in |validation_ids| which will identify the validation 124 // error as well as the values involved in the error. 125 // 126 // The output buffer binding written to by the code generated by the function 127 // is determined by the validation id specified when each specific 128 // instrumentation pass is created. 129 // 130 // The output buffer is a sequence of 32-bit values with the following 131 // format (where all elements are unsigned 32-bit unless otherwise noted): 132 // 133 // Size 134 // Record0 135 // Record1 136 // Record2 137 // ... 138 // 139 // Size is the number of 32-bit values that have been written or 140 // attempted to be written to the output buffer, excluding the Size. It is 141 // initialized to 0. If the size of attempts to write the buffer exceeds 142 // the actual size of the buffer, it is possible that this field can exceed 143 // the actual size of the buffer. 144 // 145 // Each Record* is a variable-length sequence of 32-bit values with the 146 // following format defined using static const offsets in the .cpp file: 147 // 148 // Record Size 149 // Shader ID 150 // Instruction Index 151 // Stage 152 // Stage-specific Word 0 153 // Stage-specific Word 1 154 // ... 155 // Validation Error Code 156 // Validation-specific Word 0 157 // Validation-specific Word 1 158 // Validation-specific Word 2 159 // ... 160 // 161 // Each record consists of three subsections: members common across all 162 // validation, members specific to the stage, and members specific to a 163 // validation. 164 // 165 // The Record Size is the number of 32-bit words in the record, including 166 // the Record Size word. 167 // 168 // Shader ID is a value that identifies which shader has generated the 169 // validation error. It is passed when the instrumentation pass is created. 170 // 171 // The Instruction Index is the position of the instruction within the 172 // SPIR-V file which is in error. 173 // 174 // The Stage is the pipeline stage which has generated the error as defined 175 // by the SpvExecutionModel_ enumeration. This is used to interpret the 176 // following Stage-specific words. 177 // 178 // The Stage-specific Words identify which invocation of the shader generated 179 // the error. Every stage will write a fixed number of words. Vertex shaders 180 // will write the Vertex and Instance ID. Fragment shaders will write 181 // FragCoord.xy. Compute shaders will write the GlobalInvocation ID. 182 // The tessellation eval shader will write the Primitive ID and TessCoords.uv. 183 // The tessellation control shader and geometry shader will write the 184 // Primitive ID and Invocation ID. 185 // 186 // The Validation Error Code specifies the exact error which has occurred. 187 // These are enumerated with the kInstError* static consts. This allows 188 // multiple validation layers to use the same, single output buffer. 189 // 190 // The Validation-specific Words are a validation-specific number of 32-bit 191 // words which give further information on the validation error that 192 // occurred. These are documented further in each file containing the 193 // validation-specific class which derives from this base class. 194 // 195 // Because the code that is generated checks against the size of the buffer 196 // before writing, the size of the debug out buffer can be used by the 197 // validation layer to control the number of error records that are written. 198 void GenDebugStreamWrite(uint32_t instruction_idx, uint32_t stage_idx, 199 const std::vector<uint32_t>& validation_ids, 200 InstructionBuilder* builder); 201 202 // Return true if all instructions in |ids| are constants or spec constants. 203 bool AllConstant(const std::vector<uint32_t>& ids); 204 205 // Generate in |builder| instructions to read the unsigned integer from the 206 // input buffer specified by the offsets in |offset_ids|. Given offsets 207 // o0, o1, ... oN, and input buffer ibuf, return the id for the value: 208 // 209 // ibuf[...ibuf[ibuf[o0]+o1]...+oN] 210 // 211 // The binding and the format of the input buffer is determined by each 212 // specific validation, which is specified at the creation of the pass. 213 uint32_t GenDebugDirectRead(const std::vector<uint32_t>& offset_ids, 214 InstructionBuilder* builder); 215 216 // Generate code to convert integer |value_id| to 32bit, if needed. Return 217 // an id to the 32bit equivalent. 218 uint32_t Gen32BitCvtCode(uint32_t value_id, InstructionBuilder* builder); 219 220 // Generate code to cast integer |value_id| to 32bit unsigned, if needed. 221 // Return an id to the Uint equivalent. 222 uint32_t GenUintCastCode(uint32_t value_id, InstructionBuilder* builder); 223 224 // Return new label. 225 std::unique_ptr<Instruction> NewLabel(uint32_t label_id); 226 227 // Set the name function parameter or local variable 228 std::unique_ptr<Instruction> NewName(uint32_t id, 229 const std::string& name_str); 230 231 // Set the name for a function or global variable, names will be 232 // prefixed to identify which instrumentation pass generated them. 233 std::unique_ptr<Instruction> NewGlobalName(uint32_t id, 234 const std::string& name_str); 235 236 // Set the name for a structure member 237 std::unique_ptr<Instruction> NewMemberName(uint32_t id, uint32_t member_index, 238 const std::string& name_str); 239 240 // Return id for 32-bit unsigned type 241 uint32_t GetUintId(); 242 243 // Return id for 64-bit unsigned type 244 uint32_t GetUint64Id(); 245 246 // Return id for 8-bit unsigned type 247 uint32_t GetUint8Id(); 248 249 // Return id for 32-bit unsigned type 250 uint32_t GetBoolId(); 251 252 // Return id for void type 253 uint32_t GetVoidId(); 254 255 // Return pointer to type for runtime array of uint 256 analysis::Type* GetUintXRuntimeArrayType(uint32_t width, 257 analysis::Type** rarr_ty); 258 259 // Return pointer to type for runtime array of uint 260 analysis::Type* GetUintRuntimeArrayType(uint32_t width); 261 262 // Return id for buffer uint type 263 uint32_t GetOutputBufferPtrId(); 264 265 // Return id for buffer uint type 266 uint32_t GetInputBufferTypeId(); 267 268 // Return id for buffer uint type 269 uint32_t GetInputBufferPtrId(); 270 271 // Return binding for output buffer for current validation. 272 uint32_t GetOutputBufferBinding(); 273 274 // Return binding for input buffer for current validation. 275 uint32_t GetInputBufferBinding(); 276 277 // Add storage buffer extension if needed 278 void AddStorageBufferExt(); 279 280 // Return id for debug output buffer 281 uint32_t GetOutputBufferId(); 282 283 // Return id for debug input buffer 284 uint32_t GetInputBufferId(); 285 286 // Return id for 32-bit float type 287 uint32_t GetFloatId(); 288 289 // Return id for v4float type 290 uint32_t GetVec4FloatId(); 291 292 // Return id for uint vector type of |length| 293 uint32_t GetVecUintId(uint32_t length); 294 295 // Return id for v4uint type 296 uint32_t GetVec4UintId(); 297 298 // Return id for v3uint type 299 uint32_t GetVec3UintId(); 300 301 // Return id for output function. Define if it doesn't exist with 302 // |val_spec_param_cnt| validation-specific uint32 parameters. 303 uint32_t GetStreamWriteFunctionId(uint32_t stage_idx, 304 uint32_t val_spec_param_cnt); 305 306 // Return id for input function taking |param_cnt| uint32 parameters. Define 307 // if it doesn't exist. 308 uint32_t GetDirectReadFunctionId(uint32_t param_cnt); 309 310 // Split block |block_itr| into two new blocks where the second block 311 // contains |inst_itr| and place in |new_blocks|. 312 void SplitBlock(BasicBlock::iterator inst_itr, 313 UptrVectorIterator<BasicBlock> block_itr, 314 std::vector<std::unique_ptr<BasicBlock>>* new_blocks); 315 316 // Apply instrumentation function |pfn| to every instruction in |func|. 317 // If code is generated for an instruction, replace the instruction's 318 // block with the new blocks that are generated. Continue processing at the 319 // top of the last new block. 320 bool InstrumentFunction(Function* func, uint32_t stage_idx, 321 InstProcessFunction& pfn); 322 323 // Call |pfn| on all functions in the call tree of the function 324 // ids in |roots|. 325 bool InstProcessCallTreeFromRoots(InstProcessFunction& pfn, 326 std::queue<uint32_t>* roots, 327 uint32_t stage_idx); 328 329 // Gen code into |builder| to write |field_value_id| into debug output 330 // buffer at |base_offset_id| + |field_offset|. 331 void GenDebugOutputFieldCode(uint32_t base_offset_id, uint32_t field_offset, 332 uint32_t field_value_id, 333 InstructionBuilder* builder); 334 335 // Generate instructions into |builder| which will write the members 336 // of the debug output record common for all stages and validations at 337 // |base_off|. 338 void GenCommonStreamWriteCode(uint32_t record_sz, uint32_t instruction_idx, 339 uint32_t stage_idx, uint32_t base_off, 340 InstructionBuilder* builder); 341 342 // Generate instructions into |builder| which will write 343 // |uint_frag_coord_id| at |component| of the record at |base_offset_id| of 344 // the debug output buffer . 345 void GenFragCoordEltDebugOutputCode(uint32_t base_offset_id, 346 uint32_t uint_frag_coord_id, 347 uint32_t component, 348 InstructionBuilder* builder); 349 350 // Generate instructions into |builder| which will load |var_id| and return 351 // its result id. 352 uint32_t GenVarLoad(uint32_t var_id, InstructionBuilder* builder); 353 354 // Generate instructions into |builder| which will load the uint |builtin_id| 355 // and write it into the debug output buffer at |base_off| + |builtin_off|. 356 void GenBuiltinOutputCode(uint32_t builtin_id, uint32_t builtin_off, 357 uint32_t base_off, InstructionBuilder* builder); 358 359 // Generate instructions into |builder| which will write the |stage_idx|- 360 // specific members of the debug output stream at |base_off|. 361 void GenStageStreamWriteCode(uint32_t stage_idx, uint32_t base_off, 362 InstructionBuilder* builder); 363 364 // Return true if instruction must be in the same block that its result 365 // is used. 366 bool IsSameBlockOp(const Instruction* inst) const; 367 368 // Clone operands which must be in same block as consumer instructions. 369 // Look in same_blk_pre for instructions that need cloning. Look in 370 // same_blk_post for instructions already cloned. Add cloned instruction 371 // to same_blk_post. 372 void CloneSameBlockOps( 373 std::unique_ptr<Instruction>* inst, 374 std::unordered_map<uint32_t, uint32_t>* same_blk_post, 375 std::unordered_map<uint32_t, Instruction*>* same_blk_pre, 376 BasicBlock* block_ptr); 377 378 // Update phis in succeeding blocks to point to new last block 379 void UpdateSucceedingPhis( 380 std::vector<std::unique_ptr<BasicBlock>>& new_blocks); 381 382 // Debug descriptor set index 383 uint32_t desc_set_; 384 385 // Shader module ID written into output record 386 uint32_t shader_id_; 387 388 // Map from function id to function pointer. 389 std::unordered_map<uint32_t, Function*> id2function_; 390 391 // Map from block's label id to block. TODO(dnovillo): This is superfluous wrt 392 // CFG. It has functionality not present in CFG. Consolidate. 393 std::unordered_map<uint32_t, BasicBlock*> id2block_; 394 395 // Map from instruction's unique id to offset in original file. 396 std::unordered_map<uint32_t, uint32_t> uid2offset_; 397 398 // result id for OpConstantFalse 399 uint32_t validation_id_; 400 401 // id for output buffer variable 402 uint32_t output_buffer_id_; 403 404 // ptr type id for output buffer element 405 uint32_t output_buffer_ptr_id_; 406 407 // ptr type id for input buffer element 408 uint32_t input_buffer_ptr_id_; 409 410 // id for debug output function 411 std::unordered_map<uint32_t, uint32_t> param2output_func_id_; 412 413 // ids for debug input functions 414 std::unordered_map<uint32_t, uint32_t> param2input_func_id_; 415 416 // id for input buffer variable 417 uint32_t input_buffer_id_; 418 419 // id for 32-bit float type 420 uint32_t float_id_; 421 422 // id for v4float type 423 uint32_t v4float_id_; 424 425 // id for v4uint type 426 uint32_t v4uint_id_; 427 428 // id for v3uint type 429 uint32_t v3uint_id_; 430 431 // id for 32-bit unsigned type 432 uint32_t uint_id_; 433 434 // id for 64-bit unsigned type 435 uint32_t uint64_id_; 436 437 // id for 8-bit unsigned type 438 uint32_t uint8_id_; 439 440 // id for bool type 441 uint32_t bool_id_; 442 443 // id for void type 444 uint32_t void_id_; 445 446 // boolean to remember storage buffer extension 447 bool storage_buffer_ext_defined_; 448 449 // runtime array of uint type 450 analysis::Type* uint64_rarr_ty_; 451 452 // runtime array of uint type 453 analysis::Type* uint32_rarr_ty_; 454 455 // Pre-instrumentation same-block insts 456 std::unordered_map<uint32_t, Instruction*> same_block_pre_; 457 458 // Post-instrumentation same-block op ids 459 std::unordered_map<uint32_t, uint32_t> same_block_post_; 460 461 // Map function calls to result id. Clear for every function. 462 // This is for debug input reads with constant arguments that 463 // have been generated into the first block of the function. 464 // This mechanism is used to avoid multiple identical debug 465 // input buffer reads. 466 struct vector_hash_ { operatorvector_hash_467 std::size_t operator()(const std::vector<uint32_t>& v) const { 468 std::size_t hash = v.size(); 469 for (auto& u : v) { 470 hash ^= u + 0x9e3779b9 + (hash << 11) + (hash >> 21); 471 } 472 return hash; 473 } 474 }; 475 std::unordered_map<std::vector<uint32_t>, uint32_t, vector_hash_> call2id_; 476 477 // Function currently being instrumented 478 Function* curr_func_; 479 480 // Optimize direct debug input buffer reads. Specifically, move all such 481 // reads with constant args to first block and reuse them. 482 bool opt_direct_reads_; 483 }; 484 485 } // namespace opt 486 } // namespace spvtools 487 488 #endif // LIBSPIRV_OPT_INSTRUMENT_PASS_H_ 489