[llvm] [IR] Introduce !captures metadata (PR #160913)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 27 09:40:09 PDT 2025
nikic wrote:
> > The motivating use case for this is Rust's println!() machinery, which involves storing a reference to the value inside a structure. This means that printing code (including conditional debugging code), can inhibit optimizations because the pointer escapes. With the new metadata we can annotate this as a read-only capture, which has less impact on optimizations.
>
> https://godbolt.org/z/T1479EG43
>
> I am curious about how we annotate this in Rust. Do we provide an intrinsic like `declare ptr @foo(ptr returned captures(address, read_provenance))` in Rust?
What I had in mind is to just emit the attribute for all cases where a `&Freeze` reference is stored.
An alternative way to solve the problem would be to have an intrinsic like `llvm.readonly` which returns a copy of the pointer with read-only provenance only. This is arguably more general (because it covers everything that will be done with that pointer), but would also require substantial work to make sure the intrinsic is properly ignored in all optimizations. Having captures metadata on stores seem like the more straightforward way to address this, given that we already have all the necessary infrastructure in place.
https://github.com/llvm/llvm-project/pull/160913
More information about the llvm-commits
mailing list