Skip to content

[MCP] Disable BackwardCopyPropagateBlock for copies with implicit registers. #137687

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 2 commits into from
May 8, 2025

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Apr 28, 2025

If there's an implicit-def of a super register, the propagation
must preserve this implicit-def. Knowing how and when to do this
may require target specific knowledge so just disable it for now.

Prior to 2def1c4, we checked that the copy had explicit 2 operands
when that was removed we started allowing implicit operands through.
This patch adds a check for implicit operands, but still allows
extra explicit operands which was the goal of 2def1c4.

Fixes #131478.

@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-llvm-regalloc

@llvm/pr-subscribers-backend-systemz

Author: Craig Topper (topperc)

Changes

If there's an implicit-def of a super register, the propagation
must preserve this implicit-def. Knowing how and when to do this
may require target specific knowledge so just disable it for now.

Prior to e17f07c, we checked that the copy had explicit 2 operands
when that was removed we started allowing implicit operands through.
This patch adds a check for implicit operands, but still allows
extra explicit operands which was the goal of e17f07c.

Fixes #137667.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineCopyPropagation.cpp (+1-1)
  • (added) llvm/test/CodeGen/SystemZ/pr137667.mir (+21)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index ff75b87b23128..9e82b5caed0d0 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -1200,7 +1200,7 @@ void MachineCopyPropagation::BackwardCopyPropagateBlock(
     // Ignore non-trivial COPYs.
     std::optional<DestSourcePair> CopyOperands =
         isCopyInstr(MI, *TII, UseCopyInstr);
-    if (CopyOperands) {
+    if (CopyOperands && MI.getNumImplicitOperands() == 0) {
       Register DefReg = CopyOperands->Destination->getReg();
       Register SrcReg = CopyOperands->Source->getReg();
 
diff --git a/llvm/test/CodeGen/SystemZ/pr137667.mir b/llvm/test/CodeGen/SystemZ/pr137667.mir
new file mode 100644
index 0000000000000..66038f9dce22f
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/pr137667.mir
@@ -0,0 +1,21 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc %s -mtriple=s390x-unknown-linux-gnu -run-pass=machine-cp -verify-machineinstrs -o - | FileCheck %s
+
+---
+name:            t
+tracksRegLiveness: true
+noPhis:          true
+isSSA:           false
+noVRegs:         true
+hasFakeUses:     false
+tracksDebugUserValues: true
+body:             |
+  bb.0:
+    ; CHECK-LABEL: name: t
+    ; CHECK: renamable $r14d = LLILL 0
+    ; CHECK-NEXT: renamable $r12d = COPY killed renamable $r14d, implicit-def $r12q
+    ; CHECK-NEXT: Return implicit $r12q
+    renamable $r14d = LLILL 0
+    renamable $r12d = COPY killed renamable $r14d, implicit-def $r12q
+    Return implicit $r12q
+...

@s-barannikov
Copy link
Contributor

The links are incorrect?

@topperc
Copy link
Collaborator Author

topperc commented Apr 28, 2025

The links are incorrect?

Oops. Looks like I mixed up tabs in the browser while working on this. Right links are added now and the test is renamed.

topperc added 2 commits April 28, 2025 13:20
…isters.

If there's an implicit-def of a super register, the propagation
must preserve this implicit-def. Knowing how and when to do this
may require target specific knowledge so just disable it for now.

Prior to e17f07c, we checked that the copy had explicit 2 operands
when that was removed we started allowing implicit operands through.
This patch adds a check for implicit operands, but still allows
extra explicit operands which was the goal of e17f07c.

Fixes llvm#137667.
@@ -1200,7 +1200,7 @@ void MachineCopyPropagation::BackwardCopyPropagateBlock(
// Ignore non-trivial COPYs.
std::optional<DestSourcePair> CopyOperands =
isCopyInstr(MI, *TII, UseCopyInstr);
if (CopyOperands) {
if (CopyOperands && MI.getNumImplicitOperands() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought isCopyInstr was already filtering out these hard cases

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it does in the target instruction implementations, not the base copy handling

Copy link
Contributor

Choose a reason for hiding this comment

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

Might as well just not use isCopyInstr, you're probably filtering out cases the target code decided are OK

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For RISC-V, we want to support the canonical copy instruction that appears after ExpandPostRAPseudos, "ADDI rd, rs, 0". What are you suggesting we use here instead of isCopyInstr?

Copy link
Collaborator

@preames preames left a comment

Choose a reason for hiding this comment

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

LGTM

@topperc topperc merged commit e4b4a93 into llvm:main May 8, 2025
5 of 7 checks passed
@topperc topperc deleted the pr/137667 branch May 8, 2025 23:27
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.

[MachineCopyProp, SystemZ] "Using an undefined physical register"
5 participants