[llvm] [AMDGPU] Guard RewriteMFMAFormStage recolor against unsafe def/use (PR #217396)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 1 09:11:18 PDT 2026
================
@@ -2803,35 +2816,92 @@ bool RewriteMFMAFormStage::rewrite(
findReachingUses(MI, DAG.LIS, DstReachingUses);
+ // An already-redefined dst reuses its mapped reg, so treat it as unsafe to
+ // recolor and bridge its reaching defs instead.
+ bool DstAlreadyRedef = RedefMap.contains(DstReg);
+ bool DstRecolorSafe =
+ !DstAlreadyRedef &&
+ isRecolorSafe(DstReg, DstReachingUses, RewriteCandsSet, /*IsDst=*/true);
+ const TargetRegisterClass *DstAGPRClass =
+ SRI->getEquivalentAGPRClass(DAG.MRI.getRegClass(DstReg));
for (MachineOperand *RUOp : DstReachingUses) {
MachineInstr *UserMI = RUOp->getParent();
- // Group members read the AGPR result directly.
- if (TII->isMAI(*UserMI) && RewriteCandsSet.contains(UserMI))
- continue;
-
- // If there is a non mai reaching use, then we need a copy.
- if (find(DstReachingUseCopies, RUOp) == DstReachingUseCopies.end())
+ // Decide whether this reaching use can read the dst's AGPR form directly
+ // or needs an AGPR->VGPR bridge copy.
+ // - A group-member MFMA always reads the AGPR result directly.
+ // - Any other user can skip the bridge only when the dst is recolored
+ // to AGPR (DstRecolorSafe) and its operand accepts an AGPR. When the
+ // dst is unsafe, its original reg stays VGPR, so every non-MFMA user
+ // must go through a bridge copy.
+ bool CanReadAGPR =
+ TII->isMAI(*UserMI)
+ ? RewriteCandsSet.contains(UserMI)
+ : DstRecolorSafe &&
+ userAcceptsAGPR(UserMI, DstReg, DstAGPRClass, TII, SRI);
+ if (!CanReadAGPR &&
----------------
arsenm wrote:
Do these skipped cases leave orphaned RedefMap[DstReg] entries?
https://github.com/llvm/llvm-project/pull/217396
More information about the llvm-commits
mailing list