Skip to content

Commit f9ebee2

Browse files
authored
Merge pull request #34188 from apple/revert-30710-bottomupfunction
Revert "[PassManager] Update PassManager's function worklist for newly added SILFunctions" For some programs which create large number of specialized functions each with deep call chains. This can cause redundant DFS leading to large compile times. Reverting this until we implement a better incremental DFS for updating the bottom up order of newly added functions.
2 parents 161899d + f4bbafb commit f9ebee2

File tree

5 files changed

+21
-62
lines changed

5 files changed

+21
-62
lines changed

include/swift/SILOptimizer/Analysis/FunctionOrder.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class BottomUpFunctionOrder {
3131
typedef TinyPtrVector<SILFunction *> SCC;
3232

3333
private:
34+
SILModule &M;
3435
llvm::SmallVector<SCC, 32> TheSCCs;
3536
llvm::SmallVector<SILFunction *, 32> TheFunctions;
3637

@@ -43,29 +44,24 @@ class BottomUpFunctionOrder {
4344
llvm::SmallSetVector<SILFunction *, 4> DFSStack;
4445

4546
public:
46-
BottomUpFunctionOrder(BasicCalleeAnalysis *BCA)
47-
: BCA(BCA), NextDFSNum(0) {}
48-
49-
/// DFS on 'F' to compute bottom up order
50-
void computeBottomUpOrder(SILFunction *F) {
51-
DFS(F);
52-
}
53-
54-
/// DFS on all functions in the module to compute bottom up order
55-
void computeBottomUpOrder(SILModule *M) {
56-
for (auto &F : *M)
57-
DFS(&F);
58-
}
47+
BottomUpFunctionOrder(SILModule &M, BasicCalleeAnalysis *BCA)
48+
: M(M), BCA(BCA), NextDFSNum(0) {}
5949

6050
/// Get the SCCs in bottom-up order.
6151
ArrayRef<SCC> getSCCs() {
52+
if (!TheSCCs.empty())
53+
return TheSCCs;
54+
55+
FindSCCs(M);
6256
return TheSCCs;
6357
}
6458

65-
/// Get a flattened view of all functions in all the SCCs in bottom-up order
66-
ArrayRef<SILFunction *> getBottomUpOrder() {
59+
/// Get a flattened view of all functions in all the SCCs in
60+
/// bottom-up order
61+
ArrayRef<SILFunction *> getFunctions() {
6762
if (!TheFunctions.empty())
6863
return TheFunctions;
64+
6965
for (auto SCC : getSCCs())
7066
for (auto *F : SCC)
7167
TheFunctions.push_back(F);
@@ -75,6 +71,7 @@ class BottomUpFunctionOrder {
7571

7672
private:
7773
void DFS(SILFunction *F);
74+
void FindSCCs(SILModule &M);
7875
};
7976

8077
} // end namespace swift

lib/SILOptimizer/Analysis/FunctionOrder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ void BottomUpFunctionOrder::DFS(SILFunction *Start) {
7474
}
7575
}
7676

77+
void BottomUpFunctionOrder::FindSCCs(SILModule &M) {
78+
for (auto &F : M)
79+
DFS(&F);
80+
}

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,8 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
514514
return;
515515

516516
BasicCalleeAnalysis *BCA = getAnalysis<BasicCalleeAnalysis>();
517-
BottomUpFunctionOrder BottomUpOrder(BCA);
518-
BottomUpOrder.computeBottomUpOrder(Mod);
519-
auto BottomUpFunctions = BottomUpOrder.getBottomUpOrder();
517+
BottomUpFunctionOrder BottomUpOrder(*Mod, BCA);
518+
auto BottomUpFunctions = BottomUpOrder.getFunctions();
520519

521520
assert(FunctionWorklist.empty() && "Expected empty function worklist!");
522521

@@ -569,47 +568,6 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
569568
++Entry.PipelineIdx;
570569
}
571570
clearRestartPipeline();
572-
573-
if (TailIdx == (FunctionWorklist.size() - 1)) {
574-
// No new functions to process
575-
continue;
576-
}
577-
578-
// Compute the bottom up order of the new functions and the callees in it
579-
BottomUpFunctionOrder SubBottomUpOrder(BCA);
580-
// Initialize BottomUpFunctionOrder with new functions
581-
for (auto It = FunctionWorklist.begin() + TailIdx + 1;
582-
It != FunctionWorklist.end(); It++) {
583-
SubBottomUpOrder.computeBottomUpOrder(It->F);
584-
}
585-
auto NewFunctionsBottomUp = SubBottomUpOrder.getBottomUpOrder();
586-
SmallPtrSet<SILFunction *, 8> NewBottomUpSet(NewFunctionsBottomUp.begin(),
587-
NewFunctionsBottomUp.end());
588-
589-
// Remove all the functions in the new bottom up order from FunctionWorklist
590-
llvm::DenseMap<SILFunction *, WorklistEntry> FunctionsToReorder;
591-
auto RemoveFn = [&FunctionsToReorder,
592-
&NewBottomUpSet](WorklistEntry Entry) {
593-
if (NewBottomUpSet.find(Entry.F) == NewBottomUpSet.end()) {
594-
return false;
595-
}
596-
FunctionsToReorder.insert(std::make_pair(Entry.F, Entry));
597-
return true;
598-
};
599-
std::remove_if(FunctionWorklist.begin(), FunctionWorklist.end(), RemoveFn);
600-
FunctionWorklist.erase((FunctionWorklist.begin() + FunctionWorklist.size() -
601-
FunctionsToReorder.size()),
602-
FunctionWorklist.end());
603-
604-
// Add back the functions in the new bottom up order to the FunctionWorklist
605-
for (auto it = NewFunctionsBottomUp.rbegin();
606-
it != NewFunctionsBottomUp.rend(); it++) {
607-
auto Entry = FunctionsToReorder.find(*it);
608-
if (Entry == FunctionsToReorder.end()) {
609-
continue;
610-
}
611-
FunctionWorklist.push_back((*Entry).second);
612-
}
613571
}
614572
}
615573

lib/SILOptimizer/UtilityPasses/FunctionOrderPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class FunctionOrderPrinterPass : public SILModuleTransform {
3535
/// The entry point to the transformation.
3636
void run() override {
3737
BCA = getAnalysis<BasicCalleeAnalysis>();
38-
BottomUpFunctionOrder Orderer(BCA);
39-
Orderer.computeBottomUpOrder(getModule());
38+
auto &M = *getModule();
39+
BottomUpFunctionOrder Orderer(M, BCA);
4040

4141
llvm::outs() << "Bottom up function order:\n";
4242
auto SCCs = Orderer.getSCCs();

test/DebugInfo/inlined-generics-basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public class C<R> {
9191
// IR-LABEL: ret void
9292

9393
// IR: ![[BOOL:[0-9]+]] = !DICompositeType({{.*}}name: "Bool"
94+
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
9495
// IR: ![[INT:[0-9]+]] = !DICompositeType({{.*}}name: "Int"
9596
// IR: ![[LET_INT:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[INT]])
96-
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
9797
// IR: ![[TAU_0_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sxD",
9898
// IR: ![[LET_TAU_0_0:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[TAU_0_0]])
9999
// IR: ![[TAU_1_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sqd__D",

0 commit comments

Comments
 (0)