Traces the variable pointer inst backwards. checker is called on each visited instruction.
| 532 | // Traces the variable pointer inst backwards. |
| 533 | // checker is called on each visited instruction. |
| 534 | spv_result_t TraceVariablePointers( |
| 535 | ValidationState_t& _, const Instruction* inst, |
| 536 | const std::function<spv_result_t(ValidationState_t&, const Instruction*)>& |
| 537 | checker) { |
| 538 | std::vector<const Instruction*> stack; |
| 539 | std::unordered_set<const Instruction*> seen; |
| 540 | stack.push_back(inst); |
| 541 | while (!stack.empty()) { |
| 542 | const Instruction* trace_inst = stack.back(); |
| 543 | stack.pop_back(); |
| 544 | |
| 545 | if (!seen.insert(trace_inst).second) { |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | if (auto error = checker(_, trace_inst)) { |
| 550 | return error; |
| 551 | } |
| 552 | |
| 553 | const auto untyped = spvOpcodeGeneratesUntypedPointer(trace_inst->opcode()); |
| 554 | switch (trace_inst->opcode()) { |
| 555 | case spv::Op::OpAccessChain: |
| 556 | case spv::Op::OpInBoundsAccessChain: |
| 557 | case spv::Op::OpPtrAccessChain: |
| 558 | stack.push_back(_.FindDef(trace_inst->GetOperandAs<uint32_t>(2))); |
| 559 | break; |
| 560 | case spv::Op::OpUntypedAccessChainKHR: |
| 561 | case spv::Op::OpUntypedInBoundsAccessChainKHR: |
| 562 | case spv::Op::OpUntypedPtrAccessChainKHR: |
| 563 | stack.push_back(_.FindDef(trace_inst->GetOperandAs<uint32_t>(3))); |
| 564 | break; |
| 565 | case spv::Op::OpPhi: |
| 566 | for (uint32_t i = 2; i < trace_inst->operands().size(); i += 2) { |
| 567 | stack.push_back(_.FindDef(trace_inst->GetOperandAs<uint32_t>(i))); |
| 568 | } |
| 569 | break; |
| 570 | case spv::Op::OpSelect: |
| 571 | stack.push_back(_.FindDef(trace_inst->GetOperandAs<uint32_t>(3))); |
| 572 | stack.push_back(_.FindDef(trace_inst->GetOperandAs<uint32_t>(4))); |
| 573 | break; |
| 574 | case spv::Op::OpFunctionParameter: { |
| 575 | // Jump to function calls |
| 576 | auto func = trace_inst->function(); |
| 577 | auto func_inst = _.FindDef(func->id()); |
| 578 | |
| 579 | const auto param_inst_num = trace_inst - &_.ordered_instructions()[0]; |
| 580 | uint32_t param_index = 0; |
| 581 | uint32_t inst_index = 1; |
| 582 | while (_.ordered_instructions()[param_inst_num - inst_index].opcode() != |
| 583 | spv::Op::OpFunction) { |
| 584 | if (_.ordered_instructions()[param_inst_num - inst_index].opcode() == |
| 585 | spv::Op::OpFunctionParameter) { |
| 586 | param_index++; |
| 587 | } |
| 588 | ++inst_index; |
| 589 | } |
| 590 | |
| 591 | for (const auto& use_pair : func_inst->uses()) { |
no test coverage detected