Skip to content

Commit 5b0c3d9

Browse files
authored
MachineCopyPropagation: Do not remove copies preserved by regmask (llvm#125868) (llvm#2525)
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: llvm#126107 --------- Co-authored-by: Matt Arsenault <[email protected]> (cherry picked from commit 5d2e284)
1 parent f5dcf1e commit 5b0c3d9

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

llvm/lib/CodeGen/MachineCopyPropagation.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,18 +1009,30 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
10091009
continue;
10101010
}
10111011

1012-
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
1013-
MaybeDead->dump());
1014-
10151012
// Invalidate all entries in the copy map which are not preserved by
10161013
// this register mask.
1017-
for (unsigned RegUnit : TRI->regunits(Reg))
1014+
bool MIRefedinCopyInfo = false;
1015+
for (unsigned RegUnit : TRI->regunits(Reg)) {
10181016
if (!PreservedRegUnits.test(RegUnit))
10191017
Tracker.clobberRegUnit(RegUnit, *TRI, *TII, UseCopyInstr);
1018+
else {
1019+
if (MaybeDead == Tracker.findCopyForUnit(RegUnit, *TRI)) {
1020+
MIRefedinCopyInfo = true;
1021+
}
1022+
}
1023+
}
10201024

10211025
// erase() will return the next valid iterator pointing to the next
10221026
// element after the erased one.
10231027
DI = MaybeDeadCopies.erase(DI);
1028+
1029+
// Preserved by RegMask, DO NOT remove copy
1030+
if (MIRefedinCopyInfo)
1031+
continue;
1032+
1033+
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "
1034+
<< *MaybeDead);
1035+
10241036
MaybeDead->eraseFromParent();
10251037
Changed = true;
10261038
++NumDeletes;
Lines changed: 17 additions & 0 deletions
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)