Skip to content

Commit 313c9c2

Browse files
authored
Unrolled build for #150920
Rollup merge of #150920 - hook-build-mir, r=tiif Use a hook to decouple `rustc_mir_transform` from `rustc_mir_build` I noticed that the only point of direct contact between the `rustc_mir_transform` and `rustc_mir_build` crates is a single `build_mir` function, which could easily be changed to a hook function instead. By making that function a hook, we can make `rustc_mir_transform` no longer have a dependency on `rustc_mir_build`, allowing them to be built/rebuilt independently. That should hopefully allow slightly more parallelism in clean builds and incremental rebuilds of the compiler.
2 parents 1377169 + 539e855 commit 313c9c2

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,6 @@ dependencies = [
43604360
"rustc_infer",
43614361
"rustc_macros",
43624362
"rustc_middle",
4363-
"rustc_mir_build",
43644363
"rustc_mir_dataflow",
43654364
"rustc_session",
43664365
"rustc_span",

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ declare_hooks! {
102102
/// Ensure the given scalar is valid for the given type.
103103
/// This checks non-recursive runtime validity.
104104
hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
105+
106+
/// **Do not call this directly; call the `mir_built` query instead.**
107+
///
108+
/// Creates the MIR for a given `DefId`, including unreachable code.
109+
hook build_mir_inner_impl(def: LocalDefId) -> mir::Body<'tcx>;
105110
}
106111

107112
#[cold]

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
6464
.collect()
6565
}
6666

67-
/// Create the MIR for a given `DefId`, including unreachable code. Do not call
68-
/// this directly; instead use the cached version via `mir_built`.
69-
pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
67+
/// Create the MIR for a given `DefId`, including unreachable code.
68+
///
69+
/// This is the implementation of hook `build_mir_inner_impl`, which should only
70+
/// be called by the query `mir_built`.
71+
pub(crate) fn build_mir_inner_impl<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
7072
tcx.ensure_done().thir_abstract_const(def);
7173
if let Err(e) = tcx.ensure_ok().check_match(def) {
7274
return construct_error(tcx, def, e);

compiler/rustc_mir_build/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// The `builder` module used to be named `build`, but that was causing GitHub's
1313
// "Go to file" feature to silently ignore all files in the module, probably
1414
// because it assumes that "build" is a build-output directory. See #134365.
15-
pub mod builder;
15+
mod builder;
1616
mod check_tail_calls;
1717
mod check_unsafety;
1818
mod errors;
@@ -30,4 +30,5 @@ pub fn provide(providers: &mut Providers) {
3030
providers.check_unsafety = check_unsafety::check_unsafety;
3131
providers.check_tail_calls = check_tail_calls::check_tail_calls;
3232
providers.thir_body = thir::cx::thir_body;
33+
providers.hooks.build_mir_inner_impl = builder::build_mir_inner_impl;
3334
}

compiler/rustc_mir_transform/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ rustc_index = { path = "../rustc_index" }
2020
rustc_infer = { path = "../rustc_infer" }
2121
rustc_macros = { path = "../rustc_macros" }
2222
rustc_middle = { path = "../rustc_middle" }
23-
rustc_mir_build = { path = "../rustc_mir_build" }
2423
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }
2524
rustc_session = { path = "../rustc_session" }
2625
rustc_span = { path = "../rustc_span" }

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use rustc_middle::mir::{
3030
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
3131
use rustc_middle::util::Providers;
3232
use rustc_middle::{bug, query, span_bug};
33-
use rustc_mir_build::builder::build_mir;
3433
use rustc_span::source_map::Spanned;
3534
use rustc_span::{DUMMY_SP, sym};
3635
use tracing::debug;
@@ -378,8 +377,11 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
378377
validator.qualifs_in_return_place()
379378
}
380379

380+
/// Implementation of the `mir_built` query.
381381
fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
382-
let mut body = build_mir(tcx, def);
382+
// Delegate to the main MIR building code in the `rustc_mir_build` crate.
383+
// This is the one place that is allowed to call `build_mir_inner_impl`.
384+
let mut body = tcx.build_mir_inner_impl(def);
383385

384386
// Identifying trivial consts based on their mir_built is easy, but a little wasteful.
385387
// Trying to push this logic earlier in the compiler and never even produce the Body would

0 commit comments

Comments
 (0)