Repository navigation
Fix PGO instrumentation for libcoreclr.so static libraries - #128412
Conversation
On non-Windows, add_pgo(coreclr) used target_compile_options(PRIVATE) which only applied -fprofile-instr-generate to coreclr's directly compiled sources (mscoree.cpp, exports.cpp). The vast majority of the runtime code lives in static libraries (cee_wks, utilcode, coreclrpal, etc.) that were linked in without instrumentation. Fix by adding directory-scoped PGO compile flags in pgosupport.cmake so all C/C++ sources in the coreclr tree get instrumentation/optimization flags. The existing add_pgo() function is unchanged - it still sets per-target LTO link flags for the final shared libraries. Fixes #124141 Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR updates CoreCLR’s native build configuration so Profile Guided Optimization (PGO) compile flags apply broadly across the src/coreclr C/C++ compilation units on non-Windows builds, rather than only affecting the few sources compiled directly into a shared library target.
Changes:
- Adds directory-scoped
-fprofile-instr-generate/-fprofile-instr-usecompile options for non-Windows PGO builds (C/C++ only). - Adds a directory-scoped link option for instrumentation builds to ensure profiling runtime symbols resolve when instrumented static libraries are linked into shared libraries.
- Looks for a global
coreclr.profdatato enable PGO optimization flags across the CoreCLR tree when present.
Co-authored-by: Copilot <[email protected]>
Code Review: Fix PGO instrumentation for libcoreclr.so static librariesSummaryThis PR adds directory-scoped PGO compile flags ( Verdict: ✅ Approve (no blocking issues)The fix is correct, well-scoped, well-commented, and directly addresses the root cause described in #124141. The approach is sound: directory-scope compile flags propagate into static library sub-targets, while Detailed Findings
|
|
@hoyosjs @AndyAyersMS opinions? seems to be producing instrumented bits NOTE: this only fixes libcoreclr, the System.* shared libs need additional changes, but I'd like to validate that this one works as expected first. |
|
Seems like a great idea. I wonder how hard it will be to observe the impact? |
I checked that it does produce a fully instrumented shared lib, so the next step would be to wait for an instrumented SDK to be produced & consumed by dotnet-optimization after this PR is merged. From my quick look there should be no actions needed in dotnet-optimization and it will eventually produce a profdata that includes coreclr (we will test it by inspecting pgo data coming with nuget after the roundtrip and the final optimized binary). |
|
Ping @hoyosjs @AndyAyersMS the motivation is the fact that the native PGO seems to be quite important given the recent regression |
|
I'll check this early next week - when I wrote #124141 (comment) we also got a bunch of probes. Like 5k of them which still felt like too few for libcoreclr. We also need to merge my PR in the instrumentation repo. |
@hoyosjs Is there anything blocking us to just try a round trip to see if it works? |
|
Ping @hoyosjs |
|
Looks like my previous attempt and this approach both had the right result. I was seeing 4.8k probes since 95% of the probes never end up in the the symbol table. Looking at the object files, the symbols for most are local and the linker never emits a symbol despite having the proper probe indirection to increment |
> [!NOTE] > This PR was created with the assistance of GitHub Copilot. Fixes #124141 On non-Windows, `add_pgo(coreclr)` used `target_compile_options(PRIVATE)` which only applied `-fprofile-instr-generate`/`-fprofile-instr-use` to the 2 source files directly compiled by the `coreclr` target (`mscoree.cpp`, `exports.cpp`). The runtime's real code lives in static libraries (`cee_wks`, `utilcode`, `coreclrpal`, etc.) that were linked in without instrumentation. This doesn't affect `clrjit` (all JIT sources compile directly into the shared library) or Windows (PGO uses link-time `/LTCG` flags). Fix: add directory-scoped PGO compile flags in `pgosupport.cmake` so all C/C++ sources in the coreclr tree get the flags. The existing `add_pgo()` is unchanged — it still sets per-target LTO link flags. Assembly files are excluded via `$<$<COMPILE_LANGUAGE:C,CXX>:...>`. ### Before (instrumented `libcoreclr.so`) | Section | Size | |---|---| | `__llvm_prf_names` | — | | `__llvm_prf_cnts` | — | | `__llvm_prf_data` | — | | **Estimated profiled functions** | **~30** | ### After (instrumented `libcoreclr.so`) | Section | Size | |---|---| | `__llvm_prf_names` | 283 KB | | `__llvm_prf_cnts` | 801 KB | | `__llvm_prf_data` | 1,094 KB | | **Estimated profiled functions** | **~20,000** | No changes needed in `dotnet-optimization` — the training pipeline already collects all `*.profraw` files and merges them into `coreclr.profdata`. --------- Co-authored-by: EgorBo <[email protected]> Co-authored-by: Copilot <[email protected]>
Note
This PR was created with the assistance of GitHub Copilot.
Fixes #124141
On non-Windows,
add_pgo(coreclr)usedtarget_compile_options(PRIVATE)which only applied-fprofile-instr-generate/-fprofile-instr-useto the 2 source files directly compiled by thecoreclrtarget (mscoree.cpp,exports.cpp). The runtime's real code lives in static libraries (cee_wks,utilcode,coreclrpal, etc.) that were linked in without instrumentation. This doesn't affectclrjit(all JIT sources compile directly into the shared library) or Windows (PGO uses link-time/LTCGflags).Fix: add directory-scoped PGO compile flags in
pgosupport.cmakeso all C/C++ sources in the coreclr tree get the flags. The existingadd_pgo()is unchanged — it still sets per-target LTO link flags. Assembly files are excluded via$<$<COMPILE_LANGUAGE:C,CXX>:...>.Before (instrumented
libcoreclr.so)__llvm_prf_names__llvm_prf_cnts__llvm_prf_dataAfter (instrumented
libcoreclr.so)__llvm_prf_names__llvm_prf_cnts__llvm_prf_dataNo changes needed in
dotnet-optimization— the training pipeline already collects all*.profrawfiles and merges them intocoreclr.profdata.