Skip to content

Commit b26819f

Browse files
Auto merge of #147388 - azhogin:azhogin/alloc-query-opt-etc, r=<try>
allocator_kind and related queries converted to hooks
2 parents 828c2a9 + c2d3b38 commit b26819f

File tree

8 files changed

+41
-35
lines changed

8 files changed

+41
-35
lines changed

compiler/rustc_codegen_cranelift/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1919
tcx,
2020
module,
2121
kind,
22-
tcx.alloc_error_handler_kind(()).unwrap(),
22+
tcx.alloc_error_handler_kind().unwrap(),
2323
tcx.sess.opts.unstable_opts.oom,
2424
);
2525
true

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ fn exported_symbols_for_non_proc_macro(
18261826
// Mark allocator shim symbols as exported only if they were generated.
18271827
if export_threshold == SymbolExportLevel::Rust
18281828
&& needs_allocator_shim_for_linking(tcx.dependency_formats(()), crate_type)
1829-
&& tcx.allocator_kind(()).is_some()
1829+
&& tcx.allocator_kind().is_some()
18301830
{
18311831
symbols.extend(allocator_shim_symbols(tcx));
18321832
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub fn allocator_kind_for_codegen(tcx: TyCtxt<'_>) -> Option<AllocatorKind> {
639639
use rustc_middle::middle::dependency_format::Linkage;
640640
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
641641
});
642-
if all_crate_types_any_dynamic_crate { None } else { tcx.allocator_kind(()) }
642+
if all_crate_types_any_dynamic_crate { None } else { tcx.allocator_kind() }
643643
}
644644

645645
/// Decide if this particular crate type needs an allocator shim linked in.
@@ -705,7 +705,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
705705
kind,
706706
// If allocator_kind is Some then alloc_error_handler_kind must
707707
// also be Some.
708-
tcx.alloc_error_handler_kind(()).unwrap(),
708+
tcx.alloc_error_handler_kind().unwrap(),
709709
);
710710
Some(ModuleCodegen::new_allocator(llmod_id, module))
711711
})

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,6 @@ provide! { tcx, def_id, other, cdata,
328328
is_private_dep => { cdata.private_dep }
329329
is_panic_runtime => { cdata.root.panic_runtime }
330330
is_compiler_builtins => { cdata.root.compiler_builtins }
331-
has_global_allocator => { cdata.root.has_global_allocator }
332-
has_alloc_error_handler => { cdata.root.has_alloc_error_handler }
333331
has_panic_handler => { cdata.root.has_panic_handler }
334332
is_profiler_runtime => { cdata.root.profiler_runtime }
335333
required_panic_strategy => { cdata.root.required_panic_strategy }
@@ -407,8 +405,6 @@ provide! { tcx, def_id, other, cdata,
407405
pub(in crate::rmeta) fn provide(providers: &mut Providers) {
408406
provide_cstore_hooks(providers);
409407
providers.queries = rustc_middle::query::Providers {
410-
allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),
411-
alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),
412408
is_private_dep: |_tcx, LocalCrate| false,
413409
native_library: |tcx, id| {
414410
tcx.native_libraries(id.krate)
@@ -519,8 +515,6 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
519515
},
520516

521517
dependency_formats: |tcx, ()| Arc::new(crate::dependency_format::calculate(tcx)),
522-
has_global_allocator: |tcx, LocalCrate| CStore::from_tcx(tcx).has_global_allocator(),
523-
has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
524518
postorder_cnums: |tcx, ()| {
525519
tcx.arena.alloc_from_iter(
526520
CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE).into_iter(),
@@ -706,4 +700,33 @@ fn provide_cstore_hooks(providers: &mut Providers) {
706700
cdata.imported_source_file(file_index as u32, tcx.sess);
707701
}
708702
};
703+
providers.hooks.allocator_kind = |tcx| CStore::from_tcx(tcx).allocator_kind();
704+
providers.hooks.alloc_error_handler_kind =
705+
|tcx| CStore::from_tcx(tcx).alloc_error_handler_kind();
706+
providers.hooks.has_global_allocator = |tcx, krate: CrateNum| {
707+
if krate == LOCAL_CRATE {
708+
CStore::from_tcx(tcx).has_global_allocator()
709+
} else {
710+
let cdata =
711+
rustc_data_structures::sync::FreezeReadGuard::map(CStore::from_tcx(tcx), |c| {
712+
c.get_crate_data(krate).cdata
713+
});
714+
let cdata =
715+
crate::creader::CrateMetadataRef { cdata: &cdata, cstore: &CStore::from_tcx(tcx) };
716+
cdata.root.has_global_allocator
717+
}
718+
};
719+
providers.hooks.has_alloc_error_handler = |tcx, krate: CrateNum| {
720+
if krate == LOCAL_CRATE {
721+
CStore::from_tcx(tcx).has_alloc_error_handler()
722+
} else {
723+
let cdata =
724+
rustc_data_structures::sync::FreezeReadGuard::map(CStore::from_tcx(tcx), |c| {
725+
c.get_crate_data(krate).cdata
726+
});
727+
let cdata =
728+
crate::creader::CrateMetadataRef { cdata: &cdata, cstore: &CStore::from_tcx(tcx) };
729+
cdata.root.has_alloc_error_handler
730+
}
731+
}
709732
}

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! similar to queries, but queries come with a lot of machinery for caching and incremental
44
//! compilation, whereas hooks are just plain function pointers without any of the query magic.
55
6+
use rustc_ast::expand::allocator::AllocatorKind;
67
use rustc_hir::def_id::{DefId, DefPathHash};
78
use rustc_session::StableCrateId;
89
use rustc_span::def_id::{CrateNum, LocalDefId};
@@ -102,6 +103,11 @@ declare_hooks! {
102103
/// Ensure the given scalar is valid for the given type.
103104
/// This checks non-recursive runtime validity.
104105
hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
106+
107+
hook allocator_kind() -> Option<AllocatorKind>;
108+
hook alloc_error_handler_kind() -> Option<AllocatorKind>;
109+
hook has_global_allocator(krate: CrateNum) -> bool;
110+
hook has_alloc_error_handler(krate: CrateNum) -> bool;
105111
}
106112

107113
#[cold]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ use std::sync::Arc;
6969

7070
use rustc_abi::Align;
7171
use rustc_arena::TypedArena;
72-
use rustc_ast::expand::allocator::AllocatorKind;
7372
use rustc_data_structures::fingerprint::Fingerprint;
7473
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
7574
use rustc_data_structures::sorted_map::SortedMap;
@@ -1812,20 +1811,6 @@ rustc_queries! {
18121811
desc { "checking if the crate is_compiler_builtins" }
18131812
separate_provide_extern
18141813
}
1815-
query has_global_allocator(_: CrateNum) -> bool {
1816-
// This query depends on untracked global state in CStore
1817-
eval_always
1818-
fatal_cycle
1819-
desc { "checking if the crate has_global_allocator" }
1820-
separate_provide_extern
1821-
}
1822-
query has_alloc_error_handler(_: CrateNum) -> bool {
1823-
// This query depends on untracked global state in CStore
1824-
eval_always
1825-
fatal_cycle
1826-
desc { "checking if the crate has_alloc_error_handler" }
1827-
separate_provide_extern
1828-
}
18291814
query has_panic_handler(_: CrateNum) -> bool {
18301815
fatal_cycle
18311816
desc { "checking if the crate has_panic_handler" }
@@ -2301,14 +2286,6 @@ rustc_queries! {
23012286
desc { "checking whether crate `{}` is a private dependency", c }
23022287
separate_provide_extern
23032288
}
2304-
query allocator_kind(_: ()) -> Option<AllocatorKind> {
2305-
eval_always
2306-
desc { "getting the allocator kind for the current crate" }
2307-
}
2308-
query alloc_error_handler_kind(_: ()) -> Option<AllocatorKind> {
2309-
eval_always
2310-
desc { "alloc error handler kind for the current crate" }
2311-
}
23122289

23132290
query upvars_mentioned(def_id: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
23142291
desc { |tcx| "collecting upvars mentioned in `{}`", tcx.def_path_str(def_id) }

src/tools/miri/src/shims/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6161
) -> InterpResult<'tcx, EmulateItemResult> {
6262
let this = self.eval_context_mut();
6363

64-
let Some(allocator_kind) = this.tcx.allocator_kind(()) else {
64+
let Some(allocator_kind) = this.tcx.allocator_kind() else {
6565
// in real code, this symbol does not exist without an allocator
6666
return interp_ok(EmulateItemResult::NotSupported);
6767
};

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5353
match link_name.as_str() {
5454
name if name == this.mangle_internal_symbol("__rust_alloc_error_handler") => {
5555
// Forward to the right symbol that implements this function.
56-
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
56+
let Some(handler_kind) = this.tcx.alloc_error_handler_kind() else {
5757
// in real code, this symbol does not exist without an allocator
5858
throw_unsup_format!(
5959
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"

0 commit comments

Comments
 (0)