[clang] [compiler-rt] [lld] [llvm] [MemProf] Support multiple profiled binaries (PR #217039)
Parmida Vahdatnia via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 30 06:24:35 PDT 2026
https://github.com/parmidavnia updated https://github.com/llvm/llvm-project/pull/217039
>From 7073c1e8f5fd27a0691749dc2f3e01f0496819f8 Mon Sep 17 00:00:00 2001
From: Parmida Vahdatniya <parmida.829667 at huawei.com>
Date: Tue, 18 Aug 2026 09:51:06 -0400
Subject: [PATCH 1/4] [MemProf] Support multiple profiled binaries
---
.../Inputs/multiple-profiled-dsos-lib1.inc | 10 +
.../Inputs/multiple-profiled-dsos-lib2.inc | 10 +
.../TestCases/multiple-profiled-dsos.cpp | 38 +++
llvm/include/llvm/ProfileData/MemProfReader.h | 58 +++-
llvm/lib/ProfileData/MemProfReader.cpp | 318 +++++++++++-------
llvm/tools/llvm-profdata/llvm-profdata.cpp | 41 ++-
6 files changed, 319 insertions(+), 156 deletions(-)
create mode 100644 compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
create mode 100644 compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
create mode 100644 compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
diff --git a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
new file mode 100644
index 00000000000000..d219bd7ecb6eaf
--- /dev/null
+++ b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
@@ -0,0 +1,10 @@
+#include <cstdlib>
+
+extern "C" __attribute__((noinline)) void shared_allocate() {
+ volatile char *P = static_cast<char *>(std::malloc(111));
+ for (int I = 0; I != 111; ++I)
+ P[I] = static_cast<char>(I);
+ std::free(const_cast<char *>(P));
+}
+
+extern "C" __attribute__((noinline)) void lib1_entry() { shared_allocate(); }
diff --git a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
new file mode 100644
index 00000000000000..5f2e58499c25d6
--- /dev/null
+++ b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
@@ -0,0 +1,10 @@
+#include <cstdlib>
+
+extern "C" __attribute__((noinline)) void shared_allocate() {
+ volatile char *P = static_cast<char *>(std::malloc(222));
+ for (int I = 0; I != 222; ++I)
+ P[I] = static_cast<char>(I);
+ std::free(const_cast<char *>(P));
+}
+
+extern "C" __attribute__((noinline)) void lib2_entry() { shared_allocate(); }
diff --git a/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
new file mode 100644
index 00000000000000..53388480f38440
--- /dev/null
+++ b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
@@ -0,0 +1,38 @@
+// REQUIRES: x86_64-linux, memprof-dynamic-runtime
+
+// Only the two shared libraries are MemProf-instrumented. -Bsymbolic ensures
+// each DSO calls its own externally visible shared_allocate definition.
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id -Wl,-Bsymbolic -x c++ %S/../Inputs/multiple-profiled-dsos-lib1.inc -o %t.lib1.so
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id -Wl,-Bsymbolic -x c++ %S/../Inputs/multiple-profiled-dsos-lib2.inc -o %t.lib2.so
+// RUN: %clangxx -O1 %s %t.lib1.so %t.lib2.so -Wl,-rpath,%T -o %t.main
+// RUN: rm -f %t.raw.*
+// RUN: env MEMPROF_OPTIONS=log_path=%t.raw LD_PRELOAD=%shared_libmemprof %run %t.main
+// RUN: cp %t.lib1.so %t.duplicate.so
+// RUN: not llvm-profdata show --memory %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.duplicate.so -o /dev/null 2>&1 | FileCheck %s --check-prefix=DUPLICATE
+// RUN: llvm-profdata show --memory %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.lib2.so -o %t.yaml
+// RUN: FileCheck %s --check-prefix=RAW < %t.yaml
+// RUN: llvm-profdata merge %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.lib2.so -o %t.memprofdata
+// RUN: llvm-profdata show --memory %t.memprofdata | FileCheck %s --check-prefix=INDEXED
+
+extern "C" void lib1_entry();
+extern "C" void lib2_entry();
+
+int main() {
+ lib1_entry();
+ lib2_entry();
+}
+
+// The shared_allocate linkage name is identical in both DSOs. Two allocation
+// functions proves their module-qualified GUIDs did not collapse into one key.
+// RAW: NumMibInfo: 2
+// RAW-NEXT: NumAllocFunctions: 2
+// RAW-DAG: SymbolName: shared_allocate
+// RAW-DAG: TotalSize: 111
+// RAW-DAG: SymbolName: shared_allocate
+// RAW-DAG: TotalSize: 222
+
+// INDEXED: Total contexts: 2
+// INDEXED-DAG: TotalSize: 111
+// INDEXED-DAG: TotalSize: 222
+
+// DUPLICATE: error: Duplicate profiled binary build id:
diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h
index 8fdae7a472d5fb..5ebee3e73b374c 100644
--- a/llvm/include/llvm/ProfileData/MemProfReader.h
+++ b/llvm/include/llvm/ProfileData/MemProfReader.h
@@ -13,6 +13,7 @@
#ifndef LLVM_PROFILEDATA_MEMPROFREADER_H_
#define LLVM_PROFILEDATA_MEMPROFREADER_H_
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringRef.h"
@@ -128,8 +129,14 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
static Expected<std::unique_ptr<RawMemProfReader>>
create(const Twine &Path, StringRef ProfiledBinary, bool KeepName = false);
static Expected<std::unique_ptr<RawMemProfReader>>
+ create(const Twine &Path, ArrayRef<std::string> ProfiledBinaries,
+ bool KeepName = false);
+ static Expected<std::unique_ptr<RawMemProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer, StringRef ProfiledBinary,
bool KeepName = false);
+ static Expected<std::unique_ptr<RawMemProfReader>>
+ create(std::unique_ptr<MemoryBuffer> Buffer,
+ ArrayRef<std::string> ProfiledBinaries, bool KeepName = false);
// Returns a list of build ids recorded in the segment information.
static std::vector<std::string> peekBuildIds(MemoryBuffer *DataBuffer);
@@ -157,44 +164,61 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
}
private:
- RawMemProfReader(object::OwningBinary<object::Binary> &&Bin, bool KeepName)
- : Binary(std::move(Bin)), KeepSymbolName(KeepName) {}
+ struct ProfiledModule {
+ explicit ProfiledModule(object::OwningBinary<object::Binary> &&Bin)
+ : Binary(std::move(Bin)) {}
+
+ // Keep the binary and its backing buffer alive for the lifetime of the
+ // module symbolizer.
+ object::OwningBinary<object::Binary> Binary;
+ // Symbolizer created from this module's object file and DWARF context.
+ std::unique_ptr<symbolize::SymbolizableModule> Symbolizer;
+ // Build ID used to distinguish equal function names in different modules.
+ llvm::SmallVector<uint8_t, 20> BuildId;
+ // Preferred address of the executable segment in the ELF file.
+ uint64_t PreferredTextSegmentAddress = 0;
+ // Runtime address range of this module's executable segment, recovered
+ // from the raw profile by matching the build ID.
+ uint64_t ProfiledTextSegmentStart = 0;
+ uint64_t ProfiledTextSegmentEnd = 0;
+ };
+
+ explicit RawMemProfReader(bool KeepName) : KeepSymbolName(KeepName) {}
// Initializes the RawMemProfReader with the contents in `DataBuffer`.
- Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer);
+ Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
+ ArrayRef<std::string> ProfiledBinaries);
// Read and parse the contents of the `DataBuffer` as a binary format profile.
Error readRawProfile(std::unique_ptr<MemoryBuffer> DataBuffer);
- // Initialize the segment mapping information for symbolization.
+ // Match each profiled module to its executable runtime segment using its
+ // build ID and initialize its runtime address range for symbolization.
Error setupForSymbolization();
// Symbolize and cache all the virtual addresses we encounter in the
// callstacks from the raw profile. Also prune callstack frames which we can't
// symbolize or those that belong to the runtime. For profile entries where
// the entire callstack is pruned, we drop the entry from the profile.
Error symbolizeAndFilterStackFrames(
- std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer);
+ std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer =
+ nullptr);
// Construct memprof records for each function and store it in the
// `FunctionProfileData` map. A function may have allocation profile data or
// callsite data or both.
Error mapRawProfileToRecords();
- object::SectionedAddress getModuleOffset(uint64_t VirtualAddress);
+ object::SectionedAddress getModuleOffset(uint64_t VirtualAddress,
+ const ProfiledModule &Module) const;
+ // Find the supplied profiled module whose runtime text range contains the PC.
+ ProfiledModule *findModule(uint64_t VirtualAddress) const;
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
readMemInfoBlocks(const char *Ptr);
- // The profiled binary.
- object::OwningBinary<object::Binary> Binary;
+ // The binaries which contributed symbolizable frames to the raw profile.
+ llvm::SmallVector<std::unique_ptr<ProfiledModule>, 4> ProfiledModules;
// Version of raw memprof binary currently being read. Defaults to most up
// to date version.
uint64_t MemprofRawVersion = MEMPROF_RAW_VERSION;
- // The preferred load address of the executable segment.
- uint64_t PreferredTextSegmentAddress = 0;
- // The base address of the text segment in the process during profiling.
- uint64_t ProfiledTextSegmentStart = 0;
- // The limit address of the text segment in the process during profiling.
- uint64_t ProfiledTextSegmentEnd = 0;
-
- // The memory mapped segment information for all executable segments in the
- // profiled binary (filtered from the raw profile using the build id).
+ // Memory-mapped executable segment information recorded for all binaries in
+ // the profiled process.
llvm::SmallVector<SegmentEntry, 2> SegmentInfo;
// A map from callstack id (same as key in CallStackMap below) to the heap
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index c6f7295c3422c6..6069f96d31a26a 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -16,10 +16,13 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
@@ -39,6 +42,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MD5.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -277,26 +281,53 @@ std::string getBuildIdString(const SegmentEntry &Entry) {
}
return Str;
}
+
+GlobalValue::GUID getModuleGUID(StringRef FunctionName,
+ ArrayRef<uint8_t> BuildId) {
+ StringRef CanonicalName =
+ sampleprof::FunctionSamples::getCanonicalFnName(FunctionName);
+ MD5 Hash;
+ Hash.update("MemProf module GUID");
+ Hash.update(BuildId);
+ Hash.update(CanonicalName);
+ return Hash.final().low();
+}
} // namespace
Expected<std::unique_ptr<RawMemProfReader>>
RawMemProfReader::create(const Twine &Path, const StringRef ProfiledBinary,
bool KeepName) {
+ std::string BinaryPath = ProfiledBinary.str();
+ return create(Path, ArrayRef<std::string>(BinaryPath), KeepName);
+}
+
+Expected<std::unique_ptr<RawMemProfReader>> RawMemProfReader::create(
+ const Twine &Path, ArrayRef<std::string> ProfiledBinaries, bool KeepName) {
auto BufferOr = MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = BufferOr.getError())
return report(errorCodeToError(EC), Path.getSingleStringRef());
std::unique_ptr<MemoryBuffer> Buffer(BufferOr.get().release());
- return create(std::move(Buffer), ProfiledBinary, KeepName);
+ return create(std::move(Buffer), ProfiledBinaries, KeepName);
}
Expected<std::unique_ptr<RawMemProfReader>>
RawMemProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
const StringRef ProfiledBinary, bool KeepName) {
+ std::string BinaryPath = ProfiledBinary.str();
+ return create(std::move(Buffer), ArrayRef<std::string>(BinaryPath), KeepName);
+}
+
+Expected<std::unique_ptr<RawMemProfReader>>
+RawMemProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
+ ArrayRef<std::string> ProfiledBinaries,
+ bool KeepName) {
if (Error E = checkBuffer(*Buffer))
return report(std::move(E), Buffer->getBufferIdentifier());
- if (ProfiledBinary.empty()) {
+ if (ProfiledBinaries.empty() ||
+ llvm::all_of(ProfiledBinaries,
+ [](const std::string &Path) { return Path.empty(); })) {
// Peek the build ids to print a helpful error message.
const std::vector<std::string> BuildIds = peekBuildIds(Buffer.get());
std::string ErrorMessage(
@@ -311,15 +342,9 @@ RawMemProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
/*Context=*/"");
}
- auto BinaryOr = llvm::object::createBinary(ProfiledBinary);
- if (!BinaryOr) {
- return report(BinaryOr.takeError(), ProfiledBinary);
- }
-
// Use new here since constructor is private.
- std::unique_ptr<RawMemProfReader> Reader(
- new RawMemProfReader(std::move(BinaryOr.get()), KeepName));
- if (Error E = Reader->initialize(std::move(Buffer))) {
+ std::unique_ptr<RawMemProfReader> Reader(new RawMemProfReader(KeepName));
+ if (Error E = Reader->initialize(std::move(Buffer), ProfiledBinaries)) {
return std::move(E);
}
return std::move(Reader);
@@ -394,61 +419,87 @@ void RawMemProfReader::printYAML(raw_ostream &OS) {
}
}
-Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer) {
- const StringRef FileName = Binary.getBinary()->getFileName();
-
- auto *ElfObject = dyn_cast<object::ELFObjectFileBase>(Binary.getBinary());
- if (!ElfObject) {
- return report(make_error<StringError>(Twine("Not an ELF file: "),
- inconvertibleErrorCode()),
- FileName);
- }
-
- // Check whether the profiled binary was built with position independent code
- // (PIC). Perform sanity checks for assumptions we rely on to simplify
- // symbolization.
- auto *Elf64LEObject = llvm::cast<llvm::object::ELF64LEObjectFile>(ElfObject);
- const llvm::object::ELF64LEFile &ElfFile = Elf64LEObject->getELFFile();
- auto PHdrsOr = ElfFile.program_headers();
- if (!PHdrsOr)
- return report(
- make_error<StringError>(Twine("Could not read program headers: "),
- inconvertibleErrorCode()),
- FileName);
-
- int NumExecutableSegments = 0;
- for (const auto &Phdr : *PHdrsOr) {
- if (Phdr.p_type == ELF::PT_LOAD) {
- if (Phdr.p_flags & ELF::PF_X) {
- // We assume only one text segment in the main binary for simplicity and
- // reduce the overhead of checking multiple ranges during symbolization.
- if (++NumExecutableSegments > 1) {
- return report(
- make_error<StringError>(
- "Expect only one executable load segment in the binary",
- inconvertibleErrorCode()),
- FileName);
- }
- // Segment will always be loaded at a page boundary, expect it to be
- // aligned already. Assume 4K pagesize for the machine from which the
- // profile has been collected. This should be fine for now, in case we
- // want to support other pagesizes it can be recorded in the raw profile
- // during collection.
- PreferredTextSegmentAddress = Phdr.p_vaddr;
- assert(Phdr.p_vaddr == (Phdr.p_vaddr & ~(0x1000 - 1U)) &&
- "Expect p_vaddr to always be page aligned");
- assert(Phdr.p_offset == 0 && "Expect p_offset = 0 for symbolization.");
- }
+Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
+ ArrayRef<std::string> ProfiledBinaries) {
+ // Open each profiled binary and perform the ELF and executable-segment sanity
+ // checks needed to translate runtime PCs for symbolization.
+ StringSet<> SeenBuildIds;
+ for (const std::string &BinaryPath : ProfiledBinaries) {
+ if (BinaryPath.empty())
+ continue;
+
+ auto BinaryOr = object::createBinary(BinaryPath);
+ if (!BinaryOr)
+ return report(BinaryOr.takeError(), BinaryPath);
+
+ auto Module = std::make_unique<ProfiledModule>(std::move(BinaryOr.get()));
+ const StringRef FileName = Module->Binary.getBinary()->getFileName();
+
+ auto *ElfObject =
+ dyn_cast<object::ELFObjectFileBase>(Module->Binary.getBinary());
+ if (!ElfObject)
+ return report(
+ make_error<StringError>("Not an ELF file", inconvertibleErrorCode()),
+ FileName);
+
+ auto *Elf64LEObject = dyn_cast<object::ELF64LEObjectFile>(ElfObject);
+ if (!Elf64LEObject)
+ return report(make_error<StringError>("Not a 64-bit little-endian ELF",
+ inconvertibleErrorCode()),
+ FileName);
+ const object::ELF64LEFile &ElfFile = Elf64LEObject->getELFFile();
+ auto PHdrsOr = ElfFile.program_headers();
+ if (!PHdrsOr)
+ return report(PHdrsOr.takeError(), FileName);
+
+ // Check whether the profiled binary was built with position independent
+ // code (PIC). Perform sanity checks for assumptions we rely on to simplify
+ // symbolization.
+ int NumExecutableSegments = 0;
+ for (const auto &Phdr : *PHdrsOr) {
+ if (Phdr.p_type != ELF::PT_LOAD || !(Phdr.p_flags & ELF::PF_X))
+ continue;
+ if (++NumExecutableSegments > 1)
+ return report(
+ make_error<StringError>("Expected only one executable load segment",
+ inconvertibleErrorCode()),
+ FileName);
+ // Segment will always be loaded at a page boundary, expect it to be
+ // aligned already. Assume 4K pagesize for the machine from which the
+ // profile has been collected. This should be fine for now, in case we
+ // want to support other pagesizes it can be recorded in the raw profile
+ // during collection.
+ Module->PreferredTextSegmentAddress = Phdr.p_vaddr;
+ assert(Phdr.p_vaddr == (Phdr.p_vaddr & ~(0x1000 - 1U)) &&
+ "Expect p_vaddr to always be page aligned");
}
+ if (NumExecutableSegments == 0)
+ return report(make_error<StringError>("No executable load segment",
+ inconvertibleErrorCode()),
+ FileName);
+
+ auto Triple = ElfObject->makeTriple();
+ if (!Triple.isX86())
+ return report(make_error<StringError>(Twine("Unsupported target: ") +
+ Triple.getArchName(),
+ inconvertibleErrorCode()),
+ FileName);
+
+ auto *Object = cast<object::ObjectFile>(Module->Binary.getBinary());
+ object::BuildIDRef BinaryId = object::getBuildID(Object);
+ if (BinaryId.empty())
+ return make_error<StringError>(Twine("No build id found in binary ") +
+ FileName,
+ inconvertibleErrorCode());
+ std::string BuildIdString = toHex(BinaryId, /*LowerCase=*/true);
+ if (!SeenBuildIds.insert(BuildIdString).second)
+ return make_error<StringError>(
+ Twine("Duplicate profiled binary build id: ") + BuildIdString,
+ inconvertibleErrorCode());
+ Module->BuildId.assign(BinaryId.begin(), BinaryId.end());
+ ProfiledModules.push_back(std::move(Module));
}
- auto Triple = ElfObject->makeTriple();
- if (!Triple.isX86())
- return report(make_error<StringError>(Twine("Unsupported target: ") +
- Triple.getArchName(),
- inconvertibleErrorCode()),
- FileName);
-
// Process the raw profile.
if (Error E = readRawProfile(std::move(DataBuffer)))
return E;
@@ -456,58 +507,51 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer) {
if (Error E = setupForSymbolization())
return E;
- auto *Object = cast<object::ObjectFile>(Binary.getBinary());
- std::unique_ptr<DIContext> Context = DWARFContext::create(
- *Object, DWARFContext::ProcessDebugRelocations::Process);
-
- auto SOFOr = symbolize::SymbolizableObjectFile::create(
- Object, std::move(Context), /*UntagAddresses=*/false);
- if (!SOFOr)
- return report(SOFOr.takeError(), FileName);
- auto Symbolizer = std::move(SOFOr.get());
-
- // The symbolizer ownership is moved into symbolizeAndFilterStackFrames so
- // that it is freed automatically at the end, when it is no longer used. This
- // reduces peak memory since it won't be live while also mapping the raw
- // profile into records afterwards.
- if (Error E = symbolizeAndFilterStackFrames(std::move(Symbolizer)))
+ // Create a symbolizer for every supplied module after its build ID has been
+ // matched to the raw profile. Each ProfiledModule owns both the binary and
+ // symbolizer so their backing data remains live during frame processing.
+ for (auto &Module : ProfiledModules) {
+ auto *Object = cast<object::ObjectFile>(Module->Binary.getBinary());
+ std::unique_ptr<DIContext> Context = DWARFContext::create(
+ *Object, DWARFContext::ProcessDebugRelocations::Process);
+ auto SymbolizerOr = symbolize::SymbolizableObjectFile::create(
+ Object, std::move(Context), /*UntagAddresses=*/false);
+ if (!SymbolizerOr)
+ return report(SymbolizerOr.takeError(),
+ Module->Binary.getBinary()->getFileName());
+ Module->Symbolizer = std::move(SymbolizerOr.get());
+ }
+
+ if (Error E = symbolizeAndFilterStackFrames())
return E;
return mapRawProfileToRecords();
}
Error RawMemProfReader::setupForSymbolization() {
- auto *Object = cast<object::ObjectFile>(Binary.getBinary());
- object::BuildIDRef BinaryId = object::getBuildID(Object);
- if (BinaryId.empty())
- return make_error<StringError>(Twine("No build id found in binary ") +
- Binary.getBinary()->getFileName(),
- inconvertibleErrorCode());
-
- int NumMatched = 0;
- for (const auto &Entry : SegmentInfo) {
- llvm::ArrayRef<uint8_t> SegmentId(Entry.BuildId, Entry.BuildIdSize);
- if (BinaryId == SegmentId) {
- // We assume only one text segment in the main binary for simplicity and
- // reduce the overhead of checking multiple ranges during symbolization.
- if (++NumMatched > 1) {
+ for (auto &Module : ProfiledModules) {
+ const StringRef FileName = Module->Binary.getBinary()->getFileName();
+
+ // A build ID identifies the runtime mapping belonging to this binary. We
+ // currently support one executable segment per supplied module.
+ int NumMatched = 0;
+ for (const auto &Entry : SegmentInfo) {
+ ArrayRef<uint8_t> SegmentId(Entry.BuildId, Entry.BuildIdSize);
+ if (Module->BuildId != SegmentId)
+ continue;
+ if (++NumMatched > 1)
return make_error<StringError>(
- "We expect only one executable segment in the profiled binary",
+ Twine("Expected only one executable segment for binary ") +
+ FileName,
inconvertibleErrorCode());
- }
- ProfiledTextSegmentStart = Entry.Start;
- ProfiledTextSegmentEnd = Entry.End;
+ Module->ProfiledTextSegmentStart = Entry.Start;
+ Module->ProfiledTextSegmentEnd = Entry.End;
}
+ if (NumMatched == 0)
+ return make_error<StringError>(
+ Twine("No matching executable segments found in binary ") + FileName,
+ inconvertibleErrorCode());
}
- if (NumMatched == 0)
- return make_error<StringError>(
- Twine("No matching executable segments found in binary ") +
- Binary.getBinary()->getFileName(),
- inconvertibleErrorCode());
- assert((PreferredTextSegmentAddress == 0 ||
- (PreferredTextSegmentAddress == ProfiledTextSegmentStart)) &&
- "Expect text segment address to be 0 or equal to profiled text "
- "segment start.");
return Error::success();
}
@@ -587,7 +631,7 @@ Error RawMemProfReader::mapRawProfileToRecords() {
}
Error RawMemProfReader::symbolizeAndFilterStackFrames(
- std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer) {
+ std::unique_ptr<symbolize::SymbolizableModule> Symbolizer) {
// The specifier to use when symbolization is requested.
const DILineInfoSpecifier Specifier(
DILineInfoSpecifier::FileLineInfoKind::RawValue,
@@ -608,8 +652,21 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
AllVAddrsToDiscard.contains(VAddr))
continue;
- Expected<DIInliningInfo> DIOr = Symbolizer->symbolizeInlinedCode(
- getModuleOffset(VAddr), Specifier, /*UseSymbolTable=*/false);
+ ProfiledModule *Module = nullptr;
+ symbolize::SymbolizableModule *FrameSymbolizer = Symbolizer.get();
+ object::SectionedAddress Address{VAddr};
+ if (!FrameSymbolizer) {
+ Module = findModule(VAddr);
+ if (!Module) {
+ AllVAddrsToDiscard.insert(VAddr);
+ continue;
+ }
+ FrameSymbolizer = Module->Symbolizer.get();
+ Address = getModuleOffset(VAddr, *Module);
+ }
+
+ Expected<DIInliningInfo> DIOr = FrameSymbolizer->symbolizeInlinedCode(
+ Address, Specifier, /*UseSymbolTable=*/false);
if (!DIOr)
return DIOr.takeError();
DIInliningInfo &DI = DIOr.get();
@@ -624,7 +681,17 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
for (size_t I = 0, NumFrames = DI.getNumberOfFrames(); I < NumFrames;
I++) {
const auto &DIFrame = DI.getFrame(I);
- const uint64_t Guid = memprof::getGUID(DIFrame.FunctionName);
+ // A PC identifies its module, but the normal MemProf GUID only hashes
+ // the linkage name. Qualify it with the build ID when multiple binaries
+ // are supplied so equal external symbols in different DSOs remain
+ // distinct. Keep single-binary GUIDs unchanged for compatibility.
+ // Build-ID-qualified GUIDs describe the exact profiled DSOs; applying
+ // such a profile to rebuilt DSOs additionally requires a compiler-side
+ // mechanism capable of reproducing the same module identity.
+ const uint64_t Guid =
+ Module && ProfiledModules.size() > 1
+ ? getModuleGUID(DIFrame.FunctionName, Module->BuildId)
+ : memprof::getGUID(DIFrame.FunctionName);
// 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.
@@ -785,22 +852,27 @@ Error RawMemProfReader::readRawProfile(
return Error::success();
}
+RawMemProfReader::ProfiledModule *
+RawMemProfReader::findModule(const uint64_t VirtualAddress) const {
+ for (const auto &Module : ProfiledModules)
+ if (VirtualAddress > Module->ProfiledTextSegmentStart &&
+ VirtualAddress <= Module->ProfiledTextSegmentEnd)
+ return Module.get();
+ return nullptr;
+}
+
object::SectionedAddress
-RawMemProfReader::getModuleOffset(const uint64_t VirtualAddress) {
- if (VirtualAddress > ProfiledTextSegmentStart &&
- VirtualAddress <= ProfiledTextSegmentEnd) {
- // For PIE binaries, the preferred address is zero and we adjust the virtual
- // address by start of the profiled segment assuming that the offset of the
- // segment in the binary is zero. For non-PIE binaries the preferred and
- // profiled segment addresses should be equal and this is a no-op.
- const uint64_t AdjustedAddress =
- VirtualAddress + PreferredTextSegmentAddress - ProfiledTextSegmentStart;
- return object::SectionedAddress{AdjustedAddress};
- }
- // Addresses which do not originate from the profiled text segment in the
- // binary are not adjusted. These will fail symbolization and be filtered out
- // during processing.
- return object::SectionedAddress{VirtualAddress};
+RawMemProfReader::getModuleOffset(const uint64_t VirtualAddress,
+ const ProfiledModule &Module) const {
+ assert(VirtualAddress > Module.ProfiledTextSegmentStart &&
+ VirtualAddress <= Module.ProfiledTextSegmentEnd);
+ // For position-independent binaries, remove the runtime load bias and add
+ // the executable segment's preferred ELF address. For a non-PIE binary whose
+ // preferred and runtime addresses are equal, this is a no-op.
+ const uint64_t AdjustedAddress = VirtualAddress +
+ Module.PreferredTextSegmentAddress -
+ Module.ProfiledTextSegmentStart;
+ return object::SectionedAddress{AdjustedAddress};
}
Error RawMemProfReader::readNextRecord(
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 1335daaeced6a0..72cb34becbfe73 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -119,10 +119,11 @@ static cl::opt<unsigned> MaxDbgCorrelationWarnings(
cl::desc("The maximum number of warnings to emit when correlating "
"profile from debug info (0 = no limit)"),
cl::sub(MergeSubcommand), cl::sub(ShowSubcommand), cl::init(5));
-static cl::opt<std::string> ProfiledBinary(
- "profiled-binary", cl::init(""),
- cl::desc("Path to binary from which the profile was collected."),
- cl::sub(ShowSubcommand), cl::sub(MergeSubcommand));
+static cl::list<std::string> ProfiledBinaries(
+ "profiled-binary",
+ cl::desc("Path to a binary from which the profile was collected. May be "
+ "specified more than once for MemProf profiles."),
+ cl::ZeroOrMore, cl::sub(ShowSubcommand), cl::sub(MergeSubcommand));
static cl::opt<std::string> DebugInfoFilename(
"debug-info", cl::init(""),
cl::desc(
@@ -755,8 +756,9 @@ static void overlapInput(const std::string &BaseFilename,
/// Load an input into a writer context.
static Error loadInput(
const WeightedFile &Input, SymbolRemapper *Remapper,
- const InstrProfCorrelator *Correlator, const StringRef ProfiledBinary,
- WriterContext *WC, const object::BuildIDFetcher *BIDFetcher = nullptr,
+ const InstrProfCorrelator *Correlator,
+ ArrayRef<std::string> ProfiledBinaries, WriterContext *WC,
+ const object::BuildIDFetcher *BIDFetcher = nullptr,
std::optional<ProfCorrelatorKind> BIDFetcherCorrelatorKind = std::nullopt) {
std::unique_lock<std::mutex> CtxGuard{WC->Lock};
@@ -767,7 +769,8 @@ static Error loadInput(
using ::llvm::memprof::RawMemProfReader;
if (RawMemProfReader::hasFormat(Input.Filename)) {
- auto ReaderOrErr = RawMemProfReader::create(Input.Filename, ProfiledBinary);
+ auto ReaderOrErr =
+ RawMemProfReader::create(Input.Filename, ProfiledBinaries);
if (!ReaderOrErr)
return makeError(ReaderOrErr.takeError(), Input.Filename);
std::unique_ptr<RawMemProfReader> Reader = std::move(ReaderOrErr.get());
@@ -1029,7 +1032,7 @@ static Error writeInstrProfile(StringRef OutputFilename,
static Error mergeInstrProfile(const WeightedFileVector &Inputs,
SymbolRemapper *Remapper,
int MaxDbgCorrelationWarnings,
- const StringRef ProfiledBinary) {
+ ArrayRef<std::string> ProfiledBinaries) {
const uint64_t TraceReservoirSize = TemporalProfTraceReservoirSize.getValue();
const uint64_t MaxTraceLength = TemporalProfMaxTraceLength.getValue();
if (OutputFormat == PF_Compact_Binary)
@@ -1105,7 +1108,8 @@ static Error mergeInstrProfile(const WeightedFileVector &Inputs,
if (NumThreads == 1) {
for (const auto &Input : Inputs)
- if (Error E = loadInput(Input, Remapper, Correlator.get(), ProfiledBinary,
+ if (Error E = loadInput(Input, Remapper, Correlator.get(),
+ ProfiledBinaries,
Contexts[0].get(), BIDFetcher.get(),
BIDFetcherCorrelateKind))
return E;
@@ -1138,7 +1142,7 @@ static Error mergeInstrProfile(const WeightedFileVector &Inputs,
for (const auto &Input : Inputs) {
if (hasFatalError())
break;
- Async(loadInput, Input, Remapper, Correlator.get(), ProfiledBinary,
+ Async(loadInput, Input, Remapper, Correlator.get(), ProfiledBinaries,
Contexts[Ctx].get(), BIDFetcher.get(), BIDFetcherCorrelateKind);
Ctx = (Ctx + 1) % NumThreads;
}
@@ -1566,8 +1570,8 @@ static Error supplementInstrProfile(const WeightedFileVector &Inputs,
SmallSet<instrprof_error, 4> WriterErrorCodes;
auto WC = std::make_unique<WriterContext>(OutputSparse, ErrorLock,
WriterErrorCodes);
- if (Error E = loadInput(Inputs[0], nullptr, nullptr, /*ProfiledBinary=*/"",
- WC.get()))
+ if (Error E = loadInput(Inputs[0], nullptr, nullptr,
+ /*ProfiledBinaries=*/{}, WC.get()))
return E;
if (!WC->Errors.empty())
return makeError(std::move(WC->Errors[0].first), InstrFilename);
@@ -1951,8 +1955,10 @@ static Error merge_main(StringRef ProgName) {
}
if (ProfileKind == instr)
- return mergeInstrProfile(WeightedInputs, Remapper.get(),
- MaxDbgCorrelationWarnings, ProfiledBinary);
+ return mergeInstrProfile(
+ WeightedInputs, Remapper.get(), MaxDbgCorrelationWarnings,
+ SmallVector<std::string>(ProfiledBinaries.begin(),
+ ProfiledBinaries.end()));
return mergeSampleProfile(WeightedInputs, Remapper.get(),
ProfileSymbolListFile, OutputSizeLimit);
@@ -1980,7 +1986,7 @@ static Error overlapInstrProfile(const std::string &BaseFilename,
return Error::success();
}
if (Error E = loadInput(WeightedInput, nullptr, nullptr,
- /*ProfiledBinary=*/"", &Context))
+ /*ProfiledBinaries=*/{}, &Context))
return E;
overlapInput(BaseFilename, TestFilename, &Context, Overlap, FuncFilter, OS,
IsCS);
@@ -3462,7 +3468,10 @@ static Error showMemProfProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
// Show the raw profile in YAML.
if (memprof::RawMemProfReader::hasFormat(Filename)) {
auto ReaderOr = llvm::memprof::RawMemProfReader::create(
- Filename, ProfiledBinary, /*KeepNames=*/true);
+ Filename,
+ SmallVector<std::string>(ProfiledBinaries.begin(),
+ ProfiledBinaries.end()),
+ /*KeepNames=*/true);
if (Error E = ReaderOr.takeError()) {
// Since the error can be related to the profile or the binary we do not
// pass whence. Instead additional context is provided where necessary in
>From 106c145cda9e67a97ce13d63a3ac21e3a16450c7 Mon Sep 17 00:00:00 2001
From: Parmida Vahdatniya <parmida.829667 at huawei.com>
Date: Mon, 31 Aug 2026 09:29:41 -0400
Subject: [PATCH 2/4] [MemProf] Support multiple profiled binaries PR comments
---
.../Inputs/multiple-profiled-dsos-lib1.inc | 10 ---
.../Inputs/multiple-profiled-dsos-lib2.inc | 10 ---
.../TestCases/multiple-profiled-dsos.cpp | 72 ++++++++++++----
llvm/include/llvm/ProfileData/MemProfReader.h | 9 +-
llvm/lib/ProfileData/MemProfReader.cpp | 84 +++++++++++--------
llvm/tools/llvm-profdata/llvm-profdata.cpp | 22 ++---
6 files changed, 122 insertions(+), 85 deletions(-)
delete mode 100644 compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
delete mode 100644 compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
diff --git a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
deleted file mode 100644
index d219bd7ecb6eaf..00000000000000
--- a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib1.inc
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <cstdlib>
-
-extern "C" __attribute__((noinline)) void shared_allocate() {
- volatile char *P = static_cast<char *>(std::malloc(111));
- for (int I = 0; I != 111; ++I)
- P[I] = static_cast<char>(I);
- std::free(const_cast<char *>(P));
-}
-
-extern "C" __attribute__((noinline)) void lib1_entry() { shared_allocate(); }
diff --git a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc b/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
deleted file mode 100644
index 5f2e58499c25d6..00000000000000
--- a/compiler-rt/test/memprof/Inputs/multiple-profiled-dsos-lib2.inc
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <cstdlib>
-
-extern "C" __attribute__((noinline)) void shared_allocate() {
- volatile char *P = static_cast<char *>(std::malloc(222));
- for (int I = 0; I != 222; ++I)
- P[I] = static_cast<char>(I);
- std::free(const_cast<char *>(P));
-}
-
-extern "C" __attribute__((noinline)) void lib2_entry() { shared_allocate(); }
diff --git a/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
index 53388480f38440..42a8af6b11b52e 100644
--- a/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
+++ b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
@@ -2,25 +2,29 @@
// Only the two shared libraries are MemProf-instrumented. -Bsymbolic ensures
// each DSO calls its own externally visible shared_allocate definition.
-// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id -Wl,-Bsymbolic -x c++ %S/../Inputs/multiple-profiled-dsos-lib1.inc -o %t.lib1.so
-// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id -Wl,-Bsymbolic -x c++ %S/../Inputs/multiple-profiled-dsos-lib2.inc -o %t.lib2.so
-// RUN: %clangxx -O1 %s %t.lib1.so %t.lib2.so -Wl,-rpath,%T -o %t.main
+// RUN: split-file %s %t
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id \
+// RUN: -Wl,-Bsymbolic %t/lib1.cpp -o %t.lib1.so
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id \
+// RUN: -Wl,-Bsymbolic %t/lib2.cpp -o %t.lib2.so
+// RUN: %clangxx -O1 %t/main.cpp %t.lib1.so %t.lib2.so -Wl,-rpath,%T \
+// RUN: -o %t.main
// RUN: rm -f %t.raw.*
-// RUN: env MEMPROF_OPTIONS=log_path=%t.raw LD_PRELOAD=%shared_libmemprof %run %t.main
+// RUN: env MEMPROF_OPTIONS=log_path=%t.raw LD_PRELOAD=%shared_libmemprof \
+// RUN: %run %t.main
// RUN: cp %t.lib1.so %t.duplicate.so
-// RUN: not llvm-profdata show --memory %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.duplicate.so -o /dev/null 2>&1 | FileCheck %s --check-prefix=DUPLICATE
-// RUN: llvm-profdata show --memory %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.lib2.so -o %t.yaml
+// RUN: not llvm-profdata show --memory %t.raw.* \
+// RUN: --profiled-binary %t.lib1.so \
+// RUN: --profiled-binary %t.duplicate.so -o /dev/null 2>&1 | \
+// RUN: FileCheck %s --check-prefix=DUPLICATE
+// RUN: llvm-profdata show --memory %t.raw.* \
+// RUN: --profiled-binary %t.lib1.so --profiled-binary %t.lib2.so \
+// RUN: -o %t.yaml
// RUN: FileCheck %s --check-prefix=RAW < %t.yaml
-// RUN: llvm-profdata merge %t.raw.* --profiled-binary %t.lib1.so --profiled-binary %t.lib2.so -o %t.memprofdata
-// RUN: llvm-profdata show --memory %t.memprofdata | FileCheck %s --check-prefix=INDEXED
-
-extern "C" void lib1_entry();
-extern "C" void lib2_entry();
-
-int main() {
- lib1_entry();
- lib2_entry();
-}
+// RUN: llvm-profdata merge %t.raw.* --profiled-binary %t.lib1.so \
+// RUN: --profiled-binary %t.lib2.so -o %t.memprofdata
+// RUN: llvm-profdata show --memory %t.memprofdata | \
+// RUN: FileCheck %s --check-prefix=INDEXED
// The shared_allocate linkage name is identical in both DSOs. Two allocation
// functions proves their module-qualified GUIDs did not collapse into one key.
@@ -36,3 +40,39 @@ int main() {
// INDEXED-DAG: TotalSize: 222
// DUPLICATE: error: Duplicate profiled binary build id:
+
+//--- main.cpp
+
+extern "C" void lib1_entry();
+extern "C" void lib2_entry();
+
+int main() {
+ lib1_entry();
+ lib2_entry();
+}
+
+//--- lib1.cpp
+
+#include <cstdlib>
+
+extern "C" __attribute__((noinline)) void shared_allocate() {
+ volatile char *P = static_cast<char *>(std::malloc(111));
+ for (int I = 0; I != 111; ++I)
+ P[I] = static_cast<char>(I);
+ std::free(const_cast<char *>(P));
+}
+
+extern "C" __attribute__((noinline)) void lib1_entry() { shared_allocate(); }
+
+//--- lib2.cpp
+
+#include <cstdlib>
+
+extern "C" __attribute__((noinline)) void shared_allocate() {
+ volatile char *P = static_cast<char *>(std::malloc(222));
+ for (int I = 0; I != 222; ++I)
+ P[I] = static_cast<char>(I);
+ std::free(const_cast<char *>(P));
+}
+
+extern "C" __attribute__((noinline)) void lib2_entry() { shared_allocate(); }
diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h
index 5ebee3e73b374c..413c261ca3c1e6 100644
--- a/llvm/include/llvm/ProfileData/MemProfReader.h
+++ b/llvm/include/llvm/ProfileData/MemProfReader.h
@@ -129,14 +129,14 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
static Expected<std::unique_ptr<RawMemProfReader>>
create(const Twine &Path, StringRef ProfiledBinary, bool KeepName = false);
static Expected<std::unique_ptr<RawMemProfReader>>
- create(const Twine &Path, ArrayRef<std::string> ProfiledBinaries,
+ create(const Twine &Path, ArrayRef<StringRef> ProfiledBinaries,
bool KeepName = false);
static Expected<std::unique_ptr<RawMemProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer, StringRef ProfiledBinary,
bool KeepName = false);
static Expected<std::unique_ptr<RawMemProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer,
- ArrayRef<std::string> ProfiledBinaries, bool KeepName = false);
+ ArrayRef<StringRef> ProfiledBinaries, bool KeepName = false);
// Returns a list of build ids recorded in the segment information.
static std::vector<std::string> peekBuildIds(MemoryBuffer *DataBuffer);
@@ -186,7 +186,7 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
explicit RawMemProfReader(bool KeepName) : KeepSymbolName(KeepName) {}
// Initializes the RawMemProfReader with the contents in `DataBuffer`.
Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
- ArrayRef<std::string> ProfiledBinaries);
+ ArrayRef<StringRef> ProfiledBinaries);
// Read and parse the contents of the `DataBuffer` as a binary format profile.
Error readRawProfile(std::unique_ptr<MemoryBuffer> DataBuffer);
// Match each profiled module to its executable runtime segment using its
@@ -212,7 +212,8 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
readMemInfoBlocks(const char *Ptr);
- // The binaries which contributed symbolizable frames to the raw profile.
+ // The binaries which contributed symbolizable frames to the raw profile,
+ // sorted by profiled text segment end address after initialization.
llvm::SmallVector<std::unique_ptr<ProfiledModule>, 4> ProfiledModules;
// Version of raw memprof binary currently being read. Defaults to most up
// to date version.
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index 6069f96d31a26a..8312423fe76bf8 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
+#include <algorithm>
#include <cstdint>
#include <memory>
#include <type_traits>
@@ -297,12 +298,12 @@ GlobalValue::GUID getModuleGUID(StringRef FunctionName,
Expected<std::unique_ptr<RawMemProfReader>>
RawMemProfReader::create(const Twine &Path, const StringRef ProfiledBinary,
bool KeepName) {
- std::string BinaryPath = ProfiledBinary.str();
- return create(Path, ArrayRef<std::string>(BinaryPath), KeepName);
+ return create(Path, ArrayRef<StringRef>(ProfiledBinary), KeepName);
}
-Expected<std::unique_ptr<RawMemProfReader>> RawMemProfReader::create(
- const Twine &Path, ArrayRef<std::string> ProfiledBinaries, bool KeepName) {
+Expected<std::unique_ptr<RawMemProfReader>>
+RawMemProfReader::create(const Twine &Path,
+ ArrayRef<StringRef> ProfiledBinaries, bool KeepName) {
auto BufferOr = MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = BufferOr.getError())
return report(errorCodeToError(EC), Path.getSingleStringRef());
@@ -314,20 +315,19 @@ Expected<std::unique_ptr<RawMemProfReader>> RawMemProfReader::create(
Expected<std::unique_ptr<RawMemProfReader>>
RawMemProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
const StringRef ProfiledBinary, bool KeepName) {
- std::string BinaryPath = ProfiledBinary.str();
- return create(std::move(Buffer), ArrayRef<std::string>(BinaryPath), KeepName);
+ return create(std::move(Buffer), ArrayRef<StringRef>(ProfiledBinary),
+ KeepName);
}
Expected<std::unique_ptr<RawMemProfReader>>
RawMemProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
- ArrayRef<std::string> ProfiledBinaries,
- bool KeepName) {
+ ArrayRef<StringRef> ProfiledBinaries, bool KeepName) {
if (Error E = checkBuffer(*Buffer))
return report(std::move(E), Buffer->getBufferIdentifier());
if (ProfiledBinaries.empty() ||
llvm::all_of(ProfiledBinaries,
- [](const std::string &Path) { return Path.empty(); })) {
+ [](StringRef Path) { return Path.empty(); })) {
// Peek the build ids to print a helpful error message.
const std::vector<std::string> BuildIds = peekBuildIds(Buffer.get());
std::string ErrorMessage(
@@ -420,11 +420,11 @@ void RawMemProfReader::printYAML(raw_ostream &OS) {
}
Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
- ArrayRef<std::string> ProfiledBinaries) {
+ ArrayRef<StringRef> ProfiledBinaries) {
// Open each profiled binary and perform the ELF and executable-segment sanity
// checks needed to translate runtime PCs for symbolization.
StringSet<> SeenBuildIds;
- for (const std::string &BinaryPath : ProfiledBinaries) {
+ for (StringRef BinaryPath : ProfiledBinaries) {
if (BinaryPath.empty())
continue;
@@ -438,19 +438,22 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
auto *ElfObject =
dyn_cast<object::ELFObjectFileBase>(Module->Binary.getBinary());
if (!ElfObject)
- return report(
- make_error<StringError>("Not an ELF file", inconvertibleErrorCode()),
- FileName);
-
- auto *Elf64LEObject = dyn_cast<object::ELF64LEObjectFile>(ElfObject);
- if (!Elf64LEObject)
- return report(make_error<StringError>("Not a 64-bit little-endian ELF",
+ return report(make_error<StringError>(Twine("Not an ELF file: "),
inconvertibleErrorCode()),
FileName);
+ auto *Elf64LEObject = dyn_cast<object::ELF64LEObjectFile>(ElfObject);
+ if (!Elf64LEObject)
+ return report(
+ make_error<StringError>(Twine("Not a 64-bit little-endian ELF"),
+ inconvertibleErrorCode()),
+ FileName);
const object::ELF64LEFile &ElfFile = Elf64LEObject->getELFFile();
auto PHdrsOr = ElfFile.program_headers();
if (!PHdrsOr)
- return report(PHdrsOr.takeError(), FileName);
+ return report(
+ make_error<StringError>(Twine("Could not read program headers: "),
+ inconvertibleErrorCode()),
+ FileName);
// Check whether the profiled binary was built with position independent
// code (PIC). Perform sanity checks for assumptions we rely on to simplify
@@ -474,7 +477,7 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
"Expect p_vaddr to always be page aligned");
}
if (NumExecutableSegments == 0)
- return report(make_error<StringError>("No executable load segment",
+ return report(make_error<StringError>(Twine("No executable load segment"),
inconvertibleErrorCode()),
FileName);
@@ -488,7 +491,7 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
auto *Object = cast<object::ObjectFile>(Module->Binary.getBinary());
object::BuildIDRef BinaryId = object::getBuildID(Object);
if (BinaryId.empty())
- return make_error<StringError>(Twine("No build id found in binary ") +
+ return make_error<StringError>(Twine("No build ID found in binary ") +
FileName,
inconvertibleErrorCode());
std::string BuildIdString = toHex(BinaryId, /*LowerCase=*/true);
@@ -514,12 +517,12 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
auto *Object = cast<object::ObjectFile>(Module->Binary.getBinary());
std::unique_ptr<DIContext> Context = DWARFContext::create(
*Object, DWARFContext::ProcessDebugRelocations::Process);
- auto SymbolizerOr = symbolize::SymbolizableObjectFile::create(
+ auto SOFOr = symbolize::SymbolizableObjectFile::create(
Object, std::move(Context), /*UntagAddresses=*/false);
- if (!SymbolizerOr)
- return report(SymbolizerOr.takeError(),
+ if (!SOFOr)
+ return report(SOFOr.takeError(),
Module->Binary.getBinary()->getFileName());
- Module->Symbolizer = std::move(SymbolizerOr.get());
+ Module->Symbolizer = std::move(SOFOr.get());
}
if (Error E = symbolizeAndFilterStackFrames())
@@ -552,6 +555,11 @@ Error RawMemProfReader::setupForSymbolization() {
Twine("No matching executable segments found in binary ") + FileName,
inconvertibleErrorCode());
}
+
+ std::sort(ProfiledModules.begin(), ProfiledModules.end(),
+ [](const auto &LHS, const auto &RHS) {
+ return LHS->ProfiledTextSegmentEnd < RHS->ProfiledTextSegmentEnd;
+ });
return Error::success();
}
@@ -681,7 +689,7 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
for (size_t I = 0, NumFrames = DI.getNumberOfFrames(); I < NumFrames;
I++) {
const auto &DIFrame = DI.getFrame(I);
- // A PC identifies its module, but the normal MemProf GUID only hashes
+ // The PC identified its module, but the normal MemProf GUID only hashes
// the linkage name. Qualify it with the build ID when multiple binaries
// are supplied so equal external symbols in different DSOs remain
// distinct. Keep single-binary GUIDs unchanged for compatibility.
@@ -854,11 +862,15 @@ Error RawMemProfReader::readRawProfile(
RawMemProfReader::ProfiledModule *
RawMemProfReader::findModule(const uint64_t VirtualAddress) const {
- for (const auto &Module : ProfiledModules)
- if (VirtualAddress > Module->ProfiledTextSegmentStart &&
- VirtualAddress <= Module->ProfiledTextSegmentEnd)
- return Module.get();
- return nullptr;
+ auto It = std::lower_bound(ProfiledModules.begin(), ProfiledModules.end(),
+ VirtualAddress,
+ [](const auto &Module, uint64_t Address) {
+ return Module->ProfiledTextSegmentEnd < Address;
+ });
+ if (It == ProfiledModules.end() ||
+ VirtualAddress <= (*It)->ProfiledTextSegmentStart)
+ return nullptr;
+ return It->get();
}
object::SectionedAddress
@@ -866,12 +878,16 @@ RawMemProfReader::getModuleOffset(const uint64_t VirtualAddress,
const ProfiledModule &Module) const {
assert(VirtualAddress > Module.ProfiledTextSegmentStart &&
VirtualAddress <= Module.ProfiledTextSegmentEnd);
- // For position-independent binaries, remove the runtime load bias and add
- // the executable segment's preferred ELF address. For a non-PIE binary whose
- // preferred and runtime addresses are equal, this is a no-op.
+ // For PIE binaries, the preferred address is zero and we adjust the virtual
+ // address by start of the profiled segment assuming that the offset of the
+ // segment in the binary is zero. For non-PIE binaries the preferred and
+ // profiled segment addresses should be equal and this is a no-op.
const uint64_t AdjustedAddress = VirtualAddress +
Module.PreferredTextSegmentAddress -
Module.ProfiledTextSegmentStart;
+ // Addresses which do not originate from the profiled text segment in the
+ // binary are not adjusted. These will fail symbolization and be filtered out
+ // during processing.
return object::SectionedAddress{AdjustedAddress};
}
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 72cb34becbfe73..5550bc302e7888 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -757,7 +757,7 @@ static void overlapInput(const std::string &BaseFilename,
static Error loadInput(
const WeightedFile &Input, SymbolRemapper *Remapper,
const InstrProfCorrelator *Correlator,
- ArrayRef<std::string> ProfiledBinaries, WriterContext *WC,
+ ArrayRef<StringRef> ProfiledBinaries, WriterContext *WC,
const object::BuildIDFetcher *BIDFetcher = nullptr,
std::optional<ProfCorrelatorKind> BIDFetcherCorrelatorKind = std::nullopt) {
std::unique_lock<std::mutex> CtxGuard{WC->Lock};
@@ -1032,7 +1032,7 @@ static Error writeInstrProfile(StringRef OutputFilename,
static Error mergeInstrProfile(const WeightedFileVector &Inputs,
SymbolRemapper *Remapper,
int MaxDbgCorrelationWarnings,
- ArrayRef<std::string> ProfiledBinaries) {
+ ArrayRef<StringRef> ProfiledBinaries) {
const uint64_t TraceReservoirSize = TemporalProfTraceReservoirSize.getValue();
const uint64_t MaxTraceLength = TemporalProfMaxTraceLength.getValue();
if (OutputFormat == PF_Compact_Binary)
@@ -1954,11 +1954,12 @@ static Error merge_main(StringRef ProgName) {
ZeroCounterThreshold, InstrProfColdThreshold);
}
- if (ProfileKind == instr)
- return mergeInstrProfile(
- WeightedInputs, Remapper.get(), MaxDbgCorrelationWarnings,
- SmallVector<std::string>(ProfiledBinaries.begin(),
- ProfiledBinaries.end()));
+ if (ProfileKind == instr) {
+ SmallVector<StringRef> ProfiledBinaryRefs(ProfiledBinaries.begin(),
+ ProfiledBinaries.end());
+ return mergeInstrProfile(WeightedInputs, Remapper.get(),
+ MaxDbgCorrelationWarnings, ProfiledBinaryRefs);
+ }
return mergeSampleProfile(WeightedInputs, Remapper.get(),
ProfileSymbolListFile, OutputSizeLimit);
@@ -3467,11 +3468,10 @@ static Error showMemProfProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
// Show the raw profile in YAML.
if (memprof::RawMemProfReader::hasFormat(Filename)) {
+ SmallVector<StringRef> ProfiledBinaryRefs(ProfiledBinaries.begin(),
+ ProfiledBinaries.end());
auto ReaderOr = llvm::memprof::RawMemProfReader::create(
- Filename,
- SmallVector<std::string>(ProfiledBinaries.begin(),
- ProfiledBinaries.end()),
- /*KeepNames=*/true);
+ Filename, ProfiledBinaryRefs, /*KeepNames=*/true);
if (Error E = ReaderOr.takeError()) {
// Since the error can be related to the profile or the binary we do not
// pass whence. Instead additional context is provided where necessary in
>From 66674d82da5dd2472d6c5a02b017741aa33fc6f4 Mon Sep 17 00:00:00 2001
From: Parmida Vahdatniya <parmida.829667 at huawei.com>
Date: Tue, 1 Sep 2026 09:11:39 -0400
Subject: [PATCH 3/4] [MemProf] Support multiple profiled binaries PR comments
---
llvm/lib/ProfileData/MemProfReader.cpp | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index 8312423fe76bf8..e858d35b21295a 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -511,8 +511,8 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
return E;
// Create a symbolizer for every supplied module after its build ID has been
- // matched to the raw profile. Each ProfiledModule owns both the binary and
- // symbolizer so their backing data remains live during frame processing.
+ // matched to the raw profile. The module keeps the binary and symbolizer
+ // alive throughout frame processing.
for (auto &Module : ProfiledModules) {
auto *Object = cast<object::ObjectFile>(Module->Binary.getBinary());
std::unique_ptr<DIContext> Context = DWARFContext::create(
@@ -528,6 +528,12 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
if (Error E = symbolizeAndFilterStackFrames())
return E;
+ // Symbolization is complete, so release the symbolizers and their DWARF
+ // contexts before mapping the raw profile into records. This reduces peak
+ // memory usage because these objects are no longer needed.
+ for (auto &Module : ProfiledModules)
+ Module->Symbolizer.reset();
+
return mapRawProfileToRecords();
}
>From cb0cd014689ec1e87e0a451c409f67a55519e5f1 Mon Sep 17 00:00:00 2001
From: Parmida Vahdatniya <parmida.829667 at huawei.com>
Date: Fri, 2 Oct 2026 09:44:42 -0400
Subject: [PATCH 4/4] [MemProf] Add stable module IDs for shared libraries
---
clang/lib/CodeGen/CodeGenModule.cpp | 38 ++++
clang/test/CodeGen/memprof-tu-id-note.cpp | 27 +++
.../TestCases/multiple-profiled-dsos.cpp | 8 +-
lld/ELF/Config.h | 3 +
lld/ELF/Driver.cpp | 10 ++
lld/ELF/LTO.cpp | 18 +-
lld/ELF/LTO.h | 6 +
lld/ELF/Options.td | 2 +
lld/ELF/SyntheticSections.cpp | 165 ++++++++++++++++++
lld/ELF/SyntheticSections.h | 17 ++
lld/test/ELF/lto/memprof-module-id.ll | 77 ++++++++
lld/test/ELF/memprof-module-id.s | 62 +++++++
llvm/include/llvm/BinaryFormat/ELF.h | 7 +
llvm/include/llvm/LTO/Config.h | 7 +
llvm/include/llvm/ProfileData/MemProf.h | 6 +
llvm/include/llvm/ProfileData/MemProfReader.h | 5 +-
.../Transforms/Instrumentation/MemProfUse.h | 8 +-
llvm/lib/LTO/LTO.cpp | 9 +
llvm/lib/LTO/LTOBackend.cpp | 13 ++
llvm/lib/ProfileData/MemProf.cpp | 12 ++
llvm/lib/ProfileData/MemProfReader.cpp | 69 ++++++--
.../Transforms/Instrumentation/MemProfUse.cpp | 66 +++++--
.../memprof-dump-matched-alloc-site.ll | 3 +-
.../Instrumentation/MemProfUseTest.cpp | 22 +++
24 files changed, 613 insertions(+), 47 deletions(-)
create mode 100644 clang/test/CodeGen/memprof-tu-id-note.cpp
create mode 100644 lld/test/ELF/lto/memprof-module-id.ll
create mode 100644 lld/test/ELF/memprof-module-id.s
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index eec4d9e714abe6..a7d5789b0b04bf 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -73,6 +73,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MD5.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TargetParser/AArch64TargetParser.h"
@@ -83,6 +84,7 @@
#include "llvm/Transforms/Utils/BuildLibCalls.h"
#include "llvm/Transforms/Utils/KCFIHash.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
+#include <array>
#include <optional>
#include <set>
@@ -96,6 +98,41 @@ static llvm::cl::opt<bool> LimitedCoverage(
static const char AnnotationSection[] = "llvm.metadata";
static constexpr auto ErrnoTBAAMDName = "llvm.errno.tbaa";
+static void emitMemProfTUIDNote(CodeGenModule &CGM) {
+ if (!CGM.getTriple().isOSBinFormatELF())
+ return;
+
+ const SourceManager &SM = CGM.getContext().getSourceManager();
+ OptionalFileEntryRef MainFile = SM.getFileEntryRefForID(SM.getMainFileID());
+ if (!MainFile)
+ return;
+
+ StringRef FileName = MainFile->getName();
+ llvm::MD5::MD5Result TUID =
+ llvm::MD5::hash(llvm::arrayRefFromStringRef(FileName));
+
+ llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+ constexpr std::array<uint8_t, 8> NoteNameBytes = {'L', 'L', 'V', 'M',
+ 0, 0, 0, 0};
+ llvm::Constant *NoteName =
+ llvm::ConstantDataArray::get(Ctx, ArrayRef<uint8_t>(NoteNameBytes));
+ llvm::Constant *NoteDesc =
+ llvm::ConstantDataArray::get(Ctx, ArrayRef<uint8_t>(TUID));
+ llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx);
+ llvm::Constant *Note = llvm::ConstantStruct::getAnon(
+ Ctx, {llvm::ConstantInt::get(Int32Ty, 5),
+ llvm::ConstantInt::get(Int32Ty, TUID.size()),
+ llvm::ConstantInt::get(Int32Ty, llvm::ELF::NT_LLVM_MEMPROF_TU_ID),
+ NoteName, NoteDesc});
+
+ auto *GV = new llvm::GlobalVariable(
+ CGM.getModule(), Note->getType(), /*isConstant=*/true,
+ llvm::GlobalValue::PrivateLinkage, Note, "__llvm_memprof_tu_id");
+ GV->setSection(".note.llvm.memprof.tu-id");
+ GV->setAlignment(llvm::Align(4));
+ llvm::appendToCompilerUsed(CGM.getModule(), {GV});
+}
+
static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
switch (CGM.getContext().getCXXABIKind()) {
case TargetCXXABI::AppleARM64:
@@ -1817,6 +1854,7 @@ void CodeGenModule::Release() {
getModule().addModuleFlag(
llvm::Module::Error, "MemProfProfileFilename",
llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
+ emitMemProfTUIDNote(*this);
}
if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
diff --git a/clang/test/CodeGen/memprof-tu-id-note.cpp b/clang/test/CodeGen/memprof-tu-id-note.cpp
new file mode 100644
index 00000000000000..7c03fb2a4b7ec4
--- /dev/null
+++ b/clang/test/CodeGen/memprof-tu-id-note.cpp
@@ -0,0 +1,27 @@
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fmemory-profile \
+// RUN: -emit-obj %t/a.cpp -o %t/a.o
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fmemory-profile \
+// RUN: -emit-obj %t/b.cpp -o %t/b.o
+// RUN: llvm-readelf --notes %t/a.o | FileCheck %s --check-prefix=NOTE
+// RUN: llvm-readelf --notes %t/b.o | FileCheck %s --check-prefix=NOTE
+// RUN: llvm-objcopy --dump-section \
+// RUN: .note.llvm.memprof.tu-id=%t/a.note %t/a.o
+// RUN: llvm-objcopy --dump-section \
+// RUN: .note.llvm.memprof.tu-id=%t/b.note %t/b.o
+// RUN: not cmp %t/a.note %t/b.note
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj \
+// RUN: %t/a.cpp -o %t/no-memprof.o
+// RUN: llvm-readelf --sections %t/no-memprof.o | \
+// RUN: FileCheck %s --check-prefix=NO-NOTE
+
+// NOTE: Displaying notes found in: .note.llvm.memprof.tu-id
+// NOTE: LLVM
+// NOTE-SAME: 0x00000010
+// NO-NOTE-NOT: .note.llvm.memprof.tu-id
+
+//--- a.cpp
+int a() { return 1; }
+
+//--- b.cpp
+int b() { return 2; }
diff --git a/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
index 42a8af6b11b52e..cd5648470b8db0 100644
--- a/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
+++ b/compiler-rt/test/memprof/TestCases/multiple-profiled-dsos.cpp
@@ -3,10 +3,10 @@
// Only the two shared libraries are MemProf-instrumented. -Bsymbolic ensures
// each DSO calls its own externally visible shared_allocate definition.
// RUN: split-file %s %t
-// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id \
-// RUN: -Wl,-Bsymbolic %t/lib1.cpp -o %t.lib1.so
-// RUN: %clangxx_memprof -O1 -g -fPIC -shared -Wl,--build-id \
-// RUN: -Wl,-Bsymbolic %t/lib2.cpp -o %t.lib2.so
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -fuse-ld=lld -Wl,--build-id \
+// RUN: -Wl,-z,separate-code -Wl,-Bsymbolic %t/lib1.cpp -o %t.lib1.so
+// RUN: %clangxx_memprof -O1 -g -fPIC -shared -fuse-ld=lld -Wl,--build-id \
+// RUN: -Wl,-z,separate-code -Wl,-Bsymbolic %t/lib2.cpp -o %t.lib2.so
// RUN: %clangxx -O1 %t/main.cpp %t.lib1.so %t.lib2.so -Wl,-rpath,%T \
// RUN: -o %t.main
// RUN: rm -f %t.raw.*
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 0c76e3854e6608..e97c8924d749d3 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -30,6 +30,7 @@
#include "llvm/Support/GlobPattern.h"
#include "llvm/Support/LibraryOptionsContext.h"
#include "llvm/Support/TarWriter.h"
+#include <array>
#include <atomic>
#include <memory>
#include <mutex>
@@ -211,6 +212,7 @@ class LinkerDriver {
void addFile(StringRef path, bool withLOption);
void addFile(std::unique_ptr<ELFFileBase> ef);
void addLibrary(StringRef name);
+ const std::optional<std::array<uint8_t, 16>> *getMemProfModuleId() const;
private:
Ctx &ctx;
@@ -262,6 +264,7 @@ struct Config {
llvm::StringRef init;
llvm::StringRef ltoAAPipeline;
llvm::StringRef ltoCSProfileFile;
+ llvm::StringRef ltoMemProfProfile;
llvm::StringRef ltoNewPmPasses;
llvm::StringRef ltoObjPath;
llvm::StringRef ltoSampleProfile;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index a08f6f2d384bc1..d3ceac9841c781 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -642,6 +642,11 @@ constexpr const char *saveTempsValues[] = {
LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {}
+const std::optional<std::array<uint8_t, 16>> *
+LinkerDriver::getMemProfModuleId() const {
+ return lto ? <o->getMemProfModuleId() : nullptr;
+}
+
void LinkerDriver::waitForLTOCleanup() {
if (lto)
lto->waitForLTOCleanup();
@@ -694,6 +699,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
if (!ltoSampleProfile.empty())
readFile(ctx, ltoSampleProfile);
+ StringRef ltoMemProfProfile =
+ args.getLastArgValue(OPT_lto_memprof_profile);
+ if (!ltoMemProfProfile.empty())
+ readFile(ctx, ltoMemProfProfile);
} else {
ErrAlways(ctx) << "--reproduce: " << errOrWriter.takeError();
}
@@ -1513,6 +1522,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
ErrAlways(ctx) << "invalid codegen optimization level for LTO: " << ltoCgo;
ctx.arg.ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq);
ctx.arg.ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
+ ctx.arg.ltoMemProfProfile = args.getLastArgValue(OPT_lto_memprof_profile);
ctx.arg.ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
ctx.arg.ltoBBAddrMap =
args.hasFlag(OPT_lto_basic_block_address_map,
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 0fa5673ed0f25b..e73842e105876b 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -11,6 +11,7 @@
#include "InputFiles.h"
#include "SymbolTable.h"
#include "Symbols.h"
+#include "SyntheticSections.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Filesystem.h"
#include "lld/Common/Strings.h"
@@ -53,7 +54,9 @@ static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) {
ctx.arg.thinLTOPrefixReplaceNew);
}
-static lto::Config createConfig(Ctx &ctx) {
+static lto::Config
+createConfig(Ctx &ctx,
+ const std::optional<std::array<uint8_t, 16>> &memProfModuleId) {
lto::Config c;
c.LibraryOptions = &ctx.arg.libraryOptions;
c.PTO = PipelineTuningOptions(c.LibraryOptions);
@@ -135,6 +138,8 @@ static lto::Config createConfig(Ctx &ctx) {
c.StatsFile = std::string(ctx.arg.optStatsFilename);
c.SampleProfile = std::string(ctx.arg.ltoSampleProfile);
+ c.MemoryProfile = std::string(ctx.arg.ltoMemProfProfile);
+ c.MemProfModuleId = memProfModuleId;
for (StringRef pluginFn : ctx.arg.passPlugins)
c.PassPluginFilenames.push_back(std::string(pluginFn));
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
@@ -178,7 +183,8 @@ static lto::Config createConfig(Ctx &ctx) {
return c;
}
-BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
+BitcodeCompiler::BitcodeCompiler(Ctx &ctx)
+ : ctx(ctx), memProfModuleId(computeMemProfModuleId(ctx)) {
// Initialize indexFile.
if (!ctx.arg.thinLTOIndexOnlyArg.empty())
indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);
@@ -206,13 +212,13 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
llvm::lto::LTO::LTOKind::LTOK_Default};
if (ctx.arg.dtltoDistributor.empty())
- ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend,
- ctx.arg.ltoPartitions,
+ ltoObj = std::make_unique<lto::LTO>(createConfig(ctx, memProfModuleId),
+ backend, ctx.arg.ltoPartitions,
ltoModes[ctx.arg.ltoKind]);
else
ltoObj = std::make_unique<lto::DTLTO>(
- createConfig(ctx), ctx.arg.ltoPartitions, ltoModes[ctx.arg.ltoKind],
- onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
+ createConfig(ctx, memProfModuleId), ctx.arg.ltoPartitions,
+ ltoModes[ctx.arg.ltoKind], onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
ctx.arg.thinLTOEmitImportsFiles, ctx.arg.outputFile,
ctx.arg.dtltoDistributor, ctx.arg.dtltoDistributorArgs,
ctx.arg.dtltoCompiler, ctx.arg.dtltoCompilerPrependArgs,
diff --git a/lld/ELF/LTO.h b/lld/ELF/LTO.h
index fdbeb32030eac7..5f48579eaf6bce 100644
--- a/lld/ELF/LTO.h
+++ b/lld/ELF/LTO.h
@@ -24,7 +24,9 @@
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
+#include <array>
#include <memory>
+#include <optional>
#include <vector>
namespace llvm::lto {
@@ -45,6 +47,9 @@ class BitcodeCompiler {
SmallVector<std::unique_ptr<InputFile>, 0> compile();
void setBitcodeLibFuncs(ArrayRef<StringRef> bitcodeLibFuncs);
void waitForLTOCleanup();
+ const std::optional<std::array<uint8_t, 16>> &getMemProfModuleId() const {
+ return memProfModuleId;
+ }
private:
Ctx &ctx;
@@ -56,6 +61,7 @@ class BitcodeCompiler {
llvm::DenseSet<StringRef> usedStartStop;
std::unique_ptr<llvm::raw_fd_ostream> indexFile;
llvm::DenseSet<StringRef> thinIndices;
+ std::optional<std::array<uint8_t, 16>> memProfModuleId;
};
} // namespace lld::elf
diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index 04aac1849a47e0..3db42bd4932f7a 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -664,6 +664,8 @@ defm lto_known_safe_vtables : EEq<"lto-known-safe-vtables",
def lto_obj_path_eq: JJ<"lto-obj-path=">;
def lto_sample_profile: JJ<"lto-sample-profile=">,
HelpText<"Sample profile file path">;
+def lto_memprof_profile: JJ<"lto-memprof-profile=">,
+ HelpText<"Memory profile file path">;
defm lto_validate_all_vtables_have_type_infos: BB<"lto-validate-all-vtables-have-type-infos",
"Validate that all vtables have type infos for LTO link",
"Do not validate that all vtables have type infos for LTO link">;
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 22c9b396faf574..1fc34e39c242f9 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -34,9 +34,11 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
+#include "llvm/LTO/LTO.h"
#include "llvm/Support/DJB.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/LEB128.h"
+#include "llvm/Support/MD5.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/TimeProfiler.h"
#include <cinttypes>
@@ -217,6 +219,22 @@ void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {
memcpy(hashBuf, buf.data(), hashSize);
}
+MemProfModuleIdSection::MemProfModuleIdSection(Ctx &ctx, ArrayRef<uint8_t> id)
+ : SyntheticSection(ctx, ".note.llvm.memprof.module-id", SHT_NOTE, SHF_ALLOC,
+ 4) {
+ assert(id.size() == moduleId.size());
+ llvm::copy(id, moduleId.begin());
+}
+
+void MemProfModuleIdSection::writeTo(uint8_t *buf) {
+ write32(ctx, buf, 5); // Name size
+ write32(ctx, buf + 4, moduleId.size()); // Content size
+ write32(ctx, buf + 8, NT_LLVM_MEMPROF_MODULE_ID); // Type
+ memcpy(buf + 12, "LLVM", 5); // Name string
+ memset(buf + 17, 0, 3); // Name padding
+ memcpy(buf + 20, moduleId.data(), moduleId.size());
+}
+
BssSection::BssSection(Ctx &ctx, StringRef name, uint64_t size,
uint32_t alignment)
: SyntheticSection(ctx, name, SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
@@ -4472,6 +4490,138 @@ static OutputSection *findSection(Ctx &ctx, StringRef name) {
return nullptr;
}
+static void collectMemProfTUIDs(Ctx &ctx, ArrayRef<InputSectionBase *> sections,
+ SmallVectorImpl<std::array<uint8_t, 16>> &ids) {
+ for (InputSectionBase *sec : sections) {
+ if (!sec || sec == &InputSection::discarded ||
+ sec->name != ".note.llvm.memprof.tu-id")
+ continue;
+
+ if (sec->addralign != 4) {
+ Err(ctx) << sec << ": MemProf TU ID note alignment must be 4";
+ continue;
+ }
+
+ ArrayRef<uint8_t> data = sec->content();
+ while (!data.empty()) {
+ if (data.size() < 12) {
+ Err(ctx) << sec << ": MemProf TU ID note header is too short";
+ break;
+ }
+
+ uint32_t nameSize = read32(ctx, data.data());
+ uint32_t descSize = read32(ctx, data.data() + 4);
+ uint32_t type = read32(ctx, data.data() + 8);
+ size_t descOffset = alignTo(12 + nameSize, sec->addralign);
+ size_t size = descOffset + alignTo(descSize, sec->addralign);
+ if (size > data.size()) {
+ Err(ctx) << sec << ": MemProf TU ID note is too short";
+ break;
+ }
+
+ StringRef name(reinterpret_cast<const char *>(data.data() + 12),
+ nameSize ? nameSize - 1 : 0);
+ if (name != "LLVM" || type != NT_LLVM_MEMPROF_TU_ID || descSize != 16) {
+ Err(ctx) << sec << ": invalid MemProf TU ID note";
+ break;
+ }
+
+ std::array<uint8_t, 16> id;
+ llvm::copy(data.slice(descOffset, descSize), id.begin());
+ ids.push_back(id);
+ data = data.slice(size);
+ }
+ }
+}
+
+std::optional<std::array<uint8_t, 16>> elf::computeMemProfModuleId(Ctx &ctx) {
+ if (!ctx.arg.shared || ctx.bitcodeFiles.empty())
+ return std::nullopt;
+
+ SmallVector<std::array<uint8_t, 16>, 0> ids;
+ for (ELFFileBase *file : ctx.objectFiles)
+ collectMemProfTUIDs(ctx, file->getSections(), ids);
+
+ for (BitcodeFile *file : ctx.bitcodeFiles) {
+ llvm::lto::InputFile &obj = *file->obj;
+ bool hasMemProfTUID =
+ !ctx.arg.ltoMemProfProfile.empty() ||
+ llvm::any_of(obj.symbols(), [](const auto &sym) {
+ return sym.getIRName() == "__memprof_profile_filename";
+ });
+ if (!hasMemProfTUID)
+ continue;
+ ids.push_back(MD5::hash(arrayRefFromStringRef(obj.getSourceFileName())));
+ }
+
+ if (ids.empty())
+ return std::nullopt;
+
+ llvm::sort(ids);
+ MD5 hash;
+ for (const std::array<uint8_t, 16> &id : ids)
+ hash.update(ArrayRef<uint8_t>(id));
+ return hash.final();
+}
+
+template <class ELFT>
+static std::optional<MD5::MD5Result> collectMemProfTUIDs(Ctx &ctx) {
+ using Elf_Nhdr = typename ELFT::Nhdr;
+ using Elf_Note = typename ELFT::Note;
+
+ SmallVector<std::array<uint8_t, 16>, 0> ids;
+ for (InputSectionBase *sec : ctx.inputSections) {
+ if (!sec->isLive() || sec->name != ".note.llvm.memprof.tu-id")
+ continue;
+
+ if (sec->addralign != 4) {
+ Err(ctx) << sec << ": MemProf TU ID note alignment must be 4";
+ sec->markDead();
+ continue;
+ }
+
+ ArrayRef<uint8_t> data = sec->content();
+ while (!data.empty()) {
+ if (data.size() < sizeof(Elf_Nhdr)) {
+ Err(ctx) << sec << ": MemProf TU ID note header is too short";
+ break;
+ }
+
+ auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
+ size_t size = nhdr->getSize(sec->addralign);
+ if (size > data.size()) {
+ Err(ctx) << sec << ": MemProf TU ID note is too short";
+ break;
+ }
+
+ Elf_Note note(*nhdr);
+ ArrayRef<uint8_t> desc = note.getDesc(sec->addralign);
+ if (note.getName() != "LLVM" || note.getType() != NT_LLVM_MEMPROF_TU_ID ||
+ desc.size() != 16) {
+ Err(ctx) << sec << ": invalid MemProf TU ID note";
+ break;
+ }
+
+ std::array<uint8_t, 16> id;
+ llvm::copy(desc, id.begin());
+ ids.push_back(id);
+ data = data.slice(size);
+ }
+
+ // Replace all input TU ID notes with one module ID note.
+ sec->markDead();
+ }
+
+ if (ids.empty())
+ return std::nullopt;
+
+ llvm::sort(ids);
+ MD5 hash;
+ for (const std::array<uint8_t, 16> &id : ids)
+ hash.update(ArrayRef<uint8_t>(id));
+ return hash.final();
+}
+
template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
// Add the .interp section first because it is not a SyntheticSection.
// The removeUnusedSyntheticSections() function relies on the
@@ -4484,6 +4634,21 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
auto add = [&](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };
+ if (ctx.arg.shared) {
+ std::optional<MD5::MD5Result> collectedId = collectMemProfTUIDs<ELFT>(ctx);
+ std::optional<std::array<uint8_t, 16>> id;
+ if (const auto *ltoId = ctx.driver.getMemProfModuleId())
+ id = *ltoId;
+ if (!id && collectedId) {
+ id.emplace();
+ llvm::copy(*collectedId, id->begin());
+ }
+ if (id) {
+ auto *sec = make<MemProfModuleIdSection>(ctx, *id);
+ add(*sec);
+ }
+ }
+
if (ctx.arg.zSectionHeader)
ctx.in.shStrTab =
std::make_unique<StringTableSection>(ctx, ".shstrtab", false);
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index d79a9ac07e908e..0ee032e1ba5690 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -35,12 +35,17 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Threading.h"
+#include <array>
namespace lld::elf {
class Defined;
struct PhdrEntry;
class SymbolTableBaseSection;
+// Compute the stable identifier for a MemProf-instrumented shared object
+// before LTO optimization starts.
+std::optional<std::array<uint8_t, 16>> computeMemProfModuleId(Ctx &ctx);
+
// Section for .eh_frame.
class EhFrameSection final : public SyntheticSection {
public:
@@ -163,6 +168,18 @@ class BuildIdSection : public SyntheticSection {
uint8_t *hashBuf;
};
+// The identifier derived from the translation units linked into a
+// MemProf-instrumented shared object.
+class MemProfModuleIdSection final : public SyntheticSection {
+public:
+ MemProfModuleIdSection(Ctx &, llvm::ArrayRef<uint8_t> moduleId);
+ void writeTo(uint8_t *buf) override;
+ size_t getSize() const override { return 36; }
+
+private:
+ std::array<uint8_t, 16> moduleId;
+};
+
// BssSection is used to reserve space for copy relocations and common symbols.
// We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
// that are used for writable symbols, read-only symbols and common symbols,
diff --git a/lld/test/ELF/lto/memprof-module-id.ll b/lld/test/ELF/lto/memprof-module-id.ll
new file mode 100644
index 00000000000000..db8da4209f425e
--- /dev/null
+++ b/lld/test/ELF/lto/memprof-module-id.ll
@@ -0,0 +1,77 @@
+; REQUIRES: x86
+
+; RUN: split-file %s %t
+; RUN: llvm-profdata merge %t/memprof.yaml -o %t/memprof.profdata
+
+; RUN: llvm-as %t/a.ll -o %t/a.bc
+; RUN: llvm-as %t/b.ll -o %t/b.bc
+; RUN: ld.lld -shared --save-temps --lto-debug-pass-manager \
+; RUN: --lto-memprof-profile=%t/memprof.profdata %t/a.bc %t/b.bc \
+; RUN: -o %t/full.so 2>&1 | FileCheck %s --check-prefix=USE
+; RUN: llvm-dis %t/full.so.0.0.preopt.bc -o - | FileCheck %s
+; RUN: llvm-readelf --notes %t/full.so | FileCheck %s --check-prefix=NOTE
+
+; RUN: opt -module-summary %t/a.ll -o %t/a.thin.bc
+; RUN: opt -module-summary %t/b.ll -o %t/b.thin.bc
+; RUN: ld.lld -shared --save-temps --lto-debug-pass-manager \
+; RUN: --lto-memprof-profile=%t/memprof.profdata %t/a.thin.bc %t/b.thin.bc \
+; RUN: -o %t/thin.so 2>&1 | FileCheck %s --check-prefix=USE
+; RUN: llvm-dis %t/a.thin.bc.0.preopt.bc -o - | FileCheck %s
+; RUN: llvm-dis %t/b.thin.bc.0.preopt.bc -o - | FileCheck %s
+; RUN: llvm-readelf --notes %t/thin.so | FileCheck %s --check-prefix=NOTE
+
+; CHECK: !llvm.module.flags = !{[[ID:![0-9]+]]}
+; CHECK: [[ID]] = !{i32 1, !"MemProfModuleId", !"{{.*}}"}
+
+; USE: Running pass: MemProfUsePass
+
+; NOTE: Displaying notes found in: .note.llvm.memprof.module-id
+; NOTE: LLVM
+; NOTE-SAME: 0x00000010
+; NOTE-NEXT: description data: 81 78 c7 a8 38 0f 9e 15 64 1f 28 07 9e 9e 29 70
+
+;--- memprof.yaml
+---
+HeapProfileRecords:
+ - GUID: 0x7f21e26c4887dd26
+ AllocSites:
+ - Callstack:
+ - { Function: 0x7f21e26c4887dd26, LineOffset: 0, Column: 0,
+ IsInlineFrame: false }
+ MemInfoBlock:
+ AllocCount: 1
+ TotalSize: 4
+ TotalLifetime: 1
+ TotalLifetimeAccessDensity: 1
+ CallSites: []
+...
+
+;--- a.ll
+
+source_filename = "a.cpp"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+$__memprof_profile_filename = comdat any
+ at __memprof_profile_filename = constant [16 x i8] c"memprof.profraw\00", comdat
+ at __llvm_memprof_tu_id = private constant { i32, i32, i32, [8 x i8], [16 x i8] } { i32 5, i32 16, i32 1297110017, [8 x i8] c"LLVM\00\00\00\00", [16 x i8] c"\1D\2C\7F\98\B2\5E\34\CA\2D\77\C6\2C\1F\EC\F9\02" }, section ".note.llvm.memprof.tu-id", align 4
+ at llvm.compiler.used = appending global [1 x ptr] [ptr @__llvm_memprof_tu_id], section "llvm.metadata"
+
+define i32 @a() {
+ ret i32 1
+}
+
+;--- b.ll
+
+source_filename = "b.cpp"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+$__memprof_profile_filename = comdat any
+ at __memprof_profile_filename = constant [16 x i8] c"memprof.profraw\00", comdat
+ at __llvm_memprof_tu_id = private constant { i32, i32, i32, [8 x i8], [16 x i8] } { i32 5, i32 16, i32 1297110017, [8 x i8] c"LLVM\00\00\00\00", [16 x i8] c"\D8\74\6D\4F\04\F0\59\B1\DF\E0\1B\00\ED\84\B3\24" }, section ".note.llvm.memprof.tu-id", align 4
+ at llvm.compiler.used = appending global [1 x ptr] [ptr @__llvm_memprof_tu_id], section "llvm.metadata"
+
+define i32 @b() {
+ ret i32 2
+}
diff --git a/lld/test/ELF/memprof-module-id.s b/lld/test/ELF/memprof-module-id.s
new file mode 100644
index 00000000000000..5d6f000d353620
--- /dev/null
+++ b/lld/test/ELF/memprof-module-id.s
@@ -0,0 +1,62 @@
+# REQUIRES: x86
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/b.s -o %t/b.o
+# RUN: ld.lld -shared %t/a.o %t/b.o -o %t/ab.so
+# RUN: ld.lld -shared %t/b.o %t/a.o -o %t/ba.so
+# RUN: ld.lld -shared %t/a.o -o %t/a.so
+# RUN: llvm-readelf --sections --wide %t/ab.so | \
+# RUN: FileCheck %s --check-prefix=SHARED
+# RUN: llvm-readelf --notes %t/ab.so | FileCheck %s --check-prefix=NOTE
+# RUN: llvm-objcopy --dump-section \
+# RUN: .note.llvm.memprof.module-id=%t/ab.note %t/ab.so
+# RUN: llvm-objcopy --dump-section \
+# RUN: .note.llvm.memprof.module-id=%t/ba.note %t/ba.so
+# RUN: llvm-objcopy --dump-section \
+# RUN: .note.llvm.memprof.module-id=%t/a.note %t/a.so
+# RUN: cmp %t/ab.note %t/ba.note
+# RUN: not cmp %t/ab.note %t/a.note
+# RUN: ld.lld -e a %t/a.o -o %t/a.exe
+# RUN: llvm-readelf --sections --wide %t/a.exe | \
+# RUN: FileCheck %s --check-prefix=EXEC
+
+# SHARED: .note.llvm.memprof.module-id NOTE
+# SHARED-NOT: .note.llvm.memprof.tu-id
+# NOTE: Displaying notes found in: .note.llvm.memprof.module-id
+# NOTE: LLVM
+# NOTE-SAME: 0x00000010
+# NOTE: description data: b4 ff cb 23 73 7c ec 31 5a 4a 4d 1a a2 a6 20 ce
+# EXEC: .note.llvm.memprof.tu-id NOTE
+# EXEC-NOT: .note.llvm.memprof.module-id
+
+#--- a.s
+.section .note.llvm.memprof.tu-id,"a", at note
+.p2align 2
+.long 5
+.long 16
+.long 0x4d505401
+.asciz "LLVM"
+.p2align 2
+.byte 0, 1, 2, 3, 4, 5, 6, 7
+.byte 8, 9, 10, 11, 12, 13, 14, 15
+
+.text
+.globl a
+a:
+ ret
+
+#--- b.s
+.section .note.llvm.memprof.tu-id,"a", at note
+.p2align 2
+.long 5
+.long 16
+.long 0x4d505401
+.asciz "LLVM"
+.p2align 2
+.byte 16, 17, 18, 19, 20, 21, 22, 23
+.byte 24, 25, 26, 27, 28, 29, 30, 31
+
+.text
+.globl b
+b:
+ ret
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 14fd5e27962adc..f6f7c1710abe64 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1554,6 +1554,13 @@ enum : unsigned {
NT_GNU_BUILD_ATTRIBUTE_FUNC = 0x101,
};
+// LLVM MemProf note types. These values are interpreted in the "LLVM" note
+// namespace.
+enum : unsigned {
+ NT_LLVM_MEMPROF_TU_ID = 0x4d505401,
+ NT_LLVM_MEMPROF_MODULE_ID = 0x4d505402,
+};
+
// Core note types.
enum : unsigned {
NT_PRSTATUS = 1,
diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h
index 60cb2a08c0381b..43526a111031da 100644
--- a/llvm/include/llvm/LTO/Config.h
+++ b/llvm/include/llvm/LTO/Config.h
@@ -26,6 +26,7 @@
#include "llvm/Support/LibraryOptionsContext.h"
#include "llvm/Target/TargetOptions.h"
+#include <array>
#include <functional>
#include <optional>
@@ -140,6 +141,12 @@ struct Config {
/// Sample PGO profile path.
std::string SampleProfile;
+ /// Memory profile path.
+ std::string MemoryProfile;
+
+ /// Stable identifier for the final MemProf-instrumented shared object.
+ std::optional<std::array<uint8_t, 16>> MemProfModuleId;
+
/// Name remapping file for profile data.
std::string ProfileRemapping;
diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h
index c9cafa0454f7b0..74a3cb72750735 100644
--- a/llvm/include/llvm/ProfileData/MemProf.h
+++ b/llvm/include/llvm/ProfileData/MemProf.h
@@ -490,6 +490,12 @@ struct IndexedMemProfRecord {
// mapped to functions using this GUID.
LLVM_ABI GlobalValue::GUID getGUID(const StringRef FunctionName);
+// Returns the GUID for the function name qualified by the stable MemProf
+// module ID. This distinguishes equal function names in different profiled
+// shared objects.
+LLVM_ABI GlobalValue::GUID getGUID(const StringRef FunctionName,
+ ArrayRef<uint8_t> ModuleId);
+
// Holds call site information with frame contents inline.
struct CallSiteInfo {
// The frames in the call stack
diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h
index 413c261ca3c1e6..edc2bc585ef058 100644
--- a/llvm/include/llvm/ProfileData/MemProfReader.h
+++ b/llvm/include/llvm/ProfileData/MemProfReader.h
@@ -173,8 +173,11 @@ class LLVM_ABI RawMemProfReader final : public MemProfReader {
object::OwningBinary<object::Binary> Binary;
// Symbolizer created from this module's object file and DWARF context.
std::unique_ptr<symbolize::SymbolizableModule> Symbolizer;
- // Build ID used to distinguish equal function names in different modules.
+ // Build ID used to match this module to its raw-profile segment.
llvm::SmallVector<uint8_t, 20> BuildId;
+ // Stable module ID used to distinguish equal function names when multiple
+ // profiled modules are supplied.
+ llvm::SmallVector<uint8_t, 16> ModuleId;
// Preferred address of the executable segment in the ELF file.
uint64_t PreferredTextSegmentAddress = 0;
// Runtime address range of this module's executable segment, recovered
diff --git a/llvm/include/llvm/Transforms/Instrumentation/MemProfUse.h b/llvm/include/llvm/Transforms/Instrumentation/MemProfUse.h
index 3afd88f144859c..8b23857613fe1e 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/MemProfUse.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/MemProfUse.h
@@ -47,9 +47,9 @@ namespace memprof {
// list of call sites, each of the form {LineLocation, CalleeGUID}.
LLVM_ABI DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> extractCallsFromIR(
Module &M, const TargetLibraryInfo &TLI,
- function_ref<bool(uint64_t)> IsPresentInProfile = [](uint64_t) {
- return true;
- });
+ function_ref<bool(uint64_t)> IsPresentInProfile =
+ [](uint64_t) { return true; },
+ bool UseModuleId = true);
using LocToLocMap = DenseMap<LineLocation, LineLocation>;
@@ -57,7 +57,7 @@ using LocToLocMap = DenseMap<LineLocation, LineLocation>;
// map that maps source locations in the profile to those in the current IR.
LLVM_ABI DenseMap<uint64_t, LocToLocMap>
computeUndriftMap(Module &M, IndexedInstrProfReader *MemProfReader,
- const TargetLibraryInfo &TLI);
+ const TargetLibraryInfo &TLI, bool UseModuleId = true);
} // namespace memprof
} // namespace llvm
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index a2ba9384899aa6..8b666b28c2092f 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -1449,6 +1449,15 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
WholeProgramVisibilityEnabledInLTO);
+ if (Conf.MemProfModuleId) {
+ ArrayRef<uint8_t> Id = *Conf.MemProfModuleId;
+ RegularLTO.CombinedModule->addModuleFlag(
+ Module::Error, "MemProfModuleId",
+ MDString::get(
+ RegularLTO.CombinedModule->getContext(),
+ StringRef(reinterpret_cast<const char *>(Id.data()), Id.size())));
+ }
+
if (Conf.PreOptModuleHook &&
!Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
return Error::success();
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index ed431be770f593..65b1ebec3b77a8 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -43,6 +43,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
+#include "llvm/Transforms/Instrumentation/MemProfUse.h"
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/Transforms/Utils/SplitModule.h"
#include <optional>
@@ -366,6 +367,9 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
break;
}
+ if (!Conf.MemoryProfile.empty())
+ MPM.addPass(MemProfUsePass(Conf.MemoryProfile));
+
// Parse a custom pipeline if asked to.
if (!Conf.OptPipeline.empty()) {
if (auto Err = PB.parsePassPipeline(MPM, Conf.OptPipeline)) {
@@ -681,6 +685,15 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
// the module, if applicable.
Mod.setPartialSampleProfileRatio(CombinedIndex);
+ if (Conf.MemProfModuleId) {
+ ArrayRef<uint8_t> Id = *Conf.MemProfModuleId;
+ Mod.addModuleFlag(
+ Module::Error, "MemProfModuleId",
+ MDString::get(
+ Mod.getContext(),
+ StringRef(reinterpret_cast<const char *>(Id.data()), Id.size())));
+ }
+
LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
if (CodeGenOnly) {
// If CodeGenOnly is set, we only perform code generation and skip
diff --git a/llvm/lib/ProfileData/MemProf.cpp b/llvm/lib/ProfileData/MemProf.cpp
index b00cf9a8a95400..f1bbb8ea2ff5c6 100644
--- a/llvm/lib/ProfileData/MemProf.cpp
+++ b/llvm/lib/ProfileData/MemProf.cpp
@@ -5,6 +5,7 @@
#include "llvm/ProfileData/SampleProf.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/EndianStream.h"
+#include "llvm/Support/MD5.h"
namespace llvm {
namespace memprof {
@@ -275,6 +276,17 @@ GlobalValue::GUID getGUID(const StringRef FunctionName) {
return Function::getGUIDAssumingExternalLinkage(CanonicalName);
}
+GlobalValue::GUID getGUID(const StringRef FunctionName,
+ ArrayRef<uint8_t> ModuleId) {
+ StringRef CanonicalName =
+ sampleprof::FunctionSamples::getCanonicalFnName(FunctionName);
+ MD5 Hash;
+ Hash.update("MemProf module GUID");
+ Hash.update(ModuleId);
+ Hash.update(CanonicalName);
+ return Hash.final().low();
+}
+
Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
using namespace support;
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index e858d35b21295a..3647aadd0726ae 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -25,6 +25,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/BinaryFormat/ELF.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
#include "llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h"
@@ -283,15 +284,32 @@ std::string getBuildIdString(const SegmentEntry &Entry) {
return Str;
}
-GlobalValue::GUID getModuleGUID(StringRef FunctionName,
- ArrayRef<uint8_t> BuildId) {
- StringRef CanonicalName =
- sampleprof::FunctionSamples::getCanonicalFnName(FunctionName);
- MD5 Hash;
- Hash.update("MemProf module GUID");
- Hash.update(BuildId);
- Hash.update(CanonicalName);
- return Hash.final().low();
+Expected<SmallVector<uint8_t, 16>>
+getMemProfModuleId(const object::ELF64LEFile &ElfFile) {
+ auto SectionsOr = ElfFile.sections();
+ if (!SectionsOr)
+ return SectionsOr.takeError();
+
+ for (const auto &Section : *SectionsOr) {
+ if (Section.sh_type != ELF::SHT_NOTE)
+ continue;
+
+ Error Err = Error::success();
+ for (const auto &Note : ElfFile.notes(Section, Err)) {
+ if (Note.getName() != "LLVM" ||
+ Note.getType() != ELF::NT_LLVM_MEMPROF_MODULE_ID)
+ continue;
+ ArrayRef<uint8_t> Id = Note.getDesc(Section.sh_addralign);
+ if (Id.size() != 16)
+ return createStringError(inconvertibleErrorCode(),
+ "invalid MemProf module ID size: %zu",
+ Id.size());
+ return SmallVector<uint8_t, 16>(Id);
+ }
+ if (Err)
+ return std::move(Err);
+ }
+ return SmallVector<uint8_t, 16>();
}
} // namespace
@@ -424,6 +442,10 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
// Open each profiled binary and perform the ELF and executable-segment sanity
// checks needed to translate runtime PCs for symbolization.
StringSet<> SeenBuildIds;
+ StringSet<> SeenModuleIds;
+ const bool UseModuleIds =
+ llvm::count_if(ProfiledBinaries,
+ [](StringRef Path) { return !Path.empty(); }) > 1;
for (StringRef BinaryPath : ProfiledBinaries) {
if (BinaryPath.empty())
continue;
@@ -500,6 +522,23 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer,
Twine("Duplicate profiled binary build id: ") + BuildIdString,
inconvertibleErrorCode());
Module->BuildId.assign(BinaryId.begin(), BinaryId.end());
+
+ if (UseModuleIds) {
+ auto ModuleIdOr = getMemProfModuleId(ElfFile);
+ if (!ModuleIdOr)
+ return report(ModuleIdOr.takeError(), FileName);
+ if (ModuleIdOr->empty())
+ return make_error<StringError>(
+ Twine("No MemProf module ID found in binary ") + FileName,
+ inconvertibleErrorCode());
+ std::string ModuleIdString = toHex(*ModuleIdOr, /*LowerCase=*/true);
+ if (!SeenModuleIds.insert(ModuleIdString).second)
+ return make_error<StringError>(
+ Twine("Duplicate profiled binary MemProf module id: ") +
+ ModuleIdString,
+ inconvertibleErrorCode());
+ Module->ModuleId = std::move(*ModuleIdOr);
+ }
ProfiledModules.push_back(std::move(Module));
}
@@ -696,15 +735,13 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
I++) {
const auto &DIFrame = DI.getFrame(I);
// The PC identified its module, but the normal MemProf GUID only hashes
- // the linkage name. Qualify it with the build ID when multiple binaries
- // are supplied so equal external symbols in different DSOs remain
- // distinct. Keep single-binary GUIDs unchanged for compatibility.
- // Build-ID-qualified GUIDs describe the exact profiled DSOs; applying
- // such a profile to rebuilt DSOs additionally requires a compiler-side
- // mechanism capable of reproducing the same module identity.
+ // the linkage name. Qualify it with the stable MemProf module ID when
+ // multiple binaries are supplied so equal external symbols in
+ // different DSOs remain distinct. Keep single-binary GUIDs unchanged
+ // for compatibility.
const uint64_t Guid =
Module && ProfiledModules.size() > 1
- ? getModuleGUID(DIFrame.FunctionName, Module->BuildId)
+ ? memprof::getGUID(DIFrame.FunctionName, Module->ModuleId)
: memprof::getGUID(DIFrame.FunctionName);
// Compiler generated code, e.g. the forwarding call in a C++
// non-virtual thunk, has line 0. Record such frames with offset 0
diff --git a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
index a2b242460f7cab..d1d60f3a202a80 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfUse.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/MemoryProfileInfo.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
@@ -49,6 +50,36 @@ extern cl::opt<bool> NoPGOWarnMismatch;
extern cl::opt<bool> AnnotateStringLiteralSectionPrefix;
} // namespace llvm
+static GlobalValue::GUID getMemProfGUID(const Module &M, StringRef FunctionName,
+ bool UseModuleId = true) {
+ auto *ModuleId =
+ dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfModuleId"));
+ if (!UseModuleId || !ModuleId || ModuleId->getString().size() != 16)
+ return memprof::getGUID(FunctionName);
+ return memprof::getGUID(FunctionName,
+ arrayRefFromStringRef(ModuleId->getString()));
+}
+
+static bool profileUsesModuleIds(const Module &M,
+ IndexedInstrProfReader &Reader) {
+ auto *ModuleId =
+ dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfModuleId"));
+ if (!ModuleId || ModuleId->getString().size() != 16)
+ return false;
+
+ // Profiles produced from a single binary retain the original name-only
+ // GUIDs for compatibility. Only use the module ID when this module has a
+ // corresponding module-qualified record in the profile.
+ for (const Function &F : M) {
+ if (F.isDeclaration())
+ continue;
+ auto Record = Reader.getMemProfRecord(getMemProfGUID(M, F.getName()));
+ if (Record)
+ return true;
+ consumeError(Record.takeError());
+ }
+ return false;
+}
// Matching statistics
STATISTIC(NumOfMemProfMissing, "Number of functions without memory profile.");
STATISTIC(NumOfMemProfMismatch,
@@ -241,7 +272,8 @@ struct AllocMatchInfo {
DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>>
memprof::extractCallsFromIR(Module &M, const TargetLibraryInfo &TLI,
- function_ref<bool(uint64_t)> IsPresentInProfile) {
+ function_ref<bool(uint64_t)> IsPresentInProfile,
+ bool UseModuleId) {
const InstrumentationOptions &Opts = getInstrumentationOptions(M);
DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> Calls;
@@ -273,8 +305,8 @@ memprof::extractCallsFromIR(Module &M, const TargetLibraryInfo &TLI,
StringRef CallerName = DIL->getSubprogramLinkageName();
assert(!CallerName.empty() &&
"Be sure to enable -fdebug-info-for-profiling");
- uint64_t CallerGUID = memprof::getGUID(CallerName);
- uint64_t CalleeGUID = memprof::getGUID(CalleeName);
+ uint64_t CallerGUID = getMemProfGUID(M, CallerName, UseModuleId);
+ uint64_t CalleeGUID = getMemProfGUID(M, CalleeName, UseModuleId);
// Pretend that we are calling a function with GUID == 0 if we are
// in the inline stack leading to a heap allocation function.
if (IsAlloc) {
@@ -313,15 +345,16 @@ memprof::extractCallsFromIR(Module &M, const TargetLibraryInfo &TLI,
DenseMap<uint64_t, LocToLocMap>
memprof::computeUndriftMap(Module &M, IndexedInstrProfReader *MemProfReader,
- const TargetLibraryInfo &TLI) {
+ const TargetLibraryInfo &TLI, bool UseModuleId) {
DenseMap<uint64_t, LocToLocMap> UndriftMaps;
DenseMap<uint64_t, SmallVector<memprof::CallEdgeTy, 0>> CallsFromProfile =
MemProfReader->getMemProfCallerCalleePairs();
DenseMap<uint64_t, SmallVector<memprof::CallEdgeTy, 0>> CallsFromIR =
- extractCallsFromIR(M, TLI, [&](uint64_t GUID) {
- return CallsFromProfile.contains(GUID);
- });
+ extractCallsFromIR(
+ M, TLI,
+ [&](uint64_t GUID) { return CallsFromProfile.contains(GUID); },
+ UseModuleId);
// Compute an undrift map for each CallerGUID.
for (const auto &[CallerGUID, IRAnchors] : CallsFromIR) {
@@ -603,7 +636,7 @@ static void dumpInlineCallStack(Instruction &I, CallBase *CI,
OptimizationRemarkEmitter &ORE,
DenseSet<uint64_t> &SeenFrames,
DenseSet<uint64_t> &SeenStacks,
- bool ProfileHasColumns) {
+ bool ProfileHasColumns, bool UseModuleId) {
// Dump frame info. Frames are deduplicated using FrameID.
std::string CallStack;
raw_string_ostream CallStackOS(CallStack);
@@ -613,7 +646,7 @@ static void dumpInlineCallStack(Instruction &I, CallBase *CI,
StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
if (Name.empty())
Name = DIL->getScope()->getSubprogram()->getName();
- auto CalleeGUID = Function::getGUIDAssumingExternalLinkage(Name);
+ auto CalleeGUID = getMemProfGUID(*I.getModule(), Name, UseModuleId);
uint64_t FrameID = computeStackId(CalleeGUID, getLineOffset(DIL),
ProfileHasColumns ? DIL->getColumn() : 0);
if (SeenFrames.insert(FrameID).second) {
@@ -650,7 +683,8 @@ readMemprof(const InstrumentationOptions &Opts, Module &M, Function &F,
std::set<std::vector<uint64_t>> &MatchedCallSites,
DenseMap<uint64_t, LocToLocMap> &UndriftMaps,
OptimizationRemarkEmitter &ORE, uint64_t MaxColdSize,
- DenseSet<uint64_t> &SeenStacks, DenseSet<uint64_t> &SeenFrames) {
+ DenseSet<uint64_t> &SeenStacks, DenseSet<uint64_t> &SeenFrames,
+ bool UseModuleId) {
auto &Ctx = M.getContext();
// Previously we used getIRPGOObjectName() here. If F is local linkage,
// getIRPGOObjectName() returns FuncName with prefix 'FileName;'. But
@@ -660,7 +694,7 @@ readMemprof(const InstrumentationOptions &Opts, Module &M, Function &F,
// 'unique-internal-linkage-names' can make MemProf work better for local
// linkage function.
auto FuncName = F.getName();
- auto FuncGUID = Function::getGUIDAssumingExternalLinkage(FuncName);
+ auto FuncGUID = getMemProfGUID(M, FuncName, UseModuleId);
if (Opts.memprof_print_function_guids)
errs() << "MemProf: Function GUID " << FuncGUID << " is " << FuncName
<< "\n";
@@ -768,7 +802,7 @@ readMemprof(const InstrumentationOptions &Opts, Module &M, Function &F,
if (ORE.allowExtraAnalysis(DEBUG_TYPE))
dumpInlineCallStack(I, CI, ORE, SeenFrames, SeenStacks,
- ProfileHasColumns);
+ ProfileHasColumns, UseModuleId);
// List of call stack ids computed from the location hashes on debug
// locations (leaf to inlined at root).
@@ -789,7 +823,7 @@ readMemprof(const InstrumentationOptions &Opts, Module &M, Function &F,
StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
if (Name.empty())
Name = DIL->getScope()->getSubprogram()->getName();
- auto CalleeGUID = Function::getGUIDAssumingExternalLinkage(Name);
+ auto CalleeGUID = getMemProfGUID(M, Name, UseModuleId);
auto StackId = computeStackId(CalleeGUID, getLineOffset(DIL),
ProfileHasColumns ? DIL->getColumn() : 0);
// Check if we have found the profile's leaf frame. If yes, collect
@@ -868,6 +902,8 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
return PreservedAnalyses::all();
}
+ const bool UseModuleId = profileUsesModuleIds(M, *MemProfReader);
+
const bool Changed =
Opts.memprof_annotate_static_data_prefix &&
annotateGlobalVariables(M, MemProfReader->getDataAccessProfileData());
@@ -882,7 +918,7 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(*M.begin());
DenseMap<uint64_t, LocToLocMap> UndriftMaps;
if (Opts.memprof_salvage_stale_profile)
- UndriftMaps = computeUndriftMap(M, MemProfReader.get(), TLI);
+ UndriftMaps = computeUndriftMap(M, MemProfReader.get(), TLI, UseModuleId);
// Map from the stack hash of each matched allocation context in the function
// profiles to match info such as the total profiled size (bytes), allocation
@@ -909,7 +945,7 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
readMemprof(Opts, M, F, MemProfReader.get(), TLI,
FullStackIdToAllocMatchInfo, MatchedCallSites, UndriftMaps, ORE,
- MaxColdSize, SeenStacks, SeenFrames);
+ MaxColdSize, SeenStacks, SeenFrames, UseModuleId);
}
if (Opts.memprof_print_match_info) {
diff --git a/llvm/test/Transforms/PGOProfile/memprof-dump-matched-alloc-site.ll b/llvm/test/Transforms/PGOProfile/memprof-dump-matched-alloc-site.ll
index 4d523ff85b5038..a9835fbde7f8ff 100644
--- a/llvm/test/Transforms/PGOProfile/memprof-dump-matched-alloc-site.ll
+++ b/llvm/test/Transforms/PGOProfile/memprof-dump-matched-alloc-site.ll
@@ -123,7 +123,7 @@ entry:
}
!llvm.dbg.cu = !{!0}
-!llvm.module.flags = !{!2}
+!llvm.module.flags = !{!2, !14}
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
!1 = !DIFile(filename: "memprof-dump-matched-alloc-site.cc", directory: "/")
@@ -139,3 +139,4 @@ entry:
!11 = distinct !DILocation(line: 2, column: 21, scope: !9, inlinedAt: !12)
!12 = distinct !DILocation(line: 3, column: 47, scope: !13)
!13 = distinct !DISubprogram(name: "f3", linkageName: "_Z2f3v", scope: !1, file: !1, line: 3, type: !5, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!14 = !{i32 1, !"MemProfModuleId", !"0123456789abcdef"}
diff --git a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
index 6fa5210de588db..6f00bbb1080109 100644
--- a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
+++ b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
@@ -22,6 +22,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include <array>
+
namespace llvm {
namespace memprof {
namespace {
@@ -111,6 +113,26 @@ declare !dbg !19 void @_Z2f3v()
ElementsAre(Pair(LineLocation(1, 3), memprof::getGUID("_Z2f1v")),
Pair(LineLocation(2, 3), memprof::getGUID("_Z2f2v")),
Pair(LineLocation(2, 9), memprof::getGUID("_Z2f3v"))));
+
+ const std::array<uint8_t, 16> ModuleId = {0x81, 0x78, 0xc7, 0xa8, 0x38, 0x0f,
+ 0x9e, 0x15, 0x64, 0x1f, 0x28, 0x07,
+ 0x9e, 0x9e, 0x29, 0x70};
+ M->addModuleFlag(Module::Error, "MemProfModuleId",
+ MDString::get(Ctx, StringRef(reinterpret_cast<const char *>(
+ ModuleId.data()),
+ ModuleId.size())));
+ Calls = extractCallsFromIR(*M, TLI);
+
+ ASSERT_THAT(Calls, SizeIs(1));
+ auto ModuleIt = Calls.begin();
+ const auto &[ModuleCallerGUID, ModuleCallSites] = *ModuleIt;
+ EXPECT_EQ(ModuleCallerGUID, memprof::getGUID("_Z3foov", ModuleId));
+ EXPECT_THAT(
+ ModuleCallSites,
+ ElementsAre(
+ Pair(LineLocation(1, 3), memprof::getGUID("_Z2f1v", ModuleId)),
+ Pair(LineLocation(2, 3), memprof::getGUID("_Z2f2v", ModuleId)),
+ Pair(LineLocation(2, 9), memprof::getGUID("_Z2f3v", ModuleId))));
}
TEST(MemProf, ExtractDirectCallsFromIRInline) {
More information about the cfe-commits
mailing list