Skip to content

Commit a38d0d1

Browse files
committed
[SPIRV] Fix failing assertion in SPIRVAsmPrinter
When +SPV_KHR_float_controls2 and there was a non-int OpConstantZero we would call MI.getOperand(1).getImm() when MI was not an OpTypeInt (the associated test has an OpTypeArray zeroinitialized). This patch adds the missing condition.
1 parent 5927bb5 commit a38d0d1

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,10 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
612612
// Collect the SPIRVTypes for fp16, fp32, and fp64 and the constant of
613613
// type int32 with 0 value to represent the FP Fast Math Mode.
614614
std::vector<const MachineInstr *> SPIRVFloatTypes;
615-
const MachineInstr *ConstZero = nullptr;
615+
const MachineInstr *ConstZeroInt32 = nullptr;
616616
for (const MachineInstr *MI :
617617
MAI->getMSInstrs(SPIRV::MB_TypeConstVars)) {
618-
// Skip if the instruction is not OpTypeFloat or OpConstant.
619618
unsigned OpCode = MI->getOpcode();
620-
if (OpCode != SPIRV::OpTypeFloat && OpCode != SPIRV::OpConstantNull)
621-
continue;
622619

623620
// Collect the SPIRV type if it's a float.
624621
if (OpCode == SPIRV::OpTypeFloat) {
@@ -629,14 +626,18 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
629626
continue;
630627
}
631628
SPIRVFloatTypes.push_back(MI);
632-
} else {
629+
continue;
630+
}
631+
632+
if (OpCode != SPIRV::OpConstantNull) {
633633
// Check if the constant is int32, if not skip it.
634634
const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
635635
MachineInstr *TypeMI = MRI.getVRegDef(MI->getOperand(1).getReg());
636-
if (!TypeMI || TypeMI->getOperand(1).getImm() != 32)
637-
continue;
638-
639-
ConstZero = MI;
636+
bool IsInt32Ty = TypeMI &&
637+
TypeMI->getOpcode() == SPIRV::OpTypeInt &&
638+
TypeMI->getOperand(1).getImm() == 32;
639+
if (IsInt32Ty)
640+
ConstZeroInt32 = MI;
640641
}
641642
}
642643

@@ -657,9 +658,9 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
657658
MCRegister TypeReg =
658659
MAI->getRegisterAlias(MF, MI->getOperand(0).getReg());
659660
Inst.addOperand(MCOperand::createReg(TypeReg));
660-
assert(ConstZero && "There should be a constant zero.");
661+
assert(ConstZeroInt32 && "There should be a constant zero.");
661662
MCRegister ConstReg = MAI->getRegisterAlias(
662-
ConstZero->getMF(), ConstZero->getOperand(0).getReg());
663+
ConstZeroInt32->getMF(), ConstZeroInt32->getOperand(0).getReg());
663664
Inst.addOperand(MCOperand::createReg(ConstReg));
664665
outputMCInst(Inst);
665666
}

llvm/test/CodeGen/SPIRV/non_int_constant_null.ll

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
; RUN: not llc -mtriple spirv64-unknown-unknown %s --spirv-ext=+SPV_KHR_float_controls2 -o -
2-
; Assertion `isImm() && "Wrong MachineOperand accessor"' failed
3-
; On TypeMI->getOperand(1).getImm() then TypeMI is OpTypeArray %8, %17
1+
; RUN: llc -mtriple spirv64-unknown-unknown %s --spirv-ext=+SPV_KHR_float_controls2 -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -mtriple spirv64-unknown-unknown %s --spirv-ext=+SPV_KHR_float_controls2 -o - -filetype=obj | spirv-val %}
43

54
@A = addrspace(1) constant [1 x i8] zeroinitializer
65

6+
; CHECK: OpName %[[#FOO:]] "foo"
7+
; CHECK: OpName %[[#A:]] "A"
8+
; CHECK: OpDecorate %[[#A]] Constant
9+
; CHECK: OpDecorate %[[#A]] LinkageAttributes "A" Export
10+
; CHECK: %[[#INT8:]] = OpTypeInt 8 0
11+
; CHECK: %[[#INT32:]] = OpTypeInt 32 0
12+
; CHECK: %[[#ONE:]] = OpConstant %[[#INT32]] 1
13+
; CHECK: %[[#ARR_INT8:]] = OpTypeArray %[[#INT8]] %7
14+
; CHECK: %[[#ARR_INT8_PTR:]] = OpTypePointer CrossWorkgroup %[[#ARR_INT8]]
15+
; CHECK: %[[#ARR_INT8_ZERO:]] = OpConstantNull %[[#ARR_INT8]]
16+
; CHECK: %13 = OpVariable %[[#ARR_INT8_PTR]] CrossWorkgroup %[[#ARR_INT8_ZERO]]
17+
; CHECK: %[[#FOO]] = OpFunction
18+
; CHECK: = OpLabel
19+
; CHECK: OpReturn
20+
; CHECK: OpFunctionEnd
21+
722
define spir_kernel void @foo() {
823
entry:
924
ret void

0 commit comments

Comments
 (0)