inst_debug_printf_pass.h 4.0 KB

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