[llvm] [MCP] Avoid Folding Spill Chains Over Across Source Clobbers (PR #207169)

Jack Styles via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 07:17:01 PDT 2026


https://github.com/Stylie777 updated https://github.com/llvm/llvm-project/pull/207169

>From dc718efc2f418c704a978a83eb357bfa2cc76a5f Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Thu, 2 Jul 2026 13:02:55 +0100
Subject: [PATCH 1/5] [MCP] Avoid Folding Spill Chains Over Across Source
 Clobbers

Previously, MachineCopyPropogation::eliminateSpillageCopies was able
to fold COPY instructions that were part of a spill/reload chain over
a different COPY instruction that would clobber the original source.
This was missed, and as a result would lead to the original value of
the source being lost.

To ensure the original value is retained, the uses of the COPY's outside
of the chain are tracked, and then when folding is attempted, any
instructions that are unexpected will ensure the Spill Chain is retained
for that source, maintaining the original value.

Fixes: #206839
---
 llvm/lib/CodeGen/MachineCopyPropagation.cpp   | 27 +++++++++++++++
 .../aarch64-no-fold-over-clobbered-source.mir | 33 +++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir

diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 0568890583c1d..9b17e1ccb5c92 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1337,6 +1337,8 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
   // If a COPY's Source has use or def until next COPY defines the Source,
   // we put the COPY in this set to keep property#2.
   DenseSet<const MachineInstr *> CopySourceInvalid;
+  // Track uses of a register as a source for a previous COPY
+  DenseMap<const MachineInstr *, SmallVector<const MachineInstr *>> CopySourceDefCopies;
 
   auto TryFoldSpillageCopies =
       [&, this](const SmallVectorImpl<MachineInstr *> &SC,
@@ -1376,6 +1378,26 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
           }
         };
 
+        auto HasUnexpectedCopySourceDef = [&](const MachineInstr *Copy, const MachineInstr *ExpectedDef) {
+          auto CopySouceDefPair = CopySourceDefCopies.find(Copy);
+          if (CopySouceDefPair == CopySourceDefCopies.end())
+            return false;
+
+          for (const MachineInstr *DefCopy : CopySouceDefPair->second)
+            if (DefCopy != ExpectedDef)
+              return true;
+          return false;
+        };
+        // Do not fold across an unexpected COPY def of a chains copy's source.
+        // The paired reload may be needed to restore the original source value.
+        // More info: https://github.com/llvm/llvm-project/issues/206839
+        for (size_t I = 1; I < SC.size(); ++I) {
+          if (HasUnexpectedCopySourceDef(SC[I], SC[I - 1]))
+            return;
+          if (I + 1 > SC.size() && HasUnexpectedCopySourceDef(RC[I], RC[I + 1]))
+            return;
+        }
+
         DestSourcePair InnerMostSpillCopy =
             *isCopyInstr(*SC[0], *TII, UseCopyInstr);
         DestSourcePair OuterMostSpillCopy =
@@ -1495,6 +1517,11 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
     }
 
     auto [Dst, Src] = getDstSrcMCRegs(*CopyOperands);
+    // Track potential unexpected COPY def's that use a source in the chain. These
+    // cannot be folded across to preserve the original source value.
+    if (MachineInstr *LastUseCopy = Tracker.findLastSeenUseInCopy(Dst, *TRI)) {
+      CopySourceDefCopies[LastUseCopy].push_back(&MI);
+    }
     // Check if we can find a pair spill-reload copy.
     LLVM_DEBUG(dbgs() << "MCP: Searching paired spill for reload: ");
     LLVM_DEBUG(MI.dump());
diff --git a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
new file mode 100644
index 0000000000000..4908f6f159d0a
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
@@ -0,0 +1,33 @@
+# RUN: llc -mtriple=aarch64 -run-pass=machine-cp -enable-spill-copy-elim -verify-machineinstrs -o - %s | FileCheck %s
+# When running MachineCopyPropagation, we need to make sure COPY's do not get removed
+# when calls outside of the chain clobber a source included in the chain.
+# See: https://github.com/llvm/llvm-project/issues/206839
+---
+name: spill_chain_copy_clobbers_source
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $w10, $w13, $w23, $w25, $x7
+
+    ; CHECK: renamable $w10 = COPY killed renamable $w13
+    ; CHECK: renamable $w13 = COPY killed renamable $w10
+
+    renamable $w11 = COPY killed renamable $w10  ; S1
+    renamable $w10 = COPY killed renamable $w13  ; S2
+
+    renamable $w13 = COPY killed renamable $w23  ; clobbers S2 source
+    renamable $w15 = COPY killed renamable $w13
+    HINT 0, implicit killed $w15
+
+    renamable $w13 = COPY killed renamable $w25  ; S3
+
+    HINT 0, implicit killed $x7
+
+    renamable $w25 = COPY killed renamable $w13  ; R1
+    renamable $w13 = COPY killed renamable $w10  ; R2
+    renamable $w10 = COPY killed renamable $w11  ; R3
+
+    HINT 0, implicit killed $w10
+    HINT 0, implicit killed $w13
+
+...

>From f94530dde71f91fd3578c049cd80421a952dd1e4 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Thu, 2 Jul 2026 13:45:53 +0100
Subject: [PATCH 2/5] Formatting

---
 llvm/lib/CodeGen/MachineCopyPropagation.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 9b17e1ccb5c92..f0c97f7778aec 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1338,7 +1338,8 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
   // we put the COPY in this set to keep property#2.
   DenseSet<const MachineInstr *> CopySourceInvalid;
   // Track uses of a register as a source for a previous COPY
-  DenseMap<const MachineInstr *, SmallVector<const MachineInstr *>> CopySourceDefCopies;
+  DenseMap<const MachineInstr *, SmallVector<const MachineInstr *>>
+      CopySourceDefCopies;
 
   auto TryFoldSpillageCopies =
       [&, this](const SmallVectorImpl<MachineInstr *> &SC,
@@ -1378,7 +1379,8 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
           }
         };
 
-        auto HasUnexpectedCopySourceDef = [&](const MachineInstr *Copy, const MachineInstr *ExpectedDef) {
+        auto HasUnexpectedCopySourceDef = [&](const MachineInstr *Copy,
+                                              const MachineInstr *ExpectedDef) {
           auto CopySouceDefPair = CopySourceDefCopies.find(Copy);
           if (CopySouceDefPair == CopySourceDefCopies.end())
             return false;
@@ -1517,8 +1519,8 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
     }
 
     auto [Dst, Src] = getDstSrcMCRegs(*CopyOperands);
-    // Track potential unexpected COPY def's that use a source in the chain. These
-    // cannot be folded across to preserve the original source value.
+    // Track potential unexpected COPY def's that use a source in the chain.
+    // These cannot be folded across to preserve the original source value.
     if (MachineInstr *LastUseCopy = Tracker.findLastSeenUseInCopy(Dst, *TRI)) {
       CopySourceDefCopies[LastUseCopy].push_back(&MI);
     }

>From 0770c10b75fa3e4e8ff7163c8eb8081513564417 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Mon, 6 Jul 2026 10:00:05 +0100
Subject: [PATCH 3/5] Update test to check full MIR output

---
 .../aarch64-no-fold-over-clobbered-source.mir     | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
index 4908f6f159d0a..1bac98daf2169 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
+++ b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
@@ -1,3 +1,4 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
 # RUN: llc -mtriple=aarch64 -run-pass=machine-cp -enable-spill-copy-elim -verify-machineinstrs -o - %s | FileCheck %s
 # When running MachineCopyPropagation, we need to make sure COPY's do not get removed
 # when calls outside of the chain clobber a source included in the chain.
@@ -9,9 +10,19 @@ body: |
   bb.0:
     liveins: $w10, $w13, $w23, $w25, $x7
 
-    ; CHECK: renamable $w10 = COPY killed renamable $w13
-    ; CHECK: renamable $w13 = COPY killed renamable $w10
 
+    ; CHECK-LABEL: name: spill_chain_copy_clobbers_source
+    ; CHECK: liveins: $w10, $w13, $w23, $w25, $x7
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $w11 = COPY killed renamable $w10
+    ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w13
+    ; CHECK-NEXT: renamable $w15 = COPY $w23
+    ; CHECK-NEXT: HINT 0, implicit killed $w15
+    ; CHECK-NEXT: HINT 0, implicit killed $x7
+    ; CHECK-NEXT: renamable $w13 = COPY killed renamable $w10
+    ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w11
+    ; CHECK-NEXT: HINT 0, implicit killed $w10
+    ; CHECK-NEXT: HINT 0, implicit killed $w13
     renamable $w11 = COPY killed renamable $w10  ; S1
     renamable $w10 = COPY killed renamable $w13  ; S2
 

>From c9e6af175b1dc30a5cae4356e880e614244805d0 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Tue, 7 Jul 2026 10:51:11 +0100
Subject: [PATCH 4/5] =?UTF-8?q?Respond=20to=20review=20comments=C2=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 llvm/lib/CodeGen/MachineCopyPropagation.cpp   | 29 ++++++++++---------
 .../aarch64-no-fold-over-clobbered-source.mir |  3 --
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index f0c97f7778aec..b46efc7882f6c 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1337,10 +1337,24 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
   // If a COPY's Source has use or def until next COPY defines the Source,
   // we put the COPY in this set to keep property#2.
   DenseSet<const MachineInstr *> CopySourceInvalid;
-  // Track uses of a register as a source for a previous COPY
+  // Track uses of a register as a source for a previous COPY. This avoids
+  // situations where COPY's outside of the chain use an original value that
+  // gets lost when eliminating the chain.
   DenseMap<const MachineInstr *, SmallVector<const MachineInstr *>>
       CopySourceDefCopies;
 
+  auto HasUnexpectedCopySourceDef = [&](const MachineInstr *Copy,
+                                        const MachineInstr *ExpectedDef) {
+    auto CopySouceDefPair = CopySourceDefCopies.find(Copy);
+    if (CopySouceDefPair == CopySourceDefCopies.end())
+      return false;
+
+    for (const MachineInstr *DefCopy : CopySouceDefPair->second)
+      if (DefCopy != ExpectedDef)
+        return true;
+    return false;
+  };
+
   auto TryFoldSpillageCopies =
       [&, this](const SmallVectorImpl<MachineInstr *> &SC,
                 const SmallVectorImpl<MachineInstr *> &RC) {
@@ -1379,24 +1393,13 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
           }
         };
 
-        auto HasUnexpectedCopySourceDef = [&](const MachineInstr *Copy,
-                                              const MachineInstr *ExpectedDef) {
-          auto CopySouceDefPair = CopySourceDefCopies.find(Copy);
-          if (CopySouceDefPair == CopySourceDefCopies.end())
-            return false;
-
-          for (const MachineInstr *DefCopy : CopySouceDefPair->second)
-            if (DefCopy != ExpectedDef)
-              return true;
-          return false;
-        };
         // Do not fold across an unexpected COPY def of a chains copy's source.
         // The paired reload may be needed to restore the original source value.
         // More info: https://github.com/llvm/llvm-project/issues/206839
         for (size_t I = 1; I < SC.size(); ++I) {
           if (HasUnexpectedCopySourceDef(SC[I], SC[I - 1]))
             return;
-          if (I + 1 > SC.size() && HasUnexpectedCopySourceDef(RC[I], RC[I + 1]))
+          if (I + 1 < SC.size() && HasUnexpectedCopySourceDef(RC[I], RC[I + 1]))
             return;
         }
 
diff --git a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
index 1bac98daf2169..dc9ff7a3f8fd9 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
+++ b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
@@ -18,7 +18,6 @@ body: |
     ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w13
     ; CHECK-NEXT: renamable $w15 = COPY $w23
     ; CHECK-NEXT: HINT 0, implicit killed $w15
-    ; CHECK-NEXT: HINT 0, implicit killed $x7
     ; CHECK-NEXT: renamable $w13 = COPY killed renamable $w10
     ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w11
     ; CHECK-NEXT: HINT 0, implicit killed $w10
@@ -32,8 +31,6 @@ body: |
 
     renamable $w13 = COPY killed renamable $w25  ; S3
 
-    HINT 0, implicit killed $x7
-
     renamable $w25 = COPY killed renamable $w13  ; R1
     renamable $w13 = COPY killed renamable $w10  ; R2
     renamable $w10 = COPY killed renamable $w11  ; R3

>From 9c6a4ee06a488e5ba94f3ce04030abe542d5ba0d Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Wed, 8 Jul 2026 15:16:03 +0100
Subject: [PATCH 5/5] respond to review comments

---
 llvm/lib/CodeGen/MachineCopyPropagation.cpp              | 9 ++++++++-
 .../AArch64/aarch64-no-fold-over-clobbered-source.mir    | 4 +++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index b46efc7882f6c..460bdbe316774 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1396,10 +1396,17 @@ void MachineCopyPropagation::eliminateSpillageCopies(MachineBasicBlock &MBB) {
         // Do not fold across an unexpected COPY def of a chains copy's source.
         // The paired reload may be needed to restore the original source value.
         // More info: https://github.com/llvm/llvm-project/issues/206839
+        //
+        // Spills are checked against against the previous spill
         for (size_t I = 1; I < SC.size(); ++I) {
           if (HasUnexpectedCopySourceDef(SC[I], SC[I - 1]))
             return;
-          if (I + 1 < SC.size() && HasUnexpectedCopySourceDef(RC[I], RC[I + 1]))
+        }
+        // Reloads are checked against the next reload. The final reload is
+        // skipped as this will have no corresponding outer reload to compare
+        // against.
+        for (size_t I = 0; I + 1 < RC.size(); ++I) {
+          if (HasUnexpectedCopySourceDef(RC[I], RC[I + 1]))
             return;
         }
 
diff --git a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
index dc9ff7a3f8fd9..2ceb583752b02 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
+++ b/llvm/test/CodeGen/AArch64/aarch64-no-fold-over-clobbered-source.mir
@@ -18,6 +18,7 @@ body: |
     ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w13
     ; CHECK-NEXT: renamable $w15 = COPY $w23
     ; CHECK-NEXT: HINT 0, implicit killed $w15
+    ; CHECK-NEXT: HINT 0, implicit $w25
     ; CHECK-NEXT: renamable $w13 = COPY killed renamable $w10
     ; CHECK-NEXT: renamable $w10 = COPY killed renamable $w11
     ; CHECK-NEXT: HINT 0, implicit killed $w10
@@ -29,7 +30,8 @@ body: |
     renamable $w15 = COPY killed renamable $w13
     HINT 0, implicit killed $w15
 
-    renamable $w13 = COPY killed renamable $w25  ; S3
+    renamable $w13 = COPY renamable $w25  ; S3
+    HINT 0, implicit $w25
 
     renamable $w25 = COPY killed renamable $w13  ; R1
     renamable $w13 = COPY killed renamable $w10  ; R2



More information about the llvm-commits mailing list