[llvm] [MachineScheduler] Keep live-in physreg copies ahead of inline asm (PR #219877)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 22:52:16 PDT 2026
https://github.com/cr-zhao updated https://github.com/llvm/llvm-project/pull/219877
>From bcc0f57ad8af06213d7a646eeb9e386541ac58d3 Mon Sep 17 00:00:00 2001
From: real-cpu <zhaochenrui757 at gmail.com>
Date: Sun, 30 Aug 2026 21:45:56 -0700
Subject: [PATCH 1/2] [MachineScheduler] Keep live-in physreg copies ahead of
inline asm
---
llvm/lib/CodeGen/MachineScheduler.cpp | 22 ++++++++++++
.../CodeGen/X86/inline-asm-livein-copy.ll | 35 +++++++++++++++++++
2 files changed, 57 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/inline-asm-livein-copy.ll
diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index 814ea3f8eeb05..ec3c0535fb62f 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -1130,6 +1130,28 @@ void ScheduleDAGMI::schedule() {
/// Apply each ScheduleDAGMutation step in order.
void ScheduleDAGMI::postProcessDAG() {
+ // Keep copies of block live-in physical registers ahead of inline asm in
+ // their original scheduling region. Sinking such a copy extends an
+ // unspillable physical register live range across the asm, while keeping it
+ // above the asm lets register allocation spill the virtual register instead.
+ for (SUnit &CopySU : SUnits) {
+ MachineInstr *Copy = CopySU.getInstr();
+ if (!Copy->isCopy())
+ continue;
+
+ Register DstReg = Copy->getOperand(0).getReg();
+ Register SrcReg = Copy->getOperand(1).getReg();
+ if (!DstReg.isVirtual() || !SrcReg.isPhysical() ||
+ !Copy->getParent()->isLiveIn(SrcReg))
+ continue;
+
+ for (SUnit &SU : SUnits) {
+ if (SU.NodeNum <= CopySU.NodeNum || !SU.getInstr()->isInlineAsm())
+ continue;
+ addEdge(&SU, SDep(&CopySU, SDep::Artificial));
+ }
+ }
+
for (auto &m : Mutations)
m->apply(this);
}
diff --git a/llvm/test/CodeGen/X86/inline-asm-livein-copy.ll b/llvm/test/CodeGen/X86/inline-asm-livein-copy.ll
new file mode 100644
index 0000000000000..29cc72a26fcf3
--- /dev/null
+++ b/llvm/test/CodeGen/X86/inline-asm-livein-copy.ll
@@ -0,0 +1,35 @@
+; RUN: llc -mtriple=x86_64-linux-gnu -O2 -frame-pointer=all < %s | FileCheck %s
+
+ at counter = internal global i64 0
+ at counter.1 = internal global i64 0
+
+define void @livein_copy(i64 %arg) {
+; CHECK-LABEL: livein_copy:
+; CHECK: # %bb.0:
+; CHECK-NEXT: pushq %rbp
+; CHECK: movq %rdi, {{[-0-9]+}}(%rbp) # 8-byte Spill
+; CHECK: #APP
+; CHECK-NEXT: nop
+; CHECK-NEXT: #NO_APP
+; CHECK: movq {{[-0-9]+}}(%rbp), %{{[a-z0-9]+}} # 8-byte Reload
+; CHECK: #APP
+; CHECK-NEXT: nop
+; CHECK-NEXT: #NO_APP
+; CHECK: retq
+ %count = load i64, ptr @counter
+ %inc = add i64 %count, 1
+ store i64 %inc, ptr @counter
+ %count.1 = load i64, ptr @counter.1
+ %inc.1 = add i64 %count.1, 1
+ store i64 %inc.1, ptr @counter.1
+ tail call { i64, i64, i64 } asm sideeffect "nop",
+ "=r,=r,=r,0,1,2,~{rax},~{rbx},~{rcx},~{rdx},~{r8},~{r9},~{r10},~{r11},~{r13},~{r14},~{r15},~{memory},~{cc}"
+ (i64 4660, i64 43981, i64 239)
+ %count.2 = load i64, ptr @counter.1
+ %inc.2 = add i64 %count.2, 1
+ store i64 %inc.2, ptr @counter.1
+ tail call { i64, i64, i64 } asm sideeffect "nop",
+ "=r,=r,=r,0,1,2,~{rax},~{rbx},~{rcx},~{rdx},~{r8},~{r9},~{r10},~{r11},~{r13},~{r14},~{r15},~{memory},~{cc}"
+ (i64 6699, i64 15437, i64 %arg)
+ ret void
+}
>From c2fd6d08c7b38bba576f58118d083e6b3df4d828 Mon Sep 17 00:00:00 2001
From: real-cpu <zhaochenrui757 at gmail.com>
Date: Wed, 16 Sep 2026 22:47:12 -0700
Subject: [PATCH 2/2] [AMDGPU] Update spill test checks for live-in copy
scheduling
---
llvm/test/CodeGen/AMDGPU/spill-vgpr-block.ll | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/llvm/test/CodeGen/AMDGPU/spill-vgpr-block.ll b/llvm/test/CodeGen/AMDGPU/spill-vgpr-block.ll
index 12057161ecdfd..e452649ac07d2 100644
--- a/llvm/test/CodeGen/AMDGPU/spill-vgpr-block.ll
+++ b/llvm/test/CodeGen/AMDGPU/spill-vgpr-block.ll
@@ -50,10 +50,10 @@ define i32 @non_entry_func(i32 %x) #0 {
define amdgpu_kernel void @entry_func(i32 %x) #0 {
; GISEL-LABEL: entry_func:
; GISEL: ; %bb.0:
-; GISEL-NEXT: s_mov_b64 s[10:11], s[6:7]
-; GISEL-NEXT: s_load_b32 s6, s[4:5], 0x0
+; GISEL-NEXT: s_load_b32 s12, s[4:5], 0x0
; GISEL-NEXT: v_mov_b32_e32 v31, v0
-; GISEL-NEXT: s_mov_b64 s[12:13], s[0:1]
+; GISEL-NEXT: s_mov_b64 s[10:11], s[6:7]
+; GISEL-NEXT: s_mov_b64 s[6:7], s[0:1]
; GISEL-NEXT: ;;#ASMSTART
; GISEL-NEXT: s_nop
; GISEL-NEXT: ;;#ASMEND
@@ -61,12 +61,11 @@ define amdgpu_kernel void @entry_func(i32 %x) #0 {
; GISEL-NEXT: s_mov_b32 s0, non_entry_func at abs32@lo
; GISEL-NEXT: s_mov_b32 s1, non_entry_func at abs32@hi
; GISEL-NEXT: s_add_co_ci_u32 s9, s5, 0
-; GISEL-NEXT: s_mov_b64 s[4:5], s[12:13]
+; GISEL-NEXT: s_mov_b64 s[4:5], s[6:7]
+; GISEL-NEXT: s_mov_b64 s[6:7], s[2:3]
; GISEL-NEXT: s_mov_b32 s32, 0
; GISEL-NEXT: s_wait_kmcnt 0x0
-; GISEL-NEXT: v_mov_b32_e32 v0, s6
-; GISEL-NEXT: s_mov_b64 s[6:7], s[2:3]
-; GISEL-NEXT: s_wait_alu depctr_sa_sdst(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, s12
; GISEL-NEXT: s_swappc_b64 s[30:31], s[0:1]
; GISEL-NEXT: s_endpgm
;
More information about the llvm-commits
mailing list