Skip to content

Commit aafd524

Browse files
authored
[opt] Fix bug opt::InstructionBuilder::AddVariable (KhronosGroup#6007)
The storage class operand was being added with the wrong operand type. It was assumed to be an ID. Then trying to analyze the new variable declaration instruction's defs and uses could get confused and assert out. Added a test for this.
1 parent f2dac2f commit aafd524

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

source/opt/ir_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class InstructionBuilder {
516516

517517
Instruction* AddVariable(uint32_t type_id, uint32_t storage_class) {
518518
std::vector<Operand> operands;
519-
operands.push_back({SPV_OPERAND_TYPE_ID, {storage_class}});
519+
operands.push_back({SPV_OPERAND_TYPE_STORAGE_CLASS, {storage_class}});
520520
std::unique_ptr<Instruction> new_inst(
521521
new Instruction(GetContext(), spv::Op::OpVariable, type_id,
522522
GetContext()->TakeNextId(), operands));

test/opt/ir_builder.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,38 @@ OpFunctionEnd
256256
Match(text, context.get());
257257
}
258258

259+
TEST_F(IRBuilderTest, AddVariable) {
260+
// Use Private beacuse its' enun is 7 which is higher
261+
// than the ID limit.
262+
const std::string text = R"(
263+
; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
264+
; CHECK: [[ptr:%\w+]] = OpTypePointer Private [[uint]]
265+
; CHECK: [[var:%\w+]] = OpVariable [[ptr]] Private
266+
; CHECK: OpTypeFloat
267+
OpCapability Kernel
268+
OpCapability VectorComputeINTEL
269+
OpCapability Linkage
270+
OpExtension "SPV_INTEL_vector_compute"
271+
OpMemoryModel Logical OpenCL
272+
%1 = OpTypeInt 32 0
273+
%2 = OpTypePointer Private %1
274+
%3 = OpTypeFloat 32
275+
)";
276+
277+
std::unique_ptr<IRContext> context =
278+
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
279+
EXPECT_NE(nullptr, context) << text;
280+
281+
auto* float_ty = context->get_def_use_mgr()->GetDef(3);
282+
InstructionBuilder builder(context.get(), float_ty);
283+
auto* var = builder.AddVariable(2u, uint32_t(spv::StorageClass::Private));
284+
EXPECT_NE(nullptr, var);
285+
286+
context->get_def_use_mgr()->AnalyzeInstDefUse(var); // should not assert
287+
288+
Match(text, context.get());
289+
}
290+
259291
TEST_F(IRBuilderTest, AddCompositeConstruct) {
260292
const std::string text = R"(
261293
; CHECK: [[uint:%\w+]] = OpTypeInt

0 commit comments

Comments
 (0)