[compiler-rt] [llvm] [MemProf] Support multiple profiled binaries (PR #217039)

Parmida Vahdatnia via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 06:18:42 PDT 2026


================
@@ -394,120 +419,139 @@ 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;
 
   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
----------------
parmidavnia wrote:

It's not since Symbolizer belongs to ProfiledModules now. one thing I can do to address it is release them after successful symbolization.

https://github.com/llvm/llvm-project/pull/217039


More information about the llvm-commits mailing list