inst_debug_printf_pass.cpp 11 KB

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