[llvm] 5d2e284 - MachineCopyPropagation: Do not remove copies preserved by regmask (#125868)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 10 09:26:36 PST 2025


Author: Jinsong Ji
Date: 2025-02-10T12:26:33-05:00
New Revision: 5d2e2847e09ae70e24b6c749c08028e705786113

URL: https://github.com/llvm/llvm-project/commit/5d2e2847e09ae70e24b6c749c08028e705786113
DIFF: https://github.com/llvm/llvm-project/commit/5d2e2847e09ae70e24b6c749c08028e705786113.diff

LOG: MachineCopyPropagation: Do not remove copies preserved by regmask (#125868)

llvm/llvm-project at 9e436c2daa44 tries to handle register masks and
sub-registers, it avoids clobbering RegUnit presreved by regmask. But it
then introduces invalid pointer issues.

We delete the copies without invalidate all the use in the CopyInfo, so
we dereferenced invalid pointers in next interation, causing asserts.

Fixes: #126107

---------

Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>

Added: 
    llvm/test/CodeGen/MIR/X86/pr126107.mir

Modified: 
    llvm/lib/CodeGen/MachineCopyPropagation.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 4d9d7128f73a8b6..1105b8c15515fbe 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1018,18 +1018,30 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
           continue;
         }
 
-        LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
-                   MaybeDead->dump());
-
         // Invalidate all entries in the copy map which are not preserved by
         // this register mask.
-        for (unsigned RegUnit : TRI->regunits(Reg))
+        bool MIRefedinCopyInfo = false;
+        for (unsigned RegUnit : TRI->regunits(Reg)) {
           if (!PreservedRegUnits.test(RegUnit))
             Tracker.clobberRegUnit(RegUnit, *TRI, *TII, UseCopyInstr);
+          else {
+            if (MaybeDead == Tracker.findCopyForUnit(RegUnit, *TRI)) {
+              MIRefedinCopyInfo = true;
+            }
+          }
+        }
 
         // erase() will return the next valid iterator pointing to the next
         // element after the erased one.
         DI = MaybeDeadCopies.erase(DI);
+
+        // Preserved by RegMask, DO NOT remove copy
+        if (MIRefedinCopyInfo)
+          continue;
+
+        LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "
+                          << *MaybeDead);
+
         MaybeDead->eraseFromParent();
         Changed = true;
         ++NumDeletes;

diff  --git a/llvm/test/CodeGen/MIR/X86/pr126107.mir b/llvm/test/CodeGen/MIR/X86/pr126107.mir
new file mode 100644
index 000000000000000..e8b3e47f6ff747b
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/X86/pr126107.mir
@@ -0,0 +1,17 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=machine-cp | FileCheck %s
+
+---
+name:            main
+body:             |
+ bb.0.entry:
+    liveins: $ymm7
+    ; CHECK-LABEL: name: main
+    ; CHECK: liveins: $ymm7
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $ymm6 = COPY killed renamable $ymm7
+    ; CHECK-NEXT: CALL64r killed renamable $rax, csr_64_mostregs
+    ; CHECK-NEXT: renamable $ymm6 = VPADDWZ256rr $ymm6, $ymm6
+       renamable $ymm6 = COPY killed renamable $ymm7
+       CALL64r killed renamable $rax, csr_64_mostregs
+       renamable $ymm6 = VPADDWZ256rr $ymm6, $ymm6


        


More information about the llvm-commits mailing list