Skip to content

[llvm-profgen][NFC] Factor out parseAddress#191594

Merged
aaupov merged 6 commits into
mainfrom
users/aaupov/spr/llvm-profgennfc-factor-out-parseaddress
May 1, 2026
Merged

[llvm-profgen][NFC] Factor out parseAddress#191594
aaupov merged 6 commits into
mainfrom
users/aaupov/spr/llvm-profgennfc-factor-out-parseaddress

Conversation

@aaupov
Copy link
Copy Markdown
Contributor

@aaupov aaupov commented Apr 11, 2026

Replace StringRef::getAsInteger(16) calls with explicit parseAddress to
make it easier to support buildid-prefixed addresses in a follow-up (#190863).

aaupov added 2 commits April 10, 2026 21:01
Created using spr 1.3.4
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 11, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

Created using spr 1.3.4
@aaupov aaupov requested review from HighW4y2H3ll and apolloww April 11, 2026 04:31
@aaupov aaupov marked this pull request as ready for review April 11, 2026 04:32
@llvmbot llvmbot added the PGO Profile Guided Optimizations label Apr 11, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Apr 11, 2026

@llvm/pr-subscribers-pgo

Author: Amir Ayupov (aaupov)

Changes

Replace StringRef::getAsInteger(16) calls with explicit parseAddress to
make it easier to support buildid-prefixed addresses in a follow-up (#190863).


Full diff: https://github.com/llvm/llvm-project/pull/191594.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+12-5)
diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index bbfde1256f2cc..0d90a77fca185 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -656,6 +656,13 @@ void HybridPerfReader::unwindSamples() {
                      "frame to match.");
 }
 
+/// Parse a hex address from \p Str.
+static bool parseAddress(StringRef Str, uint64_t &Addr, bool HasPrefix) {
+  if (Str.consume_front("0x") != HasPrefix)
+    return false;
+  return Str.getAsInteger(16, Addr);
+}
+
 bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
                                        SmallVectorImpl<LBREntry> &LBRStack) {
   // The raw format of LBR stack is like:
@@ -674,7 +681,7 @@ bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
   size_t Index = 0;
   uint64_t LeadingAddr;
   if (!Records.empty() && !Records[0].contains('/')) {
-    if (Records[0].getAsInteger(16, LeadingAddr)) {
+    if (parseAddress(Records[0], LeadingAddr, false)) {
       WarnInvalidLBR(TraceIt);
       TraceIt.advance();
       return false;
@@ -696,8 +703,8 @@ bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
     uint64_t Dst;
 
     // Stop at broken LBR records.
-    if (Addresses.size() < 2 || Addresses[0].substr(2).getAsInteger(16, Src) ||
-        Addresses[1].substr(2).getAsInteger(16, Dst)) {
+    if (Addresses.size() < 2 || parseAddress(Addresses[0], Src, true) ||
+        parseAddress(Addresses[1], Dst, true)) {
       WarnInvalidLBR(TraceIt);
       break;
     }
@@ -733,7 +740,7 @@ bool PerfScriptReader::extractCallstack(TraceStream &TraceIt,
   while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().starts_with(" 0x")) {
     StringRef FrameStr = TraceIt.getCurrentLine().ltrim();
     uint64_t FrameAddr = 0;
-    if (FrameStr.getAsInteger(16, FrameAddr)) {
+    if (parseAddress(FrameStr, FrameAddr, false)) {
       // We might parse a non-perf sample line like empty line and comments,
       // skip it
       TraceIt.advance();
@@ -1201,7 +1208,7 @@ PerfContent PerfScriptReader::checkPerfScriptType(StringRef FileName) {
     // Detect sample with call stack
     int32_t Count = 0;
     while (!TraceIt.isAtEoF() &&
-           !TraceIt.getCurrentLine().ltrim().getAsInteger(16, FrameAddr)) {
+           !parseAddress(TraceIt.getCurrentLine().ltrim(), FrameAddr, false)) {
       Count++;
       TraceIt.advance();
     }

Comment thread llvm/tools/llvm-profgen/PerfReader.cpp Outdated
Copy link
Copy Markdown
Member

@HighW4y2H3ll HighW4y2H3ll left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for cleanning up the Address parsing!

aaupov added 2 commits May 1, 2026 16:08
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@aaupov aaupov changed the base branch from users/aaupov/spr/main.llvm-profgennfc-factor-out-parseaddress to main May 1, 2026 23:08
@aaupov aaupov enabled auto-merge (squash) May 1, 2026 23:08
@aaupov aaupov merged commit f089c0f into main May 1, 2026
12 of 17 checks passed
@aaupov aaupov deleted the users/aaupov/spr/llvm-profgennfc-factor-out-parseaddress branch May 1, 2026 23:35
enferex pushed a commit to enferex/llvm-project that referenced this pull request May 5, 2026
Replace `StringRef::getAsInteger(16)` calls with explicit `parseAddress`
to make it easier to support buildid-prefixed addresses in a follow-up
(llvm#190863).
moar55 pushed a commit to moar55/llvm-project that referenced this pull request May 12, 2026
Replace `StringRef::getAsInteger(16)` calls with explicit `parseAddress`
to make it easier to support buildid-prefixed addresses in a follow-up
(llvm#190863).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants