[PATCH] D152502: [MCP] Do not remove redundant copy for COPY from undef

Pierre van Houtryve via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 00:29:30 PDT 2023


Pierre-vh created this revision.
Pierre-vh added reviewers: craig.topper, arsenm, foad, critson.
Herald added subscribers: StephenFan, pengfei, hiraditya, tpr.
Herald added a project: All.
Pierre-vh requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

I don't think we can safely remove the second COPY as redundant in such cases.
The first COPY (which has undef src) may be lowered to a KILL instruction instead, resulting in no COPY being emitted at all.

Testcase is X86 so it's in the same place as other testcases for this function, but this was initially spotted on AMDGPU with the following:

  renamable $vgpr24 = PRED_COPY undef renamable $vgpr25, implicit $exec
  renamable $vgpr24 = PRED_COPY killed renamable $vgpr25, implicit $exec

The second COPY waas removed as redundant, and the first one was lowered to a KILL (= removed too), causing $vgpr24 to not have $vgpr25's value.

Fixes SWDEV-401507


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152502

Files:
  llvm/lib/CodeGen/MachineCopyPropagation.cpp
  llvm/test/CodeGen/X86/machine-copy-prop.mir


Index: llvm/test/CodeGen/X86/machine-copy-prop.mir
===================================================================
--- llvm/test/CodeGen/X86/machine-copy-prop.mir
+++ llvm/test/CodeGen/X86/machine-copy-prop.mir
@@ -14,6 +14,7 @@
   define void @nocopyprop3() { ret void }
   define void @nocopyprop4() { ret void }
   define void @nocopyprop5() { ret void }
+  define void @nocopyprop6() { ret void }
 ...
 ---
 # The second copy is redundant and will be removed, check that we also remove
@@ -213,3 +214,21 @@
     $rip = COPY $rax
     $rip = COPY $rax
 ...
+---
+# The copies are obviously redundant, but the earlier COPY copies from "undef".
+# That copy may be lowered to a KILL instead and thus we cannot remove the
+# second COPY, otherwise we risk making $rax undef.
+# CHECK-LABEL: name: nocopyprop6
+# CHECK: bb.0:
+# CHECK-NEXT: $rax = COPY undef $rdi
+# CHECK-NEXT: NOOP implicit killed $rax
+# CHECK-NEXT: $rax = COPY $rdi
+# CHECK-NEXT: NOOP implicit $rax, implicit $rdi
+name: nocopyprop6
+body: |
+  bb.0:
+    $rax = COPY undef $rdi
+    NOOP implicit killed $rax
+    $rax = COPY $rdi
+    NOOP implicit $rax, implicit $rdi
+...
Index: llvm/lib/CodeGen/MachineCopyPropagation.cpp
===================================================================
--- llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -462,7 +462,8 @@
 
   auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr);
   // Check that the existing copy uses the correct sub registers.
-  if (PrevCopyOperands->Destination->isDead())
+  if (PrevCopyOperands->Destination->isDead() ||
+      PrevCopyOperands->Source->isUndef())
     return false;
   if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr))
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152502.529842.patch
Type: text/x-patch
Size: 1770 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230609/2568536e/attachment.bin>


More information about the llvm-commits mailing list