rust/compiler
bors 4b53279854 Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk
Add a fast path for lowering trivial consts

The objective of this PR is to improve compilation performance for crates that define a lot of trivial consts. This is a flamegraph of a build of a library crate that is just 100,000 trivial consts, taken from a nightly compiler:
<img width="842" height="280" alt="2025-10-25-164005_842x280_scrot" src="https://github.com/user-attachments/assets/e5400aaf-03bd-4461-b905-054aa82ca60f" />
My objective is to target all of the cycles in `eval_to_const_value_raw` that are not part of `mir_built`, because if you look at the `mir_built` for a trivial const, we already have the value available.

In this PR, the definition of a trivial const is this:
```rust
const A: usize = 0;
```
Specifically, we look for if the `mir_built` body is a single basic block containing one assign statement and a return terminator, where the assign statement assigns an `Operand::Constant(Const::Val)`. The MIR dumps for these look like:
```
const A: usize = {
    let mut _0: usize;

    bb0: {
        _0 = const 0_usize;
        return;
    }
}
```

The implementation is built around a new query, `trivial_const(LocalDefId) -> Option<(ConstValue, Ty)>` which returns the contents of the `Const::Val` in the `mir_built` if the `LocalDefId` is a trivial const.

Then I added _debug_ assertions to the beginning of `mir_for_ctfe` and `mir_promoted` to prevent trying to get the body of a trivial const, because that would defeat the optimization here. But these are deliberately _debug_ assertions because the consequence of failing the assertion is that compilation is slow, not corrupt. If we made these hard assertions, I'm sure there are obscure scenarios people will run into where the compiler would ICE instead of continuing on compilation, just a bit slower. I'd like to know about those, but I do not think serving up an ICE is worth it.

With the assertions in place, I just added logic around all the places they were hit, to skip over trying to analyze the bodies of trivial consts.

In the future, I'd like to see this work extended by:
* Pushing detection of trivial consts before MIR building
* Including DefKind::Static and DefKind::InlineConst
* Including consts like `_1 = const 0_usize; _0 = &_1`, which would make a lot of promoteds into trivial consts
* Handling less-trivial consts like `const A: usize = B`, which have `Operand::Constant(Const::Unevaluated)`
2025-10-27 11:02:41 +00:00
..
rustc Make llvm_enzyme a regular cargo feature 2025-09-15 15:31:56 +00:00
rustc_abi Rollup merge of #147134 - workingjubilee:remove-explicit-abialign-deref, r=Zalathar 2025-09-29 15:44:55 +10:00
rustc_arena some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_ast Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
rustc_ast_ir turn pointer width into an integer in target.json 2025-08-27 23:44:49 +02:00
rustc_ast_lowering Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_ast_passes Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
rustc_ast_pretty Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
rustc_attr_parsing Rollup merge of #148000 - JonathanBrouwer:wipnew2, r=jdonszelmann 2025-10-25 12:57:39 +02:00
rustc_baked_icu_data Use default locale fallback data 2025-08-28 09:48:54 +00:00
rustc_borrowck Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
rustc_builtin_macros Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
rustc_codegen_cranelift Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
rustc_codegen_gcc remove broken link 2025-10-20 20:50:44 +00:00
rustc_codegen_llvm Deduce captures(none) for a return place and parameters 2025-10-25 22:53:52 +02:00
rustc_codegen_ssa Improve the ICE message for invalid nullary intrinsic calls 2025-10-25 21:32:27 -04:00
rustc_const_eval Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk 2025-10-27 11:02:41 +00:00
rustc_data_structures Rollup merge of #147716 - zhetaicheleba:master, r=jdonszelmann 2025-10-15 23:41:05 +02:00
rustc_driver compiler: Add Windows resources to rustc-main and rustc_driver 2025-09-05 14:06:31 -04:00
rustc_driver_impl Auto merge of #147207 - Muscraft:anstyle-anstream, r=davidtwco 2025-10-22 16:22:51 +00:00
rustc_error_codes Rollup merge of #146585 - hkBst:indexing-1, r=jdonszelmann 2025-10-02 10:27:49 +02:00
rustc_error_messages Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_errors chore: Update to the latest annotate-snippets 2025-10-24 12:50:19 -06:00
rustc_expand Rollup merge of #147438 - reddevilmidzy:rename-non-inline-module-in-msg, r=fee1-dead 2025-10-18 23:54:44 +02:00
rustc_feature Add AcceptContext::suggestions 2025-10-22 21:43:50 +02:00
rustc_fluent_macro some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_fs_util Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_graphviz some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_hashes some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_hir Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_hir_analysis hir_analysis: add missing sizedness bounds 2025-10-24 15:28:34 +01:00
rustc_hir_id rustc_hir_id: Add a comment explaining why the crate exists 2025-08-20 15:04:00 -07:00
rustc_hir_pretty Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
rustc_hir_typeck Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_incremental Do not GC the current active incremental session directory 2025-10-20 02:39:55 +00:00
rustc_index Introduce indexvec macro. 2025-10-22 00:52:52 +00:00
rustc_index_macros Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_infer rename select_where_possible and select_all_or_error 2025-10-07 23:02:23 +01:00
rustc_interface Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk 2025-10-27 11:02:41 +00:00
rustc_lexer Fix a crash/mislex when more than one frontmatter closing possibility is considered 2025-09-22 15:10:41 -04:00
rustc_lint Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_lint_defs remove useless #![deny]s 2025-10-19 19:48:21 +02:00
rustc_llvm Deduce captures(none) for a return place and parameters 2025-10-25 22:53:52 +02:00
rustc_log Restrict sysroot crate imports to those defined in this repo. 2025-10-15 13:17:25 +01:00
rustc_macros use declarative macro for #[derive(TryFromU32)] 2025-10-06 14:54:38 +00:00
rustc_metadata Add a fast path for lowering trivial consts 2025-10-25 16:59:53 -04:00
rustc_middle Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk 2025-10-27 11:02:41 +00:00
rustc_mir_build Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
rustc_mir_dataflow Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
rustc_mir_transform Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk 2025-10-27 11:02:41 +00:00
rustc_monomorphize Allow unsizing pattern types with pointer base 2025-10-21 11:22:51 +00:00
rustc_next_trait_solver gamer 2025-10-24 15:34:45 +02:00
rustc_parse Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
rustc_parse_format Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_passes Rollup merge of #138679 - Shunpoco:issue-125323, r=oli-obk 2025-10-18 23:54:43 +02:00
rustc_pattern_analysis Rename various "concrete opaque type" terminology to say "hidden type" 2025-09-27 22:58:02 +01:00
rustc_privacy privacy: Introduce some caching to type visiting in DefIdVisitorSkeleton 2025-10-22 17:31:12 +03:00
rustc_proc_macro Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_public Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
rustc_public_bridge Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk 2025-10-27 11:02:41 +00:00
rustc_query_impl fixup limit handling code 2025-09-08 15:07:12 -07:00
rustc_query_system Rollup merge of #147830 - Noratrieb:reword-unstable-fingerprint-ice, r=jackh726 2025-10-22 13:20:23 -04:00
rustc_resolve Auto merge of #147914 - petrochenkov:oosmc-used, r=fmease 2025-10-26 23:44:52 +00:00
rustc_sanitizers Stop passing resolver disambiguator state to AST lowering. 2025-10-24 02:41:52 +00:00
rustc_serialize Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_session Rollup merge of #148007 - Muscraft:annotate-snippets, r=jdonszelmann 2025-10-25 12:57:39 +02:00
rustc_span Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_symbol_mangling Stop passing resolver disambiguator state to AST lowering. 2025-10-24 02:41:52 +00:00
rustc_target Deduce captures(none) for a return place and parameters 2025-10-25 22:53:52 +02:00
rustc_thread_pool some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_trait_selection Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii 2025-10-27 02:50:35 +00:00
rustc_traits Auto merge of #146096 - adwinwhite:handle_normalization_overflow_in_mono1, r=saethlin 2025-10-13 00:20:10 +00:00
rustc_transmute prefer to use repeat_n over repeat and take 2025-10-09 01:24:55 +08:00
rustc_ty_utils Deduce captures(none) for a return place and parameters 2025-10-25 22:53:52 +02:00
rustc_type_ir Rollup merge of #142339 - oli-obk:not-null-pattern-types, r=BoxyUwU 2025-10-22 07:12:09 +02:00
rustc_type_ir_macros some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_windows_rc [win] Use find-msvc-tools instead of cc to find the linker and rc on Windows 2025-09-19 12:00:30 -07:00