2
0

inst_buff_addr_check_pass.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Copyright (c) 2019 The Khronos Group Inc.
  2. // Copyright (c) 2019 Valve Corporation
  3. // Copyright (c) 2019 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_buff_addr_check_pass.h"
  17. namespace spvtools {
  18. namespace opt {
  19. uint32_t InstBuffAddrCheckPass::CloneOriginalReference(
  20. Instruction* ref_inst, InstructionBuilder* builder) {
  21. // Clone original ref with new result id (if load)
  22. assert(
  23. (ref_inst->opcode() == SpvOpLoad || ref_inst->opcode() == SpvOpStore) &&
  24. "unexpected ref");
  25. std::unique_ptr<Instruction> new_ref_inst(ref_inst->Clone(context()));
  26. uint32_t ref_result_id = ref_inst->result_id();
  27. uint32_t new_ref_id = 0;
  28. if (ref_result_id != 0) {
  29. new_ref_id = TakeNextId();
  30. new_ref_inst->SetResultId(new_ref_id);
  31. }
  32. // Register new reference and add to new block
  33. Instruction* added_inst = builder->AddInstruction(std::move(new_ref_inst));
  34. uid2offset_[added_inst->unique_id()] = uid2offset_[ref_inst->unique_id()];
  35. if (new_ref_id != 0)
  36. get_decoration_mgr()->CloneDecorations(ref_result_id, new_ref_id);
  37. return new_ref_id;
  38. }
  39. bool InstBuffAddrCheckPass::IsPhysicalBuffAddrReference(Instruction* ref_inst) {
  40. if (ref_inst->opcode() != SpvOpLoad && ref_inst->opcode() != SpvOpStore)
  41. return false;
  42. uint32_t ptr_id = ref_inst->GetSingleWordInOperand(0);
  43. analysis::DefUseManager* du_mgr = get_def_use_mgr();
  44. Instruction* ptr_inst = du_mgr->GetDef(ptr_id);
  45. if (ptr_inst->opcode() != SpvOpAccessChain) return false;
  46. uint32_t ptr_ty_id = ptr_inst->type_id();
  47. Instruction* ptr_ty_inst = du_mgr->GetDef(ptr_ty_id);
  48. if (ptr_ty_inst->GetSingleWordInOperand(0) !=
  49. SpvStorageClassPhysicalStorageBufferEXT)
  50. return false;
  51. return true;
  52. }
  53. // TODO(greg-lunarg): Refactor with InstBindlessCheckPass::GenCheckCode() ??
  54. void InstBuffAddrCheckPass::GenCheckCode(
  55. uint32_t check_id, uint32_t error_id, uint32_t ref_uptr_id,
  56. uint32_t stage_idx, Instruction* ref_inst,
  57. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  58. BasicBlock* back_blk_ptr = &*new_blocks->back();
  59. InstructionBuilder builder(
  60. context(), back_blk_ptr,
  61. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  62. // Gen conditional branch on check_id. Valid branch generates original
  63. // reference. Invalid generates debug output and zero result (if needed).
  64. uint32_t merge_blk_id = TakeNextId();
  65. uint32_t valid_blk_id = TakeNextId();
  66. uint32_t invalid_blk_id = TakeNextId();
  67. std::unique_ptr<Instruction> merge_label(NewLabel(merge_blk_id));
  68. std::unique_ptr<Instruction> valid_label(NewLabel(valid_blk_id));
  69. std::unique_ptr<Instruction> invalid_label(NewLabel(invalid_blk_id));
  70. (void)builder.AddConditionalBranch(check_id, valid_blk_id, invalid_blk_id,
  71. merge_blk_id, SpvSelectionControlMaskNone);
  72. // Gen valid branch
  73. std::unique_ptr<BasicBlock> new_blk_ptr(
  74. new BasicBlock(std::move(valid_label)));
  75. builder.SetInsertPoint(&*new_blk_ptr);
  76. uint32_t new_ref_id = CloneOriginalReference(ref_inst, &builder);
  77. (void)builder.AddBranch(merge_blk_id);
  78. new_blocks->push_back(std::move(new_blk_ptr));
  79. // Gen invalid block
  80. new_blk_ptr.reset(new BasicBlock(std::move(invalid_label)));
  81. builder.SetInsertPoint(&*new_blk_ptr);
  82. // Convert uptr from uint64 to 2 uint32
  83. Instruction* lo_uptr_inst =
  84. builder.AddUnaryOp(GetUintId(), SpvOpUConvert, ref_uptr_id);
  85. Instruction* rshift_uptr_inst =
  86. builder.AddBinaryOp(GetUint64Id(), SpvOpShiftRightLogical, ref_uptr_id,
  87. builder.GetUintConstantId(32));
  88. Instruction* hi_uptr_inst = builder.AddUnaryOp(GetUintId(), SpvOpUConvert,
  89. rshift_uptr_inst->result_id());
  90. GenDebugStreamWrite(
  91. uid2offset_[ref_inst->unique_id()], stage_idx,
  92. {error_id, lo_uptr_inst->result_id(), hi_uptr_inst->result_id()},
  93. &builder);
  94. // Gen zero for invalid load. If pointer type, need to convert uint64
  95. // zero to pointer; cannot create ConstantNull of pointer type.
  96. uint32_t null_id = 0;
  97. if (new_ref_id != 0) {
  98. uint32_t ref_type_id = ref_inst->type_id();
  99. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  100. analysis::Type* ref_type = type_mgr->GetType(ref_type_id);
  101. if (ref_type->AsPointer() != nullptr) {
  102. uint32_t null_u64_id = GetNullId(GetUint64Id());
  103. Instruction* null_ptr_inst =
  104. builder.AddUnaryOp(ref_type_id, SpvOpConvertUToPtr, null_u64_id);
  105. null_id = null_ptr_inst->result_id();
  106. } else {
  107. null_id = GetNullId(ref_type_id);
  108. }
  109. }
  110. (void)builder.AddBranch(merge_blk_id);
  111. new_blocks->push_back(std::move(new_blk_ptr));
  112. // Gen merge block
  113. new_blk_ptr.reset(new BasicBlock(std::move(merge_label)));
  114. builder.SetInsertPoint(&*new_blk_ptr);
  115. // Gen phi of new reference and zero, if necessary, and replace the
  116. // result id of the original reference with that of the Phi. Kill original
  117. // reference.
  118. if (new_ref_id != 0) {
  119. Instruction* phi_inst =
  120. builder.AddPhi(ref_inst->type_id(),
  121. {new_ref_id, valid_blk_id, null_id, invalid_blk_id});
  122. context()->ReplaceAllUsesWith(ref_inst->result_id(), phi_inst->result_id());
  123. }
  124. new_blocks->push_back(std::move(new_blk_ptr));
  125. context()->KillInst(ref_inst);
  126. }
  127. uint32_t InstBuffAddrCheckPass::GetTypeLength(uint32_t type_id) {
  128. Instruction* type_inst = get_def_use_mgr()->GetDef(type_id);
  129. switch (type_inst->opcode()) {
  130. case SpvOpTypeFloat:
  131. case SpvOpTypeInt:
  132. return type_inst->GetSingleWordInOperand(0) / 8u;
  133. case SpvOpTypeVector:
  134. case SpvOpTypeMatrix:
  135. return type_inst->GetSingleWordInOperand(1) *
  136. GetTypeLength(type_inst->GetSingleWordInOperand(0));
  137. case SpvOpTypePointer:
  138. assert(type_inst->GetSingleWordInOperand(0) ==
  139. SpvStorageClassPhysicalStorageBufferEXT &&
  140. "unexpected pointer type");
  141. return 8u;
  142. default:
  143. assert(false && "unexpected buffer reference type");
  144. return 0;
  145. }
  146. }
  147. void InstBuffAddrCheckPass::AddParam(uint32_t type_id,
  148. std::vector<uint32_t>* param_vec,
  149. std::unique_ptr<Function>* input_func) {
  150. uint32_t pid = TakeNextId();
  151. param_vec->push_back(pid);
  152. std::unique_ptr<Instruction> param_inst(new Instruction(
  153. get_module()->context(), SpvOpFunctionParameter, type_id, pid, {}));
  154. get_def_use_mgr()->AnalyzeInstDefUse(&*param_inst);
  155. (*input_func)->AddParameter(std::move(param_inst));
  156. }
  157. uint32_t InstBuffAddrCheckPass::GetSearchAndTestFuncId() {
  158. if (search_test_func_id_ == 0) {
  159. // Generate function "bool search_and_test(uint64_t ref_ptr, uint32_t len)"
  160. // which searches input buffer for buffer which most likely contains the
  161. // pointer value |ref_ptr| and verifies that the entire reference of
  162. // length |len| bytes is contained in the buffer.
  163. search_test_func_id_ = TakeNextId();
  164. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  165. std::vector<const analysis::Type*> param_types = {
  166. type_mgr->GetType(GetUint64Id()), type_mgr->GetType(GetUintId())};
  167. analysis::Function func_ty(type_mgr->GetType(GetBoolId()), param_types);
  168. analysis::Type* reg_func_ty = type_mgr->GetRegisteredType(&func_ty);
  169. std::unique_ptr<Instruction> func_inst(
  170. new Instruction(get_module()->context(), SpvOpFunction, GetBoolId(),
  171. search_test_func_id_,
  172. {{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
  173. {SpvFunctionControlMaskNone}},
  174. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  175. {type_mgr->GetTypeInstruction(reg_func_ty)}}}));
  176. get_def_use_mgr()->AnalyzeInstDefUse(&*func_inst);
  177. std::unique_ptr<Function> input_func =
  178. MakeUnique<Function>(std::move(func_inst));
  179. std::vector<uint32_t> param_vec;
  180. // Add ref_ptr and length parameters
  181. AddParam(GetUint64Id(), &param_vec, &input_func);
  182. AddParam(GetUintId(), &param_vec, &input_func);
  183. // Empty first block.
  184. uint32_t first_blk_id = TakeNextId();
  185. std::unique_ptr<Instruction> first_blk_label(NewLabel(first_blk_id));
  186. std::unique_ptr<BasicBlock> first_blk_ptr =
  187. MakeUnique<BasicBlock>(std::move(first_blk_label));
  188. InstructionBuilder builder(
  189. context(), &*first_blk_ptr,
  190. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  191. uint32_t hdr_blk_id = TakeNextId();
  192. // Branch to search loop header
  193. std::unique_ptr<Instruction> hdr_blk_label(NewLabel(hdr_blk_id));
  194. (void)builder.AddInstruction(MakeUnique<Instruction>(
  195. context(), SpvOpBranch, 0, 0,
  196. std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {hdr_blk_id}}}));
  197. input_func->AddBasicBlock(std::move(first_blk_ptr));
  198. // Linear search loop header block
  199. // TODO(greg-lunarg): Implement binary search
  200. std::unique_ptr<BasicBlock> hdr_blk_ptr =
  201. MakeUnique<BasicBlock>(std::move(hdr_blk_label));
  202. builder.SetInsertPoint(&*hdr_blk_ptr);
  203. // Phi for search index. Starts with 1.
  204. uint32_t cont_blk_id = TakeNextId();
  205. std::unique_ptr<Instruction> cont_blk_label(NewLabel(cont_blk_id));
  206. // Deal with def-use cycle caused by search loop index computation.
  207. // Create Add and Phi instructions first, then do Def analysis on Add.
  208. // Add Phi and Add instructions and do Use analysis later.
  209. uint32_t idx_phi_id = TakeNextId();
  210. uint32_t idx_inc_id = TakeNextId();
  211. std::unique_ptr<Instruction> idx_inc_inst(new Instruction(
  212. context(), SpvOpIAdd, GetUintId(), idx_inc_id,
  213. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {idx_phi_id}},
  214. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  215. {builder.GetUintConstantId(1u)}}}));
  216. std::unique_ptr<Instruction> idx_phi_inst(new Instruction(
  217. context(), SpvOpPhi, GetUintId(), idx_phi_id,
  218. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  219. {builder.GetUintConstantId(1u)}},
  220. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {first_blk_id}},
  221. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {idx_inc_id}},
  222. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cont_blk_id}}}));
  223. get_def_use_mgr()->AnalyzeInstDef(&*idx_inc_inst);
  224. // Add (previously created) search index phi
  225. (void)builder.AddInstruction(std::move(idx_phi_inst));
  226. // LoopMerge
  227. uint32_t bound_test_blk_id = TakeNextId();
  228. std::unique_ptr<Instruction> bound_test_blk_label(
  229. NewLabel(bound_test_blk_id));
  230. (void)builder.AddInstruction(MakeUnique<Instruction>(
  231. context(), SpvOpLoopMerge, 0, 0,
  232. std::initializer_list<Operand>{
  233. {SPV_OPERAND_TYPE_ID, {bound_test_blk_id}},
  234. {SPV_OPERAND_TYPE_ID, {cont_blk_id}},
  235. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {SpvLoopControlMaskNone}}}));
  236. // Branch to continue/work block
  237. (void)builder.AddInstruction(MakeUnique<Instruction>(
  238. context(), SpvOpBranch, 0, 0,
  239. std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {cont_blk_id}}}));
  240. input_func->AddBasicBlock(std::move(hdr_blk_ptr));
  241. // Continue/Work Block. Read next buffer pointer and break if greater
  242. // than ref_ptr arg.
  243. std::unique_ptr<BasicBlock> cont_blk_ptr =
  244. MakeUnique<BasicBlock>(std::move(cont_blk_label));
  245. builder.SetInsertPoint(&*cont_blk_ptr);
  246. // Add (previously created) search index increment now.
  247. (void)builder.AddInstruction(std::move(idx_inc_inst));
  248. // Load next buffer address from debug input buffer
  249. uint32_t ibuf_id = GetInputBufferId();
  250. uint32_t ibuf_ptr_id = GetInputBufferPtrId();
  251. Instruction* uptr_ac_inst = builder.AddTernaryOp(
  252. ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  253. builder.GetUintConstantId(kDebugInputDataOffset), idx_inc_id);
  254. uint32_t ibuf_type_id = GetInputBufferTypeId();
  255. Instruction* uptr_load_inst =
  256. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, uptr_ac_inst->result_id());
  257. // If loaded address greater than ref_ptr arg, break, else branch back to
  258. // loop header
  259. Instruction* uptr_test_inst =
  260. builder.AddBinaryOp(GetBoolId(), SpvOpUGreaterThan,
  261. uptr_load_inst->result_id(), param_vec[0]);
  262. (void)builder.AddConditionalBranch(uptr_test_inst->result_id(),
  263. bound_test_blk_id, hdr_blk_id,
  264. kInvalidId, SpvSelectionControlMaskNone);
  265. input_func->AddBasicBlock(std::move(cont_blk_ptr));
  266. // Bounds test block. Read length of selected buffer and test that
  267. // all len arg bytes are in buffer.
  268. std::unique_ptr<BasicBlock> bound_test_blk_ptr =
  269. MakeUnique<BasicBlock>(std::move(bound_test_blk_label));
  270. builder.SetInsertPoint(&*bound_test_blk_ptr);
  271. // Decrement index to point to previous/candidate buffer address
  272. Instruction* cand_idx_inst = builder.AddBinaryOp(
  273. GetUintId(), SpvOpISub, idx_inc_id, builder.GetUintConstantId(1u));
  274. // Load candidate buffer address
  275. Instruction* cand_ac_inst =
  276. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  277. builder.GetUintConstantId(kDebugInputDataOffset),
  278. cand_idx_inst->result_id());
  279. Instruction* cand_load_inst =
  280. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, cand_ac_inst->result_id());
  281. // Compute offset of ref_ptr from candidate buffer address
  282. Instruction* offset_inst = builder.AddBinaryOp(
  283. ibuf_type_id, SpvOpISub, param_vec[0], cand_load_inst->result_id());
  284. // Convert ref length to uint64
  285. Instruction* ref_len_64_inst =
  286. builder.AddUnaryOp(ibuf_type_id, SpvOpUConvert, param_vec[1]);
  287. // Add ref length to ref offset to compute end of reference
  288. Instruction* ref_end_inst =
  289. builder.AddBinaryOp(ibuf_type_id, SpvOpIAdd, offset_inst->result_id(),
  290. ref_len_64_inst->result_id());
  291. // Load starting index of lengths in input buffer and convert to uint32
  292. Instruction* len_start_ac_inst =
  293. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  294. builder.GetUintConstantId(kDebugInputDataOffset),
  295. builder.GetUintConstantId(0u));
  296. Instruction* len_start_load_inst = builder.AddUnaryOp(
  297. ibuf_type_id, SpvOpLoad, len_start_ac_inst->result_id());
  298. Instruction* len_start_32_inst = builder.AddUnaryOp(
  299. GetUintId(), SpvOpUConvert, len_start_load_inst->result_id());
  300. // Decrement search index to get candidate buffer length index
  301. Instruction* cand_len_idx_inst =
  302. builder.AddBinaryOp(GetUintId(), SpvOpISub, cand_idx_inst->result_id(),
  303. builder.GetUintConstantId(1u));
  304. // Add candidate length index to start index
  305. Instruction* len_idx_inst = builder.AddBinaryOp(
  306. GetUintId(), SpvOpIAdd, cand_len_idx_inst->result_id(),
  307. len_start_32_inst->result_id());
  308. // Load candidate buffer length
  309. Instruction* len_ac_inst =
  310. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  311. builder.GetUintConstantId(kDebugInputDataOffset),
  312. len_idx_inst->result_id());
  313. Instruction* len_load_inst =
  314. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, len_ac_inst->result_id());
  315. // Test if reference end within candidate buffer length
  316. Instruction* len_test_inst = builder.AddBinaryOp(
  317. GetBoolId(), SpvOpULessThanEqual, ref_end_inst->result_id(),
  318. len_load_inst->result_id());
  319. // Return test result
  320. (void)builder.AddInstruction(MakeUnique<Instruction>(
  321. context(), SpvOpReturnValue, 0, 0,
  322. std::initializer_list<Operand>{
  323. {SPV_OPERAND_TYPE_ID, {len_test_inst->result_id()}}}));
  324. // Close block
  325. input_func->AddBasicBlock(std::move(bound_test_blk_ptr));
  326. // Close function and add function to module
  327. std::unique_ptr<Instruction> func_end_inst(
  328. new Instruction(get_module()->context(), SpvOpFunctionEnd, 0, 0, {}));
  329. get_def_use_mgr()->AnalyzeInstDefUse(&*func_end_inst);
  330. input_func->SetFunctionEnd(std::move(func_end_inst));
  331. context()->AddFunction(std::move(input_func));
  332. }
  333. return search_test_func_id_;
  334. }
  335. uint32_t InstBuffAddrCheckPass::GenSearchAndTest(Instruction* ref_inst,
  336. InstructionBuilder* builder,
  337. uint32_t* ref_uptr_id) {
  338. // Enable Int64 if necessary
  339. if (!get_feature_mgr()->HasCapability(SpvCapabilityInt64)) {
  340. std::unique_ptr<Instruction> cap_int64_inst(new Instruction(
  341. context(), SpvOpCapability, 0, 0,
  342. std::initializer_list<Operand>{
  343. {SPV_OPERAND_TYPE_CAPABILITY, {SpvCapabilityInt64}}}));
  344. get_def_use_mgr()->AnalyzeInstDefUse(&*cap_int64_inst);
  345. context()->AddCapability(std::move(cap_int64_inst));
  346. }
  347. // Convert reference pointer to uint64
  348. uint32_t ref_ptr_id = ref_inst->GetSingleWordInOperand(0);
  349. Instruction* ref_uptr_inst =
  350. builder->AddUnaryOp(GetUint64Id(), SpvOpConvertPtrToU, ref_ptr_id);
  351. *ref_uptr_id = ref_uptr_inst->result_id();
  352. // Compute reference length in bytes
  353. analysis::DefUseManager* du_mgr = get_def_use_mgr();
  354. Instruction* ref_ptr_inst = du_mgr->GetDef(ref_ptr_id);
  355. uint32_t ref_ptr_ty_id = ref_ptr_inst->type_id();
  356. Instruction* ref_ptr_ty_inst = du_mgr->GetDef(ref_ptr_ty_id);
  357. uint32_t ref_len = GetTypeLength(ref_ptr_ty_inst->GetSingleWordInOperand(1));
  358. uint32_t ref_len_id = builder->GetUintConstantId(ref_len);
  359. // Gen call to search and test function
  360. const std::vector<uint32_t> args = {GetSearchAndTestFuncId(), *ref_uptr_id,
  361. ref_len_id};
  362. Instruction* call_inst =
  363. builder->AddNaryOp(GetBoolId(), SpvOpFunctionCall, args);
  364. uint32_t retval = call_inst->result_id();
  365. return retval;
  366. }
  367. void InstBuffAddrCheckPass::GenBuffAddrCheckCode(
  368. BasicBlock::iterator ref_inst_itr,
  369. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  370. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  371. // Look for reference through indexed descriptor. If found, analyze and
  372. // save components. If not, return.
  373. Instruction* ref_inst = &*ref_inst_itr;
  374. if (!IsPhysicalBuffAddrReference(ref_inst)) return;
  375. // Move original block's preceding instructions into first new block
  376. std::unique_ptr<BasicBlock> new_blk_ptr;
  377. MovePreludeCode(ref_inst_itr, ref_block_itr, &new_blk_ptr);
  378. InstructionBuilder builder(
  379. context(), &*new_blk_ptr,
  380. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  381. new_blocks->push_back(std::move(new_blk_ptr));
  382. uint32_t error_id = builder.GetUintConstantId(kInstErrorBuffAddrUnallocRef);
  383. // Generate code to do search and test if all bytes of reference
  384. // are within a listed buffer. Return reference pointer converted to uint64.
  385. uint32_t ref_uptr_id;
  386. uint32_t valid_id = GenSearchAndTest(ref_inst, &builder, &ref_uptr_id);
  387. // Generate test of search results with true branch
  388. // being full reference and false branch being debug output and zero
  389. // for the referenced value.
  390. GenCheckCode(valid_id, error_id, ref_uptr_id, stage_idx, ref_inst,
  391. new_blocks);
  392. // Move original block's remaining code into remainder/merge block and add
  393. // to new blocks
  394. BasicBlock* back_blk_ptr = &*new_blocks->back();
  395. MovePostludeCode(ref_block_itr, back_blk_ptr);
  396. }
  397. void InstBuffAddrCheckPass::InitInstBuffAddrCheck() {
  398. // Initialize base class
  399. InitializeInstrument();
  400. // Initialize class
  401. search_test_func_id_ = 0;
  402. }
  403. Pass::Status InstBuffAddrCheckPass::ProcessImpl() {
  404. // Perform bindless bounds check on each entry point function in module
  405. InstProcessFunction pfn =
  406. [this](BasicBlock::iterator ref_inst_itr,
  407. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  408. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  409. return GenBuffAddrCheckCode(ref_inst_itr, ref_block_itr, stage_idx,
  410. new_blocks);
  411. };
  412. bool modified = InstProcessEntryPointCallTree(pfn);
  413. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  414. }
  415. Pass::Status InstBuffAddrCheckPass::Process() {
  416. if (!get_feature_mgr()->HasCapability(
  417. SpvCapabilityPhysicalStorageBufferAddressesEXT))
  418. return Status::SuccessWithoutChange;
  419. InitInstBuffAddrCheck();
  420. return ProcessImpl();
  421. }
  422. } // namespace opt
  423. } // namespace spvtools