Skip to content

Commit 311dde6

Browse files
committed
Auto merge of #155244 - zetanumbers:no_dynamic_dispatch, r=<try>
Experiment: No dynamic dispatch for query functions
2 parents 14196db + dd7ab46 commit 311dde6

Some content is hidden

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

42 files changed

+661
-551
lines changed

Cargo.lock

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4169,7 +4169,6 @@ dependencies = [
41694169
"rustc_parse",
41704170
"rustc_passes",
41714171
"rustc_privacy",
4172-
"rustc_query_impl",
41734172
"rustc_resolve",
41744173
"rustc_session",
41754174
"rustc_span",
@@ -4303,6 +4302,7 @@ dependencies = [
43034302
"bitflags",
43044303
"either",
43054304
"gsgdt",
4305+
"measureme",
43064306
"parking_lot",
43074307
"polonius-engine",
43084308
"rustc_abi",
@@ -4564,23 +4564,6 @@ dependencies = [
45644564
"rustc_target",
45654565
]
45664566

4567-
[[package]]
4568-
name = "rustc_query_impl"
4569-
version = "0.0.0"
4570-
dependencies = [
4571-
"measureme",
4572-
"rustc_abi",
4573-
"rustc_data_structures",
4574-
"rustc_errors",
4575-
"rustc_hir",
4576-
"rustc_macros",
4577-
"rustc_middle",
4578-
"rustc_serialize",
4579-
"rustc_span",
4580-
"rustc_thread_pool",
4581-
"tracing",
4582-
]
4583-
45844567
[[package]]
45854568
name = "rustc_resolve"
45864569
version = "0.0.0"

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 126 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_hir::attrs::Linkage;
2222
use rustc_middle::dep_graph;
2323
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, SanitizerFnAttrs};
2424
use rustc_middle::mono::Visibility;
25+
use rustc_middle::query::{QueryHashHelper, QueryHelper};
2526
use rustc_middle::ty::TyCtxt;
2627
use rustc_session::config::{DebugInfo, Offload};
2728
use rustc_span::Symbol;
@@ -62,122 +63,150 @@ pub(crate) fn compile_codegen_unit(
6263
let start_time = Instant::now();
6364

6465
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
65-
let (module, _) = tcx.dep_graph.with_task(
66-
dep_node,
67-
tcx,
68-
cgu_name,
69-
module_codegen,
70-
Some(dep_graph::hash_result),
71-
);
72-
let time_to_codegen = start_time.elapsed();
7366

74-
// We assume that the cost to run LLVM on a CGU is proportional to
75-
// the time we needed for codegenning it.
76-
let cost = time_to_codegen.as_nanos() as u64;
67+
#[derive(Default)]
68+
struct CompileCguHashHelper;
7769

78-
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
79-
let cgu = tcx.codegen_unit(cgu_name);
80-
let _prof_timer =
81-
tcx.prof.generic_activity_with_arg_recorder("codegen_module", |recorder| {
82-
recorder.record_arg(cgu_name.to_string());
83-
recorder.record_arg(cgu.size_estimate().to_string());
84-
});
85-
// Instantiate monomorphizations without filling out definitions yet...
86-
let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
87-
{
88-
let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
89-
90-
// Declare and store globals shared by all offload kernels
91-
//
92-
// These globals are left in the LLVM-IR host module so all kernels can access them.
93-
// They are necessary for correct offload execution. We do this here to simplify the
94-
// `offload` intrinsic, avoiding the need for tracking whether it's the first
95-
// intrinsic call or not.
96-
let has_host_offload = cx
97-
.sess()
98-
.opts
99-
.unstable_opts
100-
.offload
101-
.iter()
102-
.any(|o| matches!(o, Offload::Host(_) | Offload::Test));
103-
if has_host_offload && !cx.sess().target.is_like_gpu {
104-
cx.offload_globals.replace(Some(OffloadGlobals::declare(&cx)));
105-
}
70+
impl QueryHashHelper<ModuleCodegen<ModuleLlvm>> for CompileCguHashHelper {
71+
const NO_HASH: bool = false;
10672

107-
let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
108-
for &(mono_item, data) in &mono_items {
109-
mono_item.predefine::<Builder<'_, '_, '_>>(
110-
&mut cx,
111-
cgu_name.as_str(),
112-
data.linkage,
113-
data.visibility,
114-
);
115-
}
73+
fn hash_value_fn(
74+
hcx: &mut rustc_middle::ich::StableHashingContext<'_>,
75+
result: &ModuleCodegen<ModuleLlvm>,
76+
) -> rustc_data_structures::fingerprint::Fingerprint {
77+
dep_graph::hash_result(hcx, result)
78+
}
11679

117-
// ... and now that we have everything pre-defined, fill out those definitions.
118-
for &(mono_item, item_data) in &mono_items {
119-
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
120-
}
80+
fn format_value(_: &ModuleCodegen<ModuleLlvm>) -> String {
81+
unimplemented!()
82+
}
83+
}
12184

122-
// If this codegen unit contains the main function, also create the
123-
// wrapper here
124-
if let Some(entry) =
125-
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
85+
impl<'tcx> QueryHelper<'tcx, Symbol, ModuleCodegen<ModuleLlvm>> for CompileCguHashHelper {
86+
fn try_load_from_disk_fn(
87+
_: TyCtxt<'tcx>,
88+
_: dep_graph::SerializedDepNodeIndex,
89+
) -> Option<ModuleCodegen<ModuleLlvm>> {
90+
unimplemented!()
91+
}
92+
93+
fn will_cache_on_disk_for_key(_: Symbol) -> bool {
94+
unimplemented!()
95+
}
96+
97+
fn invoke_provider_fn(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
98+
// Module codegen
99+
let cgu = tcx.codegen_unit(cgu_name);
100+
let _prof_timer =
101+
tcx.prof.generic_activity_with_arg_recorder("codegen_module", |recorder| {
102+
recorder.record_arg(cgu_name.to_string());
103+
recorder.record_arg(cgu.size_estimate().to_string());
104+
});
105+
// Instantiate monomorphizations without filling out definitions yet...
106+
let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
126107
{
127-
let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
128-
attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
129-
}
108+
let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
109+
110+
// Declare and store globals shared by all offload kernels
111+
//
112+
// These globals are left in the LLVM-IR host module so all kernels can access them.
113+
// They are necessary for correct offload execution. We do this here to simplify the
114+
// `offload` intrinsic, avoiding the need for tracking whether it's the first
115+
// intrinsic call or not.
116+
let has_host_offload = cx
117+
.sess()
118+
.opts
119+
.unstable_opts
120+
.offload
121+
.iter()
122+
.any(|o| matches!(o, Offload::Host(_) | Offload::Test));
123+
if has_host_offload && !cx.sess().target.is_like_gpu {
124+
cx.offload_globals.replace(Some(OffloadGlobals::declare(&cx)));
125+
}
130126

131-
// Define Objective-C module info and module flags. Note, the module info will
132-
// also be added to the `llvm.compiler.used` variable, created later.
133-
//
134-
// These are only necessary when we need the linker to do its Objective-C-specific
135-
// magic. We could theoretically do it unconditionally, but at a slight cost to linker
136-
// performance in the common case where it's unnecessary.
137-
if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
138-
if cx.objc_abi_version() == 1 {
139-
cx.define_objc_module_info();
127+
let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
128+
for &(mono_item, data) in &mono_items {
129+
mono_item.predefine::<Builder<'_, '_, '_>>(
130+
&mut cx,
131+
cgu_name.as_str(),
132+
data.linkage,
133+
data.visibility,
134+
);
140135
}
141-
cx.add_objc_module_flags();
142-
}
143136

144-
// Finalize code coverage by injecting the coverage map. Note, the coverage map will
145-
// also be added to the `llvm.compiler.used` variable, created next.
146-
if cx.sess().instrument_coverage() {
147-
cx.coverageinfo_finalize();
148-
}
137+
// ... and now that we have everything pre-defined, fill out those definitions.
138+
for &(mono_item, item_data) in &mono_items {
139+
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
140+
}
149141

150-
// Create the llvm.used variable.
151-
if !cx.used_statics.is_empty() {
152-
cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
153-
}
142+
// If this codegen unit contains the main function, also create the
143+
// wrapper here
144+
if let Some(entry) =
145+
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
146+
{
147+
let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
148+
attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
149+
}
154150

155-
// Create the llvm.compiler.used variable.
156-
{
157-
let compiler_used_statics = cx.compiler_used_statics.borrow();
158-
if !compiler_used_statics.is_empty() {
159-
cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
151+
// Define Objective-C module info and module flags. Note, the module info will
152+
// also be added to the `llvm.compiler.used` variable, created later.
153+
//
154+
// These are only necessary when we need the linker to do its Objective-C-specific
155+
// magic. We could theoretically do it unconditionally, but at a slight cost to linker
156+
// performance in the common case where it's unnecessary.
157+
if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
158+
if cx.objc_abi_version() == 1 {
159+
cx.define_objc_module_info();
160+
}
161+
cx.add_objc_module_flags();
160162
}
161-
}
162163

163-
// Run replace-all-uses-with for statics that need it. This must
164-
// happen after the llvm.used variables are created.
165-
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
166-
unsafe {
167-
llvm::LLVMReplaceAllUsesWith(old_g, new_g);
168-
llvm::LLVMDeleteGlobal(old_g);
164+
// Finalize code coverage by injecting the coverage map. Note, the coverage map will
165+
// also be added to the `llvm.compiler.used` variable, created next.
166+
if cx.sess().instrument_coverage() {
167+
cx.coverageinfo_finalize();
168+
}
169+
170+
// Create the llvm.used variable.
171+
if !cx.used_statics.is_empty() {
172+
cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
173+
}
174+
175+
// Create the llvm.compiler.used variable.
176+
{
177+
let compiler_used_statics = cx.compiler_used_statics.borrow();
178+
if !compiler_used_statics.is_empty() {
179+
cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
180+
}
181+
}
182+
183+
// Run replace-all-uses-with for statics that need it. This must
184+
// happen after the llvm.used variables are created.
185+
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
186+
unsafe {
187+
llvm::LLVMReplaceAllUsesWith(old_g, new_g);
188+
llvm::LLVMDeleteGlobal(old_g);
189+
}
169190
}
170-
}
171191

172-
// Finalize debuginfo
173-
if cx.sess().opts.debuginfo != DebugInfo::None {
174-
cx.debuginfo_finalize();
192+
// Finalize debuginfo
193+
if cx.sess().opts.debuginfo != DebugInfo::None {
194+
cx.debuginfo_finalize();
195+
}
175196
}
176-
}
177197

178-
ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
198+
ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
199+
}
179200
}
180201

202+
let (module, _) =
203+
tcx.dep_graph.with_task::<_, _, CompileCguHashHelper>(dep_node, tcx, cgu_name);
204+
let time_to_codegen = start_time.elapsed();
205+
206+
// We assume that the cost to run LLVM on a CGU is proportional to
207+
// the time we needed for codegenning it.
208+
let cost = time_to_codegen.as_nanos() as u64;
209+
181210
(module, cost)
182211
}
183212

compiler/rustc_interface/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ rustc_monomorphize = { path = "../rustc_monomorphize" }
3434
rustc_parse = { path = "../rustc_parse" }
3535
rustc_passes = { path = "../rustc_passes" }
3636
rustc_privacy = { path = "../rustc_privacy" }
37-
rustc_query_impl = { path = "../rustc_query_impl" }
3837
rustc_resolve = { path = "../rustc_resolve" }
3938
rustc_session = { path = "../rustc_session" }
4039
rustc_span = { path = "../rustc_span" }

compiler/rustc_interface/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use rustc_data_structures::jobserver::{self, Proxy};
99
use rustc_data_structures::stable_hasher::StableHasher;
1010
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
1111
use rustc_lint::LintStore;
12+
use rustc_middle::query::impl_::print_query_stack;
1213
use rustc_middle::ty;
1314
use rustc_middle::ty::CurrentGcx;
1415
use rustc_middle::util::Providers;
1516
use rustc_parse::lexer::StripTokens;
1617
use rustc_parse::new_parser_from_source_str;
1718
use rustc_parse::parser::Recovery;
1819
use rustc_parse::parser::attr::AllowLeadingUnsafe;
19-
use rustc_query_impl::print_query_stack;
2020
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
2121
use rustc_session::parse::ParseSess;
2222
use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};

compiler/rustc_interface/src/passes.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ fn resolver_for_lowering_raw<'tcx>(
787787
) -> (&'tcx Steal<(ty::ResolverAstLowering<'tcx>, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
788788
let arenas = Resolver::arenas();
789789
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
790-
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
790+
let (krate, pre_configured_attrs) = tcx.crate_for_resolver.borrow_mut().take().unwrap();
791791
let mut resolver = Resolver::new(
792792
tcx,
793793
&pre_configured_attrs,
@@ -901,7 +901,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
901901
rustc_mir_transform::provide(providers);
902902
rustc_monomorphize::provide(providers);
903903
rustc_privacy::provide(&mut providers.queries);
904-
rustc_query_impl::provide(providers);
904+
rustc_middle::query::impl_::provide(providers);
905905
rustc_resolve::provide(&mut providers.queries);
906906
rustc_hir_analysis::provide(&mut providers.queries);
907907
rustc_hir_typeck::provide(&mut providers.queries);
@@ -969,8 +969,6 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
969969
callback(sess, &mut providers);
970970
}
971971

972-
let incremental = dep_graph.is_fully_enabled();
973-
974972
// Note: this function body is the origin point of the widely-used 'tcx lifetime.
975973
//
976974
// `gcx_cell` is defined here and `&gcx_cell` is passed to `create_global_ctxt`, which then
@@ -995,12 +993,11 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
995993
&hir_arena,
996994
untracked,
997995
dep_graph,
998-
rustc_query_impl::make_dep_kind_vtables(&arena),
999-
rustc_query_impl::query_system(
996+
rustc_middle::query::impl_::make_dep_kind_vtables(&arena),
997+
rustc_middle::query::impl_::query_system(
1000998
providers.queries,
1001999
providers.extern_queries,
10021000
query_result_on_disk_cache,
1003-
incremental,
10041001
),
10051002
providers.hooks,
10061003
compiler.current_gcx.clone(),
@@ -1016,8 +1013,8 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
10161013
&pre_configured_attrs,
10171014
crate_name,
10181015
)));
1019-
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
10201016
feed.output_filenames(Arc::new(outputs));
1017+
*tcx.crate_for_resolver.borrow_mut() = Some((krate, pre_configured_attrs));
10211018

10221019
let res = f(tcx);
10231020
// FIXME maybe run finish even when a fatal error occurred? or at least

compiler/rustc_interface/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use rustc_data_structures::jobserver::Proxy;
1717
use rustc_data_structures::sync;
1818
use rustc_metadata::{DylibError, EncodedMetadata, load_symbol_from_dylib};
1919
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
20+
use rustc_middle::query::impl_::{CollectActiveJobsKind, collect_active_query_jobs};
2021
use rustc_middle::ty::{CurrentGcx, TyCtxt};
21-
use rustc_query_impl::{CollectActiveJobsKind, collect_active_query_jobs};
2222
use rustc_session::config::{
2323
Cfg, CrateType, OutFileName, OutputFilenames, OutputTypes, Sysroot, host_tuple,
2424
};
@@ -183,8 +183,8 @@ pub(crate) fn run_in_thread_pool_with_globals<
183183
use std::process;
184184

185185
use rustc_data_structures::defer;
186+
use rustc_middle::query::impl_::break_query_cycle;
186187
use rustc_middle::ty::tls;
187-
use rustc_query_impl::break_query_cycle;
188188

189189
let thread_stack_size = init_stack_size(thread_builder_diag);
190190

0 commit comments

Comments
 (0)