inst_debug_printf_pass.h 4.0 KB

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