Skip to content

Commit 8dec7cf

Browse files
committed
[VPlan] Allow generating vectors with VPInstruction::ptradd. NFC
Currently a ptradd can only generate a scalar, or a series of scalars per-lane. In an upcoming patch to expand VPWidenPointerRecipe into smaller recipes, we need to be able to generate a vector ptradd, which currently we can't do. This adds support for generating vectors by checking to see if the offset operand is a vector: If it isn't, it will generate per-lane scalars as per usual.
1 parent 7eb14d9 commit 8dec7cf

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,10 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
958958
ExtractPenultimateElement,
959959
LogicalAnd, // Non-poison propagating logical And.
960960
// Add an offset in bytes (second operand) to a base pointer (first
961-
// operand). Only generates scalar values (either for the first lane only or
962-
// for all lanes, depending on its uses).
961+
// operand). The base pointer must be scalar, but the offset can be a
962+
// scalar, multiple scalars, or a vector. If the offset is multiple scalars
963+
// then it will generate multiple scalar values (either for the first lane
964+
// only or for all lanes, depending on its uses).
963965
PtrAdd,
964966
// Returns a scalar boolean value, which is true if any lane of its
965967
// (boolean) vector operands is true. It produces the reduced value across
@@ -998,7 +1000,7 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
9981000
/// values per all lanes, stemming from an original ingredient. This method
9991001
/// identifies the (rare) cases of VPInstructions that do so as well, w/o an
10001002
/// underlying ingredient.
1001-
bool doesGeneratePerAllLanes() const;
1003+
bool doesGeneratePerAllLanes(VPTransformState &State) const;
10021004

10031005
/// Returns true if we can generate a scalar for the first lane only if
10041006
/// needed.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,9 @@ unsigned VPInstruction::getNumOperandsForOpcode(unsigned Opcode) {
494494
}
495495
#endif
496496

497-
bool VPInstruction::doesGeneratePerAllLanes() const {
498-
return Opcode == VPInstruction::PtrAdd && !vputils::onlyFirstLaneUsed(this);
497+
bool VPInstruction::doesGeneratePerAllLanes(VPTransformState &State) const {
498+
return Opcode == VPInstruction::PtrAdd && !vputils::onlyFirstLaneUsed(this) &&
499+
!State.hasVectorValue(getOperand(1));
499500
}
500501

501502
bool VPInstruction::canGenerateScalarForFirstLane() const {
@@ -848,10 +849,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
848849
return Builder.CreateLogicalAnd(A, B, Name);
849850
}
850851
case VPInstruction::PtrAdd: {
851-
assert(vputils::onlyFirstLaneUsed(this) &&
852-
"can only generate first lane for PtrAdd");
853852
Value *Ptr = State.get(getOperand(0), VPLane(0));
854-
Value *Addend = State.get(getOperand(1), VPLane(0));
853+
Value *Addend = State.get(getOperand(1), vputils::onlyFirstLaneUsed(this));
855854
return Builder.CreatePtrAdd(Ptr, Addend, Name, getGEPNoWrapFlags());
856855
}
857856
case VPInstruction::AnyOf: {
@@ -911,9 +910,6 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
911910
}
912911
}
913912

914-
assert(!doesGeneratePerAllLanes() &&
915-
"Should only generate a vector value or single scalar, not scalars "
916-
"for all lanes.");
917913
return Ctx.TTI.getArithmeticInstrCost(getOpcode(), ResTy, Ctx.CostKind);
918914
}
919915

@@ -1001,7 +997,7 @@ void VPInstruction::execute(VPTransformState &State) {
1001997
bool GeneratesPerFirstLaneOnly = canGenerateScalarForFirstLane() &&
1002998
(vputils::onlyFirstLaneUsed(this) ||
1003999
isVectorToScalar() || isSingleScalar());
1004-
bool GeneratesPerAllLanes = doesGeneratePerAllLanes();
1000+
bool GeneratesPerAllLanes = doesGeneratePerAllLanes(State);
10051001
if (GeneratesPerAllLanes) {
10061002
for (unsigned Lane = 0, NumLanes = State.VF.getFixedValue();
10071003
Lane != NumLanes; ++Lane) {

0 commit comments

Comments
 (0)