Skip to content

Commit

Permalink
instrument: Clean up generation code (#5090)
Browse files Browse the repository at this point in the history
-Make more use of InstructionBuilder instruction helper methods
-Use MakeUnique<>() rather than new
-Add InstrumentPass::GenReadFunctionCall() which optimizes function
calls in a loop with constant arguments and no side effects.

This is a prepatory change for future work on the instrumentation
code which will add more generated functions.
  • Loading branch information
jeremyg-lunarg authored Feb 3, 2023
1 parent d8759a1 commit 5890763
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 313 deletions.
7 changes: 3 additions & 4 deletions source/opt/inst_bindless_check_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ uint32_t InstBindlessCheckPass::GenLastByteIdx(RefAnalysis* ref,
if (sum_id == 0)
sum_id = curr_offset_id;
else {
Instruction* sum_inst = builder->AddBinaryOp(GetUintId(), spv::Op::OpIAdd,
sum_id, curr_offset_id);
Instruction* sum_inst =
builder->AddIAdd(GetUintId(), sum_id, curr_offset_id);
sum_id = sum_inst->result_id();
}
++ac_in_idx;
Expand All @@ -507,8 +507,7 @@ uint32_t InstBindlessCheckPass::GenLastByteIdx(RefAnalysis* ref,
uint32_t bsize = ByteSize(curr_ty_id, matrix_stride, col_major, in_matrix);
uint32_t last = bsize - 1;
uint32_t last_id = builder->GetUintConstantId(last);
Instruction* sum_inst =
builder->AddBinaryOp(GetUintId(), spv::Op::OpIAdd, sum_id, last_id);
Instruction* sum_inst = builder->AddIAdd(GetUintId(), sum_id, last_id);
return sum_inst->result_id();
}

Expand Down
29 changes: 8 additions & 21 deletions source/opt/inst_buff_addr_check_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ uint32_t InstBuffAddrCheckPass::GetSearchAndTestFuncId() {
uint32_t hdr_blk_id = TakeNextId();
// Branch to search loop header
std::unique_ptr<Instruction> hdr_blk_label(NewLabel(hdr_blk_id));
(void)builder.AddInstruction(MakeUnique<Instruction>(
context(), spv::Op::OpBranch, 0, 0,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {hdr_blk_id}}}));
(void)builder.AddBranch(hdr_blk_id);
input_func->AddBasicBlock(std::move(first_blk_ptr));
// Linear search loop header block
// TODO(greg-lunarg): Implement binary search
Expand Down Expand Up @@ -293,17 +291,10 @@ uint32_t InstBuffAddrCheckPass::GetSearchAndTestFuncId() {
uint32_t bound_test_blk_id = TakeNextId();
std::unique_ptr<Instruction> bound_test_blk_label(
NewLabel(bound_test_blk_id));
(void)builder.AddInstruction(MakeUnique<Instruction>(
context(), spv::Op::OpLoopMerge, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {bound_test_blk_id}},
{SPV_OPERAND_TYPE_ID, {cont_blk_id}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{uint32_t(spv::LoopControlMask::MaskNone)}}}));
(void)builder.AddLoopMerge(bound_test_blk_id, cont_blk_id,
uint32_t(spv::LoopControlMask::MaskNone));
// Branch to continue/work block
(void)builder.AddInstruction(MakeUnique<Instruction>(
context(), spv::Op::OpBranch, 0, 0,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {cont_blk_id}}}));
(void)builder.AddBranch(cont_blk_id);
input_func->AddBasicBlock(std::move(hdr_blk_ptr));
// Continue/Work Block. Read next buffer pointer and break if greater
// than ref_ptr arg.
Expand Down Expand Up @@ -386,10 +377,8 @@ uint32_t InstBuffAddrCheckPass::GetSearchAndTestFuncId() {
GetBoolId(), spv::Op::OpULessThanEqual, ref_end_inst->result_id(),
len_load_inst->result_id());
// Return test result
(void)builder.AddInstruction(MakeUnique<Instruction>(
context(), spv::Op::OpReturnValue, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {len_test_inst->result_id()}}}));
(void)builder.AddUnaryOp(0, spv::Op::OpReturnValue,
len_test_inst->result_id());
// Close block
input_func->AddBasicBlock(std::move(bound_test_blk_ptr));
// Close function and add function to module
Expand Down Expand Up @@ -422,10 +411,8 @@ uint32_t InstBuffAddrCheckPass::GenSearchAndTest(Instruction* ref_inst,
uint32_t ref_len = GetTypeLength(ref_ptr_ty_inst->GetSingleWordInOperand(1));
uint32_t ref_len_id = builder->GetUintConstantId(ref_len);
// Gen call to search and test function
const std::vector<uint32_t> args = {GetSearchAndTestFuncId(), *ref_uptr_id,
ref_len_id};
Instruction* call_inst =
builder->AddNaryOp(GetBoolId(), spv::Op::OpFunctionCall, args);
Instruction* call_inst = builder->AddFunctionCall(
GetBoolId(), GetSearchAndTestFuncId(), {*ref_uptr_id, ref_len_id});
uint32_t retval = call_inst->result_id();
return retval;
}
Expand Down
9 changes: 4 additions & 5 deletions source/opt/inst_debug_printf_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void InstDebugPrintfPass::GenOutputValues(Instruction* val_inst,
const analysis::Type* c_ty = v_ty->element_type();
uint32_t c_ty_id = type_mgr->GetId(c_ty);
for (uint32_t c = 0; c < v_ty->element_count(); ++c) {
Instruction* c_inst = builder->AddIdLiteralOp(
c_ty_id, spv::Op::OpCompositeExtract, val_inst->result_id(), c);
Instruction* c_inst =
builder->AddCompositeExtract(c_ty_id, val_inst->result_id(), {c});
GenOutputValues(c_inst, val_ids, builder);
}
return;
Expand All @@ -44,9 +44,8 @@ void InstDebugPrintfPass::GenOutputValues(Instruction* val_inst,
// Select between uint32 zero or one
uint32_t zero_id = builder->GetUintConstantId(0);
uint32_t one_id = builder->GetUintConstantId(1);
Instruction* sel_inst =
builder->AddTernaryOp(GetUintId(), spv::Op::OpSelect,
val_inst->result_id(), one_id, zero_id);
Instruction* sel_inst = builder->AddSelect(
GetUintId(), val_inst->result_id(), one_id, zero_id);
val_ids->push_back(sel_inst->result_id());
return;
}
Expand Down
Loading

0 comments on commit 5890763

Please sign in to comment.