[llvm] Verify operand size for shift operation. (PR #215245)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 04:10:10 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: VladimirMedic
<details>
<summary>Changes</summary>
D16 image sample returns 4-time 16-bit samples packed into 64 bits. The backend legalizes extractelement <4 x half>, 1 (channel 1 — the upper half of the first dword) into trunc(srl(i64, 16)). At normal optimization levels the DAG combiner splits the i64 into two i32s before instruction selection, hiding the problem. At -O0 the i64 survives intact. This creates an invalid COPY from a 64-bit virtual register to a 32-bit physical register, which asserts in SIInstrInfo::copyPhysReg.
To fix it, in isExtractHiElt (both the SelectionDAG version in AMDGPUISelDAGToDAG.cpp and the GlobalISel version in AMDGPUInstructionSelector.cpp), check that the shift source is exactly 32 bits before folding.
---
Full diff: https://github.com/llvm/llvm-project/pull/215245.diff
3 Files Affected:
- (modified) llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp (+8-1)
- (modified) llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp (+8-1)
- (added) llvm/test/CodeGen/AMDGPU/fma-mix-hi16-srl-i64.ll (+22)
``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp b/llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp
index 40345819a3f8d..40c90ea1fb558 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp
@@ -68,7 +68,14 @@ static bool isExtractHiElt(SDValue In, SDValue &Out) {
if (Srl.getOpcode() == ISD::SRL) {
if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
if (ShiftAmt->getZExtValue() == 16) {
- Out = stripBitcast(Srl.getOperand(0));
+ SDValue Result = stripBitcast(Srl.getOperand(0));
+ // V_FMA_MIX_F32 reads hi16 from a 32-bit source via op_sel. If the
+ // shift source is wider (e.g. a 64-bit D16 image sample result), we
+ // cannot fold it directly. Do not assign Out before this check since
+ // the caller may pass the same SDValue for both In and Out.
+ if (Result.getValueSizeInBits() != 32)
+ return false;
+ Out = Result;
return true;
}
}
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
index 677312f4f0f52..4693eb38f8f63 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
@@ -2937,7 +2937,14 @@ static bool isExtractHiElt(MachineRegisterInfo &MRI, Register In,
if (mi_match(Trunc, MRI, m_GLShr(m_Reg(LShlSrc), m_Reg(Cst)))) {
Cst = stripCopy(Cst, MRI);
if (mi_match(Cst, MRI, m_SpecificICst(16))) {
- Out = stripBitCast(LShlSrc, MRI);
+ Register Result = stripBitCast(LShlSrc, MRI);
+ // V_FMA_MIX_F32 reads hi16 from a 32-bit source via op_sel. If the
+ // shift source is wider (e.g. a 64-bit D16 image sample result), we
+ // cannot fold it directly. Do not assign Out before this check since
+ // the caller may pass the same register for both In and Out.
+ if (MRI.getType(Result).getSizeInBits() != 32)
+ return false;
+ Out = Result;
return true;
}
}
diff --git a/llvm/test/CodeGen/AMDGPU/fma-mix-hi16-srl-i64.ll b/llvm/test/CodeGen/AMDGPU/fma-mix-hi16-srl-i64.ll
new file mode 100644
index 0000000000000..c23ad19b4b9f4
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/fma-mix-hi16-srl-i64.ll
@@ -0,0 +1,22 @@
+; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx1200 -O0 < %s | FileCheck %s
+
+; Test that isExtractHiElt in SelectVOP3PMadMixModsImpl does not fold a 64-bit
+; source into V_FMA_MIX_F32 which expects a 32-bit source operand.
+; At -O0 the DAG combiner does not decompose i64 to i32, so the pattern
+; trunc(srl(i64, 16)) survives to isel. isExtractHiElt must reject it because
+; V_FMA_MIX_F32 reads hi16 from a 32-bit register via op_sel.
+
+; CHECK-LABEL: _amdgpu_cs_main:
+; CHECK: s_load_b64
+; CHECK: v_fma_mix_f32
+; CHECK: global_store_b64
+; CHECK: ; return to shader part epilog
+define amdgpu_cs float @_amdgpu_cs_main(ptr addrspace(4) inreg %p, ptr addrspace(1) %out) {
+entry:
+ %vec = load <4 x half>, ptr addrspace(4) %p
+ %0 = extractelement <4 x half> %vec, i64 1
+ %ch1.ext = fpext half %0 to float
+ %mul = fmul float %ch1.ext, 0.000000e+00
+ store <4 x half> %vec, ptr addrspace(1) %out
+ ret float %mul
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/215245
More information about the llvm-commits
mailing list