[llvm] [VPlan] Support multiple F(Max|Min)Num reductions. (PR #161735)
Mel Chen via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 27 23:57:03 PDT 2025
================
@@ -880,52 +868,72 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
}
VPBasicBlock *LatchVPBB = LoopRegion->getExitingBasicBlock();
+ VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
+ VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->begin());
VPBuilder Builder(LatchVPBB->getTerminator());
- auto *LatchExitingBranch = cast<VPInstruction>(LatchVPBB->getTerminator());
- assert(LatchExitingBranch->getOpcode() == VPInstruction::BranchOnCount &&
+ VPValue *AnyNaN = nullptr;
+ SmallPtrSet<VPValue *, 2> RdxResults;
+ for (VPReductionPHIRecipe *RedPhiR : ReductionsToConvert) {
+ assert(RecurrenceDescriptor::isFPMinMaxNumRecurrenceKind(
+ RedPhiR->getRecurrenceKind()) &&
+ "unsupported reduction");
+
+ VPValue *MinMaxOp = GetMinMaxCompareValue(RedPhiR);
+ if (!MinMaxOp)
+ return false;
+
+ VPValue *IsNaN = Builder.createFCmp(CmpInst::FCMP_UNO, MinMaxOp, MinMaxOp);
+ VPValue *HasNaN = Builder.createNaryOp(VPInstruction::AnyOf, {IsNaN});
+ if (AnyNaN)
+ AnyNaN = Builder.createOr(AnyNaN, HasNaN);
+ else
+ AnyNaN = HasNaN;
+
+ // If we exit early due to NaNs, compute the final reduction result based
+ // on the reduction phi at the beginning of the last vector iteration.
+ auto *RdxResult = find_singleton<VPSingleDefRecipe>(
+ RedPhiR->getBackedgeValue()->users(),
+ [RedPhiR](VPUser *U, bool) -> VPSingleDefRecipe * {
+ auto *VPI = dyn_cast<VPInstruction>(U);
+ if (VPI && VPI->getOpcode() == VPInstruction::ComputeReductionResult)
+ return VPI;
+ assert(U == RedPhiR &&
+ "Backedge value must only be used by "
+ "ComputeReductionResult and the reduction phi");
----------------
Mel-Chen wrote:
If CM.usePredicatedReductionSelect() is false, there might be a DU chain like backedge recipe --> select --> ComputeReductionResult. Would this still be able to find ComputeReductionResult correctly?
https://github.com/llvm/llvm-project/pull/161735
More information about the llvm-commits
mailing list