https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/238705
When an inline asm input is tied to a physical register output, the input's virtual register was created in the class for its value type. That class is not required to contain the physical register, in which case the input is materialized in the wrong bank and then copied into the output register. On AMDGPU, an immediate tied to an AGPR output was materialized in an SGPR, then copied to the AGPR, through a VGPR on gfx908. Use the minimal class of the physical register when the class for the value type does not contain it. Untyped operands already did this unconditionally; this extends it to typed operands. Keep the value type's class if the physical register's minimal class is not allocatable. Co-authored-by: Claude Opus 5.5 <[email protected]> >From b61a0eeb23acd8c0abf30328f9b3aec634c6e57d Mon Sep 17 00:00:00 2001 From: Matt Arsenault <[email protected]> Date: Fri, 23 Oct 2026 07:58:14 -0700 Subject: [PATCH] SelectionDAG: Use the physical register class for tied inline asm inputs When an inline asm input is tied to a physical register output, the input's virtual register was created in the class for its value type. That class is not required to contain the physical register, in which case the input is materialized in the wrong bank and then copied into the output register. On AMDGPU, an immediate tied to an AGPR output was materialized in an SGPR, then copied to the AGPR, through a VGPR on gfx908. Use the minimal class of the physical register when the class for the value type does not contain it. Untyped operands already did this unconditionally; this extends it to typed operands. Keep the value type's class if the physical register's minimal class is not allocatable. Co-authored-by: Claude Opus 5.5 <[email protected]> --- .../SelectionDAG/SelectionDAGBuilder.cpp | 19 ++++++++++++++---- .../AMDGPU/inline-asm-tied-physreg-class.ll | 20 ++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 2e35f199c7fba..21ce32906ef46 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -10700,10 +10700,21 @@ static bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info, auto *R = cast<RegisterSDNode>(Info.AsmNodeOperands[CurOp + 1]); Register TiedReg = R->getReg(); MVT RegVT = R->getSimpleValueType(0); - const TargetRegisterClass *RC = - TiedReg.isVirtual() ? MRI.getRegClass(TiedReg) - : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT) - : TRI.getMinimalPhysRegClass(TiedReg); + const TargetRegisterClass *RC; + if (TiedReg.isVirtual()) { + RC = MRI.getRegClass(TiedReg); + } else if (RegVT == MVT::Untyped) { + RC = TRI.getMinimalPhysRegClass(TiedReg); + } else { + RC = TLI.getRegClassFor(RegVT); + if (!RC->contains(TiedReg)) { + const TargetRegisterClass *PhysRC = + TRI.getMinimalPhysRegClass(TiedReg); + if (PhysRC->isAllocatable()) + RC = PhysRC; + } + } + for (unsigned I = 0, E = Flag.getNumOperandRegisters(); I != E; ++I) Regs.push_back(MRI.createVirtualRegister(RC)); diff --git a/llvm/test/CodeGen/AMDGPU/inline-asm-tied-physreg-class.ll b/llvm/test/CodeGen/AMDGPU/inline-asm-tied-physreg-class.ll index 1461e33d15491..18c8411b56442 100644 --- a/llvm/test/CodeGen/AMDGPU/inline-asm-tied-physreg-class.ll +++ b/llvm/test/CodeGen/AMDGPU/inline-asm-tied-physreg-class.ll @@ -9,10 +9,7 @@ define void @tied_agpr_imm() { ; GFX908-LABEL: tied_agpr_imm: ; GFX908: ; %bb.0: ; GFX908-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) -; GFX908-NEXT: s_mov_b32 s4, 7 -; GFX908-NEXT: v_mov_b32_e32 v1, s4 -; GFX908-NEXT: s_nop 1 -; GFX908-NEXT: v_accvgpr_write_b32 a1, v1 +; GFX908-NEXT: v_accvgpr_write_b32 a1, 7 ; GFX908-NEXT: ;;#ASMSTART ; GFX908-NEXT: ; def a1 ; GFX908-NEXT: ;;#ASMEND @@ -24,8 +21,7 @@ define void @tied_agpr_imm() { ; GFX90A-LABEL: tied_agpr_imm: ; GFX90A: ; %bb.0: ; GFX90A-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) -; GFX90A-NEXT: s_mov_b32 s4, 7 -; GFX90A-NEXT: v_accvgpr_write_b32 a1, s4 +; GFX90A-NEXT: v_accvgpr_write_b32 a1, 7 ; GFX90A-NEXT: ;;#ASMSTART ; GFX90A-NEXT: ; def a1 ; GFX90A-NEXT: ;;#ASMEND @@ -102,8 +98,8 @@ define void @tied_agpr64(i64 %x) { ; GFX908-LABEL: tied_agpr64: ; GFX908: ; %bb.0: ; GFX908-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) -; GFX908-NEXT: v_accvgpr_write_b32 a3, v1 ; GFX908-NEXT: v_accvgpr_write_b32 a2, v0 +; GFX908-NEXT: v_accvgpr_write_b32 a3, v1 ; GFX908-NEXT: ;;#ASMSTART ; GFX908-NEXT: ; def a[2:3] ; GFX908-NEXT: ;;#ASMEND @@ -115,8 +111,8 @@ define void @tied_agpr64(i64 %x) { ; GFX90A-LABEL: tied_agpr64: ; GFX90A: ; %bb.0: ; GFX90A-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) -; GFX90A-NEXT: v_accvgpr_write_b32 a3, v1 ; GFX90A-NEXT: v_accvgpr_write_b32 a2, v0 +; GFX90A-NEXT: v_accvgpr_write_b32 a3, v1 ; GFX90A-NEXT: ;;#ASMSTART ; GFX90A-NEXT: ; def a[2:3] ; GFX90A-NEXT: ;;#ASMEND @@ -133,11 +129,11 @@ define void @tied_agpr64_sgpr(i64 inreg %x) { ; GFX908-LABEL: tied_agpr64_sgpr: ; GFX908: ; %bb.0: ; GFX908-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) -; GFX908-NEXT: v_mov_b32_e32 v2, s16 -; GFX908-NEXT: v_mov_b32_e32 v0, s17 +; GFX908-NEXT: v_mov_b32_e32 v0, s16 +; GFX908-NEXT: v_mov_b32_e32 v1, s17 ; GFX908-NEXT: s_nop 0 -; GFX908-NEXT: v_accvgpr_write_b32 a2, v2 -; GFX908-NEXT: v_accvgpr_write_b32 a3, v0 +; GFX908-NEXT: v_accvgpr_write_b32 a2, v0 +; GFX908-NEXT: v_accvgpr_write_b32 a3, v1 ; GFX908-NEXT: ;;#ASMSTART ; GFX908-NEXT: ; def a[2:3] ; GFX908-NEXT: ;;#ASMEND _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
