[lld] [lld] Handle missing IRPGO profile without asserting (PR #203806)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 14 19:11:18 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: Peter Chen J. (peter941221)

<details>
<summary>Changes</summary>

Fixes #<!-- -->203761

`BPOrderer::computeOrder` called `takeError()` on the `InstrProfReader::create(...)` result and then still accessed the same `Expected` with `get()`. When `--irpgo-profile` points at a missing file, that turns a normal profile-open failure into an assertion instead of a regular linker error.

Handle the `Expected` directly, report the file error through `lld::error`, and return without building a startup order. Add missing-profile regressions for both the ELF and Mach-O balanced-partitioning entry points that share this code.

Validation:
- `build-lld/bin/llvm-lit -sv -j1 lld/test/ELF/bp-section-orderer-missing-profile.s lld/test/MachO/bp-section-orderer-missing-profile.s`
- `clang++ -O2 -fuse-ld=lld --ld-path=/home/peter/work/llvm-project-203761/build-lld/bin/ld.lld -Wl,--irpgo-profile=non_existent_file.profdata,--bp-startup-sort=function /tmp/llvm203761_main.cpp`

AI tool disclosure: I used OpenAI Codex to help inspect the code path, draft the patch, and draft the PR text. I reviewed and tested the final change myself before sending it for review.


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


3 Files Affected:

- (modified) lld/include/lld/Common/BPSectionOrdererBase.inc (+5-2) 
- (added) lld/test/ELF/bp-section-orderer-missing-profile.s (+9) 
- (added) lld/test/MachO/bp-section-orderer-missing-profile.s (+10) 


``````````diff
diff --git a/lld/include/lld/Common/BPSectionOrdererBase.inc b/lld/include/lld/Common/BPSectionOrdererBase.inc
index daac8785f8da7..087d0356db03d 100644
--- a/lld/include/lld/Common/BPSectionOrdererBase.inc
+++ b/lld/include/lld/Common/BPSectionOrdererBase.inc
@@ -177,9 +177,12 @@ auto BPOrderer<D>::computeOrder(
   if (!profilePath.empty()) {
     auto fs = vfs::getRealFileSystem();
     auto readerOrErr = InstrProfReader::create(profilePath, *fs);
-    lld::checkError(readerOrErr.takeError());
+    if (!readerOrErr) {
+      lld::error(toString(readerOrErr.takeError()));
+      return DenseMap<const Section *, int>();
+    }
 
-    reader = std::move(readerOrErr.get());
+    reader = std::move(*readerOrErr);
     for (auto &entry : *reader) {
       // Read all entries
       (void)entry;
diff --git a/lld/test/ELF/bp-section-orderer-missing-profile.s b/lld/test/ELF/bp-section-orderer-missing-profile.s
new file mode 100644
index 0000000000000..4712d12ae084f
--- /dev/null
+++ b/lld/test/ELF/bp-section-orderer-missing-profile.s
@@ -0,0 +1,9 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: not ld.lld %t.o --irpgo-profile=%t.missing.profdata --bp-startup-sort=function 2>&1 | FileCheck %s
+
+# CHECK: error: No such file or directory
+
+.globl _start
+_start:
+  ret
diff --git a/lld/test/MachO/bp-section-orderer-missing-profile.s b/lld/test/MachO/bp-section-orderer-missing-profile.s
new file mode 100644
index 0000000000000..9bdec1e6e69a8
--- /dev/null
+++ b/lld/test/MachO/bp-section-orderer-missing-profile.s
@@ -0,0 +1,10 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
+# RUN: not %lld -arch x86_64 -lSystem -e _main -o /dev/null %t.o --irpgo-profile=%t.missing.profdata --bp-startup-sort=function 2>&1 | FileCheck %s
+
+# CHECK: error: No such file or directory
+
+.text
+.globl _main
+_main:
+  ret

``````````

</details>


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


More information about the llvm-commits mailing list