[llvm] [VPlan] Materialize Build(Struct)Vectors for VPReplicateRecipes. (NFCI) (PR #151487)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 1 11:40:30 PDT 2025
================
@@ -3192,6 +3192,51 @@ void VPlanTransforms::materializeVectorTripCount(
Plan.getVectorTripCount().setUnderlyingValue(NewC->getValue());
}
+void VPlanTransforms::materializeBuildVectors(VPlan &Plan) {
+ if (Plan.hasScalarVFOnly())
+ return;
+
+ VPTypeAnalysis TypeInfo(Plan);
+ VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+ auto VPBBsOutsideLoopRegion = VPBlockUtils::blocksOnly<VPBasicBlock>(
+ vp_depth_first_shallow(Plan.getEntry()));
+ auto VPBBsInsideLoopRegion = VPBlockUtils::blocksOnly<VPBasicBlock>(
+ vp_depth_first_shallow(LoopRegion->getEntry()));
+ for (VPBasicBlock *VPBB :
+ concat<VPBasicBlock *>(VPBBsOutsideLoopRegion, VPBBsInsideLoopRegion)) {
+ for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
+ auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
+ if (!RepR || RepR->isSingleScalar())
+ continue;
+ VPInstruction *BuildVector = nullptr;
+ for (VPUser *U : to_vector(RepR->users())) {
+ VPRegionBlock *ParentRegion =
+ cast<VPRecipeBase>(U)->getParent()->getParent();
+ if (U->usesScalars(RepR) && ParentRegion == LoopRegion)
+ continue;
+
+ if (!BuildVector) {
+ Type *ScalarTy = TypeInfo.inferScalarType(RepR);
+ unsigned Opc = ScalarTy->isStructTy()
+ ? VPInstruction::BuildStructVector
+ : VPInstruction::BuildVector;
+ BuildVector = new VPInstruction(Opc, {RepR});
+ BuildVector->insertAfter(RepR);
+ }
+
+ // Only update a single operand per users, as the same user is added
+ // multiple times, once per use.
+ // TODO: Introduce de-duplicating iterator over users.
+ for (unsigned Idx = 0; Idx != U->getNumOperands(); ++Idx)
+ if (U->getOperand(Idx) == RepR) {
+ U->setOperand(Idx, BuildVector);
+ break;
+ }
----------------
artagnon wrote:
It seems that later in the code, RepR is totally useless, and all it's used for is to get its users. Can we remove all ReplicateRecipes that have users, after replacing the operands of the users with the BuildVector? Once we have the BuildVectors, what purpose does the ReplicateRecipe serve?
https://github.com/llvm/llvm-project/pull/151487
More information about the llvm-commits
mailing list