Skip to content

Commit

Permalink
Use RPO for iterating blocks in TypeInference
Browse files Browse the repository at this point in the history
Summary:
This reduces the time taken in TypeInference by over 35%,
and since TypeInference is the biggest part of the optimization
pipeline, it reduces the total time for compilation by over 10%.

Reviewed By: neildhar

Differential Revision: D66790755

fbshipit-source-id: f59d8d1db78ce73aaecf673dfff6e42fc2ca44b1
  • Loading branch information
avp authored and facebook-github-bot committed Dec 5, 2024
1 parent 3149ba9 commit 48653e7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Optimizer/Scalar/TypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,11 +1158,19 @@ bool TypeInferenceImpl::runOnFunctionsAndVars(
dbgs() << "\nStart Type Inference on " << functions.size()
<< " functions and " << vars.size() << "vars.\n");

// Post-order traversal of blocks for each function.
// Reverse when iterating, so that we visit the blocks in reverse post-order.
// Precomputed to avoid recomputing it in the inner loop.
llvh::DenseMap<Function *, std::vector<BasicBlock *>> funcPostOrders{};

// Begin by clearing the existing types and storing pre-pass types.
// This prevents us from relying on the previous inference pass's type info,
// which can be too loose (if things have been simplified, etc.).
for (Function *F : functions)
// Also precompute the post-order traversal for each function.
for (Function *F : functions) {
clearTypesInFunction(F);
funcPostOrders.try_emplace(F, postOrderAnalysis(F));
}

for (Variable *V : vars) {
prePassTypes_.try_emplace(V, V->getType());
Expand Down Expand Up @@ -1194,8 +1202,9 @@ bool TypeInferenceImpl::runOnFunctionsAndVars(
// Infer types of instructions.
bool inferredInst = false;
for (Function *F : functions) {
for (auto &bbit : *F) {
for (auto &it : bbit) {
llvh::ArrayRef<BasicBlock *> postOrder = funcPostOrders[F];
for (auto *bbit : llvh::reverse(postOrder)) {
for (auto &it : *bbit) {
Instruction *I = &it;
inferredInst |= inferInstruction(I);
}
Expand Down

0 comments on commit 48653e7

Please sign in to comment.