inst_debug_printf_pass.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include "inst_debug_printf_pass.h"
  17. #include "source/util/string_utils.h"
  18. #include "spirv/unified1/NonSemanticDebugPrintf.h"
  19. namespace spvtools {
  20. namespace opt {
  21. void InstDebugPrintfPass::GenOutputValues(Instruction* val_inst,
  22. std::vector<uint32_t>* val_ids,
  23. InstructionBuilder* builder) {
  24. uint32_t val_ty_id = val_inst->type_id();
  25. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  26. analysis::Type* val_ty = type_mgr->GetType(val_ty_id);
  27. switch (val_ty->kind()) {
  28. case analysis::Type::kVector: {
  29. analysis::Vector* v_ty = val_ty->AsVector();
  30. const analysis::Type* c_ty = v_ty->element_type();
  31. uint32_t c_ty_id = type_mgr->GetId(c_ty);
  32. for (uint32_t c = 0; c < v_ty->element_count(); ++c) {
  33. Instruction* c_inst =
  34. builder->AddCompositeExtract(c_ty_id, val_inst->result_id(), {c});
  35. GenOutputValues(c_inst, val_ids, builder);
  36. }
  37. return;
  38. }
  39. case analysis::Type::kBool: {
  40. // Select between uint32 zero or one
  41. uint32_t zero_id = builder->GetUintConstantId(0);
  42. uint32_t one_id = builder->GetUintConstantId(1);
  43. Instruction* sel_inst = builder->AddSelect(
  44. GetUintId(), val_inst->result_id(), one_id, zero_id);
  45. val_ids->push_back(sel_inst->result_id());
  46. return;
  47. }
  48. case analysis::Type::kFloat: {
  49. analysis::Float* f_ty = val_ty->AsFloat();
  50. switch (f_ty->width()) {
  51. case 16: {
  52. // Convert float16 to float32 and recurse
  53. Instruction* f32_inst = builder->AddUnaryOp(
  54. GetFloatId(), spv::Op::OpFConvert, val_inst->result_id());
  55. GenOutputValues(f32_inst, val_ids, builder);
  56. return;
  57. }
  58. case 64: {
  59. // Bitcast float64 to uint64 and recurse
  60. Instruction* ui64_inst = builder->AddUnaryOp(
  61. GetUint64Id(), spv::Op::OpBitcast, val_inst->result_id());
  62. GenOutputValues(ui64_inst, val_ids, builder);
  63. return;
  64. }
  65. case 32: {
  66. // Bitcase float32 to uint32
  67. Instruction* bc_inst = builder->AddUnaryOp(
  68. GetUintId(), spv::Op::OpBitcast, val_inst->result_id());
  69. val_ids->push_back(bc_inst->result_id());
  70. return;
  71. }
  72. default:
  73. assert(false && "unsupported float width");
  74. return;
  75. }
  76. }
  77. case analysis::Type::kInteger: {
  78. analysis::Integer* i_ty = val_ty->AsInteger();
  79. switch (i_ty->width()) {
  80. case 64: {
  81. Instruction* ui64_inst = val_inst;
  82. if (i_ty->IsSigned()) {
  83. // Bitcast sint64 to uint64
  84. ui64_inst = builder->AddUnaryOp(GetUint64Id(), spv::Op::OpBitcast,
  85. val_inst->result_id());
  86. }
  87. // Break uint64 into 2x uint32
  88. Instruction* lo_ui64_inst = builder->AddUnaryOp(
  89. GetUintId(), spv::Op::OpUConvert, ui64_inst->result_id());
  90. Instruction* rshift_ui64_inst = builder->AddBinaryOp(
  91. GetUint64Id(), spv::Op::OpShiftRightLogical,
  92. ui64_inst->result_id(), builder->GetUintConstantId(32));
  93. Instruction* hi_ui64_inst = builder->AddUnaryOp(
  94. GetUintId(), spv::Op::OpUConvert, rshift_ui64_inst->result_id());
  95. val_ids->push_back(lo_ui64_inst->result_id());
  96. val_ids->push_back(hi_ui64_inst->result_id());
  97. return;
  98. }
  99. case 8: {
  100. Instruction* ui8_inst = val_inst;
  101. if (i_ty->IsSigned()) {
  102. // Bitcast sint8 to uint8
  103. ui8_inst = builder->AddUnaryOp(GetUint8Id(), spv::Op::OpBitcast,
  104. val_inst->result_id());
  105. }
  106. // Convert uint8 to uint32
  107. Instruction* ui32_inst = builder->AddUnaryOp(
  108. GetUintId(), spv::Op::OpUConvert, ui8_inst->result_id());
  109. val_ids->push_back(ui32_inst->result_id());
  110. return;
  111. }
  112. case 32: {
  113. Instruction* ui32_inst = val_inst;
  114. if (i_ty->IsSigned()) {
  115. // Bitcast sint32 to uint32
  116. ui32_inst = builder->AddUnaryOp(GetUintId(), spv::Op::OpBitcast,
  117. val_inst->result_id());
  118. }
  119. // uint32 needs no further processing
  120. val_ids->push_back(ui32_inst->result_id());
  121. return;
  122. }
  123. default:
  124. // TODO(greg-lunarg): Support non-32-bit int
  125. assert(false && "unsupported int width");
  126. return;
  127. }
  128. }
  129. default:
  130. assert(false && "unsupported type");
  131. return;
  132. }
  133. }
  134. void InstDebugPrintfPass::GenOutputCode(
  135. Instruction* printf_inst, uint32_t stage_idx,
  136. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  137. BasicBlock* back_blk_ptr = &*new_blocks->back();
  138. InstructionBuilder builder(
  139. context(), back_blk_ptr,
  140. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  141. // Gen debug printf record validation-specific values. The format string
  142. // will have its id written. Vectors will need to be broken down into
  143. // component values. float16 will need to be converted to float32. Pointer
  144. // and uint64 will need to be converted to two uint32 values. float32 will
  145. // need to be bitcast to uint32. int32 will need to be bitcast to uint32.
  146. std::vector<uint32_t> val_ids;
  147. bool is_first_operand = false;
  148. printf_inst->ForEachInId(
  149. [&is_first_operand, &val_ids, &builder, this](const uint32_t* iid) {
  150. // skip set operand
  151. if (!is_first_operand) {
  152. is_first_operand = true;
  153. return;
  154. }
  155. Instruction* opnd_inst = get_def_use_mgr()->GetDef(*iid);
  156. if (opnd_inst->opcode() == spv::Op::OpString) {
  157. uint32_t string_id_id = builder.GetUintConstantId(*iid);
  158. val_ids.push_back(string_id_id);
  159. } else {
  160. GenOutputValues(opnd_inst, &val_ids, &builder);
  161. }
  162. });
  163. GenDebugStreamWrite(uid2offset_[printf_inst->unique_id()], stage_idx, val_ids,
  164. &builder);
  165. context()->KillInst(printf_inst);
  166. }
  167. void InstDebugPrintfPass::GenDebugPrintfCode(
  168. BasicBlock::iterator ref_inst_itr,
  169. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  170. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  171. // If not DebugPrintf OpExtInst, return.
  172. Instruction* printf_inst = &*ref_inst_itr;
  173. if (printf_inst->opcode() != spv::Op::OpExtInst) return;
  174. if (printf_inst->GetSingleWordInOperand(0) != ext_inst_printf_id_) return;
  175. if (printf_inst->GetSingleWordInOperand(1) !=
  176. NonSemanticDebugPrintfDebugPrintf)
  177. return;
  178. // Initialize DefUse manager before dismantling module
  179. (void)get_def_use_mgr();
  180. // Move original block's preceding instructions into first new block
  181. std::unique_ptr<BasicBlock> new_blk_ptr;
  182. MovePreludeCode(ref_inst_itr, ref_block_itr, &new_blk_ptr);
  183. new_blocks->push_back(std::move(new_blk_ptr));
  184. // Generate instructions to output printf args to printf buffer
  185. GenOutputCode(printf_inst, stage_idx, new_blocks);
  186. // Caller expects at least two blocks with last block containing remaining
  187. // code, so end block after instrumentation, create remainder block, and
  188. // branch to it
  189. uint32_t rem_blk_id = TakeNextId();
  190. std::unique_ptr<Instruction> rem_label(NewLabel(rem_blk_id));
  191. BasicBlock* back_blk_ptr = &*new_blocks->back();
  192. InstructionBuilder builder(
  193. context(), back_blk_ptr,
  194. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  195. (void)builder.AddBranch(rem_blk_id);
  196. // Gen remainder block
  197. new_blk_ptr.reset(new BasicBlock(std::move(rem_label)));
  198. builder.SetInsertPoint(&*new_blk_ptr);
  199. // Move original block's remaining code into remainder block and add
  200. // to new blocks
  201. MovePostludeCode(ref_block_itr, &*new_blk_ptr);
  202. new_blocks->push_back(std::move(new_blk_ptr));
  203. }
  204. void InstDebugPrintfPass::InitializeInstDebugPrintf() {
  205. // Initialize base class
  206. InitializeInstrument();
  207. }
  208. Pass::Status InstDebugPrintfPass::ProcessImpl() {
  209. // Perform printf instrumentation on each entry point function in module
  210. InstProcessFunction pfn =
  211. [this](BasicBlock::iterator ref_inst_itr,
  212. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  213. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  214. return GenDebugPrintfCode(ref_inst_itr, ref_block_itr, stage_idx,
  215. new_blocks);
  216. };
  217. (void)InstProcessEntryPointCallTree(pfn);
  218. // Remove DebugPrintf OpExtInstImport instruction
  219. Instruction* ext_inst_import_inst =
  220. get_def_use_mgr()->GetDef(ext_inst_printf_id_);
  221. context()->KillInst(ext_inst_import_inst);
  222. // If no remaining non-semantic instruction sets, remove non-semantic debug
  223. // info extension from module and feature manager
  224. bool non_sem_set_seen = false;
  225. for (auto c_itr = context()->module()->ext_inst_import_begin();
  226. c_itr != context()->module()->ext_inst_import_end(); ++c_itr) {
  227. const std::string set_name = c_itr->GetInOperand(0).AsString();
  228. if (spvtools::utils::starts_with(set_name, "NonSemantic.")) {
  229. non_sem_set_seen = true;
  230. break;
  231. }
  232. }
  233. if (!non_sem_set_seen) {
  234. for (auto c_itr = context()->module()->extension_begin();
  235. c_itr != context()->module()->extension_end(); ++c_itr) {
  236. const std::string ext_name = c_itr->GetInOperand(0).AsString();
  237. if (ext_name == "SPV_KHR_non_semantic_info") {
  238. context()->KillInst(&*c_itr);
  239. break;
  240. }
  241. }
  242. context()->get_feature_mgr()->RemoveExtension(kSPV_KHR_non_semantic_info);
  243. }
  244. return Status::SuccessWithChange;
  245. }
  246. Pass::Status InstDebugPrintfPass::Process() {
  247. ext_inst_printf_id_ =
  248. get_module()->GetExtInstImportId("NonSemantic.DebugPrintf");
  249. if (ext_inst_printf_id_ == 0) return Status::SuccessWithoutChange;
  250. InitializeInstDebugPrintf();
  251. return ProcessImpl();
  252. }
  253. } // namespace opt
  254. } // namespace spvtools