[llvm] [MemProf] Match call sites with line 0 (C++ non-virtual thunks) (PR #229919)

David Li via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 14:54:54 PDT 2026


https://github.com/david-xl updated https://github.com/llvm/llvm-project/pull/229919

>From b70ea8a69193a030257d8db5bd8867299d5de082 Mon Sep 17 00:00:00 2001
From: David Li <57157229+david-xl at users.noreply.github.com>
Date: Wed, 7 Oct 2026 13:33:00 -0700
Subject: [PATCH 1/3] [MemProf] Match call sites with line 0 (C++ non-virtual
 thunks)

The forwarding call in a C++ non-virtual thunk (`_ZThn...`, a DISubprogram
with DIFlagThunk at the method's line L) has a DILocation with line 0. The
profiler records that frame with line offset 0, but MemProfUse computed the
offset as `(0 - L) & 0xffff`, so the call site never matched and the thunk
got no `!callsite` metadata. Once the method is inlined into the thunk, the
thunk's private copy of the call chain can no longer be redirected to a
cold clone during context disambiguation. For example:

```c++
struct A { virtual ~A(); virtual void f(); long a; };
struct B { virtual ~B(); virtual void g(); long b; };
struct C : A, B { void f() override; void g() override; };
void C::g() { b = (long)new char[10]; }
void viaB(B *p) { p->g(); }  // -> _ZThn16_N1C1gEv -> C::g(), cold
int main() { C c; c.g(); ... } // -> C::g() directly, not cold
```

The allocation in `C::g()` is cold only through the thunk, so it needs a
clone, but the thunk's inlined copy of `C::g()` keeps the not-cold
allocation.

Factor the three identical `GetOffset` lambdas into `getLineOffset()` and
make it return 0 when the location's line is below the subprogram's start
line, and apply the same clamp in `RawMemProfReader` (where the `uint32_t`
subtraction wrapped). Stack ids change only for frames that could never
match before.
---
 llvm/lib/ProfileData/MemProfReader.cpp        |   8 +-
 .../Transforms/Instrumentation/MemProfUse.cpp |  38 ++---
 .../PGOProfile/memprof_thunk_line0.ll         | 134 ++++++++++++++++++
 llvm/unittests/ProfileData/MemProfTest.cpp    |  54 +++++++
 4 files changed, 214 insertions(+), 20 deletions(-)
 create mode 100644 llvm/test/Transforms/PGOProfile/memprof_thunk_line0.ll

diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index c476e1a08cd59d..c6f7295c3422c6 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -625,7 +625,13 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
            I++) {
         const auto &DIFrame = DI.getFrame(I);
         const uint64_t Guid = memprof::getGUID(DIFrame.FunctionName);
-        const Frame F(Guid, DIFrame.Line - DIFrame.StartLine, DIFrame.Column,
+        // Compiler generated code, e.g. the forwarding call in a C++
+        // non-virtual thunk, has line 0. Record such frames with offset 0
+        // (matching the compiler side) instead of a wrapped around value.
+        const uint32_t LineOffset = DIFrame.Line >= DIFrame.StartLine
+                                        ? DIFrame.Line - DIFrame.StartLine
+                                        : 0;
+        const Frame F(Guid, LineOffset, DIFrame.Column,
                       // Only the last entry is not an inlined location.
                       I != NumFrames - 1);
         // Here we retain a mapping from the GUID to canonical symbol name
diff --git a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
index d0c3fdb2c026aa..445411581474e8 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
@@ -141,6 +141,21 @@ static uint64_t computeStackId(const memprof::Frame &Frame) {
   return computeStackId(Frame.Function, Frame.LineOffset, Frame.Column);
 }
 
+// Compute the line offset of a debug location relative to the start line of
+// its enclosing subprogram, in the same way as the profile (see
+// memprof::Frame::LineOffset). Locations whose line is smaller than the
+// subprogram's line, most commonly compiler generated code with line 0 such
+// as the forwarding call in a C++ non-virtual thunk (DIFlagThunk), are
+// recorded by the profiler with offset 0. Do the same here instead of
+// wrapping to a large offset that can never match the profile.
+static uint32_t getLineOffset(const DILocation *DIL) {
+  unsigned Line = DIL->getLine();
+  unsigned SubprogramLine = DIL->getScope()->getSubprogram()->getLine();
+  if (Line < SubprogramLine)
+    return 0;
+  return (Line - SubprogramLine) & 0xffff;
+}
+
 static AllocationType getAllocType(const AllocationInfo *AllocInfo) {
   return getAllocType(AllocInfo->Info.getTotalLifetimeAccessDensity(),
                       AllocInfo->Info.getAllocCount(),
@@ -278,11 +293,6 @@ memprof::extractCallsFromIR(Module &M, const TargetLibraryInfo &TLI,
                             function_ref<bool(uint64_t)> IsPresentInProfile) {
   DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> Calls;
 
-  auto GetOffset = [](const DILocation *DIL) {
-    return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
-           0xffff;
-  };
-
   for (Function &F : M) {
     if (F.isDeclaration())
       continue;
@@ -330,7 +340,7 @@ memprof::extractCallsFromIR(Module &M, const TargetLibraryInfo &TLI,
             }
           }
 
-          LineLocation Loc = {GetOffset(DIL), DIL->getColumn()};
+          LineLocation Loc = {getLineOffset(DIL), DIL->getColumn()};
           Calls[CallerGUID].emplace_back(Loc, CalleeGUID);
           CalleeName = CallerName;
           IsLeaf = false;
@@ -638,11 +648,6 @@ static void dumpInlineCallStack(Instruction &I, CallBase *CI,
                                 DenseSet<uint64_t> &SeenFrames,
                                 DenseSet<uint64_t> &SeenStacks,
                                 bool ProfileHasColumns) {
-  auto GetOffset = [](const DILocation *DIL) {
-    return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
-           0xffff;
-  };
-
   // Dump frame info.  Frames are deduplicated using FrameID.
   std::string CallStack;
   raw_string_ostream CallStackOS(CallStack);
@@ -653,12 +658,12 @@ static void dumpInlineCallStack(Instruction &I, CallBase *CI,
     if (Name.empty())
       Name = DIL->getScope()->getSubprogram()->getName();
     auto CalleeGUID = Function::getGUIDAssumingExternalLinkage(Name);
-    uint64_t FrameID = computeStackId(CalleeGUID, GetOffset(DIL),
+    uint64_t FrameID = computeStackId(CalleeGUID, getLineOffset(DIL),
                                       ProfileHasColumns ? DIL->getColumn() : 0);
     if (SeenFrames.insert(FrameID).second) {
       std::string DictMsg;
       raw_string_ostream DictOS(DictMsg);
-      DictOS << "frame: " << FrameID << " " << Name << ":" << GetOffset(DIL)
+      DictOS << "frame: " << FrameID << " " << Name << ":" << getLineOffset(DIL)
              << ":" << (ProfileHasColumns ? DIL->getColumn() : 0);
       ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "MemProfUse", CI)
                << DictOS.str());
@@ -792,11 +797,6 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
     assert(Idx <= CS.Frames.size() && CS.Frames[Idx - 1].Function == FuncGUID);
   }
 
-  auto GetOffset = [](const DILocation *DIL) {
-    return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
-           0xffff;
-  };
-
   // Now walk the instructions, looking up the associated profile data using
   // debug locations.
   for (auto &BB : F) {
@@ -836,7 +836,7 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
         if (Name.empty())
           Name = DIL->getScope()->getSubprogram()->getName();
         auto CalleeGUID = Function::getGUIDAssumingExternalLinkage(Name);
-        auto StackId = computeStackId(CalleeGUID, GetOffset(DIL),
+        auto StackId = computeStackId(CalleeGUID, getLineOffset(DIL),
                                       ProfileHasColumns ? DIL->getColumn() : 0);
         // Check if we have found the profile's leaf frame. If yes, collect
         // the rest of the call's inlined context starting here. If not, see if
diff --git a/llvm/test/Transforms/PGOProfile/memprof_thunk_line0.ll b/llvm/test/Transforms/PGOProfile/memprof_thunk_line0.ll
new file mode 100644
index 00000000000000..782a1d0842a344
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/memprof_thunk_line0.ll
@@ -0,0 +1,134 @@
+;; Test that a call whose debug location has line 0 is matched to the profile.
+;;
+;; clang gives the forwarding call in a C++ non-virtual thunk (a DISubprogram
+;; with DIFlagThunk) a DILocation with line 0, and the profiler records the
+;; thunk frame with line offset 0. Previously the compiler computed the offset
+;; as (0 - <subprogram line>) & 0xffff, so the thunk's call never received
+;; !callsite metadata and, once the method was inlined into the thunk, the
+;; thunk's private copy of the call chain could not be redirected to the cold
+;; clone during context disambiguation.
+;;
+;; The test case is generated from (with -O2 -gmlt -fdebug-info-for-profiling):
+;;
+;; struct A { virtual ~A(); virtual void f(); long a; };
+;; struct B { virtual ~B(); virtual void g(); long b; };
+;; struct C : A, B { void f() override; void g() override; };
+;; void C::g() {
+;;   b = (long)new char[10];
+;; }
+;; void viaB(B *p) { p->g(); } // calls _ZThn16_N1C1gEv -> _ZN1C1gEv (cold)
+;; int main() {
+;;   C c; c.g();               // calls _ZN1C1gEv directly (notcold)
+;;   ...
+;; }
+
+; REQUIRES: x86_64-linux
+; RUN: split-file %s %t
+; RUN: llvm-profdata merge %t/memprof_thunk_line0.yaml -o %t/memprof_thunk_line0.memprofdata
+; RUN: opt < %t/memprof_thunk_line0.ll -passes='memprof-use<profile-filename=%t/memprof_thunk_line0.memprofdata>' -memprof-print-match-info -S 2>&1 | FileCheck %s
+
+;--- memprof_thunk_line0.yaml
+---
+HeapProfileRecords:
+  - GUID:            _ZN1C1gEv
+    AllocSites:
+      - Callstack:
+          - { Function: _ZN1C1gEv, LineOffset: 1, Column: 13, IsInlineFrame: false }
+          - { Function: _ZThn16_N1C1gEv, LineOffset: 0, Column: 0, IsInlineFrame: false }
+        MemInfoBlock:
+          AllocCount:      1
+          TotalSize:       10
+          TotalLifetime:   200000
+          TotalLifetimeAccessDensity: 0
+      - Callstack:
+          - { Function: _ZN1C1gEv, LineOffset: 1, Column: 13, IsInlineFrame: false }
+          - { Function: main, LineOffset: 1, Column: 10, IsInlineFrame: false }
+        MemInfoBlock:
+          AllocCount:      1
+          TotalSize:       10
+          TotalLifetime:   0
+          TotalLifetimeAccessDensity: 20000
+    CallSites:       []
+  - GUID:            _ZThn16_N1C1gEv
+    AllocSites:      []
+    CallSites:
+      - Frames:
+        - { Function: _ZThn16_N1C1gEv, LineOffset: 0, Column: 0, IsInlineFrame: false }
+  - GUID:            main
+    AllocSites:      []
+    CallSites:
+      - Frames:
+        - { Function: main, LineOffset: 1, Column: 10, IsInlineFrame: false }
+...
+;--- memprof_thunk_line0.ll
+;; Stack ids below:
+;;   5301324431836061490  = frame _ZN1C1gEv:1:13 (the allocation)
+;;   7590385790444870038  = frame main:1:10
+;;  -2980577181169989635  = frame _ZThn16_N1C1gEv:0:0 (15466166892539561981 unsigned)
+
+;; From -memprof-print-match-info: both allocation contexts and both callsites,
+;; including the thunk's line 0 call, are matched.
+; CHECK: MemProf cold context with id 7723570926052210122 has total profiled size 10 is matched with 1 frames
+; CHECK: MemProf notcold context with id 9778334180364273387 has total profiled size 10 is matched with 1 frames
+; CHECK: MemProf callsite match for inline call stack 7590385790444870038
+; CHECK: MemProf callsite match for inline call stack 15466166892539561981
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+;; Both contexts match the allocation, one notcold (from main) and one cold
+;; (through the thunk).
+; CHECK-LABEL: define void @_ZN1C1gEv
+; CHECK: call ptr @_Znam(i64 10){{.*}} !memprof ![[M:[0-9]+]], !callsite ![[C:[0-9]+]]
+define void @_ZN1C1gEv(ptr %this) !dbg !10 {
+entry:
+  %call = call ptr @_Znam(i64 10), !dbg !13
+  ret void
+}
+
+declare ptr @_Znam(i64)
+
+;; The thunk's forwarding call has a line 0 location. It must be matched to the
+;; profile's callsite record for the thunk, which has line offset 0.
+; CHECK-LABEL: define void @_ZThn16_N1C1gEv
+; CHECK: call void @_ZN1C1gEv(ptr %p){{.*}} !callsite ![[CTHUNK:[0-9]+]]
+define void @_ZThn16_N1C1gEv(ptr %this) !dbg !14 {
+entry:
+  %p = getelementptr i8, ptr %this, i64 -16
+  call void @_ZN1C1gEv(ptr %p), !dbg !15
+  ret void
+}
+
+; CHECK-LABEL: define i32 @main
+; CHECK: call void @_ZN1C1gEv(ptr %c){{.*}} !callsite ![[CMAIN:[0-9]+]]
+define i32 @main() !dbg !16 {
+entry:
+  %c = alloca i8, i64 32
+  call void @_ZN1C1gEv(ptr %c), !dbg !17
+  ret i32 0
+}
+
+; CHECK: ![[M]] = !{![[MIB1:[0-9]+]], ![[MIB2:[0-9]+]]}
+; CHECK: ![[MIB1]] = !{![[STACK1:[0-9]+]], !"notcold"}
+; CHECK: ![[STACK1]] = !{i64 5301324431836061490, i64 7590385790444870038}
+; CHECK: ![[MIB2]] = !{![[STACK2:[0-9]+]], !"cold"}
+; CHECK: ![[STACK2]] = !{i64 5301324431836061490, i64 -2980577181169989635}
+; CHECK: ![[C]] = !{i64 5301324431836061490}
+; CHECK: ![[CTHUNK]] = !{i64 -2980577181169989635}
+; CHECK: ![[CMAIN]] = !{i64 7590385790444870038}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None)
+!1 = !DIFile(filename: "thunk.cc", directory: "/")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!10 = distinct !DISubprogram(name: "g", linkageName: "_ZN1C1gEv", scope: !1, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!11 = !DISubroutineType(types: !12)
+!12 = !{}
+!13 = !DILocation(line: 5, column: 13, scope: !10)
+!14 = distinct !DISubprogram(linkageName: "_ZThn16_N1C1gEv", scope: !1, file: !1, line: 4, type: !11, flags: DIFlagArtificial | DIFlagThunk | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!15 = !DILocation(line: 0, scope: !14)
+!16 = distinct !DISubprogram(name: "main", linkageName: "main", scope: !1, file: !1, line: 8, type: !11, scopeLine: 8, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!17 = !DILocation(line: 9, column: 10, scope: !16)
diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp
index 6ea951eee920bf..e242444b67980c 100644
--- a/llvm/unittests/ProfileData/MemProfTest.cpp
+++ b/llvm/unittests/ProfileData/MemProfTest.cpp
@@ -250,6 +250,60 @@ TEST(MemProf, FillsValue) {
                               FrameContains("abc", 5U, 30U, false)))));
 }
 
+// A frame whose line is smaller than its function's start line, as is the case
+// for compiler generated code with line 0 such as the forwarding call in a C++
+// non-virtual thunk, is recorded with line offset 0 (matching the offset the
+// compiler computes for it) rather than a wrapped around value.
+TEST(MemProf, LineBelowStartLineHasZeroOffset) {
+  auto Symbolizer = std::make_unique<MockSymbolizer>();
+
+  EXPECT_CALL(*Symbolizer, symbolizeInlinedCode(SectionedAddress{0x1000},
+                                                specifier(), false))
+      .Times(1)
+      .WillRepeatedly(Return(makeInliningInfo({
+          {"foo", 10, 5, 30},
+      })));
+
+  EXPECT_CALL(*Symbolizer, symbolizeInlinedCode(SectionedAddress{0x2000},
+                                                specifier(), false))
+      .Times(1)
+      .WillRepeatedly(Return(makeInliningInfo({
+          {"_ZThn16_N1C1gEv", 0, 4, 0},
+      })));
+
+  CallStackMap CSM;
+  CSM[0x1] = {0x1000, 0x2000};
+
+  llvm::MapVector<uint64_t, MemInfoBlock> Prof;
+  Prof[0x1].AllocCount = 1;
+
+  auto Seg = makeSegments();
+
+  RawMemProfReader Reader(std::move(Symbolizer), Seg, Prof, CSM,
+                          /*KeepName=*/true);
+
+  llvm::DenseMap<llvm::GlobalValue::GUID, MemProfRecord> Records;
+  for (const auto &Pair : Reader)
+    Records.insert({Pair.first, Pair.second});
+
+  const llvm::GlobalValue::GUID FooId = memprof::getGUID("foo");
+  ASSERT_TRUE(Records.contains(FooId));
+  const MemProfRecord &Foo = Records[FooId];
+  ASSERT_THAT(Foo.AllocSites, SizeIs(1));
+  EXPECT_THAT(Foo.AllocSites[0].CallStack[0],
+              FrameContains("foo", 5U, 30U, false));
+  EXPECT_THAT(Foo.AllocSites[0].CallStack[1],
+              FrameContains("_ZThn16_N1C1gEv", 0U, 0U, false));
+
+  const llvm::GlobalValue::GUID ThunkId = memprof::getGUID("_ZThn16_N1C1gEv");
+  ASSERT_TRUE(Records.contains(ThunkId));
+  EXPECT_THAT(
+      Records[ThunkId].CallSites,
+      ElementsAre(testing::Field(
+          &CallSiteInfo::Frames,
+          ElementsAre(FrameContains("_ZThn16_N1C1gEv", 0U, 0U, false)))));
+}
+
 TEST(MemProf, PortableWrapper) {
   MemInfoBlock Info(/*size=*/16, /*access_count=*/7, /*alloc_timestamp=*/1000,
                     /*dealloc_timestamp=*/2000, /*alloc_cpu=*/3,

>From 1df53d7f63fa08ffcc20c2822af7356dab2999c7 Mon Sep 17 00:00:00 2001
From: David Li <57157229+david-xl at users.noreply.github.com>
Date: Wed, 7 Oct 2026 14:45:42 -0700
Subject: [PATCH 2/3] Address review comments: simplify comment, fold unit test
 into FillsValue

---
 .../Transforms/Instrumentation/MemProfUse.cpp |  8 +-
 llvm/unittests/ProfileData/MemProfTest.cpp    | 84 ++++++-------------
 2 files changed, 29 insertions(+), 63 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
index 445411581474e8..e1fe569bf3be90 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
@@ -142,12 +142,8 @@ static uint64_t computeStackId(const memprof::Frame &Frame) {
 }
 
 // Compute the line offset of a debug location relative to the start line of
-// its enclosing subprogram, in the same way as the profile (see
-// memprof::Frame::LineOffset). Locations whose line is smaller than the
-// subprogram's line, most commonly compiler generated code with line 0 such
-// as the forwarding call in a C++ non-virtual thunk (DIFlagThunk), are
-// recorded by the profiler with offset 0. Do the same here instead of
-// wrapping to a large offset that can never match the profile.
+// its enclosing subprogram, clamped at 0 as the profile reader does (e.g. for
+// locations without debug info whose line is 0).
 static uint32_t getLineOffset(const DILocation *DIL) {
   unsigned Line = DIL->getLine();
   unsigned SubprogramLine = DIL->getScope()->getSubprogram()->getLine();
diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp
index e242444b67980c..a58bcb9c5c12e1 100644
--- a/llvm/unittests/ProfileData/MemProfTest.cpp
+++ b/llvm/unittests/ProfileData/MemProfTest.cpp
@@ -161,8 +161,17 @@ TEST(MemProf, FillsValue) {
           {"abc", 10, 5, 30},
       })));
 
+  // A frame whose line (0, e.g. missing debug info) is below the function's
+  // start line; its line offset must be clamped to 0 rather than wrap around.
+  EXPECT_CALL(*Symbolizer, symbolizeInlinedCode(SectionedAddress{0x4000},
+                                                specifier(), false))
+      .Times(1)
+      .WillRepeatedly(Return(makeInliningInfo({
+          {"def", 0, 4, 0},
+      })));
+
   CallStackMap CSM;
-  CSM[0x1] = {0x1000, 0x2000, 0x3000};
+  CSM[0x1] = {0x1000, 0x2000, 0x3000, 0x4000};
 
   llvm::MapVector<uint64_t, MemInfoBlock> Prof;
   Prof[0x1].AllocCount = 1;
@@ -183,11 +192,12 @@ TEST(MemProf, FillsValue) {
   // bar() { foo(); }                Y               Y
   // inline xyz() { bar(); }         N               Y
   // abc() { xyz(); }                N               Y
+  // def() { abc(); }                N               Y
 
-  // We expect 4 records. We attach alloc site data to foo and bar, i.e.
+  // We expect 5 records. We attach alloc site data to foo and bar, i.e.
   // all frames bottom up until we find a non-inline frame. We attach call site
-  // data to bar, xyz and abc.
-  ASSERT_THAT(Records, SizeIs(4));
+  // data to bar, xyz, abc and def.
+  ASSERT_THAT(Records, SizeIs(5));
 
   // Check the memprof record for foo.
   const llvm::GlobalValue::GUID FooId = memprof::getGUID("foo");
@@ -203,6 +213,8 @@ TEST(MemProf, FillsValue) {
               FrameContains("xyz", 5U, 30U, true));
   EXPECT_THAT(Foo.AllocSites[0].CallStack[3],
               FrameContains("abc", 5U, 30U, false));
+  EXPECT_THAT(Foo.AllocSites[0].CallStack[4],
+              FrameContains("def", 0U, 0U, false));
   EXPECT_TRUE(Foo.CallSites.empty());
 
   // Check the memprof record for bar.
@@ -219,6 +231,8 @@ TEST(MemProf, FillsValue) {
               FrameContains("xyz", 5U, 30U, true));
   EXPECT_THAT(Bar.AllocSites[0].CallStack[3],
               FrameContains("abc", 5U, 30U, false));
+  EXPECT_THAT(Bar.AllocSites[0].CallStack[4],
+              FrameContains("def", 0U, 0U, false));
 
   EXPECT_THAT(Bar.CallSites,
               ElementsAre(testing::Field(
@@ -248,60 +262,16 @@ TEST(MemProf, FillsValue) {
                   &CallSiteInfo::Frames,
                   ElementsAre(FrameContains("xyz", 5U, 30U, true),
                               FrameContains("abc", 5U, 30U, false)))));
-}
-
-// A frame whose line is smaller than its function's start line, as is the case
-// for compiler generated code with line 0 such as the forwarding call in a C++
-// non-virtual thunk, is recorded with line offset 0 (matching the offset the
-// compiler computes for it) rather than a wrapped around value.
-TEST(MemProf, LineBelowStartLineHasZeroOffset) {
-  auto Symbolizer = std::make_unique<MockSymbolizer>();
-
-  EXPECT_CALL(*Symbolizer, symbolizeInlinedCode(SectionedAddress{0x1000},
-                                                specifier(), false))
-      .Times(1)
-      .WillRepeatedly(Return(makeInliningInfo({
-          {"foo", 10, 5, 30},
-      })));
-
-  EXPECT_CALL(*Symbolizer, symbolizeInlinedCode(SectionedAddress{0x2000},
-                                                specifier(), false))
-      .Times(1)
-      .WillRepeatedly(Return(makeInliningInfo({
-          {"_ZThn16_N1C1gEv", 0, 4, 0},
-      })));
 
-  CallStackMap CSM;
-  CSM[0x1] = {0x1000, 0x2000};
-
-  llvm::MapVector<uint64_t, MemInfoBlock> Prof;
-  Prof[0x1].AllocCount = 1;
-
-  auto Seg = makeSegments();
-
-  RawMemProfReader Reader(std::move(Symbolizer), Seg, Prof, CSM,
-                          /*KeepName=*/true);
-
-  llvm::DenseMap<llvm::GlobalValue::GUID, MemProfRecord> Records;
-  for (const auto &Pair : Reader)
-    Records.insert({Pair.first, Pair.second});
-
-  const llvm::GlobalValue::GUID FooId = memprof::getGUID("foo");
-  ASSERT_TRUE(Records.contains(FooId));
-  const MemProfRecord &Foo = Records[FooId];
-  ASSERT_THAT(Foo.AllocSites, SizeIs(1));
-  EXPECT_THAT(Foo.AllocSites[0].CallStack[0],
-              FrameContains("foo", 5U, 30U, false));
-  EXPECT_THAT(Foo.AllocSites[0].CallStack[1],
-              FrameContains("_ZThn16_N1C1gEv", 0U, 0U, false));
-
-  const llvm::GlobalValue::GUID ThunkId = memprof::getGUID("_ZThn16_N1C1gEv");
-  ASSERT_TRUE(Records.contains(ThunkId));
-  EXPECT_THAT(
-      Records[ThunkId].CallSites,
-      ElementsAre(testing::Field(
-          &CallSiteInfo::Frames,
-          ElementsAre(FrameContains("_ZThn16_N1C1gEv", 0U, 0U, false)))));
+  // Check the memprof record for def.
+  const llvm::GlobalValue::GUID DefId = memprof::getGUID("def");
+  ASSERT_TRUE(Records.contains(DefId));
+  const MemProfRecord &Def = Records[DefId];
+  EXPECT_TRUE(Def.AllocSites.empty());
+  EXPECT_THAT(Def.CallSites,
+              ElementsAre(testing::Field(
+                  &CallSiteInfo::Frames,
+                  ElementsAre(FrameContains("def", 0U, 0U, false)))));
 }
 
 TEST(MemProf, PortableWrapper) {

>From ec89a9abd39bfe026c26bd682025860eed9aa4c7 Mon Sep 17 00:00:00 2001
From: David Li <57157229+david-xl at users.noreply.github.com>
Date: Wed, 7 Oct 2026 14:51:18 -0700
Subject: [PATCH 3/3] Drop the 0xffff mask on the line offset; the profile
 reader keeps the full offset

---
 llvm/lib/Transforms/Instrumentation/MemProfUse.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
index e1fe569bf3be90..d78a3ada2044ad 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
@@ -149,7 +149,7 @@ static uint32_t getLineOffset(const DILocation *DIL) {
   unsigned SubprogramLine = DIL->getScope()->getSubprogram()->getLine();
   if (Line < SubprogramLine)
     return 0;
-  return (Line - SubprogramLine) & 0xffff;
+  return Line - SubprogramLine;
 }
 
 static AllocationType getAllocType(const AllocationInfo *AllocInfo) {



More information about the llvm-commits mailing list