Skip to content

Commit 5d2e284

Browse files
jsjiarsenm
andauthored
MachineCopyPropagation: Do not remove copies preserved by regmask (#125868)
llvm/llvm-project@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 <[email protected]>
1 parent 0a470a9 commit 5d2e284

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Diff for: llvm/lib/CodeGen/MachineCopyPropagation.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -1018,18 +1018,30 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
10181018
continue;
10191019
}
10201020

1021-
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
1022-
MaybeDead->dump());
1023-
10241021
// Invalidate all entries in the copy map which are not preserved by
10251022
// this register mask.
1026-
for (unsigned RegUnit : TRI->regunits(Reg))
1023+
bool MIRefedinCopyInfo = false;
1024+
for (unsigned RegUnit : TRI->regunits(Reg)) {
10271025
if (!PreservedRegUnits.test(RegUnit))
10281026
Tracker.clobberRegUnit(RegUnit, *TRI, *TII, UseCopyInstr);
1027+
else {
1028+
if (MaybeDead == Tracker.findCopyForUnit(RegUnit, *TRI)) {
1029+
MIRefedinCopyInfo = true;
1030+
}
1031+
}
1032+
}
10291033

10301034
// erase() will return the next valid iterator pointing to the next
10311035
// element after the erased one.
10321036
DI = MaybeDeadCopies.erase(DI);
1037+
1038+
// Preserved by RegMask, DO NOT remove copy
1039+
if (MIRefedinCopyInfo)
1040+
continue;
1041+
1042+
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "
1043+
<< *MaybeDead);
1044+
10331045
MaybeDead->eraseFromParent();
10341046
Changed = true;
10351047
++NumDeletes;

Diff for: llvm/test/CodeGen/MIR/X86/pr126107.mir

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=machine-cp | FileCheck %s
3+
4+
---
5+
name: main
6+
body: |
7+
bb.0.entry:
8+
liveins: $ymm7
9+
; CHECK-LABEL: name: main
10+
; CHECK: liveins: $ymm7
11+
; CHECK-NEXT: {{ $}}
12+
; CHECK-NEXT: renamable $ymm6 = COPY killed renamable $ymm7
13+
; CHECK-NEXT: CALL64r killed renamable $rax, csr_64_mostregs
14+
; CHECK-NEXT: renamable $ymm6 = VPADDWZ256rr $ymm6, $ymm6
15+
renamable $ymm6 = COPY killed renamable $ymm7
16+
CALL64r killed renamable $rax, csr_64_mostregs
17+
renamable $ymm6 = VPADDWZ256rr $ymm6, $ymm6

0 commit comments

Comments
 (0)