Skip to content

Commit bc849e5

Browse files
committed
Revert "[FuncSpec] Add Phi nodes to the InstCostVisitor."
This reverts commit 03f1d09 because of a crash reported on https://reviews.llvm.org/D154852
1 parent 9957225 commit bc849e5

File tree

3 files changed

+7
-143
lines changed

3 files changed

+7
-143
lines changed

llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
126126
SCCPSolver &Solver;
127127

128128
ConstMap KnownConstants;
129-
// Basic blocks known to be unreachable after constant propagation.
130-
DenseSet<BasicBlock *> DeadBlocks;
131-
// PHI nodes we have visited before.
132-
DenseSet<Instruction *> VisitedPHIs;
133-
// PHI nodes we have visited once without successfully constant folding them.
134-
// Once the InstCostVisitor has processed all the specialization arguments,
135-
// it should be possible to determine whether those PHIs can be folded
136-
// (some of their incoming values may have become constant or dead).
137-
SmallVector<Instruction *> PendingPHIs;
138129

139130
ConstMap::iterator LastVisited;
140131

@@ -143,10 +134,7 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
143134
TargetTransformInfo &TTI, SCCPSolver &Solver)
144135
: DL(DL), BFI(BFI), TTI(TTI), Solver(Solver) {}
145136

146-
Cost getUserBonus(Instruction *User, Value *Use = nullptr,
147-
Constant *C = nullptr);
148-
149-
Cost getBonusFromPendingPHIs();
137+
Cost getUserBonus(Instruction *User, Value *Use, Constant *C);
150138

151139
private:
152140
friend class InstVisitor<InstCostVisitor, Constant *>;
@@ -155,7 +143,6 @@ class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {
155143
Cost estimateBranchInst(BranchInst &I);
156144

157145
Constant *visitInstruction(Instruction &I) { return nullptr; }
158-
Constant *visitPHINode(PHINode &I);
159146
Constant *visitFreezeInst(FreezeInst &I);
160147
Constant *visitCallBase(CallBase &I);
161148
Constant *visitLoadInst(LoadInst &I);

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ static cl::opt<unsigned> MaxClones(
7878
"The maximum number of clones allowed for a single function "
7979
"specialization"));
8080

81-
static cl::opt<unsigned> MaxIncomingPhiValues(
82-
"funcspec-max-incoming-phi-values", cl::init(4), cl::Hidden, cl::desc(
83-
"The maximum number of incoming values a PHI node can have to be "
84-
"considered during the specialization bonus estimation"));
85-
8681
static cl::opt<unsigned> MinFunctionSize(
8782
"funcspec-min-function-size", cl::init(100), cl::Hidden, cl::desc(
8883
"Don't specialize functions that have less than this number of "
@@ -109,7 +104,6 @@ static cl::opt<bool> SpecializeLiteralConstant(
109104
// the combination of size and latency savings in comparison to the non
110105
// specialized version of the function.
111106
static Cost estimateBasicBlocks(SmallVectorImpl<BasicBlock *> &WorkList,
112-
DenseSet<BasicBlock *> &DeadBlocks,
113107
ConstMap &KnownConstants, SCCPSolver &Solver,
114108
BlockFrequencyInfo &BFI,
115109
TargetTransformInfo &TTI) {
@@ -124,12 +118,6 @@ static Cost estimateBasicBlocks(SmallVectorImpl<BasicBlock *> &WorkList,
124118
if (!Weight)
125119
continue;
126120

127-
// These blocks are considered dead as far as the InstCostVisitor is
128-
// concerned. They haven't been proven dead yet by the Solver, but
129-
// may become if we propagate the constant specialization arguments.
130-
if (!DeadBlocks.insert(BB).second)
131-
continue;
132-
133121
for (Instruction &I : *BB) {
134122
// Disregard SSA copies.
135123
if (auto *II = dyn_cast<IntrinsicInst>(&I))
@@ -164,19 +152,9 @@ static Constant *findConstantFor(Value *V, ConstMap &KnownConstants) {
164152
return nullptr;
165153
}
166154

167-
Cost InstCostVisitor::getBonusFromPendingPHIs() {
168-
Cost Bonus = 0;
169-
while (!PendingPHIs.empty()) {
170-
Instruction *Phi = PendingPHIs.pop_back_val();
171-
Bonus += getUserBonus(Phi);
172-
}
173-
return Bonus;
174-
}
175-
176155
Cost InstCostVisitor::getUserBonus(Instruction *User, Value *Use, Constant *C) {
177156
// Cache the iterator before visiting.
178-
LastVisited = Use ? KnownConstants.insert({Use, C}).first
179-
: KnownConstants.end();
157+
LastVisited = KnownConstants.insert({Use, C}).first;
180158

181159
if (auto *I = dyn_cast<SwitchInst>(User))
182160
return estimateSwitchInst(*I);
@@ -203,15 +181,13 @@ Cost InstCostVisitor::getUserBonus(Instruction *User, Value *Use, Constant *C) {
203181

204182
for (auto *U : User->users())
205183
if (auto *UI = dyn_cast<Instruction>(U))
206-
if (UI != User && Solver.isBlockExecutable(UI->getParent()))
184+
if (Solver.isBlockExecutable(UI->getParent()))
207185
Bonus += getUserBonus(UI, User, C);
208186

209187
return Bonus;
210188
}
211189

212190
Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
213-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
214-
215191
if (I.getCondition() != LastVisited->first)
216192
return 0;
217193

@@ -232,13 +208,10 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
232208
WorkList.push_back(BB);
233209
}
234210

235-
return estimateBasicBlocks(WorkList, DeadBlocks, KnownConstants, Solver, BFI,
236-
TTI);
211+
return estimateBasicBlocks(WorkList, KnownConstants, Solver, BFI, TTI);
237212
}
238213

239214
Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
240-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
241-
242215
if (I.getCondition() != LastVisited->first)
243216
return 0;
244217

@@ -250,39 +223,10 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
250223
Succ->getUniquePredecessor() == I.getParent())
251224
WorkList.push_back(Succ);
252225

253-
return estimateBasicBlocks(WorkList, DeadBlocks, KnownConstants, Solver, BFI,
254-
TTI);
255-
}
256-
257-
Constant *InstCostVisitor::visitPHINode(PHINode &I) {
258-
if (I.getNumIncomingValues() > MaxIncomingPhiValues)
259-
return nullptr;
260-
261-
bool Inserted = VisitedPHIs.insert(&I).second;
262-
Constant *Const = nullptr;
263-
264-
for (unsigned Idx = 0, E = I.getNumIncomingValues(); Idx != E; ++Idx) {
265-
Value *V = I.getIncomingValue(Idx);
266-
if (auto *Inst = dyn_cast<Instruction>(V))
267-
if (Inst == &I || DeadBlocks.contains(I.getIncomingBlock(Idx)))
268-
continue;
269-
Constant *C = findConstantFor(V, KnownConstants);
270-
if (!C) {
271-
if (Inserted)
272-
PendingPHIs.push_back(&I);
273-
return nullptr;
274-
}
275-
if (!Const)
276-
Const = C;
277-
else if (C != Const)
278-
return nullptr;
279-
}
280-
return Const;
226+
return estimateBasicBlocks(WorkList, KnownConstants, Solver, BFI, TTI);
281227
}
282228

283229
Constant *InstCostVisitor::visitFreezeInst(FreezeInst &I) {
284-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
285-
286230
if (isGuaranteedNotToBeUndefOrPoison(LastVisited->second))
287231
return LastVisited->second;
288232
return nullptr;
@@ -309,8 +253,6 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
309253
}
310254

311255
Constant *InstCostVisitor::visitLoadInst(LoadInst &I) {
312-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
313-
314256
if (isa<ConstantPointerNull>(LastVisited->second))
315257
return nullptr;
316258
return ConstantFoldLoadFromConstPtr(LastVisited->second, I.getType(), DL);
@@ -333,8 +275,6 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
333275
}
334276

335277
Constant *InstCostVisitor::visitSelectInst(SelectInst &I) {
336-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
337-
338278
if (I.getCondition() != LastVisited->first)
339279
return nullptr;
340280

@@ -350,8 +290,6 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
350290
}
351291

352292
Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
353-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
354-
355293
bool Swap = I.getOperand(1) == LastVisited->first;
356294
Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
357295
Constant *Other = findConstantFor(V, KnownConstants);
@@ -365,14 +303,10 @@ Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
365303
}
366304

367305
Constant *InstCostVisitor::visitUnaryOperator(UnaryOperator &I) {
368-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
369-
370306
return ConstantFoldUnaryOpOperand(I.getOpcode(), LastVisited->second, DL);
371307
}
372308

373309
Constant *InstCostVisitor::visitBinaryOperator(BinaryOperator &I) {
374-
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
375-
376310
bool Swap = I.getOperand(1) == LastVisited->first;
377311
Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
378312
Constant *Other = findConstantFor(V, KnownConstants);
@@ -779,17 +713,13 @@ bool FunctionSpecializer::findSpecializations(Function *F, Cost SpecCost,
779713
AllSpecs[Index].CallSites.push_back(&CS);
780714
} else {
781715
// Calculate the specialisation gain.
782-
Cost Score = 0;
716+
Cost Score = 0 - SpecCost;
783717
InstCostVisitor Visitor = getInstCostVisitorFor(F);
784718
for (ArgInfo &A : S.Args)
785719
Score += getSpecializationBonus(A.Formal, A.Actual, Visitor);
786-
Score += Visitor.getBonusFromPendingPHIs();
787-
788-
LLVM_DEBUG(dbgs() << "FnSpecialization: Specialization score = "
789-
<< Score << "\n");
790720

791721
// Discard unprofitable specialisations.
792-
if (!ForceSpecialization && Score <= SpecCost)
722+
if (!ForceSpecialization && Score <= 0)
793723
continue;
794724

795725
// Create a new specialisation entry.

llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -287,56 +287,3 @@ TEST_F(FunctionSpecializationTest, Misc) {
287287
Bonus = Specializer.getSpecializationBonus(F->getArg(3), Undef, Visitor);
288288
EXPECT_TRUE(Bonus == 0);
289289
}
290-
291-
TEST_F(FunctionSpecializationTest, PhiNode) {
292-
const char *ModuleString = R"(
293-
define void @foo(i32 %a, i32 %b, i32 %i) {
294-
entry:
295-
br label %loop
296-
loop:
297-
switch i32 %i, label %default
298-
[ i32 1, label %case1
299-
i32 2, label %case2 ]
300-
case1:
301-
%0 = add i32 %a, 1
302-
br label %bb
303-
case2:
304-
%1 = sub i32 %b, 1
305-
br label %bb
306-
bb:
307-
%2 = phi i32 [ %0, %case1 ], [ %1, %case2 ], [ %2, %bb ]
308-
%3 = icmp eq i32 %2, 2
309-
br i1 %3, label %bb, label %loop
310-
default:
311-
ret void
312-
}
313-
)";
314-
315-
Module &M = parseModule(ModuleString);
316-
Function *F = M.getFunction("foo");
317-
FunctionSpecializer Specializer = getSpecializerFor(F);
318-
InstCostVisitor Visitor = Specializer.getInstCostVisitorFor(F);
319-
320-
Constant *One = ConstantInt::get(IntegerType::getInt32Ty(M.getContext()), 1);
321-
322-
auto FuncIter = F->begin();
323-
for (int I = 0; I < 4; ++I)
324-
++FuncIter;
325-
326-
BasicBlock &BB = *FuncIter;
327-
328-
Instruction &Phi = BB.front();
329-
Instruction &Icmp = *++BB.begin();
330-
331-
Cost Bonus = Specializer.getSpecializationBonus(F->getArg(0), One, Visitor) +
332-
Specializer.getSpecializationBonus(F->getArg(1), One, Visitor) +
333-
Specializer.getSpecializationBonus(F->getArg(2), One, Visitor);
334-
EXPECT_TRUE(Bonus > 0);
335-
336-
// phi + icmp
337-
Cost Ref = getInstCost(Phi) + getInstCost(Icmp);
338-
Bonus = Visitor.getBonusFromPendingPHIs();
339-
EXPECT_EQ(Bonus, Ref);
340-
EXPECT_TRUE(Bonus > 0);
341-
}
342-

0 commit comments

Comments
 (0)