1 // Copyright (c) 2020 The Khronos Group Inc. 2 // Copyright (c) 2020 Valve Corporation 3 // Copyright (c) 2020 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_INST_DEBUG_PRINTF_PASS_H_ 18 #define LIBSPIRV_OPT_INST_DEBUG_PRINTF_PASS_H_ 19 20 #include "instrument_pass.h" 21 22 namespace spvtools { 23 namespace opt { 24 25 // This class/pass is designed to support the debug printf GPU-assisted layer 26 // of https://github.com/KhronosGroup/Vulkan-ValidationLayers. Its internal and 27 // external design may change as the layer evolves. 28 class InstDebugPrintfPass : public InstrumentPass { 29 public: 30 // For test harness only InstDebugPrintfPass()31 InstDebugPrintfPass() : InstrumentPass(7, 23, kInstValidationIdDebugPrintf) {} 32 // For all other interfaces InstDebugPrintfPass(uint32_t desc_set,uint32_t shader_id)33 InstDebugPrintfPass(uint32_t desc_set, uint32_t shader_id) 34 : InstrumentPass(desc_set, shader_id, kInstValidationIdDebugPrintf) {} 35 36 ~InstDebugPrintfPass() override = default; 37 38 // See optimizer.hpp for pass user documentation. 39 Status Process() override; 40 name()41 const char* name() const override { return "inst-printf-pass"; } 42 43 private: 44 // Generate instructions for OpDebugPrintf. 45 // 46 // If |ref_inst_itr| is an OpDebugPrintf, return in |new_blocks| the result 47 // of replacing it with buffer write instructions within its block at 48 // |ref_block_itr|. The instructions write a record to the printf 49 // output buffer stream including |function_idx, instruction_idx, stage_idx| 50 // and removes the OpDebugPrintf. The block at |ref_block_itr| can just be 51 // replaced with the block in |new_blocks|. Besides the buffer writes, this 52 // block will comprise all instructions preceding and following 53 // |ref_inst_itr|. 54 // 55 // This function is designed to be passed to 56 // InstrumentPass::InstProcessEntryPointCallTree(), which applies the 57 // function to each instruction in a module and replaces the instruction 58 // if warranted. 59 // 60 // This instrumentation function utilizes GenDebugStreamWrite() to write its 61 // error records. The validation-specific part of the error record will 62 // consist of a uint32 which is the id of the format string plus a sequence 63 // of uint32s representing the values of the remaining operands of the 64 // DebugPrintf. 65 void GenDebugPrintfCode(BasicBlock::iterator ref_inst_itr, 66 UptrVectorIterator<BasicBlock> ref_block_itr, 67 uint32_t stage_idx, 68 std::vector<std::unique_ptr<BasicBlock>>* new_blocks); 69 70 // Generate a sequence of uint32 instructions in |builder| (if necessary) 71 // representing the value of |val_inst|, which must be a buffer pointer, a 72 // uint64, or a scalar or vector of type uint32, float32 or float16. Append 73 // the ids of all values to the end of |val_ids|. 74 void GenOutputValues(Instruction* val_inst, std::vector<uint32_t>* val_ids, 75 InstructionBuilder* builder); 76 77 // Generate instructions to write a record containing the operands of 78 // |printf_inst| arguments to printf buffer, adding new code to the end of 79 // the last block in |new_blocks|. Kill OpDebugPrintf instruction. 80 void GenOutputCode(Instruction* printf_inst, uint32_t stage_idx, 81 std::vector<std::unique_ptr<BasicBlock>>* new_blocks); 82 83 // Initialize state for instrumenting bindless checking 84 void InitializeInstDebugPrintf(); 85 86 // Apply GenDebugPrintfCode to every instruction in module. 87 Pass::Status ProcessImpl(); 88 89 uint32_t ext_inst_printf_id_; 90 }; 91 92 } // namespace opt 93 } // namespace spvtools 94 95 #endif // LIBSPIRV_OPT_INST_DEBUG_PRINTF_PASS_H_ 96