Skip to content

[llvm-profgen][NFC] Reuse isLBRSample#191595

Merged
aaupov merged 7 commits into
mainfrom
users/aaupov/spr/llvm-profgennfc-reuse-islbrsample
May 2, 2026
Merged

[llvm-profgen][NFC] Reuse isLBRSample#191595
aaupov merged 7 commits into
mainfrom
users/aaupov/spr/llvm-profgennfc-reuse-islbrsample

Conversation

@aaupov
Copy link
Copy Markdown
Contributor

@aaupov aaupov commented Apr 11, 2026

Replace StringRef::starts_with(" 0x") calls with explicit isLBRSample checks.
This is needed to support buildid-prefixed addresses in a follow-up #190863.

aaupov added 2 commits April 10, 2026 21:02
Created using spr 1.3.4
@aaupov aaupov requested review from HighW4y2H3ll and apolloww April 11, 2026 04:33
@aaupov aaupov marked this pull request as ready for review April 11, 2026 04:33
@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::starts_with(" 0x") calls with explicit isLBRSample checks.
This is needed to support buildid-prefixed addresses in a follow-up #190863.


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

2 Files Affected:

  • (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+13-8)
  • (modified) llvm/tools/llvm-profgen/PerfReader.h (+1-1)
diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index 9dda11a7ade50..b933a740ff55c 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -737,7 +737,7 @@ bool PerfScriptReader::extractCallstack(TraceStream &TraceIt,
   // It's in bottom-up order with each frame in one line.
 
   // Extract stack frames from sample
-  while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().starts_with(" 0x")) {
+  while (!TraceIt.isAtEoF() && !isLBRSample(TraceIt.getCurrentLine(), true)) {
     StringRef FrameStr = TraceIt.getCurrentLine().ltrim();
     uint64_t FrameAddr = 0;
     if (parseAddress(FrameStr, FrameAddr)) {
@@ -785,7 +785,7 @@ bool PerfScriptReader::extractCallstack(TraceStream &TraceIt,
   // Skip other unrelated line, find the next valid LBR line
   // Note that even for empty call stack, we should skip the address at the
   // bottom, otherwise the following pass may generate a truncated callstack
-  while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().starts_with(" 0x")) {
+  while (!TraceIt.isAtEoF() && !isLBRSample(TraceIt.getCurrentLine(), true)) {
     TraceIt.advance();
   }
   // Filter out broken stack sample. We may not have complete frame info
@@ -830,14 +830,14 @@ void HybridPerfReader::parseSample(TraceStream &TraceIt, uint64_t Count) {
   // Parsing call stack and populate into PerfSample.CallStack
   if (!extractCallstack(TraceIt, Sample->CallStack)) {
     // Skip the next LBR line matched current call stack
-    if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().starts_with(" 0x"))
+    if (!TraceIt.isAtEoF() && isLBRSample(TraceIt.getCurrentLine(), true))
       TraceIt.advance();
     return;
   }
 
   warnIfMissingMMap();
 
-  if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().starts_with(" 0x")) {
+  if (!TraceIt.isAtEoF() && isLBRSample(TraceIt.getCurrentLine(), true)) {
     // Parsing LBR stack and populate into PerfSample.LBRStack
     if (extractLBRStack(TraceIt, Sample->LBRStack)) {
       if (IgnoreStackSamples) {
@@ -1162,13 +1162,18 @@ void PerfScriptReader::parseAndAggregateTrace() {
 // 40062f 0x5c6313f/0x5c63170/P/-/-/0  0x5c630e7/0x5c63130/P/-/-/0 ...
 // A heuristic for fast detection by checking whether a
 // leading "  0x" and the '/' exist.
-bool PerfScriptReader::isLBRSample(StringRef Line) {
+bool PerfScriptReader::isLBRSample(StringRef Line, bool CheckLineStart) {
   // Skip the leading instruction pointer
   SmallVector<StringRef, 32> Records;
-  Line.trim().split(Records, " ", 2, false);
+  if (!CheckLineStart)
+    Line = Line.trim();
+  Line.split(Records, " ", 2, CheckLineStart);
   if (Records.size() < 2)
     return false;
-  if (Records[1].starts_with("0x") && Records[1].contains('/'))
+  StringRef Token = Records[1];
+  if (!Token.contains('/'))
+    return false;
+  if (Token.starts_with("0x"))
     return true;
   return false;
 }
@@ -1213,7 +1218,7 @@ PerfContent PerfScriptReader::checkPerfScriptType(StringRef FileName) {
       TraceIt.advance();
     }
     if (!TraceIt.isAtEoF()) {
-      if (isLBRSample(TraceIt.getCurrentLine())) {
+      if (isLBRSample(TraceIt.getCurrentLine(), false)) {
         if (Count > 0)
           return HasAggCount ? PerfContent::AggLBRStack : PerfContent::LBRStack;
         else
diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h
index 83c4fb0447c5c..358d61067a4ef 100644
--- a/llvm/tools/llvm-profgen/PerfReader.h
+++ b/llvm/tools/llvm-profgen/PerfReader.h
@@ -636,7 +636,7 @@ class PerfScriptReader : public PerfReaderBase {
 
 protected:
   // Check whether a given line is LBR sample
-  static bool isLBRSample(StringRef Line);
+  static bool isLBRSample(StringRef Line, bool CheckLineStart);
   // Check whether a given line is MMAP event
   static bool isMMapEvent(StringRef Line);
   // Update base address based on mmap events

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 11, 2026

🐧 Linux x64 Test Results

  • 193335 tests passed
  • 5025 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 11, 2026

🪟 Windows x64 Test Results

  • 133265 tests passed
  • 3079 tests skipped

✅ The build succeeded and all tests passed.

aaupov added 2 commits April 11, 2026 21:16
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
Comment thread llvm/tools/llvm-profgen/PerfReader.cpp
Comment thread llvm/tools/llvm-profgen/PerfReader.cpp Outdated
Created using spr 1.3.4
aaupov added a commit that referenced this pull request Apr 17, 2026
Created using spr 1.3.4
Comment thread llvm/tools/llvm-profgen/PerfReader.cpp
aaupov added 2 commits May 1, 2026 16:40
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-reuse-islbrsample to main May 1, 2026 23:41
@aaupov aaupov enabled auto-merge (squash) May 1, 2026 23:41
@aaupov aaupov merged commit 45b1195 into main May 2, 2026
12 of 17 checks passed
@aaupov aaupov deleted the users/aaupov/spr/llvm-profgennfc-reuse-islbrsample branch May 2, 2026 00:12
enferex pushed a commit to enferex/llvm-project that referenced this pull request May 5, 2026
Replace `StringRef::starts_with(" 0x")` calls with explicit
`isLBRSample` checks.
This is needed 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::starts_with(" 0x")` calls with explicit
`isLBRSample` checks.
This is needed 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