Skip to content

Commit 6f64eb1

Browse files
Make THIR building a stealable query
1 parent bd80018 commit 6f64eb1

File tree

20 files changed

+60
-42
lines changed

20 files changed

+60
-42
lines changed

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ macro_rules! arena_types {
1414
[] layouts: rustc_target::abi::Layout,
1515
// AdtDef are interned and compared by address
1616
[] adt_def: rustc_middle::ty::AdtDef,
17+
[] steal_thir: rustc_data_structures::steal::Steal<rustc_middle::thir::Thir<$tcx>>,
1718
[] steal_mir: rustc_data_structures::steal::Steal<rustc_middle::mir::Body<$tcx>>,
1819
[decode] mir: rustc_middle::mir::Body<$tcx>,
1920
[] steal_promoted:

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
285285
// required that their size stay the same, but we don't want to change
286286
// it inadvertently. This assert just ensures we're aware of any change.
287287
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
288-
static_assert_size!(DepNode, 17);
288+
static_assert_size!(DepNode, 18);
289289

290290
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
291291
static_assert_size!(DepNode, 24);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ rustc_queries! {
220220
desc { "checking if the crate is_panic_runtime" }
221221
}
222222

223+
/// Fetch the THIR for a given body.
224+
query thir_body(key: ty::WithOptConstParam<LocalDefId>) -> (&'tcx Steal<thir::Thir<'tcx>>, thir::ExprId) {
225+
desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key.did.to_def_id()) }
226+
}
227+
223228
/// Set of all the `DefId`s in this crate that have MIR associated with
224229
/// them. This includes all the body owners, but also things like struct
225230
/// constructors.

compiler/rustc_middle/src/thir.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,29 @@ use std::fmt;
2424
use std::ops::Index;
2525

2626
newtype_index! {
27+
#[derive(HashStable)]
2728
pub struct ArmId {
2829
DEBUG_FORMAT = "a{}"
2930
}
3031
}
3132

3233
newtype_index! {
34+
#[derive(HashStable)]
3335
pub struct ExprId {
3436
DEBUG_FORMAT = "e{}"
3537
}
3638
}
3739

3840
newtype_index! {
41+
#[derive(HashStable)]
3942
pub struct StmtId {
4043
DEBUG_FORMAT = "s{}"
4144
}
4245
}
4346

4447
macro_rules! thir_with_elements {
4548
($($name:ident: $id:ty => $value:ty,)*) => {
49+
#[derive(Debug, HashStable)]
4650
pub struct Thir<'tcx> {
4751
$(
4852
pub $name: IndexVec<$id, $value>,
@@ -76,13 +80,13 @@ thir_with_elements! {
7680
stmts: StmtId => Stmt<'tcx>,
7781
}
7882

79-
#[derive(Copy, Clone, Debug)]
83+
#[derive(Copy, Clone, Debug, HashStable)]
8084
pub enum LintLevel {
8185
Inherited,
8286
Explicit(hir::HirId),
8387
}
8488

85-
#[derive(Debug)]
89+
#[derive(Debug, HashStable)]
8690
pub struct Block {
8791
pub targeted_by_break: bool,
8892
pub region_scope: region::Scope,
@@ -93,21 +97,21 @@ pub struct Block {
9397
pub safety_mode: BlockSafety,
9498
}
9599

96-
#[derive(Copy, Clone, Debug)]
100+
#[derive(Copy, Clone, Debug, HashStable)]
97101
pub enum BlockSafety {
98102
Safe,
99103
ExplicitUnsafe(hir::HirId),
100104
PushUnsafe,
101105
PopUnsafe,
102106
}
103107

104-
#[derive(Debug)]
108+
#[derive(Debug, HashStable)]
105109
pub struct Stmt<'tcx> {
106110
pub kind: StmtKind<'tcx>,
107111
pub opt_destruction_scope: Option<region::Scope>,
108112
}
109113

110-
#[derive(Debug)]
114+
#[derive(Debug, HashStable)]
111115
pub enum StmtKind<'tcx> {
112116
Expr {
113117
/// scope for this statement; may be used as lifetime of temporaries
@@ -157,7 +161,7 @@ rustc_data_structures::static_assert_size!(Expr<'_>, 144);
157161
/// MIR simplifications are already done in the impl of `Thir`. For
158162
/// example, method calls and overloaded operators are absent: they are
159163
/// expected to be converted into `Expr::Call` instances.
160-
#[derive(Debug)]
164+
#[derive(Debug, HashStable)]
161165
pub struct Expr<'tcx> {
162166
/// type of this expression
163167
pub ty: Ty<'tcx>,
@@ -173,7 +177,7 @@ pub struct Expr<'tcx> {
173177
pub kind: ExprKind<'tcx>,
174178
}
175179

176-
#[derive(Debug)]
180+
#[derive(Debug, HashStable)]
177181
pub enum ExprKind<'tcx> {
178182
Scope {
179183
region_scope: region::Scope,
@@ -363,19 +367,19 @@ pub enum ExprKind<'tcx> {
363367
},
364368
}
365369

366-
#[derive(Debug)]
370+
#[derive(Debug, HashStable)]
367371
pub struct FieldExpr {
368372
pub name: Field,
369373
pub expr: ExprId,
370374
}
371375

372-
#[derive(Debug)]
376+
#[derive(Debug, HashStable)]
373377
pub struct FruInfo<'tcx> {
374378
pub base: ExprId,
375379
pub field_types: Box<[Ty<'tcx>]>,
376380
}
377381

378-
#[derive(Debug)]
382+
#[derive(Debug, HashStable)]
379383
pub struct Arm<'tcx> {
380384
pub pattern: Pat<'tcx>,
381385
pub guard: Option<Guard<'tcx>>,
@@ -385,19 +389,19 @@ pub struct Arm<'tcx> {
385389
pub span: Span,
386390
}
387391

388-
#[derive(Debug)]
392+
#[derive(Debug, HashStable)]
389393
pub enum Guard<'tcx> {
390394
If(ExprId),
391395
IfLet(Pat<'tcx>, ExprId),
392396
}
393397

394-
#[derive(Copy, Clone, Debug)]
398+
#[derive(Copy, Clone, Debug, HashStable)]
395399
pub enum LogicalOp {
396400
And,
397401
Or,
398402
}
399403

400-
#[derive(Debug)]
404+
#[derive(Debug, HashStable)]
401405
pub enum InlineAsmOperand<'tcx> {
402406
In {
403407
reg: InlineAsmRegOrRegClass,
@@ -431,19 +435,19 @@ pub enum InlineAsmOperand<'tcx> {
431435
},
432436
}
433437

434-
#[derive(Copy, Clone, Debug, PartialEq)]
438+
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
435439
pub enum BindingMode {
436440
ByValue,
437441
ByRef(BorrowKind),
438442
}
439443

440-
#[derive(Clone, Debug, PartialEq)]
444+
#[derive(Clone, Debug, PartialEq, HashStable)]
441445
pub struct FieldPat<'tcx> {
442446
pub field: Field,
443447
pub pattern: Pat<'tcx>,
444448
}
445449

446-
#[derive(Clone, Debug, PartialEq)]
450+
#[derive(Clone, Debug, PartialEq, HashStable)]
447451
pub struct Pat<'tcx> {
448452
pub ty: Ty<'tcx>,
449453
pub span: Span,
@@ -456,7 +460,7 @@ impl<'tcx> Pat<'tcx> {
456460
}
457461
}
458462

459-
#[derive(Copy, Clone, Debug, PartialEq)]
463+
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
460464
pub struct PatTyProj<'tcx> {
461465
pub user_ty: CanonicalUserType<'tcx>,
462466
}
@@ -483,7 +487,7 @@ impl<'tcx> PatTyProj<'tcx> {
483487
}
484488
}
485489

486-
#[derive(Copy, Clone, Debug, PartialEq)]
490+
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
487491
pub struct Ascription<'tcx> {
488492
pub user_ty: PatTyProj<'tcx>,
489493
/// Variance to use when relating the type `user_ty` to the **type of the value being
@@ -508,7 +512,7 @@ pub struct Ascription<'tcx> {
508512
pub user_ty_span: Span,
509513
}
510514

511-
#[derive(Clone, Debug, PartialEq)]
515+
#[derive(Clone, Debug, PartialEq, HashStable)]
512516
pub enum PatKind<'tcx> {
513517
Wild,
514518

@@ -586,7 +590,7 @@ pub enum PatKind<'tcx> {
586590
},
587591
}
588592

589-
#[derive(Copy, Clone, Debug, PartialEq)]
593+
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
590594
pub struct PatRange<'tcx> {
591595
pub lo: &'tcx ty::Const<'tcx>,
592596
pub hi: &'tcx ty::Const<'tcx>,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath, ObjectLifetime
1313
use crate::middle::stability;
1414
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
1515
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
16+
use crate::thir::Thir;
1617
use crate::traits;
1718
use crate::ty::query::{self, OnDiskCache, TyCtxtAt};
1819
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
@@ -1041,6 +1042,10 @@ impl<'tcx> TyCtxt<'tcx> {
10411042
}
10421043
}
10431044

1045+
pub fn alloc_steal_thir(self, thir: Thir<'tcx>) -> &'tcx Steal<Thir<'tcx>> {
1046+
self.arena.alloc(Steal::new(thir))
1047+
}
1048+
10441049
pub fn alloc_steal_mir(self, mir: Body<'tcx>) -> &'tcx Steal<Body<'tcx>> {
10451050
self.arena.alloc(Steal::new(mir))
10461051
}

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::mir::interpret::GlobalId;
1818
use crate::mir::interpret::{ConstAlloc, LitToConstError, LitToConstInput};
1919
use crate::mir::interpret::{ConstValue, EvalToAllocationRawResult, EvalToConstValueResult};
2020
use crate::mir::mono::CodegenUnit;
21+
use crate::thir;
2122
use crate::traits::query::{
2223
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
2324
CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
669669
}
670670
}
671671

672-
#[derive(Debug, Copy, Clone)]
672+
#[derive(Debug, Copy, Clone, HashStable)]
673673
pub enum UpvarSubsts<'tcx> {
674674
Closure(SubstsRef<'tcx>),
675675
Generator(SubstsRef<'tcx>),

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::build::matches::ArmHasGuard;
22
use crate::build::ForGuard::OutsideGuard;
33
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
4-
use rustc_middle::thir::*;
54
use rustc_middle::mir::*;
5+
use rustc_middle::thir::*;
66
use rustc_session::lint::builtin::UNSAFE_OP_IN_UNSAFE_FN;
77
use rustc_session::lint::Level;
88
use rustc_span::Span;

compiler/rustc_mir_build/src/build/expr/as_constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! See docs in build/expr/mod.rs
22
33
use crate::build::Builder;
4-
use rustc_middle::thir::*;
54
use rustc_middle::mir::*;
5+
use rustc_middle::thir::*;
66
use rustc_middle::ty::CanonicalUserTypeAnnotation;
77

88
impl<'a, 'tcx> Builder<'a, 'tcx> {

compiler/rustc_mir_build/src/build/expr/as_operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use crate::build::expr::category::Category;
44
use crate::build::{BlockAnd, BlockAndExtension, Builder};
5-
use rustc_middle::thir::*;
65
use rustc_middle::middle::region;
76
use rustc_middle::mir::*;
7+
use rustc_middle::thir::*;
88

99
impl<'a, 'tcx> Builder<'a, 'tcx> {
1010
/// Returns an operand suitable for use until the end of the current

compiler/rustc_mir_build/src/build/expr/as_place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use crate::build::expr::category::Category;
44
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
55
use crate::build::{BlockAnd, BlockAndExtension, Builder};
6-
use rustc_middle::thir::*;
76
use rustc_hir::def_id::DefId;
87
use rustc_hir::HirId;
98
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
109
use rustc_middle::middle::region;
1110
use rustc_middle::mir::AssertKind::BoundsCheck;
1211
use rustc_middle::mir::*;
12+
use rustc_middle::thir::*;
1313
use rustc_middle::ty::AdtDef;
1414
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
1515
use rustc_span::Span;

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use rustc_index::vec::Idx;
55
use crate::build::expr::as_place::PlaceBase;
66
use crate::build::expr::category::{Category, RvalueFunc};
77
use crate::build::{BlockAnd, BlockAndExtension, Builder};
8-
use rustc_middle::thir::*;
98
use rustc_middle::middle::region;
109
use rustc_middle::mir::AssertKind;
1110
use rustc_middle::mir::Place;
1211
use rustc_middle::mir::*;
12+
use rustc_middle::thir::*;
1313
use rustc_middle::ty::{self, Ty, UpvarSubsts};
1414
use rustc_span::Span;
1515

compiler/rustc_mir_build/src/build/expr/as_temp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use crate::build::scope::DropKind;
44
use crate::build::{BlockAnd, BlockAndExtension, Builder};
5-
use rustc_middle::thir::*;
65
use rustc_data_structures::stack::ensure_sufficient_stack;
76
use rustc_middle::middle::region;
87
use rustc_middle::mir::*;
8+
use rustc_middle::thir::*;
99

1010
impl<'a, 'tcx> Builder<'a, 'tcx> {
1111
/// Compile `expr` into a fresh temporary. This is used when building

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
33
use crate::build::expr::category::{Category, RvalueFunc};
44
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
5-
use rustc_middle::thir::*;
65
use rustc_ast::InlineAsmOptions;
76
use rustc_data_structures::fx::FxHashMap;
87
use rustc_data_structures::stack::ensure_sufficient_stack;
98
use rustc_hir as hir;
109
use rustc_index::vec::Idx;
1110
use rustc_middle::mir::*;
11+
use rustc_middle::thir::*;
1212
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};
1313
use std::iter;
1414

compiler/rustc_mir_build/src/build/expr/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::build::scope::BreakableTarget;
22
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
3-
use rustc_middle::thir::*;
43
use rustc_middle::middle::region;
54
use rustc_middle::mir::*;
5+
use rustc_middle::thir::*;
66

77
impl<'a, 'tcx> Builder<'a, 'tcx> {
88
/// Builds a block of MIR statements to evaluate the THIR `expr`.

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::build;
22
use crate::build::expr::as_place::PlaceBuilder;
33
use crate::build::scope::DropKind;
4-
use crate::thir::build_thir;
54
use crate::thir::pattern::pat_from_hir;
65
use rustc_attr::{self as attr, UnwindAttr};
76
use rustc_errors::ErrorReported;
@@ -106,7 +105,8 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
106105
};
107106

108107
let body = tcx.hir().body(body_id);
109-
let (thir, expr) = build_thir(tcx, def, &body.value);
108+
let (thir, expr) = tcx.thir_body(def);
109+
let thir = thir.steal();
110110
let ty = tcx.type_of(fn_def_id);
111111
let mut abi = fn_sig.abi;
112112
let implicit_argument = match ty.kind() {
@@ -214,8 +214,8 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
214214

215215
let return_ty = typeck_results.node_type(id);
216216

217-
let ast_expr = &tcx.hir().body(body_id).value;
218-
let (thir, expr) = build_thir(tcx, def, ast_expr);
217+
let (thir, expr) = tcx.thir_body(def);
218+
let thir = thir.steal();
219219

220220
build::construct_const(&thir, &infcx, expr, def, id, return_ty, return_ty_span)
221221
};

compiler/rustc_mir_build/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ pub fn provide(providers: &mut Providers) {
3131
providers.mir_built = build::mir_built;
3232
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
3333
providers.thir_check_unsafety_for_const_arg = check_unsafety::thir_check_unsafety_for_const_arg;
34+
providers.thir_body = thir::cx::thir_body;
3435
}

0 commit comments

Comments
 (0)