You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
attributes: help LLVM understand that some spans are never going to do anything (#1600) (#1605)
## Motivation
Adding `#[instrument(level = "debug")]` attributes to functions in rustc
caused a performance regression (in release, where `debug!` is fully
optimized out) across all crates:
rust-lang/rust#89048 (comment)
While trying to debug this, I noticed that spans don't have the same
advantage that events have wrt to how LLVM sees them. Spans (or more
precisely, the enter-guard), will get dropped at the end of the scope,
which throws a spanner into the LLVM optimization pipeline. I am not
entirely sure where the problem is, but I am moderately certain that the
issue is that even entering a dummy span is too much code for LLVM to
reliably (or at all) optimize out.
## Solution
My hope is that in trusting the Rust compiler to generate cool code when using
drop flags, we can essentially generate a drop flag that depends on something we
know (due to events working as expected) to be optimizable.
So instead of doing
```rust
let _x = span!();
let _y = _x.enter();
// lotsa code
drop(_y)
```
we do
```rust
let _x;
let _y;
let must_drop = false;
if level_enabled!(DEBUG) {
must_drop = true;
_x = span!();
_y = _x.enter();
}
// lotsa code
if must_drop {
drop(_y)
}
```
I believe this will allow LLVM to properly optimize this again. Testing that
right now, but I wanted to open this PR immediately for review.
0 commit comments