-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Propeller] Read the CFG profile from the propeller directive. #160422
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-backend-x86 Author: Rahman Lavaee (rlavaee) ChangesThe CFG allows us to do layout optimization in the compiler. Furthermore, it allows further branch optimization. Full diff: https://github.com/llvm/llvm-project/pull/160422.diff 3 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
index f0cfa7663c5fa..82dd5feb31dba 100644
--- a/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
+++ b/llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h
@@ -50,6 +50,10 @@ struct FunctionPathAndClusterInfo {
// the edge a -> b (a is not cloned). The index of the path in this vector
// determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
SmallVector<SmallVector<unsigned>> ClonePaths;
+ // Node counts for each basic block.
+ DenseMap<UniqueBBID, uint64_t> NodeCounts;
+ // Edge counts for each edge, stored as a nested map.
+ DenseMap<UniqueBBID, DenseMap<UniqueBBID, uint64_t>> EdgeCounts;
};
class BasicBlockSectionsProfileReader {
@@ -77,6 +81,11 @@ class BasicBlockSectionsProfileReader {
SmallVector<SmallVector<unsigned>>
getClonePathsForFunction(StringRef FuncName) const;
+ // Returns the profile count for the edge from `SrcBBID` to `SinkBBID` in
+ // function `FuncName` or zero if it does not exist.
+ uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
+ const UniqueBBID &SinkBBID) const;
+
private:
StringRef getAliasName(StringRef FuncName) const {
auto R = FuncAliasMap.find(FuncName);
@@ -183,6 +192,9 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
SmallVector<SmallVector<unsigned>>
getClonePathsForFunction(StringRef FuncName) const;
+ uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
+ const UniqueBBID &DestBBID) const;
+
// Initializes the FunctionNameToDIFilename map for the current module and
// then reads the profile for the matching functions.
bool doInitialization(Module &M) override;
diff --git a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
index 7baeb3fd7bcee..912654b45aa7e 100644
--- a/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp
@@ -76,6 +76,20 @@ BasicBlockSectionsProfileReader::getClonePathsForFunction(
return ProgramPathAndClusterInfo.lookup(getAliasName(FuncName)).ClonePaths;
}
+uint64_t BasicBlockSectionsProfileReader::getEdgeCount(
+ StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &SinkBBID) const {
+ auto It = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
+ if (It == ProgramPathAndClusterInfo.end())
+ return 0;
+ auto NodeIt = It->second.EdgeCounts.find(SrcBBID);
+ if (NodeIt == It->second.EdgeCounts.end())
+ return 0;
+ auto EdgeIt = NodeIt->second.find(SinkBBID);
+ if (EdgeIt == NodeIt->second.end())
+ return 0;
+ return EdgeIt->second;
+}
+
// Reads the version 1 basic block sections profile. Profile for each function
// is encoded as follows:
// m <module_name>
@@ -240,6 +254,38 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
}
continue;
}
+ case 'g': { // CFG profile specifier.
+ // Skip the profile when we the profile iterator (FI) refers to the
+ // past-the-end element.
+ if (FI == ProgramPathAndClusterInfo.end())
+ continue;
+ // For each node, its CFG profile is encoded as
+ // <src>:<count>,<sink_1>:<count_1>,<sink_2>:<count_2>,...
+ for (auto BasicBlockEdgeProfile : Values) {
+ if (BasicBlockEdgeProfile.empty())
+ continue;
+ SmallVector<StringRef, 4> NodeEdgeCounts;
+ BasicBlockEdgeProfile.split(NodeEdgeCounts, ',');
+ UniqueBBID SrcBBID;
+ for (int i = 0; i < NodeEdgeCounts.size(); ++i) {
+ auto [BBIDStr, CountStr] = NodeEdgeCounts[i].split(':');
+ auto BBID = parseUniqueBBID(BBIDStr);
+ if (!BBID)
+ return BBID.takeError();
+ unsigned long long Count = 0;
+ if (getAsUnsignedInteger(CountStr, 10, Count))
+ return createProfileParseError(
+ Twine("unsigned integer expected: '") + CountStr + "'");
+ if (i == 0) {
+ // The first element represents the source and its total count.
+ FI->second.NodeCounts[SrcBBID = *BBID] = Count;
+ continue;
+ }
+ FI->second.EdgeCounts[SrcBBID][*BBID] = Count;
+ }
+ }
+ continue;
+ }
default:
return createProfileParseError(Twine("invalid specifier: '") +
Twine(Specifier) + "'");
@@ -440,6 +486,12 @@ BasicBlockSectionsProfileReaderWrapperPass::getClonePathsForFunction(
return BBSPR.getClonePathsForFunction(FuncName);
}
+uint64_t BasicBlockSectionsProfileReaderWrapperPass::getEdgeCount(
+ StringRef FuncName, const UniqueBBID &SrcBBID,
+ const UniqueBBID &SinkBBID) const {
+ return BBSPR.getEdgeCount(FuncName, SrcBBID, SinkBBID);
+}
+
BasicBlockSectionsProfileReader &
BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() {
return BBSPR;
diff --git a/llvm/test/CodeGen/X86/basic-block-sections-clusters-error.ll b/llvm/test/CodeGen/X86/basic-block-sections-clusters-error.ll
index d6f3d5010b556..751ab76722c07 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-clusters-error.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-clusters-error.ll
@@ -57,6 +57,19 @@
; RUN: echo 'p 1 2 3 2' >> %t13
; RUN: not --crash llc < %s -O0 -mtriple=x86_64 -function-sections -basic-block-sections=%t13 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR13
; CHECK-ERROR13: LLVM ERROR: invalid profile {{.*}} at line 4: duplicate cloned block in path: '2'
+; RUN: echo 'v1' > %t14
+; RUN: echo 'f dummy1' >> %t14
+; RUN: echo 'c 0 1' >> %t14
+; RUN: echo 'g 0,1:2' >> %t14
+; RUN: not --crash llc < %s -O0 -mtriple=x86_64 -function-sections -basic-block-sections=%t14 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR14
+; CHECK-ERROR14: LLVM ERROR: invalid profile {{.*}} at line 4: unsigned integer expected: ''
+; RUN: echo 'v1' > %t15
+; RUN: echo 'f dummy1' >> %t15
+; RUN: echo 'c 0 1' >> %t15
+; RUN: echo 'g 0:4,1:2:3' >> %t15
+; RUN: not --crash llc < %s -O0 -mtriple=x86_64 -function-sections -basic-block-sections=%t15 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR15
+; CHECK-ERROR15: LLVM ERROR: invalid profile {{.*}} at line 4: unsigned integer expected: '2:3'
+
define i32 @dummy1(i32 %x, i32 %y, i32 %z) {
entry:
|
ae14e22
to
0ed4590
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
mahesh-attarde
pushed a commit
to mahesh-attarde/llvm-project
that referenced
this pull request
Oct 3, 2025
…160422) The CFG allows us to do layout optimization in the compiler. Furthermore, it allows further branch optimization.
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.
The CFG allows us to do layout optimization in the compiler. Furthermore, it allows further branch optimization.