Skip to content

Commit 529b0b4

Browse files
committed
Optionally don't steal the THIR
1 parent 9b0268a commit 529b0b4

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ fn test_unstable_options_tracking_hash() {
715715
untracked!(no_analysis, true);
716716
untracked!(no_leak_check, true);
717717
untracked!(no_parallel_backend, true);
718+
untracked!(no_steal_thir, true);
718719
untracked!(parse_crate_root_only, true);
719720
// `pre_link_arg` is omitted because it just forwards to `pre_link_args`.
720721
untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ rustc_queries! {
535535
separate_provide_extern
536536
}
537537

538-
/// Fetch the THIR for a given body.
538+
/// Fetch the THIR for a given body. The THIR body gets stolen by unsafety checking unless
539+
/// `-Zno-steal-thir` is on.
539540
query thir_body(key: LocalDefId) -> Result<(&'tcx Steal<thir::Thir<'tcx>>, thir::ExprId), ErrorGuaranteed> {
540541
// Perf tests revealed that hashing THIR is inefficient (see #85729).
541542
no_hash

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,14 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
201201
/// Handle closures/coroutines/inline-consts, which is unsafecked with their parent body.
202202
fn visit_inner_body(&mut self, def: LocalDefId) {
203203
if let Ok((inner_thir, expr)) = self.tcx.thir_body(def) {
204-
// Runs all other queries that depend on THIR.
204+
// Run all other queries that depend on THIR.
205205
self.tcx.ensure_done().mir_built(def);
206-
let inner_thir = &inner_thir.steal();
206+
let inner_thir = if self.tcx.sess.opts.unstable_opts.no_steal_thir {
207+
&inner_thir.borrow()
208+
} else {
209+
// We don't have other use for the THIR. Steal it to reduce memory usage.
210+
&inner_thir.steal()
211+
};
207212
let hir_context = self.tcx.local_def_id_to_hir_id(def);
208213
let safety_context = mem::replace(&mut self.safety_context, SafetyContext::Safe);
209214
let mut inner_visitor = UnsafetyVisitor {
@@ -1157,7 +1162,12 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
11571162
let Ok((thir, expr)) = tcx.thir_body(def) else { return };
11581163
// Runs all other queries that depend on THIR.
11591164
tcx.ensure_done().mir_built(def);
1160-
let thir = &thir.steal();
1165+
let thir = if self.tcx.sess.opts.unstable_opts.no_steal_thir {
1166+
&thir.borrow()
1167+
} else {
1168+
// We don't have other use for the THIR. Steal it to reduce memory usage.
1169+
&thir.steal()
1170+
};
11611171

11621172
let hir_id = tcx.local_def_id_to_hir_id(def);
11631173
let safety_context = tcx.hir_fn_sig_by_hir_id(hir_id).map_or(SafetyContext::Safe, |fn_sig| {

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,8 @@ options! {
23662366
"run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"),
23672367
no_profiler_runtime: bool = (false, parse_no_value, [TRACKED],
23682368
"prevent automatic injection of the profiler_builtins crate"),
2369+
no_steal_thir: bool = (false, parse_bool, [UNTRACKED],
2370+
"don't steal the THIR when we're done with it; useful for rustc drivers (default: no)"),
23692371
no_trait_vptr: bool = (false, parse_no_value, [TRACKED],
23702372
"disable generation of trait vptr in vtable for upcasting"),
23712373
no_unique_section_names: bool = (false, parse_bool, [TRACKED],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `no-steal-thir`
2+
3+
By default, to save on memory, the THIR body (obtained from the `tcx.thir_body` query) is stolen
4+
once no longer used. This is inconvenient for authors of rustc drivers who want to access the THIR.
5+
6+
This option disables the stealing. This has no observable effect on compiler behavior, only on
7+
memory usage.

0 commit comments

Comments
 (0)