Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/passes/DeadArgumentElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ struct DAE : public Pass {
}
}

// For each function, the set of callers. This is used to propagate changes,
// e.g. if we remove a return value from a function, the calls might benefit
// from optimization. It is ok if this is an over-approximation, that is, if
// we think there are more callers than there are, as it would just lead to
// unneeded extra scanning of calling functions (in the example just given, if
// a caller did not actually call, they would not benefit from optimization,
// but no harm is done, and no optimization is missed). Such over-
// approximation can happen in later optimization iterations: We may manage to
// remove a call from a function to another (say, after applying a constant
// param, we see the call is not reached). This is somewhat rare, and the cost
// of computing this map is significant, so we compute it once at the start
// and then use that possibly-over-approximating data.
std::vector<std::unordered_set<Name>> callers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can make these even faster by making the inner set into another vector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done. I can't seem to measure a speedup from it, but maybe it depends on CPU or workload, and the vectors should be better.


bool iteration(Module* module, DAEFunctionInfoMap& infoMap) {
allDroppedCalls.clear();

Expand Down Expand Up @@ -246,15 +260,10 @@ struct DAE : public Pass {
std::vector<bool> tailCallees(numFunctions);
std::vector<bool> hasUnseenCalls(numFunctions);

// For each function, the set of callers.
std::vector<std::unordered_set<Name>> callers(numFunctions);

for (auto& [func, info] : infoMap) {
for (auto& [name, calls] : info.calls) {
auto targetIndex = indexes[name];
auto& allCallsToName = allCalls[targetIndex];
auto& allCallsToName = allCalls[indexes[name]];
allCallsToName.insert(allCallsToName.end(), calls.begin(), calls.end());
callers[targetIndex].insert(func);
}
for (auto& callee : info.tailCallees) {
tailCallees[indexes[callee]] = true;
Expand All @@ -273,6 +282,16 @@ struct DAE : public Pass {
}
}

// See comment above, we compute callers once and never again.
if (callers.empty()) {
callers.resize(numFunctions);
for (auto& [func, info] : infoMap) {
for (auto& [name, calls] : info.calls) {
callers[indexes[name]].insert(func);
}
}
}

// Track which functions we changed that are worth re-optimizing at the end.
std::unordered_set<Function*> worthOptimizing;

Expand Down
Loading