Skip to content

Commit

Permalink
Avoid a branch on key being local for queries that use the same local…
Browse files Browse the repository at this point in the history
… and extern providers
  • Loading branch information
bjorn3 committed Oct 25, 2021
1 parent 56694b0 commit f5c3e83
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 27 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::Instance;
use rustc_middle::ty::{SymbolName, TyCtxt};
Expand Down Expand Up @@ -363,7 +363,7 @@ pub fn provide(providers: &mut Providers) {
providers.wasm_import_module_map = wasm_import_module_map;
}

pub fn provide_extern(providers: &mut Providers) {
pub fn provide_extern(providers: &mut ExternProviders) {
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_hir::def_id::CrateNum;
use rustc_hir::LangItem;
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::dependency_format::Dependencies;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
use rustc_session::cstore::{self, CrateSource};
use rustc_session::utils::NativeLibKind;
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn provide(providers: &mut Providers) {
crate::target_features::provide(providers);
}

pub fn provide_extern(providers: &mut Providers) {
pub fn provide_extern(providers: &mut ExternProviders) {
crate::back::symbol_export::provide_extern(providers);
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_errors::ErrorReported;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf, TyAndLayout};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::{
config::{self, OutputFilenames, PrintRequest},
Expand Down Expand Up @@ -80,7 +80,7 @@ pub trait CodegenBackend {
}

fn provide(&self, _providers: &mut Providers) {}
fn provide_extern(&self, _providers: &mut Providers) {}
fn provide_extern(&self, _providers: &mut ExternProviders) {}
fn codegen_crate<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Compiler {
pub(crate) output_file: Option<PathBuf>,
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
pub(crate) override_queries:
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,
}

impl Compiler {
Expand Down Expand Up @@ -155,7 +155,7 @@ pub struct Config {
///
/// The second parameter is local providers and the third parameter is external providers.
pub override_queries:
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,

/// This is a callback from the driver that is called to create a codegen backend.
pub make_codegen_backend:
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_metadata::creader::CStore;
use rustc_metadata::{encode_metadata, EncodedMetadata};
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_mir_build as mir_build;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
Expand Down Expand Up @@ -764,8 +764,8 @@ pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
*providers
});

pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
let mut extern_providers = *DEFAULT_QUERY_PROVIDERS;
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<ExternProviders> = SyncLazy::new(|| {
let mut extern_providers = ExternProviders::default();
rustc_metadata::provide_extern(&mut extern_providers);
rustc_codegen_ssa::provide_extern(&mut extern_providers);
extern_providers
Expand Down Expand Up @@ -816,7 +816,6 @@ pub fn create_global_ctxt<'tcx>(
codegen_backend.provide(&mut local_providers);

let mut extern_providers = *DEFAULT_EXTERN_QUERY_PROVIDERS;
codegen_backend.provide(&mut extern_providers);
codegen_backend.provide_extern(&mut extern_providers);

if let Some(callback) = compiler.override_queries {
Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ enum QueryModifier {

/// Always evaluate the query, ignoring its dependencies
EvalAlways(Ident),

/// Use a separate query provider for local and extern crates
SeparateProvideExtern(Ident),
}

impl Parse for QueryModifier {
Expand Down Expand Up @@ -118,6 +121,8 @@ impl Parse for QueryModifier {
Ok(QueryModifier::Anon(modifier))
} else if modifier == "eval_always" {
Ok(QueryModifier::EvalAlways(modifier))
} else if modifier == "separate_provide_extern" {
Ok(QueryModifier::SeparateProvideExtern(modifier))
} else {
Err(Error::new(modifier.span(), "unknown query modifier"))
}
Expand Down Expand Up @@ -214,6 +219,9 @@ struct QueryModifiers {

// Always evaluate the query, ignoring its dependencies
eval_always: Option<Ident>,

/// Use a separate query provider for local and extern crates
separate_provide_extern: Option<Ident>,
}

/// Process query modifiers into a struct, erroring on duplicates
Expand All @@ -227,6 +235,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
let mut no_hash = None;
let mut anon = None;
let mut eval_always = None;
let mut separate_provide_extern = None;
for modifier in query.modifiers.0.drain(..) {
match modifier {
QueryModifier::LoadCached(tcx, id, block) => {
Expand Down Expand Up @@ -317,6 +326,15 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
}
eval_always = Some(ident);
}
QueryModifier::SeparateProvideExtern(ident) => {
if separate_provide_extern.is_some() {
panic!(
"duplicate modifier `separate_provide_extern` for query `{}`",
query.name
);
}
separate_provide_extern = Some(ident);
}
}
}
let desc = desc.unwrap_or_else(|| {
Expand All @@ -332,6 +350,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
no_hash,
anon,
eval_always,
separate_provide_extern,
}
}

Expand Down Expand Up @@ -462,6 +481,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
if let Some(eval_always) = &modifiers.eval_always {
attributes.push(quote! { (#eval_always) });
};
// Pass on the separate_provide_extern modifier
if let Some(separate_provide_extern) = &modifiers.separate_provide_extern {
attributes.push(quote! { (#separate_provide_extern) });
}

// This uses the span of the query definition for the commas,
// which can be important if we later encounter any ambiguity
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_middle::hir::exports::Export;
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, TyCtxt, Visibility};
use rustc_session::cstore::{CrateSource, CrateStore, ForeignModule};
use rustc_session::utils::NativeLibKind;
Expand All @@ -26,7 +26,7 @@ use std::any::Any;
macro_rules! provide {
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
$($name:ident => $compute:block)*) => {
pub fn provide_extern(providers: &mut Providers) {
pub fn provide_extern(providers: &mut ExternProviders) {
$(fn $name<$lt>(
$tcx: TyCtxt<$lt>,
def_id_arg: ty::query::query_keys::$name<$lt>,
Expand All @@ -51,7 +51,7 @@ macro_rules! provide {
$compute
})*

*providers = Providers {
*providers = ExternProviders {
$($name,)*
..*providers
};
Expand Down
Loading

0 comments on commit f5c3e83

Please sign in to comment.