Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store static initializers in metadata instead of the MIR of statics. #116564

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Store static initializers in metadata instead of the MIR of statics.
  • Loading branch information
oli-obk committed Feb 15, 2024
commit be6ccf13e32e8b4b8d92d14eb555d75377d05e24
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub fn provide(providers: &mut Providers) {
providers.eval_to_const_value_raw = const_eval::eval_to_const_value_raw_provider;
providers.eval_to_allocation_raw = const_eval::eval_to_allocation_raw_provider;
providers.eval_static_initializer_raw = |tcx, def_id| {
assert!(tcx.is_static(def_id));
let instance = ty::Instance::mono(tcx, def_id);
assert!(tcx.is_static(def_id.to_def_id()));
let instance = ty::Instance::mono(tcx, def_id.to_def_id());
let gid = rustc_middle::mir::interpret::GlobalId { instance, promoted: None };
let param_env = ty::ParamEnv::reveal_all();
Ok(tcx.eval_to_allocation_raw(param_env.and(gid))?.alloc_id)
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ provide! { tcx, def_id, other, cdata,
fn_arg_names => { table }
coroutine_kind => { table_direct }
coroutine_for_closure => { table }
eval_static_initializer_raw => {
Ok(cdata
.root
.tables
.eval_static_initializer_raw
.get(cdata, def_id.index)
.map(|lazy| lazy.decode((cdata, tcx)))
.unwrap_or_else(|| panic!("{def_id:?} does not have eval_static_initializer_raw")))
}
trait_def => { table }
deduced_param_attrs => { table }
is_type_alias_impl_trait => {
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,9 @@ fn should_encode_mir(
(true, mir_opt_base)
}
// Constants
DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst
| DefKind::Static(..)
| DefKind::Const => (true, false),
DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => {
(true, false)
}
// Coroutines require optimized MIR to compute layout.
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
// Full-fledged functions + closures
Expand Down Expand Up @@ -1454,6 +1452,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
.coroutine_for_closure
.set_some(def_id.index, self.tcx.coroutine_for_closure(def_id).into());
}
if let DefKind::Static(_) = def_kind {
if !self.tcx.is_foreign_item(def_id) {
let data = self.tcx.eval_static_initializer_raw(def_id).unwrap();
record!(self.tables.eval_static_initializer_raw[def_id] <- data);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want a review of these changes you'll have to find someone else to do it, I have no clue what is happening here.

}
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
self.encode_info_for_adt(local_id);
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ define_tables! {
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
coroutine_for_closure: Table<DefIndex, RawDefId>,
eval_static_initializer_raw: Table<DefIndex, LazyValue<mir::interpret::AllocId>>,
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
trait_item_def_id: Table<DefIndex, RawDefId>,
expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ rustc_queries! {
tcx.def_path_str(key)
}
cache_on_disk_if { key.is_local() }
separate_provide_extern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this might need review by someone else. I have no idea about the tradeoffs involved in configuring a query.

}

/// Evaluates const items or anonymous constants
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ trivially_parameterized_over_tcx! {
crate::middle::lib_features::FeatureStability,
crate::middle::resolve_bound_vars::ObjectLifetimeDefault,
crate::mir::ConstQualifs,
crate::mir::interpret::AllocId,
ty::AssocItemContainer,
ty::Asyncness,
ty::DeducedParamAttrs,
Expand Down