Skip to content

Commit 13dac8f

Browse files
committed
Auto merge of #122721 - oli-obk:merge_queries, r=davidtwco
Replace `mir_built` query with a hook and use mir_const everywhere instead A small perf improvement due to less dep graph handling. Mostly just a cleanup to get rid of one of our many mir queries
2 parents 0824b30 + 6201ad9 commit 13dac8f

File tree

12 files changed

+62
-81
lines changed

12 files changed

+62
-81
lines changed

compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const BASE_HIR: &[&str] = &[
6565
const BASE_IMPL: &[&str] =
6666
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
6767

68-
/// DepNodes for mir_built/Optimized, which is relevant in "executable"
68+
/// DepNodes for exported mir bodies, which is relevant in "executable"
6969
/// code, i.e., functions+methods
7070
const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
7171

compiler/rustc_middle/src/hooks/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,10 @@ declare_hooks! {
7777
///
7878
/// (Eligible functions might nevertheless be skipped for other reasons.)
7979
hook is_eligible_for_coverage(key: LocalDefId) -> bool;
80+
81+
/// Create the MIR for a given `DefId` - this includes
82+
/// unreachable code.
83+
/// You do not want to call this yourself, instead use the cached version
84+
/// via `mir_built`
85+
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
8086
}

compiler/rustc_middle/src/query/mod.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -505,21 +505,15 @@ rustc_queries! {
505505
separate_provide_extern
506506
}
507507

508-
/// Fetch the MIR for a given `DefId` right after it's built - this includes
509-
/// unreachable code.
508+
/// Build the MIR for a given `DefId` and prepare it for const qualification.
509+
///
510+
/// See the [rustc dev guide] for more info.
511+
///
512+
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/construction.html
510513
query mir_built(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
511514
desc { |tcx| "building MIR for `{}`", tcx.def_path_str(key) }
512515
}
513516

514-
/// Fetch the MIR for a given `DefId` up till the point where it is
515-
/// ready for const qualification.
516-
///
517-
/// See the README for the `mir` module for details.
518-
query mir_const(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
519-
desc { |tcx| "preparing `{}` for borrow checking", tcx.def_path_str(key) }
520-
no_hash
521-
}
522-
523517
/// Try to build an abstract representation of the given constant.
524518
query thir_abstract_const(
525519
key: DefId

compiler/rustc_mir_build/src/build/custom/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! present, and if so we branch off into this module, which implements the attribute by
77
//! implementing a custom lowering from THIR to MIR.
88
//!
9-
//! The result of this lowering is returned "normally" from the `mir_built` query, with the only
9+
//! The result of this lowering is returned "normally" from the `build_mir` hook, with the only
1010
//! notable difference being that the `injected` field in the body is set. Various components of the
1111
//! MIR pipeline, like borrowck and the pass manager will then consult this field (via
1212
//! `body.should_skip()`) to skip the parts of the MIR pipeline that precede the MIR phase the user

compiler/rustc_mir_build/src/build/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
1818
use rustc_middle::middle::region;
1919
use rustc_middle::mir::interpret::Scalar;
2020
use rustc_middle::mir::*;
21+
use rustc_middle::query::TyCtxtAt;
2122
use rustc_middle::thir::{
2223
self, BindingMode, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir,
2324
};
@@ -30,13 +31,6 @@ use rustc_target::spec::abi::Abi;
3031

3132
use super::lints;
3233

33-
pub(crate) fn mir_built(
34-
tcx: TyCtxt<'_>,
35-
def: LocalDefId,
36-
) -> &rustc_data_structures::steal::Steal<Body<'_>> {
37-
tcx.alloc_steal_mir(mir_build(tcx, def))
38-
}
39-
4034
pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
4135
tcx: TyCtxt<'tcx>,
4236
def_id: LocalDefId,
@@ -54,7 +48,8 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
5448
}
5549

5650
/// Construct the MIR for a given `DefId`.
57-
fn mir_build<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
51+
pub(crate) fn mir_build<'tcx>(tcx: TyCtxtAt<'tcx>, def: LocalDefId) -> Body<'tcx> {
52+
let tcx = tcx.tcx;
5853
tcx.ensure_with_value().thir_abstract_const(def);
5954
if let Err(e) = tcx.check_match(def) {
6055
return construct_error(tcx, def, e);

compiler/rustc_mir_build/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ mod errors;
2222
pub mod lints;
2323
mod thir;
2424

25-
use rustc_middle::query::Providers;
25+
use rustc_middle::util::Providers;
2626

2727
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
2828

2929
pub fn provide(providers: &mut Providers) {
3030
providers.check_match = thir::pattern::check_match;
3131
providers.lit_to_const = thir::constant::lit_to_const;
32-
providers.mir_built = build::mir_built;
32+
providers.hooks.build_mir = build::mir_build;
3333
providers.closure_saved_names_of_captured_variables =
3434
build::closure_saved_names_of_captured_variables;
3535
providers.check_unsafety = check_unsafety::check_unsafety;

compiler/rustc_mir_transform/src/lib.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn provide(providers: &mut Providers) {
127127
cross_crate_inline::provide(providers);
128128
providers.queries = query::Providers {
129129
mir_keys,
130-
mir_const,
130+
mir_built,
131131
mir_const_qualif,
132132
mir_promoted,
133133
mir_drops_elaborated_and_const_checked,
@@ -259,9 +259,9 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
259259

260260
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
261261
// cannot yet be stolen), because `mir_promoted()`, which steals
262-
// from `mir_const()`, forces this query to execute before
262+
// from `mir_built()`, forces this query to execute before
263263
// performing the steal.
264-
let body = &tcx.mir_const(def).borrow();
264+
let body = &tcx.mir_built(def).borrow();
265265

266266
if body.return_ty().references_error() {
267267
// It's possible to reach here without an error being emitted (#121103).
@@ -279,19 +279,13 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
279279
validator.qualifs_in_return_place()
280280
}
281281

282-
/// Make MIR ready for const evaluation. This is run on all MIR, not just on consts!
283-
/// FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query).
284-
/// We used to have this for pre-miri MIR based const eval.
285-
fn mir_const(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
282+
fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
286283
// MIR unsafety check uses the raw mir, so make sure it is run.
287284
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
288285
tcx.ensure_with_value().mir_unsafety_check_result(def);
289286
}
290287

291-
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.
292-
tcx.ensure_with_value().has_ffi_unwind_calls(def);
293-
294-
let mut body = tcx.mir_built(def).steal();
288+
let mut body = tcx.build_mir(def);
295289

296290
pass_manager::dump_mir_for_phase_change(tcx, &body);
297291

@@ -339,7 +333,9 @@ fn mir_promoted(
339333
| DefKind::AnonConst => tcx.mir_const_qualif(def),
340334
_ => ConstQualifs::default(),
341335
};
342-
let mut body = tcx.mir_const(def).steal();
336+
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.
337+
tcx.ensure_with_value().has_ffi_unwind_calls(def);
338+
let mut body = tcx.mir_built(def).steal();
343339
if let Some(error_reported) = const_qualifs.tainted_by_errors {
344340
body.tainted_by_errors = Some(error_reported);
345341
}

tests/ui/binding/issue-53114-safety-checks.stderr

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
error[E0793]: reference to packed field is unaligned
2+
--> $DIR/issue-53114-safety-checks.rs:23:13
3+
|
4+
LL | let _ = &p.b;
5+
| ^^^^
6+
|
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
9+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
10+
11+
error[E0793]: reference to packed field is unaligned
12+
--> $DIR/issue-53114-safety-checks.rs:28:17
13+
|
14+
LL | let (_,) = (&p.b,);
15+
| ^^^^
16+
|
17+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
18+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
19+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
20+
121
error[E0133]: access to union field is unsafe and requires unsafe function or block
222
--> $DIR/issue-53114-safety-checks.rs:24:13
323
|
@@ -31,20 +51,20 @@ LL | let (_,) = (&u2.a,);
3151
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
3252

3353
error[E0793]: reference to packed field is unaligned
34-
--> $DIR/issue-53114-safety-checks.rs:23:13
54+
--> $DIR/issue-53114-safety-checks.rs:37:16
3555
|
36-
LL | let _ = &p.b;
37-
| ^^^^
56+
LL | let _: _ = &p.b;
57+
| ^^^^
3858
|
3959
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
4060
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
4161
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
4262

4363
error[E0793]: reference to packed field is unaligned
44-
--> $DIR/issue-53114-safety-checks.rs:28:17
64+
--> $DIR/issue-53114-safety-checks.rs:42:20
4565
|
46-
LL | let (_,) = (&p.b,);
47-
| ^^^^
66+
LL | let (_,): _ = (&p.b,);
67+
| ^^^^
4868
|
4969
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
5070
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
@@ -83,20 +103,20 @@ LL | let (_,): _ = (&u2.a,);
83103
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
84104

85105
error[E0793]: reference to packed field is unaligned
86-
--> $DIR/issue-53114-safety-checks.rs:37:16
106+
--> $DIR/issue-53114-safety-checks.rs:51:11
87107
|
88-
LL | let _: _ = &p.b;
89-
| ^^^^
108+
LL | match &p.b { _ => { } }
109+
| ^^^^
90110
|
91111
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
92112
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
93113
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
94114

95115
error[E0793]: reference to packed field is unaligned
96-
--> $DIR/issue-53114-safety-checks.rs:42:20
116+
--> $DIR/issue-53114-safety-checks.rs:56:12
97117
|
98-
LL | let (_,): _ = (&p.b,);
99-
| ^^^^
118+
LL | match (&p.b,) { (_,) => { } }
119+
| ^^^^
100120
|
101121
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
102122
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
@@ -134,26 +154,6 @@ LL | match (&u2.a,) { (_,) => { } }
134154
|
135155
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
136156

137-
error[E0793]: reference to packed field is unaligned
138-
--> $DIR/issue-53114-safety-checks.rs:51:11
139-
|
140-
LL | match &p.b { _ => { } }
141-
| ^^^^
142-
|
143-
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
144-
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
145-
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
146-
147-
error[E0793]: reference to packed field is unaligned
148-
--> $DIR/issue-53114-safety-checks.rs:56:12
149-
|
150-
LL | match (&p.b,) { (_,) => { } }
151-
| ^^^^
152-
|
153-
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
154-
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
155-
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
156-
157157
error: aborting due to 18 previous errors
158158

159159
Some errors have detailed explanations: E0133, E0793.

tests/ui/coroutine/clone-rpit.next.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ note: ...which requires promoting constants in MIR for `foo::{closure#0}`...
1414
|
1515
LL | move |_: ()| {
1616
| ^^^^^^^^^^^^
17-
note: ...which requires preparing `foo::{closure#0}` for borrow checking...
18-
--> $DIR/clone-rpit.rs:14:5
19-
|
20-
LL | move |_: ()| {
21-
| ^^^^^^^^^^^^
2217
note: ...which requires checking if `foo::{closure#0}` contains FFI-unwind calls...
2318
--> $DIR/clone-rpit.rs:14:5
2419
|

tests/ui/resolve/multiple_definitions_attribute_merging.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ LL | struct Dealigned<T>(u8, T);
1818
|
1919
= Box<dyn Any>
2020
query stack during panic:
21-
#0 [mir_const] preparing `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq` for borrow checking
22-
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
21+
#0 [mir_built] building MIR for `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
22+
#1 [check_unsafety] unsafety-checking `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
2323
end of query stack
2424
error: aborting due to 2 previous errors
2525

tests/ui/resolve/proc_macro_generated_packed.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | struct Dealigned<T>(u8, T);
99
|
1010
= Box<dyn Any>
1111
query stack during panic:
12-
#0 [mir_const] preparing `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq` for borrow checking
13-
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
12+
#0 [mir_built] building MIR for `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
13+
#1 [check_unsafety] unsafety-checking `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
1414
end of query stack
1515
error: aborting due to 1 previous error
1616

tests/ui/type-alias-enum-variants/self-in-enum-definition.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ note: ...which requires const checking `Alpha::V3::{constant#0}`...
3434
|
3535
LL | V3 = Self::V1 {} as u8 + 2,
3636
| ^^^^^^^^^^^^^^^^^^^^^
37-
note: ...which requires preparing `Alpha::V3::{constant#0}` for borrow checking...
38-
--> $DIR/self-in-enum-definition.rs:5:10
39-
|
40-
LL | V3 = Self::V1 {} as u8 + 2,
41-
| ^^^^^^^^^^^^^^^^^^^^^
4237
note: ...which requires building MIR for `Alpha::V3::{constant#0}`...
4338
--> $DIR/self-in-enum-definition.rs:5:10
4439
|

0 commit comments

Comments
 (0)