Skip to content

[RegAlloc] Avoid split past block-prologue interference - #209704

Merged
sommerlukas merged 4 commits into
llvm:mainfrom
sommerlukas:leave-before-assert
Jul 31, 2026
Merged

sommerlukas merged 4 commits into
llvm:mainfrom
sommerlukas:leave-before-assert

Conversation

@sommerlukas

Copy link
Copy Markdown
Contributor

leaveIntvAtTop skips the target-specific block prologue when determining the insertion point.

If the block prologue itself contains an instruction that interferes with the interval, we must leave the interval before the block, otherwise we will hit an assertion in splitLiveThroughBlock.

@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-regalloc

@llvm/pr-subscribers-backend-amdgpu

Author: Lukas Sommer (sommerlukas)

Changes

leaveIntvAtTop skips the target-specific block prologue when determining the insertion point.

If the block prologue itself contains an instruction that interferes with the interval, we must leave the interval before the block, otherwise we will hit an assertion in splitLiveThroughBlock.


Full diff: https://github.com/llvm/llvm-project/pull/209704.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/RegAllocGreedy.cpp (+8-1)
  • (added) llvm/test/CodeGen/AMDGPU/greedy-wwm-copy-prologue-assert.mir (+107)
diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp
index 6b8a9b8190f9a..799816191eca1 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -843,8 +843,15 @@ bool RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
         SlotIndex::isEarlierInstr(LIS->getInstructionIndex(*FirstNonDebugInstr),
                                   SA->getFirstSplitPoint(Number)))
       return false;
+
     // Interference for the live-in value.
-    if (Intf.first() <= Indexes->getMBBStartIdx(Number))
+    Register Reg = SA->getParent().reg();
+    auto InsertPt = MBB->SkipPHIsLabelsAndDebug(MBB->begin(), Reg);
+    SlotIndex InsertIdx = InsertPt == MBB->end()
+                              ? Indexes->getMBBEndIdx(Number)
+                              : LIS->getInstructionIndex(*InsertPt);
+    if (Intf.first() <= Indexes->getMBBStartIdx(Number) ||
+        SlotIndex::isEarlierInstr(Intf.first(), InsertIdx))
       BCS[B].Entry = SpillPlacement::MustSpill;
     else
       BCS[B].Entry = SpillPlacement::PrefSpill;
diff --git a/llvm/test/CodeGen/AMDGPU/greedy-wwm-copy-prologue-assert.mir b/llvm/test/CodeGen/AMDGPU/greedy-wwm-copy-prologue-assert.mir
new file mode 100644
index 0000000000000..0c59d92012e35
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/greedy-wwm-copy-prologue-assert.mir
@@ -0,0 +1,107 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
+# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=greedy -o - %s | FileCheck %s
+
+# Check that splitting doesn't attempt to split after the prologue, if the first
+# interference is in the prologue. The hot loop in bb.1 keeps %0 in $vgpr0.
+# On the cold exit, the pre-existing WWM_COPY in bb.2 is a block prologue and
+# also occupies $vgpr0. Therefore, we can't split in bb.2. In this case,
+# spilling is chosen instead.
+# This is a regression test for the SplitKit interference assertion in 
+# 'leaveIntvAtTop'.
+#
+--- |
+  define amdgpu_kernel void @wwm_copy_prologue_assert() #0 {
+    ret void
+  }
+
+  attributes #0 = { "amdgpu-num-vgpr"="2" }
+...
+---
+name:            wwm_copy_prologue_assert
+tracksRegLiveness: true
+registers:
+  - { id: 0, class: vgpr_32, flags: [ WWM_REG ] }
+  - { id: 1, class: vgpr_32, flags: [ WWM_REG ] }
+machineFunctionInfo:
+  isEntryFunction: true
+  scratchRSrcReg:  '$sgpr0_sgpr1_sgpr2_sgpr3'
+  stackPtrOffsetReg: '$sgpr32'
+  sgprForEXECCopy: '$sgpr100_sgpr101'
+body:             |
+  ; CHECK-LABEL: name: wwm_copy_prologue_assert
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[V_ADD_U32_e32_:%[0-9]+]]:vgpr_32 = V_ADD_U32_e32 $vgpr1, $vgpr1, implicit $exec
+  ; CHECK-NEXT:   S_NOP 0, implicit [[V_ADD_U32_e32_]], implicit $vgpr1
+  ; CHECK-NEXT:   S_BRANCH %bb.1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   successors: %bb.1(0x7fffffff), %bb.2(0x00000001)
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   S_NOP 0, implicit [[V_ADD_U32_e32_]], implicit $vgpr1
+  ; CHECK-NEXT:   S_CBRANCH_VCCNZ %bb.1, implicit undef $vcc
+  ; CHECK-NEXT:   S_BRANCH %bb.2
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   successors: %bb.3(0x80000000)
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   SI_SPILL_WWM_V32_SAVE [[V_ADD_U32_e32_]], %stack.0, $sgpr32, 0, implicit $exec :: (store (s32) into %stack.0, addrspace 5)
+  ; CHECK-NEXT:   [[WWM_COPY:%[0-9]+]]:vgpr_32 = lr-split WWM_COPY $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.3:
+  ; CHECK-NEXT:   successors: %bb.4(0x80000000)
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.4:
+  ; CHECK-NEXT:   successors: %bb.5(0x80000000)
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   S_NOP 0, implicit [[WWM_COPY]], implicit $vgpr1
+  ; CHECK-NEXT:   [[SI_SPILL_WWM_V32_RESTORE:%[0-9]+]]:vgpr_32 = SI_SPILL_WWM_V32_RESTORE %stack.0, $sgpr32, 0, implicit $exec :: (load (s32) from %stack.0, addrspace 5)
+  ; CHECK-NEXT:   S_NOP 0, implicit [[SI_SPILL_WWM_V32_RESTORE]], implicit $vgpr1
+  ; CHECK-NEXT:   S_BRANCH %bb.5
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.5:
+  ; CHECK-NEXT:   liveins: $vgpr1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   S_NOP 0, implicit [[SI_SPILL_WWM_V32_RESTORE]], implicit $vgpr1
+  ; CHECK-NEXT:   S_ENDPGM 0
+  bb.0:
+    successors: %bb.1
+    liveins: $vgpr1
+    %0:vgpr_32 = V_ADD_U32_e32 $vgpr1, $vgpr1, implicit $exec
+    S_NOP 0, implicit %0, implicit $vgpr1
+    S_BRANCH %bb.1
+
+  bb.1:
+    successors: %bb.1(0x7fffffff), %bb.2(0x00000001)
+    liveins: $vgpr1
+    S_NOP 0, implicit %0, implicit $vgpr1
+    S_CBRANCH_VCCNZ %bb.1, implicit undef $vcc
+    S_BRANCH %bb.2
+
+  bb.2:
+    successors: %bb.3
+    liveins: $vgpr1
+    %1:vgpr_32 = lr-split WWM_COPY $vgpr1
+
+  bb.3:
+    successors: %bb.4
+    liveins: $vgpr1
+
+  bb.4:
+    successors: %bb.5
+    liveins: $vgpr1
+    S_NOP 0, implicit killed %1, implicit $vgpr1
+    S_NOP 0, implicit %0, implicit $vgpr1
+    S_BRANCH %bb.5
+
+  bb.5:
+    liveins: $vgpr1
+    S_NOP 0, implicit %0, implicit $vgpr1
+    S_ENDPGM 0
+...

@cdevadas cdevadas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some reviewers outside of AMD as well. It is a generic file change and need attention of a wider audience.

@@ -0,0 +1,105 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
# RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -stress-regalloc=2 -passes='greedy<wwm>' -o - %s | FileCheck %s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better have a runline for legacy PM as well. For that, you can construct the RUN line with options --start-before=greedy,1 --stop-after=greedy,1 just to run the second invocation of the greedy allocator alone.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't stressing PM mechanics, no point in retaining the legacy PM run line. I think we should push towards only using the new PM for mir tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any harm in retaining the legacy PM RUN line, even though the NPM switch is coming soon.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is because it requires the hacky -start-before...stop-after sequence to get the right filter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've dropped the legacy PM run line again.

@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 201325 tests passed
  • 5515 tests skipped

✅ The build succeeded and all tests passed.

@sommerlukas
sommerlukas force-pushed the leave-before-assert branch from 35cb88c to 532a9e5 Compare July 16, 2026 09:33
@sommerlukas
sommerlukas force-pushed the leave-before-assert branch 2 times, most recently from 7ca1eab to 6bdce9d Compare July 23, 2026 15:13
@sommerlukas

Copy link
Copy Markdown
Contributor Author

Ping.

@sommerlukas
sommerlukas requested a review from qcolombet July 23, 2026 16:10
`leaveIntvAtTop` skips the target-specific block prologue when
determining the insertion point.

If the block prologue itself contains an instruction that interferes
with the interval, we must leave the interval before the block,
otherwise we will hit an assertion in `splitLiveThroughBlock`.

Signed-off-by: Lukas Sommer <[email protected]>
Signed-off-by: Lukas Sommer <[email protected]>
@sommerlukas
sommerlukas force-pushed the leave-before-assert branch from 6bdce9d to 17cc730 Compare July 31, 2026 04:53
@sommerlukas
sommerlukas merged commit 9d10180 into llvm:main Jul 31, 2026
10 checks passed
@sommerlukas
sommerlukas deleted the leave-before-assert branch July 31, 2026 07:58
jgreenbaum pushed a commit to jgreenbaum/llvm-project that referenced this pull request Aug 3, 2026
`leaveIntvAtTop` skips the target-specific block prologue when
determining the insertion point.

If the block prologue itself contains an instruction that interferes
with the interval, we must leave the interval before the block,
otherwise we will hit an assertion in `splitLiveThroughBlock`.

---------

Signed-off-by: Lukas Sommer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants