[PATCH] D146037: [RISCV] Fix regression due to interaction of MachineOutliner and MachineCopyPropagation

Alex Bradbury via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 05:41:56 PDT 2023


asb created this revision.
asb added a reviewer: llvm-commits.
Herald added subscribers: jobnoorman, luke, wingo, pmatos, VincentWu, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
asb requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

D144535 <https://reviews.llvm.org/D144535> enabled machine copy propagation for RISC-V and added it to the pass pipeline in addPreEmitPass2 (after the MachineOutliner).  Unfortunately, the MachineCopyPropagation pass is unable to correctly analyse outlined functions, and will delete copy instructions where a register is set that is intended to be live-out. RISCVInstrInfo::buildOutlinedFrame will directly insert a `JALR`, while a similar function going through the normal codegen path would have a PseudoRet with operands indicating registers that are live-out.

This patch does the simplest fix, which is to run MachineCopyPropagation before the MachineOutliner. I think it would make sense to land something like this quickly, but I'd also be interested in discussing thoughts on a "better" follow-up fix that might allow MCP to run later in the pipeline.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146037

Files:
  llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
  llvm/test/CodeGen/RISCV/O3-pipeline.ll
  llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll


Index: llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll
===================================================================
--- llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll
+++ llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll
@@ -6,8 +6,6 @@
 ; register copy, which is at risk of being removed by MachineCopyPropagation
 ; if it can't see that the copy is used. At the time of writing this test
 ; case, MCP will remove the copy if it is run after the machine outliner.
-; FIXME: Ensure machine copy propagation is run before the machine outliner so
-; the copy in the outlined function isn't erroneously removed.
 
 define signext i32 @nge(i32 signext %a, i32 signext %b) nounwind {
 entry:
@@ -196,4 +194,5 @@
 ; RV64I-NEXT:    lui s0, 524288
 ; RV64I-NEXT:    addiw s0, s0, -1
 ; RV64I-NEXT:    lui a0, 524288
+; RV64I-NEXT:    mv a1, s0
 ; RV64I-NEXT:    jr t0
Index: llvm/test/CodeGen/RISCV/O3-pipeline.ll
===================================================================
--- llvm/test/CodeGen/RISCV/O3-pipeline.ll
+++ llvm/test/CodeGen/RISCV/O3-pipeline.ll
@@ -166,6 +166,7 @@
 ; CHECK-NEXT:       Implement the 'patchable-function' attribute
 ; CHECK-NEXT:       Branch relaxation pass
 ; CHECK-NEXT:       RISCV Make Compressible
+; CHECK-NEXT:       Machine Copy Propagation Pass
 ; CHECK-NEXT:       Contiguously Lay Out Funclets
 ; CHECK-NEXT:       StackMap Liveness Analysis
 ; CHECK-NEXT:       Live DEBUG_VALUE analysis
@@ -176,7 +177,6 @@
 ; CHECK-NEXT:       Machine Optimization Remark Emitter
 ; CHECK-NEXT:       Stack Frame Layout Analysis
 ; CHECK-NEXT:       RISCV pseudo instruction expansion pass
-; CHECK-NEXT:       Machine Copy Propagation Pass
 ; CHECK-NEXT:       RISCV atomic pseudo instruction expansion pass
 ; CHECK-NEXT:       Lazy Machine Block Frequency Analysis
 ; CHECK-NEXT:       Machine Optimization Remark Emitter
Index: llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -337,16 +337,19 @@
 void RISCVPassConfig::addPreEmitPass() {
   addPass(&BranchRelaxationPassID);
   addPass(createRISCVMakeCompressibleOptPass());
+
+  // TODO: It would potentially be better to schedule copy propagation after
+  // expanding pseudos (in addPreEmitPass2). However, performing copy
+  // propagation after the machine outliner (which runs after addPreEmitPass)
+  // currently leads to incorrect code-gen, where copies to registers within
+  // outlined functions are removed erroneously.
+  if (TM->getOptLevel() >= CodeGenOpt::Default && EnableRISCVCopyPropagation)
+    addPass(createMachineCopyPropagationPass(true));
 }
 
 void RISCVPassConfig::addPreEmitPass2() {
   addPass(createRISCVExpandPseudoPass());
 
-  // Do the copy propagation after expanding pseudos because we may produce some
-  // MVs when expanding.
-  if (TM->getOptLevel() >= CodeGenOpt::Default && EnableRISCVCopyPropagation)
-    addPass(createMachineCopyPropagationPass(true));
-
   // Schedule the expansion of AMOs at the last possible moment, avoiding the
   // possibility for other passes to break the requirements for forward
   // progress in the LR/SC block.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146037.505049.patch
Type: text/x-patch
Size: 3318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230314/ef1fef52/attachment.bin>


More information about the llvm-commits mailing list