[llvm] Fix trunc nonstandard int types (PR #191393)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 04:08:46 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-spir-v
Author: idubinov
<details>
<summary>Changes</summary>
In some cases, the trunc dst type was widened to higher bit size, which changes behavior of the instruction.
Now, in case of need of widening, the trunc instruction is replaced with AND with appropriate mask.
Assisted-by: Claude Code.
---
Full diff: https://github.com/llvm/llvm-project/pull/191393.diff
2 Files Affected:
- (modified) llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp (+57-9)
- (modified) llvm/test/CodeGen/SPIRV/trunc-nonstd-bitwidth.ll (+37-1)
``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
index aead1ce735c49..3bba1836b4ff4 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
@@ -397,25 +397,40 @@ static unsigned widenBitWidthToNextPow2(unsigned BitWidth) {
return std::min(std::max(1u << Log2_32_Ceil(BitWidth), 8u), 128u);
}
-static void widenScalarType(Register Reg, MachineRegisterInfo &MRI) {
+static unsigned widenScalarType(Register Reg, MachineRegisterInfo &MRI) {
+ // returns original size or 0 if no change
LLT RegType = MRI.getType(Reg);
if (!RegType.isScalar())
- return;
+ return 0;
unsigned CurrentWidth = RegType.getScalarSizeInBits();
unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
- if (NewWidth != CurrentWidth)
+ if (NewWidth != CurrentWidth) {
MRI.setType(Reg, LLT::scalar(NewWidth));
+ return CurrentWidth;
+ }
+ return 0;
}
-static void widenCImmType(MachineOperand &MOP) {
+static unsigned widenCImmType(MachineOperand &MOP) {
+ // returns original size or 0 if no change
const ConstantInt *CImmVal = MOP.getCImm();
unsigned CurrentWidth = CImmVal->getBitWidth();
unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
- if (NewWidth != CurrentWidth) {
- // Replace the immediate value with the widened version
- MOP.setCImm(ConstantInt::get(CImmVal->getType()->getContext(),
- CImmVal->getValue().zextOrTrunc(NewWidth)));
- }
+ if (NewWidth == CurrentWidth)
+ return 0;
+
+ // Replace the immediate value with the widened version
+ MOP.setCImm(ConstantInt::get(CImmVal->getType()->getContext(),
+ CImmVal->getValue().zextOrTrunc(NewWidth)));
+ return CurrentWidth;
+}
+
+static unsigned widenOperand(MachineOperand &MOP, MachineRegisterInfo &MRI) {
+ // returns original size or 0 if no change
+ if (MOP.isReg())
+ return widenScalarType(MOP.getReg(), MRI);
+ else if (MOP.isCImm())
+ return widenCImmType(MOP);
}
static void setInsertPtAfterDef(MachineIRBuilder &MIB, MachineInstr *Def) {
@@ -486,6 +501,39 @@ generateAssignInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR,
ST->canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions) ||
ST->canUseExtension(SPIRV::Extension::SPV_INTEL_int4);
+ if (!IsExtendedInts) {
+ // Some instructions' behavior relies on register size, e.g. G_TRUNC.
+ // Process them before general register widening.
+ for (MachineBasicBlock &MBB : MF) {
+ for (MachineInstr &MI : MBB) {
+ unsigned MIOp = MI.getOpcode();
+ if (MIOp == TargetOpcode::G_TRUNC) {
+ assert(MI.getNumOperands() == 2);
+
+ widenOperand(MI.getOperand(1), MRI); // SRC
+
+ unsigned OriginalDstWidth = widenOperand(MI.getOperand(0), MRI);
+ if (OriginalDstWidth != 0) {
+ // Dst was widened - replace G_TRUNC with G_AND & appropriate mask.
+ Register DstReg = MI.getOperand(0).getReg();
+ Register SrcReg = MI.getOperand(1).getReg();
+ unsigned NewDstWidth = MRI.getType(DstReg).getScalarSizeInBits();
+
+ // Create mask constant with lower OriginalDstWidth bits set.
+ MIB.setInsertPt(MBB, MI.getIterator());
+ APInt Mask = APInt::getLowBitsSet(NewDstWidth, OriginalDstWidth);
+ auto MaskReg = MIB.buildConstant(LLT::scalar(NewDstWidth), Mask);
+
+ // Replace G_TRUNC with G_AND.
+ MI.setDesc(ST->getInstrInfo()->get(TargetOpcode::G_AND));
+ MI.getOperand(1).setReg(SrcReg);
+ MI.addOperand(MachineOperand::CreateReg(MaskReg.getReg(0), false));
+ }
+ }
+ }
+ }
+ }
+
for (MachineBasicBlock *MBB : post_order(&MF)) {
if (MBB->empty())
continue;
diff --git a/llvm/test/CodeGen/SPIRV/trunc-nonstd-bitwidth.ll b/llvm/test/CodeGen/SPIRV/trunc-nonstd-bitwidth.ll
index 07af684fe0b48..bedc3b59089f8 100644
--- a/llvm/test/CodeGen/SPIRV/trunc-nonstd-bitwidth.ll
+++ b/llvm/test/CodeGen/SPIRV/trunc-nonstd-bitwidth.ll
@@ -17,6 +17,8 @@
; CHECK-DAG: %[[#PtrStruct:]] = OpTypePointer CrossWorkgroup %[[#Struct]]
; CHECK-EXT-DAG: %[[#Int40:]] = OpTypeInt 40 0
; CHECK-EXT-DAG: %[[#Int50:]] = OpTypeInt 50 0
+; CHECK-EXT-DAG: %[[#Int24:]] = OpTypeInt 24 0
+; CHECK-EXT-DAG: %[[#Int5:]] = OpTypeInt 5 0
; CHECK-NOEXT-DAG: %[[#Int40:]] = OpTypeInt 64 0
; CHECK-DAG: %[[#PtrInt40:]] = OpTypePointer CrossWorkgroup %[[#Int40]]
@@ -38,7 +40,27 @@
; CHECK-EXT: %[[#Tq:]] = OpUConvert %[[#Int40]] %[[#Q]]
; CHECK-EXT: OpStore %[[#QArg]] %[[#Tq]]
-; CHECK-NOEXT: OpStore %[[#QArg]] %[[#Q]]
+; CHECK-NOEXT: %[[#TqNoext:]] = OpBitwiseAnd %[[#]] %[[#Q]] %[[#]]
+; CHECK-NOEXT: OpStore %[[#QArg]] %[[#TqNoext]]
+
+; Test 3: trunc to small non-standard width (i64 -> i24)
+; CHECK: OpFunction
+; CHECK: %[[#T3Arg:]] = OpFunctionParameter
+; CHECK: %[[#T3Val:]] = OpFunctionParameter
+; CHECK-EXT: %[[#T3Tr:]] = OpUConvert %[[#Int24]] %[[#T3Val]]
+; CHECK-EXT: OpStore %[[#T3Arg]] %[[#T3Tr]]
+; CHECK-NOEXT: %[[#T3Noext:]] = OpBitwiseAnd %[[#]] %[[#T3Val]] %[[#]]
+; CHECK-NOEXT: OpStore %[[#T3Arg]] %[[#T3Noext]]
+
+; Test 4: trunc to i5 (non-power-of-2, < 8 bits)
+; In NOEXT mode, i5 widens to i8, so mask with 0x1F
+; CHECK: OpFunction
+; CHECK: %[[#T4Arg:]] = OpFunctionParameter
+; CHECK: %[[#T4Val:]] = OpFunctionParameter
+; CHECK-EXT: %[[#T4Tr:]] = OpUConvert %[[#Int5]] %[[#T4Val]]
+; CHECK-EXT: OpStore %[[#T4Arg]] %[[#T4Tr]]
+; CHECK-NOEXT: %[[#T4Noext:]] = OpBitwiseAnd %[[#]] %[[#T4Val]] %[[#]]
+; CHECK-NOEXT: OpStore %[[#T4Arg]] %[[#T4Noext]]
%struct = type <{ i32, i8, [3 x i8] }>
@@ -54,3 +76,17 @@ define spir_kernel void @bar(ptr addrspace(1) %qarg, i50 %q) {
store i40 %tq, ptr addrspace(1) %qarg
ret void
}
+
+; trunc to small non-standard width (i64 -> i24)
+define spir_kernel void @trunc_to_i24(ptr addrspace(1) %arg, i64 %val) {
+ %tr = trunc i64 %val to i24
+ store i24 %tr, ptr addrspace(1) %arg
+ ret void
+}
+
+; trunc to i5 (non-power-of-2, < 8 bits)
+define spir_kernel void @trunc_to_i5(ptr addrspace(1) %arg, i16 %val) {
+ %tr = trunc i16 %val to i5
+ store i5 %tr, ptr addrspace(1) %arg
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/191393
More information about the llvm-commits
mailing list