Skip to content

Commit 4dcbc42

Browse files
committed
Move collection into collect method
1 parent 0e389ce commit 4dcbc42

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ class SILGlobalOpt {
116116
bool run();
117117

118118
protected:
119+
/// Reset all the maps of global variables.
120+
void reset();
121+
122+
/// Collect all global variables.
123+
void collect();
124+
119125
/// If this is a call to a global initializer, map it.
120126
void collectGlobalInitCall(ApplyInst *AI);
121127

@@ -807,11 +813,12 @@ bool SILGlobalOpt::tryRemoveGlobalAddr(SILGlobalVariable *global) {
807813
return false;
808814

809815
for (auto *addr : GlobalAddrMap[global]) {
810-
eraseUsesOfInstruction(addr, [](SILInstruction *) {});
816+
eraseUsesOfInstruction(addr);
811817
addr->eraseFromParent();
812818
}
813819

814-
GlobalAddrMap.erase(global);
820+
reset();
821+
collect();
815822
return true;
816823
}
817824

@@ -821,7 +828,8 @@ bool SILGlobalOpt::tryRemoveGlobalAlloc(SILGlobalVariable *global,
821828
return false;
822829

823830
alloc->eraseFromParent();
824-
AllocGlobalStore.erase(global);
831+
reset();
832+
collect();
825833
return true;
826834
}
827835

@@ -1002,7 +1010,16 @@ void SILGlobalOpt::optimizeGlobalAccess(SILGlobalVariable *SILG,
10021010

10031011
}
10041012

1005-
bool SILGlobalOpt::run() {
1013+
void SILGlobalOpt::reset() {
1014+
AllocGlobalStore.clear();
1015+
GlobalVarStore.clear();
1016+
GlobalAddrMap.clear();
1017+
GlobalAccessMap.clear();
1018+
GlobalLoadMap.clear();
1019+
GlobalInitCallMap.clear();
1020+
}
1021+
1022+
void SILGlobalOpt::collect() {
10061023
for (auto &F : *Module) {
10071024
// TODO: Add support for ownership.
10081025
if (F.hasOwnership()) {
@@ -1038,7 +1055,13 @@ bool SILGlobalOpt::run() {
10381055
}
10391056
}
10401057
}
1058+
}
1059+
1060+
bool SILGlobalOpt::run() {
1061+
// Collect all the global variables and associated instructions.
1062+
collect();
10411063

1064+
// Optimize based on what we just collected.
10421065
for (auto &InitCalls : GlobalInitCallMap) {
10431066
// Don't optimize functions that are marked with the opt.never attribute.
10441067
bool shouldOptimize = true;
@@ -1086,10 +1109,13 @@ bool SILGlobalOpt::run() {
10861109
HasChanged |= tryRemoveGlobalAlloc(alloc.first, alloc.second);
10871110
}
10881111

1112+
// Copy the globals so we don't get issues with modifying while iterating.
1113+
SmallVector<SILGlobalVariable *, 12> globals;
10891114
for (auto &global : Module->getSILGlobals()) {
1090-
// TODO: For some reaons, if we keep iterating through `getSILGlobals`
1091-
// after `tryRemoveUnusedGlobal` is called, we gen an error.
1092-
HasChanged |= tryRemoveUnusedGlobal(&global);
1115+
globals.push_back(&global);
1116+
}
1117+
for (auto *global : globals) {
1118+
HasChanged |= tryRemoveUnusedGlobal(global);
10931119
}
10941120

10951121
return HasChanged;

0 commit comments

Comments
 (0)