-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Fix stack overflow in allPathsGoThroughCold past 6b11573b8c5e #106384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: Danial Klimkin (dklimkin) ChangesRecursion here causes stack overflow on large inputs. Fixing by unrolling via a stack. Full diff: https://github.com/llvm/llvm-project/pull/106384.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 603a1565e48c45..1c1ca3d3795f31 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -62,6 +62,7 @@
#include <iterator>
#include <map>
#include <optional>
+#include <stack>
#include <vector>
using namespace llvm;
@@ -1762,54 +1763,53 @@ static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
}
}
-static bool
-allBBPathsGoThroughCold(BasicBlock *BB,
- SmallDenseMap<BasicBlock *, bool, 16> &Visited) {
- // If BB contains a cold callsite this path through the CG is cold.
- // Ignore whether the instructions actually are guranteed to transfer
- // execution. Divergent behavior is considered unlikely.
- if (any_of(*BB, [](Instruction &I) {
- if (auto *CB = dyn_cast<CallBase>(&I))
- return CB->hasFnAttr(Attribute::Cold);
- return false;
- })) {
- Visited[BB] = true;
- return true;
- }
-
- auto Succs = successors(BB);
- // We found a path that doesn't go through any cold callsite.
- if (Succs.empty())
- return false;
+static bool allPathsGoThroughCold(Function &F) {
+ SmallDenseMap<BasicBlock *, bool, 16> ColdPaths;
+ ColdPaths[&F.front()] = false;
+ std::stack<BasicBlock *> Jobs;
+ Jobs.push(&F.front());
+
+ while (!Jobs.empty()) {
+ BasicBlock *BB = Jobs.top();
+ Jobs.pop();
+
+ // If block contains a cold callsite this path through the CG is cold.
+ // Ignore whether the instructions actually are guaranteed to transfer
+ // execution. Divergent behavior is considered unlikely.
+ if (any_of(*BB, [](Instruction &I) {
+ if (auto *CB = dyn_cast<CallBase>(&I))
+ return CB->hasFnAttr(Attribute::Cold);
+ return false;
+ })) {
+ ColdPaths[BB] = true;
+ continue;
+ }
- // We didn't find a cold callsite in this BB, so check that all successors
- // contain a cold callsite (or that their successors do).
- // Potential TODO: We could use static branch hints to assume certain
- // successor paths are inherently cold, irrespective of if they contain a cold
- // callsite.
- for (auto *Succ : Succs) {
- // Start with false, this is necessary to ensure we don't turn loops into
- // cold.
- auto R = Visited.try_emplace(Succ, false);
- if (!R.second) {
- if (R.first->second)
- continue;
+ auto Succs = successors(BB);
+ // We found a path that doesn't go through any cold callsite.
+ if (Succs.empty())
return false;
+
+ // We didn't find a cold callsite in this BB, so check that all successors
+ // contain a cold callsite (or that their successors do).
+ // Potential TODO: We could use static branch hints to assume certain
+ // successor paths are inherently cold, irrespective of if they contain a
+ // cold callsite.
+ for (llvm::BasicBlock *Succ : Succs) {
+ // Start with false, this is necessary to ensure we don't turn loops into
+ // cold.
+ auto [iter, inserted] = ColdPaths.try_emplace(Succ, false);
+ if (!inserted) {
+ if (iter->second)
+ continue;
+ return false;
+ }
+ Jobs.push(Succ);
}
- if (!allBBPathsGoThroughCold(Succ, Visited))
- return false;
- Visited[Succ] = true;
}
-
return true;
}
-static bool allPathsGoThroughCold(Function &F) {
- SmallDenseMap<BasicBlock *, bool, 16> Visited;
- Visited[&F.front()] = false;
- return allBBPathsGoThroughCold(&F.front(), Visited);
-}
-
// Set the cold function attribute if possible.
static void addColdAttrs(const SCCNodeSet &SCCNodes,
SmallSet<Function *, 8> &Changed) {
|
goldsteinn
reviewed
Aug 29, 2024
goldsteinn
reviewed
Aug 29, 2024
Thank you for the fix, looks correct although a few nits. |
goldsteinn
reviewed
Aug 30, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Recursion here causes stack overflow on large inputs. Fixing by unrolling via a stack.