Move ensure_sufficient_stack to data_structures

We anticipate this to have uses in all sorts of crates and keeping it in
`rustc_data_structures` enables access to it from more locations without
necessarily pulling in the large `librustc` crate.
This commit is contained in:
Simonas Kazlauskas 2020-03-14 20:13:55 +02:00 committed by Mateusz Mikuła
parent 968f442c7c
commit a5c5365031
16 changed files with 33 additions and 33 deletions

View file

@ -3161,7 +3161,6 @@ checksum = "81dfcfbb0ddfd533abf8c076e3b49d1e5042d1962526a12ce2c66d514b24cca3"
dependencies = [ dependencies = [
"rustc-ap-rustc_data_structures", "rustc-ap-rustc_data_structures",
"smallvec 1.0.0", "smallvec 1.0.0",
"stacker",
] ]
[[package]] [[package]]
@ -3706,6 +3705,7 @@ dependencies = [
"serialize", "serialize",
"smallvec 1.0.0", "smallvec 1.0.0",
"stable_deref_trait", "stable_deref_trait",
"stacker",
"winapi 0.3.8", "winapi 0.3.8",
] ]

View file

@ -3,11 +3,11 @@ use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericAr
use rustc_ast::ast::*; use rustc_ast::ast::*;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_ast::ptr::P as AstP; use rustc_ast::ptr::P as AstP;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::struct_span_err; use rustc_errors::struct_span_err;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned}; use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};

View file

@ -2,9 +2,9 @@ use super::{ImplTraitContext, LoweringContext, ParamMode};
use rustc_ast::ast::*; use rustc_ast::ast::*;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_span::{source_map::Spanned, Span}; use rustc_span::{source_map::Spanned, Span};
impl<'a, 'hir> LoweringContext<'a, 'hir> { impl<'a, 'hir> LoweringContext<'a, 'hir> {

View file

@ -28,6 +28,7 @@ rustc_index = { path = "../librustc_index", package = "rustc_index" }
bitflags = "1.2.1" bitflags = "1.2.1"
measureme = "0.7.1" measureme = "0.7.1"
libc = "0.2" libc = "0.2"
stacker = "0.1.6"
[dependencies.parking_lot] [dependencies.parking_lot]
version = "0.10" version = "0.10"

View file

@ -80,6 +80,7 @@ pub mod stable_set;
#[macro_use] #[macro_use]
pub mod stable_hasher; pub mod stable_hasher;
pub mod sharded; pub mod sharded;
pub mod stack;
pub mod sync; pub mod sync;
pub mod thin_vec; pub mod thin_vec;
pub mod tiny_list; pub mod tiny_list;

View file

@ -0,0 +1,17 @@
// This is the amount of bytes that need to be left on the stack before increasing the size.
// It must be at least as large as the stack required by any code that does not call
// `ensure_sufficient_stack`.
const RED_ZONE: usize = 100 * 1024; // 100k
// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
// on. This flag has performance relevant characteristics. Don't set it too high.
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
/// from this.
///
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
}

View file

@ -34,4 +34,3 @@ byteorder = { version = "1.3" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] } smallvec = { version = "1.0", features = ["union", "may_dangle"] }
measureme = "0.7.1" measureme = "0.7.1"
rustc_session = { path = "../librustc_session" } rustc_session = { path = "../librustc_session" }
stacker = "0.1.6"

View file

@ -13,24 +13,6 @@ use rustc_span::symbol::{sym, Symbol};
use std::num::IntErrorKind; use std::num::IntErrorKind;
// This is the amount of bytes that need to be left on the stack before increasing the size.
// It must be at least as large as the stack required by any code that does not call
// `ensure_sufficient_stack`.
const RED_ZONE: usize = 100 * 1024; // 100k
// Ony the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
// on. This flag has performance relevant characteristics. Don't set it too high.
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
/// from this.
///
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
pub fn ensure_sufficient_stack<R, F: FnOnce() -> R>(f: F) -> R {
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
}
pub fn update_limits(sess: &Session, krate: &ast::Crate) { pub fn update_limits(sess: &Session, krate: &ast::Crate) {
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128); update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576); update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);

View file

@ -1,12 +1,12 @@
pub use self::def_id_forest::DefIdForest; pub use self::def_id_forest::DefIdForest;
use crate::middle::limits::ensure_sufficient_stack;
use crate::ty; use crate::ty;
use crate::ty::context::TyCtxt; use crate::ty::context::TyCtxt;
use crate::ty::TyKind::*; use crate::ty::TyKind::*;
use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef}; use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
use crate::ty::{AdtKind, Visibility}; use crate::ty::{AdtKind, Visibility};
use crate::ty::{DefId, SubstsRef}; use crate::ty::{DefId, SubstsRef};
use rustc_data_structures::stack::ensure_sufficient_stack;
mod def_id_forest; mod def_id_forest;

View file

@ -69,7 +69,7 @@ impl QueryContext for TyCtxt<'tcx> {
// Use the `ImplicitCtxt` while we execute the query. // Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&new_icx, |_| { tls::enter_context(&new_icx, |_| {
crate::middle::limits::ensure_sufficient_stack(|| compute(*self)) rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
}) })
}) })
} }

View file

@ -369,7 +369,7 @@ fn collect_items_rec<'tcx>(
recursion_depth_reset = Some(check_recursion_limit(tcx, instance, recursion_depths)); recursion_depth_reset = Some(check_recursion_limit(tcx, instance, recursion_depths));
check_type_length_limit(tcx, instance); check_type_length_limit(tcx, instance);
rustc::middle::limits::ensure_sufficient_stack(|| { rustc_data_structures::stack::ensure_sufficient_stack(|| {
collect_neighbours(tcx, instance, &mut neighbors); collect_neighbours(tcx, instance, &mut neighbors);
}); });
} }
@ -1148,7 +1148,7 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
Some(GlobalAlloc::Memory(alloc)) => { Some(GlobalAlloc::Memory(alloc)) => {
trace!("collecting {:?} with {:#?}", alloc_id, alloc); trace!("collecting {:?} with {:#?}", alloc_id, alloc);
for &((), inner) in alloc.relocations().values() { for &((), inner) in alloc.relocations().values() {
rustc_middle::limits::ensure_sufficient_stack(|| { rustc_data_structures::stack::ensure_sufficient_stack(|| {
collect_miri(tcx, inner, output); collect_miri(tcx, inner, output);
}); });
} }

View file

@ -3,8 +3,8 @@
use crate::build::scope::DropKind; use crate::build::scope::DropKind;
use crate::build::{BlockAnd, BlockAndExtension, Builder}; use crate::build::{BlockAnd, BlockAndExtension, Builder};
use crate::hair::*; use crate::hair::*;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_middle::middle::region; use rustc_middle::middle::region;
use rustc_middle::mir::*; use rustc_middle::mir::*;

View file

@ -18,9 +18,9 @@ use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime}; use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
use crate::traits::error_reporting::InferCtxtExt; use crate::traits::error_reporting::InferCtxtExt;
use rustc_ast::ast::Ident; use rustc_ast::ast::Ident;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::ErrorReported; use rustc_errors::ErrorReported;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder}; use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::subst::{InternalSubsts, Subst}; use rustc_middle::ty::subst::{InternalSubsts, Subst};
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness}; use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};

View file

@ -7,8 +7,8 @@ use crate::infer::canonical::OriginalQueryValues;
use crate::infer::{InferCtxt, InferOk}; use crate::infer::{InferCtxt, InferOk};
use crate::traits::error_reporting::InferCtxtExt; use crate::traits::error_reporting::InferCtxtExt;
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal}; use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_infer::traits::Normalized; use rustc_infer::traits::Normalized;
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder}; use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::subst::Subst; use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};

View file

@ -37,12 +37,12 @@ use crate::traits::error_reporting::InferCtxtExt;
use crate::traits::project::ProjectionCacheKeyExt; use crate::traits::project::ProjectionCacheKeyExt;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items; use rustc_hir::lang_items;
use rustc_index::bit_set::GrowableBitSet; use rustc_index::bit_set::GrowableBitSet;
use rustc_middle::dep_graph::{DepKind, DepNodeIndex}; use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
use rustc_middle::limits::ensure_sufficient_stack;
use rustc_middle::ty::fast_reject; use rustc_middle::ty::fast_reject;
use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::relate::TypeRelation;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};

View file

@ -191,12 +191,12 @@ fn dtorck_constraint_for_ty<'tcx>(
ty::Array(ety, _) | ty::Slice(ety) => { ty::Array(ety, _) | ty::Slice(ety) => {
// single-element containers, behave like their element // single-element containers, behave like their element
rustc_middle::limits::ensure_sufficient_stack(|| { rustc_data_structures::stack::ensure_sufficient_stack(|| {
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints) dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)
})?; })?;
} }
ty::Tuple(tys) => rustc_middle::limits::ensure_sufficient_stack(|| { ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in tys.iter() { for ty in tys.iter() {
dtorck_constraint_for_ty( dtorck_constraint_for_ty(
tcx, tcx,
@ -210,7 +210,7 @@ fn dtorck_constraint_for_ty<'tcx>(
Ok::<_, NoSolution>(()) Ok::<_, NoSolution>(())
})?, })?,
ty::Closure(_, substs) => rustc_middle::limits::ensure_sufficient_stack(|| { ty::Closure(_, substs) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in substs.as_closure().upvar_tys() { for ty in substs.as_closure().upvar_tys() {
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?; dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
} }