[llvm] [LV] Add support for partial alias masking with tail folding (PR #182457)
Benjamin Maxwell via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 16 04:47:03 PDT 2026
================
@@ -8679,6 +8750,55 @@ void LoopVectorizationPlanner::attachRuntimeChecks(
}
}
+void LoopVectorizationPlanner::materializeAliasMaskCheckBlock(
+ VPlan &Plan, ArrayRef<PointerDiffInfo> DiffChecks, bool HasBranchWeights) {
+ assert(CM.foldTailByMasking() && "Expected tail folding to be enabled");
+ VPBasicBlock *ClampedVFCheck =
+ Plan.createVPBasicBlock("vector.clamped.vf.check");
+
+ VPValue *ClampedVF = VPlanTransforms::materializeAliasMask(
+ Plan, ClampedVFCheck,
+ *CM.Legal->getRuntimePointerChecking()->getDiffChecks());
+ VPBuilder Builder(ClampedVFCheck);
+ DebugLoc DL = DebugLoc::getCompilerGenerated();
+ Type *TCTy = VPTypeAnalysis(Plan).inferScalarType(Plan.getTripCount());
+
+ // Check the "ClampedVF" from the alias mask is larger than one.
+ VPValue *IsScalar =
+ Builder.createICmp(CmpInst::ICMP_ULE, ClampedVF,
+ Plan.getConstantInt(TCTy, 1), DL, "vf.is.scalar");
+
+ VPValue *TripCount = Plan.getTripCount();
+ VPValue *MaxUIntTripCount =
+ Plan.getConstantInt(cast<IntegerType>(TCTy)->getMask());
+ VPValue *DistanceToMax = Builder.createSub(MaxUIntTripCount, TripCount);
+
+ // For tail-folding: Don't execute the vector loop if (UMax - n) < ClampedVF.
+ // Note: The ClampedVF may not be a power-of-two. This means loop exit
+ // condition (index.next == n.vec) may not be correct in the case of an
+ // overflow. The issue is `n.vec` could be zero due to an overflow, but
+ // index.next is not guaranteed to overflow to zero as the VF is not a
+ // power-of-two).
+ VPValue *TripCountCheck = Builder.createICmp(
+ ICmpInst::ICMP_ULT, DistanceToMax, ClampedVF, DL, "vf.step.overflow");
+
+ VPValue *Cond = Builder.createOr(IsScalar, TripCountCheck, DL);
+ VPlanTransforms::attachCheckBlock(Plan, Cond, ClampedVFCheck,
+ HasBranchWeights);
+
+ // Materialize the trip count early as this will add a use of (VFxUF) that
+ // needs to be replaced with the ClampedVF.
+ VPlanTransforms::materializeVectorTripCount(Plan, Plan.getVectorPreheader(),
+ /*TailByMasking=*/true,
+ /*RequiresScalarEpilogue=*/false,
+ &Plan.getVFxUF());
----------------
MacDue wrote:
I've adjusted the assertion, but passing the `ClampedVF` here is not really desirable as it means the trip-count will logic will be hoisted into the alias-mask check block (where it will run even if the check fails).
https://github.com/llvm/llvm-project/pull/182457
More information about the llvm-commits
mailing list