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

Use unused_generic_params from crate metadata #109109

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,15 @@ provide! { tcx, def_id, other, cdata,
lookup_default_body_stability => { table }
lookup_deprecation_entry => { table }
params_in_repr => { table }
unused_generic_params => { table }
// FIXME: Could be defaulted, but `LazyValue<UnusedGenericParams>` is not `FixedSizeEncoding`..
unused_generic_params => {
Copy link
Member

Choose a reason for hiding this comment

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

You had a FIXME in the other PR, could you add this here as well? Would be nice to just default

Copy link
Member

Choose a reason for hiding this comment

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

Oh also, is there a reason why the type doesn't implement that trait? Could you just add the impl? Or is there something to it

Copy link
Member Author

@compiler-errors compiler-errors Mar 14, 2023

Choose a reason for hiding this comment

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

For us to have a defaulted table, the value must implement FixedSizeEncoding and IsDefault. Lazy<T> can be FixedSizeEncoding, but can't be checked for defaultness because they're just a file offset.

I could instead encode the table value as Option<LazyValue<UnusedGenericParams>>, but then ProcessQueryValue<T> for Option<T> panics if the value decoded is None -- we could use specializtion to do:

impl ProcessQueryValue<'_, UnusedGenericParams> for Option<UnusedGenericParams> {
    #[inline(always)]
    fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> UnusedGenericParams {
        self.unwrap_or_else(|| UnusedGenericParams::new_all_used())
    }
}

but I don't feel like using specialization here is compelling enough to save a few lines in the decoder rather than providing an explicit body here.

Copy link
Member

Choose a reason for hiding this comment

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

haha, yeah, it's fine

cdata
.root
.tables
.unused_generic_params
.get(cdata, def_id.index)
.map_or_else(|| ty::UnusedGenericParams::new_all_used(), |lazy| lazy.decode((cdata, tcx)))
}
opt_def_kind => { table_direct }
impl_parent => { table }
impl_polarity => { table_direct }
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> {

#[inline(always)]
fn query_crate_is_local(&self) -> bool {
true
self.def_id().is_local()
}

fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
Expand All @@ -76,7 +76,7 @@ impl<'tcx> Key for ty::Instance<'tcx> {

#[inline(always)]
fn query_crate_is_local(&self) -> bool {
true
self.def_id().is_local()
}

fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_monomorphize/src/polymorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ fn unused_generic_params<'tcx>(
tcx: TyCtxt<'tcx>,
instance: ty::InstanceDef<'tcx>,
) -> UnusedGenericParams {
assert!(instance.def_id().is_local());

if !tcx.sess.opts.unstable_opts.polymorphize {
// If polymorphization disabled, then all parameters are used.
return UnusedGenericParams::new_all_used();
Expand Down Expand Up @@ -100,13 +102,6 @@ fn should_polymorphize<'tcx>(
return false;
}

// Polymorphization results are stored in cross-crate metadata only when there are unused
// parameters, so assume that non-local items must have only used parameters (else this query
// would not be invoked, and the cross-crate metadata used instead).
if !def_id.is_local() {
return false;
}

// Foreign items have no bodies to analyze.
if tcx.is_foreign_item(def_id) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions tests/codegen-units/polymorphization/auxiliary/poly-dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// compile-flags: -Zpolymorphize=on

#[inline(never)]
pub fn foo<T>() {}
11 changes: 11 additions & 0 deletions tests/codegen-units/polymorphization/poly-foreign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// aux-build:poly-dep.rs
// compile-flags: --crate-type=lib -Zprint-mono-items=eager -Zpolymorphize=on

extern crate poly_dep;

pub static FN1: fn() = poly_dep::foo::<i32>;
pub static FN2: fn() = poly_dep::foo::<u32>;

//~ MONO_ITEM static FN1
//~ MONO_ITEM static FN2
//~ MONO_ITEM fn poly_dep::foo::<T>