[llvm] MachineCopyPropagation: Do not remove copies preserved by regmask (PR #125868)
Jinsong Ji via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 10 06:33:29 PST 2025
https://github.com/jsji updated https://github.com/llvm/llvm-project/pull/125868
>From 76a533d155b9b66a9524525871217471e56725ce Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Tue, 4 Feb 2025 13:45:00 -0800
Subject: [PATCH 1/5] MachineCopyPropagation: Do not remove copies preserved by
regmask
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.
---
llvm/lib/CodeGen/MachineCopyPropagation.cpp | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 460749a739c763..ba63d4c69d0900 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->dump());
+
MaybeDead->eraseFromParent();
Changed = true;
++NumDeletes;
>From dfc2935da8bfdc528205b1cbcf1b0699fa8bf1ee Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Wed, 5 Feb 2025 11:59:31 -0500
Subject: [PATCH 2/5] Update llvm/lib/CodeGen/MachineCopyPropagation.cpp
Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
---
llvm/lib/CodeGen/MachineCopyPropagation.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index ba63d4c69d0900..84139b1e689c94 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1039,8 +1039,7 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
if (MIRefedinCopyInfo)
continue;
- LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
- MaybeDead->dump());
+ LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead;
MaybeDead->eraseFromParent();
Changed = true;
>From 6b9546ec79208608dd6d442d8edbfd286241a6ba Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Wed, 5 Feb 2025 21:13:13 -0500
Subject: [PATCH 3/5] Update MachineCopyPropagation.cpp
Fix missing )
---
llvm/lib/CodeGen/MachineCopyPropagation.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 84139b1e689c94..f111be1f3f6a76 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1039,7 +1039,7 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
if (MIRefedinCopyInfo)
continue;
- LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead;
+ LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead);
MaybeDead->eraseFromParent();
Changed = true;
>From 6eb5d5df26cc42ffef1f4003bf399952f2a51fed Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Thu, 6 Feb 2025 06:34:35 -0800
Subject: [PATCH 4/5] clangformat
---
llvm/lib/CodeGen/MachineCopyPropagation.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index f111be1f3f6a76..8da95861ab18fd 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1039,7 +1039,8 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
if (MIRefedinCopyInfo)
continue;
- LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead);
+ LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "
+ << *MaybeDead);
MaybeDead->eraseFromParent();
Changed = true;
>From d29e0571fd24842cb62d8e3227640c4d23bf291c Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Mon, 10 Feb 2025 06:32:17 -0800
Subject: [PATCH 5/5] Add tests
---
llvm/test/CodeGen/MIR/X86/pr126107.mir | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 llvm/test/CodeGen/MIR/X86/pr126107.mir
diff --git a/llvm/test/CodeGen/MIR/X86/pr126107.mir b/llvm/test/CodeGen/MIR/X86/pr126107.mir
new file mode 100644
index 00000000000000..e8b3e47f6ff747
--- /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