Re: [PATCH] kbuild: rust: pass AutoFDO flags to Clang with inline helpers
From: Jenhao Chen
Date: Fri Nov 13 2026 - 04:11:33 EST
On Wed, Nov 11, 2026 at 7:48 PM Miguel Ojeda
<miguel.ojeda.sandonis@xxxxxxxxx> wrote:
>
> This sounds reasonable, but my LLM spotted that Clang will run
> `AddDiscriminatorsPass` again, i.e. on the optimized bitcode that
> `rustc` produced. So the profile ends up indexed by Clang's renumbered
> discriminators, and then the build that uses the profile will not find
> a match for some code (depending on inlining decisions), i.e. that
> part of the profile is silently dropped.
>
> It proposes `-mllvm -no-discriminators` in
> `CC_FLAGS_RUST_INLINE_HELPERS` so that Clang doesn't add them, i.e.
> using the `rustc` ones.
>
> Rong, Han: is that expected?
Hi Miguel,
Nice catch -- I tested this on drivers/android/binder/rust_binder_main.o
(arm64, Clang 22 + rustc 1.91) and confirmed that the duplicate
AddDiscriminatorsPass run is real and that passing
`-mllvm -no-discriminators` to the post-llvm-link $(CC) step improves
profile matching in both stages:
1. Why the second AddDiscriminatorsPass renumbers discriminators:
In Stage 1, rustc runs AddDiscriminatorsPass on pre-inlining IR and
then runs its optimization/inlining pipeline before emitting .bc.
When post-llvm-link $(CC) runs AddDiscriminatorsPass again on the
already-inlined .m.bc, llvm/lib/Transforms/Utils/AddDiscriminators.cpp
keys blocks by (filename, line) without checking inlinedAt and
overwrites existing base discriminators via
cloneWithBaseDiscriminator(). On rust_binder_main.o, that second pass
increases non-zero DWARF .debug_line discriminator rows from 6,245 to
9,313 (+3,068 rows, +49%).
In the profile-use build, rustc's SampleProfileLoaderPass runs before
rustc's inliner (against pre-inlining discriminators), while Stage 3
sees a different post-inlining CFG once Stage 1 has applied the
profile.
2. Measured round-trip impact (42 sampled functions in
rust_binder_main.o):
- Stage 1 (rustc -Zprofile-sample-use): annotated branch_weights
increase from 454 to 490 (+7.9%) when the profile is generated and
used with `-mllvm -no-discriminators` in Stage 3.
- Stage 3 ($(CC) -fprofile-sample-use): SampleProfileLoader record
coverage increases from 39.3% (2,832 / 7,205) to 54.0%
(3,707 / 6,860, +14.7 percentage points), and sample coverage
increases from 42.9% (757.8K / 1,767.6K) to 56.4%
(989.6K / 1,753.4K, +13.6 percentage points; 969 -> 1,035
branch_weights). Against a hardware ETM profile, Stage 3 matched
function entry counts also increase by +33.9%.
3. Side-effect checks:
- C helpers: rust/helpers/helpers*.bc is compiled separately via
cmd_rust_helper (without CC_FLAGS_RUST_INLINE_HELPERS), so C
helpers still get their pre-inlining base discriminators before
llvm-link.
- Flow-sensitive discriminators: MIRAddFSDiscriminators in
llvm/lib/CodeGen/MIRFSDiscriminator.cpp checks
EnableFSDiscriminator and shouldEmitDebugInfoForProfiling() rather
than NoDiscriminators, so post-link FS discriminators remain
enabled.
Rong, Han, Miguel, Nathan: would you prefer adding
`-mllvm -no-discriminators` directly to `CC_FLAGS_RUST_INLINE_HELPERS`
in the top-level Makefile, or appending it in scripts/Makefile.autofdo
under `ifdef CONFIG_RUST_INLINE_HELPERS`? Happy to send v2 once we align
on the preferred spot.
Thanks,
Jenhao