Repository navigation
Identify trivial consts without building MIR - #165112
nnethercote wants to merge 5 commits into
Conversation
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Identify trivial consts without building MIR
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (fc37d66): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. Next, please: If you can, justify the regressions found in this try perf run in writing along with @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -2.3%, secondary -2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.2%, secondary -1.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 490.291s -> 489.459s (-0.17%) |
|
I wonder whether this interacts with const generics somehow, because there's #165070, which creates more MIR for type system constants. |
not really! this should all be compatible and nice. to be honest, this new trivial_const is amusingly similar to gca type system direct constants - trivial_const lowers a hir expr to a mir value, const_of_item lowers a hir expr to a ty::Const. It's probably possible to be cute here and merge handling code of something or other here, but, that's not particularly important (and this PR should probably not care about gca at all and just leave it up to const generics folks (me) to deal with any cleanup) (also, gca type system direct constants probably ought to be considered trivial constants, but, afaik they are not right now, but whatever, doesn't matter, we're a ways away from considering perf with gca. I'll do that at some point!) |
|
I started a Zulip thread to talk about this because it seems like it warrants some wider attention. |
A const or static whose body is one scalar literal of a primitive type (or a negated integer literal of a signed one), with no macro call, no adjustment and a type check with no error, mismatch or diagnostic, gets its value read off the literal in hir_ty::consteval::trivial_value: the literal's little-endian bytes at its type's size, as lowering's lower_literal_to_operand encodes it and the evaluator's write_allocation truncates it, with no memory map. Every other body goes to evaluate_by_mir, the previous path, unchanged. rustc's trivial_const, in the form of rust-lang/rust#165112 (decided before MIR is built). The test consteval::trivial runs both paths on 21 trivial items and asserts one interned allocation each, and checks 10 non-trivial bodies (an operation, a path, a cast, a block, a float negation, type errors) are refused.
This comment has been minimized.
This comment has been minimized.
bbcb9ea to
148c3bd
Compare
The MIR pretty printer prints trivial consts in a special way, which turns out to be very useful for writing a test that detects which consts are considered trivial and which are not.
Currently it has two paths after the MIR is built. - `trivial_const` path: steal the body, dump MIR if necessary, return the body. - Other path: dump MIR if necessary, run optimization passes, steal the body. The differing order of steal/dump isn't meaningful. This commit combines them into a single path with one optional step: - dump MIR if necessary, run optimization passes if non-`trivial_const`, steal the body. It's neater and will enable subsequent commits.
This avoids useless work: elaboration, creating an obligation, and evaluating the obligation.
From `rustc_mir_build` to `rustc_middle`. Necessary because the next commit will use it from `rustc_mir_transform`. `rustc_middle` is the obvious spot for something that is needed by both `rustc_mir_build` and `rustc_mir_transform`, and it's next to `LitToConstInput`. `parse_float_into_constval` and `parse_float_into_scalar` both must move as well.
This improves compile times in some cases because less MIR is built. It requires several changes. - `trivial_const` now analyzes HIR instead of MIR. The HIR analysis gives very similar results but succeeds in slightly fewer cases, visible in the changes to `tests/ui/consts/trivial-consts.rs`. - `mir_const_qualif`: it now checks `trivial_const` and handles that case specially with an early return to avoid building MIR. - In `mir_promoted`, we no longer need `ensure_done().trivial_const(def)`. (It was there because `mir_promoted` comes immediately afterward and it steals the MIR body that `trivial_const` used to require.) - In `run_required_analyses`, the `is_trivial_const` skip moved above `check_unsafety`, because the latter needs THIR and forced `mir_built` to run before stealing the THIR. It also eliminates some of the awkward interactions between `trivial_const` and MIR building, as seen from the deleted comments and the removed `trivial_const_provider` function. There were some other minor test changes. - Some cycle errors lost a "...which requires building MIR" entry; the cycle error still occurs. - `tests/mir-opt/unusual_item_types.rs` needs non-trivial consts for MIR to still be produced and dumped.
148c3bd to
0b9a047
Compare
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Identify trivial consts without building MIR
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This perf run might show false regressions, because of rust-lang/rustc-perf#2650 |
|
Finished benchmarking commit (6ac36b4): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -3.0%, secondary -1.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -4.8%, secondary -2.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 461.669s -> 461.947s (0.06%) |
|
Seems to have been fine, I think because the perf run started minutes before the problematic change actually landed |
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
This is now in a decent state, with some good testing. Best reviewed one commit at a time. |
|
☔ The latest upstream changes (presumably #166674) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
View all comments
This PR replaces the existing MIR-based
trivial_constcheck with a HIR-based one, to avoid constructing the MIR unnecessarily for trivial consts. (MIR-based passes such as borrow checking, unsafety checking, etc., are skipped for trivial consts.)Perf results are good: the best case is libc-0.2.172, where we see best-case wins of -6% icounts, -7% cycles and -7% max-rss.
r? @saethlin