Skip to content

Commit 11e745a

Browse files
committed
Support mapping Instructions to VPValues in VPRecipeBuilder
1 parent b817fa0 commit 11e745a

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9052,16 +9052,16 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
90529052
for (Instruction &I : drop_end(BB->instructionsWithoutDebug(false))) {
90539053
Instruction *Instr = &I;
90549054

9055-
// A special case. Mapping handled in
9056-
// VPRecipeBuilder::getVPValueOrAddLiveIn().
9055+
// Special case: Handle extractvalues from struct ret calls.
90579056
if (auto *ExtractValue = dyn_cast<ExtractValueInst>(Instr)) {
9058-
bool IsUniform = LoopVectorizationPlanner::getDecisionAndClampRange(
9059-
[&](ElementCount VF) {
9060-
return CM.isUniformAfterVectorization(ExtractValue, VF);
9061-
},
9062-
Range);
9063-
if (!IsUniform)
9057+
if (auto *CI = dyn_cast<CallInst>(ExtractValue->getAggregateOperand())) {
9058+
auto *R = RecipeBuilder.getRecipe(cast<Instruction>(CI));
9059+
assert(R->getNumDefinedValues() ==
9060+
cast<StructType>(CI->getType())->getNumElements());
9061+
unsigned Idx = ExtractValue->getIndices()[0];
9062+
RecipeBuilder.setRecipe(Instr, R->getVPValue(Idx));
90649063
continue;
9064+
}
90659065
}
90669066

90679067
SmallVector<VPValue *, 4> Operands;

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ class VPRecipeBuilder {
5454
EdgeMaskCacheTy EdgeMaskCache;
5555
BlockMaskCacheTy BlockMaskCache;
5656

57+
using RecipeOrResult = PointerUnion<VPRecipeBase*, VPValue*>;
58+
5759
// VPlan construction support: Hold a mapping from ingredients to
5860
// their recipe.
59-
DenseMap<Instruction *, VPRecipeBase *> Ingredient2Recipe;
61+
DenseMap<Instruction *, RecipeOrResult> Ingredient2Recipe;
6062

6163
/// Cross-iteration reduction & first-order recurrence phis for which we need
6264
/// to add the incoming value from the backedge after all recipes have been
@@ -132,6 +134,14 @@ class VPRecipeBuilder {
132134
Ingredient2Recipe[I] = R;
133135
}
134136

137+
// Set the recipe value for a given ingredient.
138+
void setRecipe(Instruction* I, VPValue* RecipeResult) {
139+
assert(!Ingredient2Recipe.contains(I) &&
140+
"Cannot reset recipe for instruction.");
141+
assert(RecipeResult->getDefiningRecipe() && "Value must be defined by a recipe.");
142+
Ingredient2Recipe[I] = RecipeResult;
143+
}
144+
135145
/// Create the mask for the vector loop header block.
136146
void createHeaderMask();
137147

@@ -158,9 +168,12 @@ class VPRecipeBuilder {
158168
VPRecipeBase *getRecipe(Instruction *I) {
159169
assert(Ingredient2Recipe.count(I) &&
160170
"Recording this ingredients recipe was not requested");
161-
assert(Ingredient2Recipe[I] != nullptr &&
171+
assert(Ingredient2Recipe[I] &&
162172
"Ingredient doesn't have a recipe");
163-
return Ingredient2Recipe[I];
173+
auto RecipeInfo = Ingredient2Recipe[I];
174+
if (auto* R = RecipeInfo.dyn_cast<VPRecipeBase*>())
175+
return R;
176+
return RecipeInfo.get<VPValue*>()->getDefiningRecipe();
164177
}
165178

166179
/// Build a VPReplicationRecipe for \p I. If it is predicated, add the mask as
@@ -179,17 +192,10 @@ class VPRecipeBuilder {
179192

180193
VPValue *getVPValueOrAddLiveIn(Value *V) {
181194
if (auto *I = dyn_cast<Instruction>(V)) {
182-
if (auto *R = Ingredient2Recipe.lookup(I))
183-
return R->getVPSingleValue();
184-
}
185-
// Ugh: Not sure where to handle this :(
186-
if (auto *EVI = dyn_cast<ExtractValueInst>(V)) {
187-
Value *AggOp = EVI->getAggregateOperand();
188-
if (auto *R = getRecipe(cast<Instruction>(AggOp))) {
189-
assert(R->getNumDefinedValues() ==
190-
cast<StructType>(AggOp->getType())->getNumElements());
191-
unsigned Idx = EVI->getIndices()[0];
192-
return R->getVPValue(Idx);
195+
if (auto RecipeInfo = Ingredient2Recipe.lookup(I)) {
196+
if (auto* R = RecipeInfo.dyn_cast<VPRecipeBase*>())
197+
return R->getVPSingleValue();
198+
return RecipeInfo.get<VPValue*>();
193199
}
194200
}
195201
return Plan.getOrAddLiveIn(V);

0 commit comments

Comments
 (0)