Skip to content

Commit 8af2ecc

Browse files
committed
Fix MachineVerifier failure
This fixes the following error: ``` *** Bad machine code: inconsistent constant size *** - function: foo - basic block: %bb.1 entry (0x9ec9298) - instruction: %12:iid(s8) = G_CONSTANT i4 1 ```
1 parent 5f0b40a commit 8af2ecc

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,32 @@ static SPIRVType *propagateSPIRVType(MachineInstr *MI, SPIRVGlobalRegistry *GR,
380380
// To support current approach and limitations wrt. bit width here we widen a
381381
// scalar register with a bit width greater than 1 to valid sizes and cap it to
382382
// 64 width.
383-
static void widenScalarLLTNextPow2(Register Reg, MachineRegisterInfo &MRI) {
383+
static unsigned widenBitWidthToNextPow2(unsigned BitWidth) {
384+
if (BitWidth == 1)
385+
return 1; // No need to widen 1-bit values
386+
return std::min(std::max(1u << Log2_32_Ceil(BitWidth), 8u), 64u);
387+
}
388+
389+
static void widenScalarType(Register Reg, MachineRegisterInfo &MRI) {
384390
LLT RegType = MRI.getType(Reg);
385391
if (!RegType.isScalar())
386392
return;
387-
unsigned Sz = RegType.getScalarSizeInBits();
388-
if (Sz == 1)
389-
return;
390-
unsigned NewSz = std::min(std::max(1u << Log2_32_Ceil(Sz), 8u), 64u);
391-
if (NewSz != Sz)
392-
MRI.setType(Reg, LLT::scalar(NewSz));
393+
unsigned CurrentWidth = RegType.getScalarSizeInBits();
394+
unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
395+
if (NewWidth != CurrentWidth)
396+
MRI.setType(Reg, LLT::scalar(NewWidth));
397+
}
398+
399+
static void widenCImmType(MachineOperand &MOP) {
400+
const ConstantInt *CImmVal = MOP.getCImm();
401+
unsigned CurrentWidth = CImmVal->getBitWidth();
402+
unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
403+
if (NewWidth != CurrentWidth) {
404+
// Replace the immediate value with the widened version
405+
MOP.setCImm(ConstantInt::get(
406+
CImmVal->getType()->getContext(),
407+
CImmVal->getValue().zextOrTrunc(NewWidth)));
408+
}
393409
}
394410

395411
static void setInsertPtAfterDef(MachineIRBuilder &MIB, MachineInstr *Def) {
@@ -506,10 +522,13 @@ generateAssignInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR,
506522
unsigned MIOp = MI.getOpcode();
507523

508524
if (!IsExtendedInts) {
509-
// validate bit width of scalar registers
510-
for (const auto &MOP : MI.operands())
525+
// validate bit width of scalar registers and immediates
526+
for (auto &MOP : MI.operands()) {
511527
if (MOP.isReg())
512-
widenScalarLLTNextPow2(MOP.getReg(), MRI);
528+
widenScalarType(MOP.getReg(), MRI);
529+
else if (MOP.isCImm())
530+
widenCImmType(MOP);
531+
}
513532
}
514533

515534
if (isSpvIntrinsic(MI, Intrinsic::spv_assign_ptr_type)) {

llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_int4/negative.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv32-unknown-unknown --spirv-ext=+SPV_INTEL_arbitrary_precision_integers %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-INT-4
22

3-
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-INT-8
3+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-INT-8
44
; No error would be reported in comparison to Khronos llvm-spirv, because type adjustments to integer size are made
55
; in case no appropriate extension is enabled. Here we expect that the type is adjusted to 8 bits.
66

0 commit comments

Comments
 (0)