Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c8e16ec

Browse files
committed
Auto merge of rust-lang#139576 - davidtwco:paramenv-multiple-fields, r=<try>
split `ParamEnv::caller_bounds` into multiple fields This patch splits `ParamEnv::caller_bounds` into multiple fields, one for each `ClauseKind`. This is intended to be a performance optimisation, avoiding unnecessary iteration over large `ParamEnv`s when most kinds of clauses aren't relevant in any given context. I make this change in three steps (reflected in the commits): 1. Changing the interface of `ParamEnv` - Replace `ParamEnv::caller_bounds` with functions returning iterators over specific kinds of clause, and then updating all users. A `ParamEnv::all_clauses` function, identical in behaviour to `ParamEnv::caller_bounds` remains for the handful of cases where iteration over all clauses is desirable. 2. Interning `ParamEnv` - In anticipation of changing the internals of `ParamEnv`, to avoid the type growing and to keep it `Copy`, `ParamEnv` is interned, becoming a pointer to `ParamEnvInner`. 3. Changing the internals of `ParamEnv` - Replacing the `caller_bounds` field with a field for each type of clause and updating the creation of `ParamEnv` to partition a sequence of `Clause`s into the appropriate fields. This is intended to help improve the performance of rust-lang#137944, which results in many new predicates in the `ParamEnv`, and in particular, lots of new host effect predicates which can't be fast-path'd away. As part of that work, I've experimented with variations on this patch which had worse performance: - Uninterned `ParamEnv` - `Box<[Clause<'tcx>]>` fields rather than `Clauses<'tcx>` - Box<[SpecificPredicateType<'tcx>]>` fields rather than `Clauses<'tcx>` r? types
2 parents 97c966b + 99aca75 commit c8e16ec

File tree

114 files changed

+716
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+716
-480
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
254254
let place = &self.move_data.move_paths[mpi].place;
255255
let ty = place.ty(self.body, self.infcx.tcx).ty;
256256

257-
if self.infcx.param_env.caller_bounds().iter().any(|c| {
258-
c.as_trait_clause().is_some_and(|pred| {
259-
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
260-
})
257+
if self.infcx.param_env.trait_clauses().any(|pred| {
258+
pred.skip_binder().self_ty() == ty && self.infcx.tcx.is_fn_trait(pred.def_id())
261259
}) {
262260
// Suppress the next suggestion since we don't want to put more bounds onto
263261
// something that already has `Fn`-like bounds (or is a closure), so we can't

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
226226
// Normalize the assumptions we use to borrowck the program.
227227
let mut constraints = vec![];
228228
let mut known_type_outlives_obligations = vec![];
229-
for bound in param_env.caller_bounds() {
230-
if let Some(outlives) = bound.as_type_outlives_clause() {
231-
self.normalize_and_push_type_outlives_obligation(
232-
outlives,
233-
span,
234-
&mut known_type_outlives_obligations,
235-
&mut constraints,
236-
);
237-
};
229+
for outlives in param_env.type_outlives_clauses() {
230+
self.normalize_and_push_type_outlives_obligation(
231+
outlives,
232+
span,
233+
&mut known_type_outlives_obligations,
234+
&mut constraints,
235+
);
238236
}
239237

240238
let unnormalized_input_output_tys = self

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
394394
let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() {
395395
let instance = ty::Instance::expect_resolve(
396396
fx.tcx,
397-
ty::TypingEnv::fully_monomorphized(),
397+
ty::TypingEnv::fully_monomorphized(fx.tcx),
398398
def_id,
399399
fn_args,
400400
source_info.span,

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ fn codegen_stmt<'tcx>(
679679
let func_ref = fx.get_function_ref(
680680
Instance::resolve_for_fn_ptr(
681681
fx.tcx,
682-
ty::TypingEnv::fully_monomorphized(),
682+
ty::TypingEnv::fully_monomorphized(fx.tcx),
683683
def_id,
684684
args,
685685
)
@@ -730,8 +730,10 @@ fn codegen_stmt<'tcx>(
730730

731731
fn is_wide_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
732732
ty.builtin_deref(true).is_some_and(|pointee_ty| {
733-
fx.tcx
734-
.type_has_metadata(pointee_ty, ty::TypingEnv::fully_monomorphized())
733+
fx.tcx.type_has_metadata(
734+
pointee_ty,
735+
ty::TypingEnv::fully_monomorphized(fx.tcx),
736+
)
735737
})
736738
}
737739

@@ -862,7 +864,7 @@ fn codegen_stmt<'tcx>(
862864
NullOp::OffsetOf(fields) => fx
863865
.tcx
864866
.offset_of_subfield(
865-
ty::TypingEnv::fully_monomorphized(),
867+
ty::TypingEnv::fully_monomorphized(fx.tcx),
866868
layout,
867869
fields.iter(),
868870
)

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
7171
},
7272
ty::FnPtr(..) => pointer_ty(tcx),
7373
ty::RawPtr(pointee_ty, _) | ty::Ref(_, pointee_ty, _) => {
74-
if tcx.type_has_metadata(*pointee_ty, ty::TypingEnv::fully_monomorphized()) {
74+
if tcx.type_has_metadata(*pointee_ty, ty::TypingEnv::fully_monomorphized(tcx)) {
7575
return None;
7676
} else {
7777
pointer_ty(tcx)
@@ -91,7 +91,7 @@ fn clif_pair_type_from_ty<'tcx>(
9191
(clif_type_from_ty(tcx, types[0])?, clif_type_from_ty(tcx, types[1])?)
9292
}
9393
ty::RawPtr(pointee_ty, _) | ty::Ref(_, pointee_ty, _) => {
94-
if tcx.type_has_metadata(*pointee_ty, ty::TypingEnv::fully_monomorphized()) {
94+
if tcx.type_has_metadata(*pointee_ty, ty::TypingEnv::fully_monomorphized(tcx)) {
9595
(pointer_ty(tcx), pointer_ty(tcx))
9696
} else {
9797
return None;
@@ -327,7 +327,7 @@ impl<'tcx> rustc_abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> {
327327

328328
impl<'tcx> layout::HasTypingEnv<'tcx> for FunctionCx<'_, '_, 'tcx> {
329329
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
330-
ty::TypingEnv::fully_monomorphized()
330+
ty::TypingEnv::fully_monomorphized(self.tcx)
331331
}
332332
}
333333

@@ -344,7 +344,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
344344
{
345345
self.instance.instantiate_mir_and_normalize_erasing_regions(
346346
self.tcx,
347-
ty::TypingEnv::fully_monomorphized(),
347+
ty::TypingEnv::fully_monomorphized(self.tcx),
348348
ty::EarlyBinder::bind(value),
349349
)
350350
}
@@ -490,7 +490,7 @@ impl<'tcx> rustc_abi::HasDataLayout for FullyMonomorphizedLayoutCx<'tcx> {
490490

491491
impl<'tcx> layout::HasTypingEnv<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
492492
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
493-
ty::TypingEnv::fully_monomorphized()
493+
ty::TypingEnv::fully_monomorphized(self.0)
494494
}
495495
}
496496

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
7878
let cv = fx.monomorphize(constant.const_);
7979
// This cannot fail because we checked all required_consts in advance.
8080
let val = cv
81-
.eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)
81+
.eval(fx.tcx, ty::TypingEnv::fully_monomorphized(fx.tcx), constant.span)
8282
.expect("erroneous constant missed by mono item collection");
8383
(val, cv.ty())
8484
}
@@ -267,9 +267,9 @@ fn data_id_for_static(
267267
assert!(!definition);
268268
assert!(!tcx.is_mutable_static(def_id));
269269

270-
let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
270+
let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized(tcx));
271271
let align = tcx
272-
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
272+
.layout_of(ty::TypingEnv::fully_monomorphized(tcx).as_query_input(ty))
273273
.unwrap()
274274
.align
275275
.abi

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl DebugContext {
210210

211211
type_names::push_generic_params(
212212
tcx,
213-
tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), args),
213+
tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(tcx), args),
214214
&mut name,
215215
);
216216

@@ -275,9 +275,10 @@ impl DebugContext {
275275
let span = tcx.def_span(def_id);
276276
let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
277277

278-
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::TypingEnv::fully_monomorphized());
278+
let static_type =
279+
Instance::mono(tcx, def_id).ty(tcx, ty::TypingEnv::fully_monomorphized(tcx));
279280
let static_layout = tcx
280-
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(static_type))
281+
.layout_of(ty::TypingEnv::fully_monomorphized(tcx).as_query_input(static_type))
281282
.unwrap();
282283
// FIXME use the actual type layout
283284
let type_id = self.debug_type(tcx, type_dbg, static_type);

compiler/rustc_codegen_cranelift/src/debuginfo/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl DebugContext {
129129

130130
let name = type_names::compute_debuginfo_type_name(tcx, ptr_type, true);
131131

132-
if !tcx.type_has_metadata(ptr_type, ty::TypingEnv::fully_monomorphized()) {
132+
if !tcx.type_has_metadata(ptr_type, ty::TypingEnv::fully_monomorphized(tcx)) {
133133
let pointer_type_id =
134134
self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_pointer_type);
135135
let pointer_entry = self.dwarf.unit.get_mut(pointer_type_id);

compiler/rustc_codegen_cranelift/src/inline_asm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
9292
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
9393
let instance = ty::Instance::resolve_for_fn_ptr(
9494
fx.tcx,
95-
ty::TypingEnv::fully_monomorphized(),
95+
ty::TypingEnv::fully_monomorphized(fx.tcx),
9696
def_id,
9797
args,
9898
)
@@ -225,11 +225,11 @@ pub(crate) fn codegen_naked_asm<'tcx>(
225225
InlineAsmOperand::Const { ref value } => {
226226
let cv = instance.instantiate_mir_and_normalize_erasing_regions(
227227
tcx,
228-
ty::TypingEnv::fully_monomorphized(),
228+
ty::TypingEnv::fully_monomorphized(tcx),
229229
ty::EarlyBinder::bind(value.const_),
230230
);
231231
let const_value = cv
232-
.eval(tcx, ty::TypingEnv::fully_monomorphized(), value.span)
232+
.eval(tcx, ty::TypingEnv::fully_monomorphized(tcx), value.span)
233233
.expect("erroneous constant missed by mono item collection");
234234

235235
let value = rustc_codegen_ssa::common::asm_const_to_str(
@@ -248,13 +248,13 @@ pub(crate) fn codegen_naked_asm<'tcx>(
248248

249249
let const_ = instance.instantiate_mir_and_normalize_erasing_regions(
250250
tcx,
251-
ty::TypingEnv::fully_monomorphized(),
251+
ty::TypingEnv::fully_monomorphized(tcx),
252252
ty::EarlyBinder::bind(value.const_),
253253
);
254254
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
255255
let instance = ty::Instance::resolve_for_fn_ptr(
256256
tcx,
257-
ty::TypingEnv::fully_monomorphized(),
257+
ty::TypingEnv::fully_monomorphized(tcx),
258258
def_id,
259259
args,
260260
)

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
682682
.tcx
683683
.check_validity_requirement((
684684
requirement,
685-
ty::TypingEnv::fully_monomorphized().as_query_input(ty),
685+
ty::TypingEnv::fully_monomorphized(fx.tcx).as_query_input(ty),
686686
))
687687
.expect("expect to have layout during codegen");
688688

@@ -743,7 +743,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
743743
let const_val = fx
744744
.tcx
745745
.const_eval_instance(
746-
ty::TypingEnv::fully_monomorphized(),
746+
ty::TypingEnv::fully_monomorphized(fx.tcx),
747747
instance,
748748
source_info.span,
749749
)

0 commit comments

Comments
 (0)