[llvm] [IPO] NFC: avoid recalculating FunctionId hashes (PR #109014)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 17 10:01:11 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (itrofimow)
<details>
<summary>Changes</summary>
This patch aims to reduce amount of time spent in recalculating `FunctionId` hashes (heavy md5) during `ProfiledCallGraph` constructions
---
Full diff: https://github.com/llvm/llvm-project/pull/109014.diff
1 Files Affected:
- (modified) llvm/include/llvm/Transforms/IPO/ProfiledCallGraph.h (+28-8)
``````````diff
diff --git a/llvm/include/llvm/Transforms/IPO/ProfiledCallGraph.h b/llvm/include/llvm/Transforms/IPO/ProfiledCallGraph.h
index 3e7cc74fcf23b6..a09a35f804d78e 100644
--- a/llvm/include/llvm/Transforms/IPO/ProfiledCallGraph.h
+++ b/llvm/include/llvm/Transforms/IPO/ProfiledCallGraph.h
@@ -58,6 +58,20 @@ struct ProfiledCallGraphNode {
edges Edges;
};
+struct PrehashedFunctionId {
+
+ PrehashedFunctionId() = default;
+
+ /* implicit */ PrehashedFunctionId(FunctionId FId) : Id(FId), Hash(hash_value(Id)) {}
+
+ FunctionId Id;
+ uint64_t Hash;
+};
+
+inline uint64_t hash_value(const PrehashedFunctionId& Obj) {
+ return Obj.Hash;
+}
+
class ProfiledCallGraph {
public:
using iterator = ProfiledCallGraphNode::iterator;
@@ -135,19 +149,23 @@ class ProfiledCallGraph {
ProfiledCallGraphNode *getEntryNode() { return &Root; }
void addProfiledFunction(FunctionId Name) {
+ addProfiledFunction(PrehashedFunctionId{Name});
+ }
+
+private:
+ void addProfiledFunction(PrehashedFunctionId Name) {
if (!ProfiledFunctions.count(Name)) {
// Link to synthetic root to make sure every node is reachable
// from root. This does not affect SCC order.
// Store the pointer of the node because the map can be rehashed.
auto &Node =
- ProfiledCallGraphNodeList.emplace_back(ProfiledCallGraphNode(Name));
+ ProfiledCallGraphNodeList.emplace_back(ProfiledCallGraphNode(Name.Id));
ProfiledFunctions[Name] = &Node;
Root.Edges.emplace(&Root, ProfiledFunctions[Name], 0);
}
}
-private:
- void addProfiledCall(FunctionId CallerName, FunctionId CalleeName,
+ void addProfiledCall(PrehashedFunctionId CallerName, PrehashedFunctionId CalleeName,
uint64_t Weight = 0) {
assert(ProfiledFunctions.count(CallerName));
auto CalleeIt = ProfiledFunctions.find(CalleeName);
@@ -155,7 +173,7 @@ class ProfiledCallGraph {
return;
ProfiledCallGraphEdge Edge(ProfiledFunctions[CallerName],
CalleeIt->second, Weight);
- auto &Edges = ProfiledFunctions[CallerName]->Edges;
+ auto &Edges = Edge.Source->Edges;
auto [EdgeIt, Inserted] = Edges.insert(Edge);
if (!Inserted) {
// Accumulate weight to the existing edge.
@@ -166,19 +184,21 @@ class ProfiledCallGraph {
}
void addProfiledCalls(const FunctionSamples &Samples) {
- addProfiledFunction(Samples.getFunction());
+ const PrehashedFunctionId SamplesFunction{Samples.getFunction()};
+
+ addProfiledFunction(SamplesFunction);
for (const auto &Sample : Samples.getBodySamples()) {
for (const auto &[Target, Frequency] : Sample.second.getCallTargets()) {
addProfiledFunction(Target);
- addProfiledCall(Samples.getFunction(), Target, Frequency);
+ addProfiledCall(SamplesFunction, Target, Frequency);
}
}
for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) {
for (const auto &InlinedSamples : CallsiteSamples.second) {
addProfiledFunction(InlinedSamples.first);
- addProfiledCall(Samples.getFunction(), InlinedSamples.first,
+ addProfiledCall(SamplesFunction, InlinedSamples.first,
InlinedSamples.second.getHeadSamplesEstimate());
addProfiledCalls(InlinedSamples.second);
}
@@ -206,7 +226,7 @@ class ProfiledCallGraph {
ProfiledCallGraphNode Root;
// backing buffer for ProfiledCallGraphNodes.
std::list<ProfiledCallGraphNode> ProfiledCallGraphNodeList;
- HashKeyMap<llvm::DenseMap, FunctionId, ProfiledCallGraphNode*>
+ HashKeyMap<llvm::DenseMap, PrehashedFunctionId, ProfiledCallGraphNode*>
ProfiledFunctions;
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/109014
More information about the llvm-commits
mailing list