Skip to content

MachineCopyPropagation: Do not remove copies preserved by regmask #125868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2025

Conversation

jsji
Copy link
Member

@jsji jsji commented Feb 5, 2025

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

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.
@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-llvm-regalloc

Author: Jinsong Ji (jsji)

Changes

llvm/llvm-project@9e436c2 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.


Full diff: https://github.com/llvm/llvm-project/pull/125868.diff

1 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineCopyPropagation.cpp (+16-4)
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;

@bzEq
Copy link
Collaborator

bzEq commented Feb 5, 2025

Could you please provide the test causing assertion failure? MIR test is preferred.
Ok, I have read the comments in the previous PR.

@JaydeepChauhan14
Copy link
Contributor

@arsenm , @evanphx , @bzEq can please review pr.

@ostannard
Copy link
Collaborator

Issue #126107 has a reproducer for this, so that can be added as a test. I've checked that applying this patch does fix that assertion.

@jsji jsji self-assigned this Feb 10, 2025
@jsji
Copy link
Member Author

jsji commented Feb 10, 2025

Issue #126107 has a reproducer for this, so that can be added as a test. I've checked that applying this patch does fix that assertion.

Thanks! Added MIR test from #126107.

@jsji jsji requested a review from arsenm February 10, 2025 14:34
Copy link
Collaborator

@ostannard ostannard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM

if (MIRefedinCopyInfo)
continue;

LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing close-paren, which is causing the build failure.

Suggested change
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead;
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: " << *MaybeDead);

@jsji jsji merged commit 5d2e284 into llvm:main Feb 10, 2025
8 checks passed
jthackray added a commit to jthackray/llvm-project that referenced this pull request Feb 11, 2025
Update the `generate-tests.py` script to create new tests for
`atomicrmw {fadd,fmin,fmax}` and test these with `half`, `float`,
`bfloat` and `double`.

Generate auto-tests to check both with and without `+lsfe`, so
that when llvm#125868 is merged, `+lsfe` will use a single atomic
floating-point instruction.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…vm#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: llvm#126107

---------

Co-authored-by: Matt Arsenault <[email protected]>
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…vm#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: llvm#126107

---------

Co-authored-by: Matt Arsenault <[email protected]>
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…vm#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: llvm#126107

---------

Co-authored-by: Matt Arsenault <[email protected]>
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Jun 12, 2025
…vm#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MachineCP] Assertion `Reg.isPhysical()' failed in MCRegUnitIterator
6 participants