Skip to content

[InlineSpiller] Hoist spills only when all of its subranges are available - #177703

Merged
mshockwave merged 3 commits into
llvm:mainfrom
mshockwave:patch/fix-spill-hoisting
Jan 29, 2026
Merged

mshockwave merged 3 commits into
llvm:mainfrom
mshockwave:patch/fix-spill-hoisting

Conversation

@mshockwave

@mshockwave mshockwave commented Jan 23, 2026 •

Copy link
Copy Markdown
Member

Context: #176001 (comment)

When we're hoisting a spill to another block, and storing one of its sibling values there instead, we need to make sure all sub ranges of that sibling value is alive at the insertion point. Otherwise, we might accidentally store compromised subranges values back to the spill slot.

@llvmbot

llvmbot commented Jan 23, 2026 •

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-llvm-regalloc

@llvm/pr-subscribers-backend-hexagon

Author: Min-Yih Hsu (mshockwave)

Changes

A potential solution to #176001

I still need to write up what I think happened to that issue. And add test cases to this PR.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/InlineSpiller.cpp (+5-2)
  • (modified) llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir (+4-4)
diff --git a/llvm/lib/CodeGen/InlineSpiller.cpp b/llvm/lib/CodeGen/InlineSpiller.cpp
index 68370303a3aef..07a1581da2b1a 100644
--- a/llvm/lib/CodeGen/InlineSpiller.cpp
+++ b/llvm/lib/CodeGen/InlineSpiller.cpp
@@ -1409,8 +1409,11 @@ bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
 
   for (const Register &SibReg : Siblings) {
     LiveInterval &LI = LIS.getInterval(SibReg);
-    VNInfo *VNI = LI.getVNInfoAt(Idx);
-    if (VNI) {
+    if (!LI.getVNInfoAt(Idx))
+      continue;
+    if (all_of(LI.subranges(), [&](const LiveInterval::SubRange &SR) {
+          return SR.getVNInfoAt(Idx);
+        })) {
       LiveReg = SibReg;
       return true;
     }
diff --git a/llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir b/llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir
index 019d593c34948..04a19868b0671 100644
--- a/llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir
+++ b/llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir
@@ -152,12 +152,12 @@ body:             |
   ; CHECK-NEXT:   renamable $d0 = A2_tfrpi 2
   ; CHECK-NEXT:   renamable $d13 = A2_tfrpi -1
   ; CHECK-NEXT:   renamable $d13 = S2_asl_r_p_acc killed renamable $d13, killed renamable $d0, renamable $r4
-  ; CHECK-NEXT:   S2_storerd_io %stack.0, 0, renamable $d2 :: (store (s64) into %stack.0)
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1.for.body:
   ; CHECK-NEXT:   successors: %bb.3(0x40000000), %bb.2(0x40000000)
-  ; CHECK-NEXT:   liveins: $d8:0x0000000000000001, $d13, $r20
+  ; CHECK-NEXT:   liveins: $d2:0x0000000000000002, $d8:0x0000000000000001, $d13, $r20
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   S2_storerd_io %stack.0, 0, renamable $d2 :: (store (s64) into %stack.0)
   ; CHECK-NEXT:   ADJCALLSTACKDOWN 0, 0, implicit-def dead $r29, implicit-def dead $r30, implicit $r31, implicit $r30, implicit $r29
   ; CHECK-NEXT:   J2_call @lrand48, implicit-def dead $d0, implicit-def dead $d1, implicit-def dead $d2, implicit-def dead $d3, implicit-def dead $d4, implicit-def dead $d5, implicit-def dead $d6, implicit-def dead $d7, implicit-def dead $r28, implicit-def dead $r31, implicit-def dead $p0, implicit-def dead $p1, implicit-def dead $p2, implicit-def dead $p3, implicit-def dead $m0, implicit-def dead $m1, implicit-def dead $lc0, implicit-def dead $lc1, implicit-def dead $sa0, implicit-def dead $sa1, implicit-def dead $usr, implicit-def $usr_ovf, implicit-def dead $cs0, implicit-def dead $cs1, implicit-def dead $w0, implicit-def dead $w1, implicit-def dead $w2, implicit-def dead $w3, implicit-def dead $w4, implicit-def dead $w5, implicit-def dead $w6, implicit-def dead $w7, implicit-def dead $w8, implicit-def dead $w9, implicit-def dead $w10, implicit-def dead $w11, implicit-def dead $w12, implicit-def dead $w13, implicit-def dead $w14, implicit-def dead $w15, implicit-def dead $q0, implicit-def dead $q1, implicit-def dead $q2, implicit-def dead $q3, implicit-def $r0
   ; CHECK-NEXT:   ADJCALLSTACKUP 0, 0, implicit-def dead $r29, implicit-def dead $r30, implicit-def dead $r31, implicit $r29
@@ -180,7 +180,7 @@ body:             |
   ; CHECK-NEXT:   J2_call @lrand48, implicit-def dead $d0, implicit-def dead $d1, implicit-def dead $d2, implicit-def dead $d3, implicit-def dead $d4, implicit-def dead $d5, implicit-def dead $d6, implicit-def dead $d7, implicit-def dead $r28, implicit-def dead $r31, implicit-def dead $p0, implicit-def dead $p1, implicit-def dead $p2, implicit-def dead $p3, implicit-def dead $m0, implicit-def dead $m1, implicit-def dead $lc0, implicit-def dead $lc1, implicit-def dead $sa0, implicit-def dead $sa1, implicit-def dead $usr, implicit-def $usr_ovf, implicit-def dead $cs0, implicit-def dead $cs1, implicit-def dead $w0, implicit-def dead $w1, implicit-def dead $w2, implicit-def dead $w3, implicit-def dead $w4, implicit-def dead $w5, implicit-def dead $w6, implicit-def dead $w7, implicit-def dead $w8, implicit-def dead $w9, implicit-def dead $w10, implicit-def dead $w11, implicit-def dead $w12, implicit-def dead $w13, implicit-def dead $w14, implicit-def dead $w15, implicit-def dead $q0, implicit-def dead $q1, implicit-def dead $q2, implicit-def dead $q3
   ; CHECK-NEXT:   renamable $d2 = L2_loadrd_io %stack.0, 0 :: (load (s64) from %stack.0)
   ; CHECK-NEXT:   ADJCALLSTACKUP 0, 0, implicit-def dead $r29, implicit-def dead $r30, implicit-def dead $r31, implicit $r29
-  ; CHECK-NEXT:   renamable $d0 = S2_asl_r_p killed renamable $d11, killed renamable $r4
+  ; CHECK-NEXT:   renamable $d0 = S2_asl_r_p killed renamable $d11, renamable $r4
   ; CHECK-NEXT:   renamable $d1 = COPY renamable $d13
   ; CHECK-NEXT:   renamable $d1 = S2_lsr_i_p_and killed renamable $d1, killed renamable $d9, 9
   ; CHECK-NEXT:   renamable $d0 = S2_asl_i_p_and killed renamable $d0, killed renamable $d12, 42
@@ -192,7 +192,7 @@ body:             |
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.if.end82:
   ; CHECK-NEXT:   successors: %bb.3(0x40000000), %bb.1(0x40000000)
-  ; CHECK-NEXT:   liveins: $d8:0x0000000000000001, $d13, $r20
+  ; CHECK-NEXT:   liveins: $d2:0x0000000000000002, $d8:0x0000000000000001, $d13, $r20
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   renamable $r20 = A2_addi killed renamable $r20, -1
   ; CHECK-NEXT:   renamable $p0 = C2_cmpeqi renamable $r20, 0

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated
if (!LI.getVNInfoAt(Idx))
continue;
if (all_of(LI.subranges(), [&](const LiveInterval::SubRange &SR) {
return SR.getVNInfoAt(Idx);

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.

Does it need to verify the value numbers are the same?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If you're suggesting checking against OrigVNI, my understanding is that it might lead to false negative: the sibling registers here are split from the original register. And when a new split register interval is created, its value number always starts from zero.
For example, a live interval %438 [2792r,3104r:0)[3688r,6816r:1) 0@2792r 1@3688ris split into two sibling registers

  • %438 [2792r,3104r:0) 0@2792r
  • %439 [3688r,6816r:0) 0@3688r

Let's say the OrigVNI (which is a pre-split VNI) is 1@3688r here, and it's checking against a sibling VNI (which is a post-split VNI) of 0@3688r. Even their value numbers are different, they're actually referring to the same value. It suffices to check just the slot index because I think the splitter creates two new LiveIntervals with different value numbers only if they have disjoint slot index ranges.

@mshockwave
mshockwave force-pushed the patch/fix-spill-hoisting branch from 99eba23 to 2745bb7 Compare January 26, 2026 19:05
@mshockwave mshockwave changed the title [InlineSpiller][WIP] Hoist spills only when all of its subranges are available [InlineSpiller] Hoist spills only when all of its subranges are available Jan 26, 2026
@mshockwave

Copy link
Copy Markdown
Member Author

I think this PR is ready for review.

@github-actions

Copy link
Copy Markdown

⚠️ undef deprecator found issues in your code. ⚠️

You can test this locally with the following command:
git diff -U0 --pickaxe-regex -S '([^a-zA-Z0-9#_-]undef([^a-zA-Z0-9_-]|$)|UndefValue::get)' 'HEAD~1' HEAD llvm/test/CodeGen/RISCV/pr176001.ll llvm/lib/CodeGen/InlineSpiller.cpp

The following files introduce new uses of undef:

  • llvm/test/CodeGen/RISCV/pr176001.ll

Undef is now deprecated and should only be used in the rare cases where no replacement is possible. For example, a load of uninitialized memory yields undef. You should use poison values for placeholders instead.

In tests, avoid using undef and having tests that trigger undefined behavior. If you need an operand with some unimportant value, you can add a new argument to the function and use that instead.

For example, this is considered a bad practice:

define void @fn() {
  ...
  br i1 undef, ...
}

Please use the following instead:

define void @fn(i1 %cond) {
  ...
  br i1 %cond, ...
}

Please refer to the Undefined Behavior Manual for more information.

@wangpc-pp wangpc-pp 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.

LGTM.

@mshockwave

Copy link
Copy Markdown
Member Author

@arsenm do you have any other comments?

@mshockwave
mshockwave requested a review from arsenm January 29, 2026 01:46
Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Resolved

@topperc topperc 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.

LGTM

@mshockwave mshockwave added this to the LLVM 22.x Release milestone Jan 29, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in LLVM Release Status Jan 29, 2026
@mshockwave
mshockwave merged commit 75f03a6 into llvm:main Jan 29, 2026
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Jan 29, 2026
@mshockwave

Copy link
Copy Markdown
Member Author

/cherry-pick 75f03a6

@llvmbot

llvmbot commented Jan 29, 2026

Copy link
Copy Markdown
Member

/pull-request #178737

c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Jan 30, 2026
…able (llvm#177703)

Context:
llvm#176001 (comment)

When we're hoisting a spill to another block, and storing one of its
sibling values there instead, we need to make sure all sub ranges of
that sibling value is alive at the insertion point. Otherwise, we might
accidentally store compromised subranges values back to the spill slot.

(cherry picked from commit 75f03a6)
honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Jan 30, 2026
…able (llvm#177703)

Context:
llvm#176001 (comment)

When we're hoisting a spill to another block, and storing one of its
sibling values there instead, we need to make sure all sub ranges of
that sibling value is alive at the insertion point. Otherwise, we might
accidentally store compromised subranges values back to the spill slot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

5 participants