Skip to content

Commit 1436572

Browse files
committed
Move query hooks out of the crate root (and into abi).
1 parent 0e0304a commit 1436572

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

crates/rustc_codegen_spirv/src/abi.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::ErrorReported;
1010
use rustc_index::vec::Idx;
1111
use rustc_middle::bug;
1212
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
13+
use rustc_middle::ty::query::Providers;
1314
use rustc_middle::ty::subst::SubstsRef;
1415
use rustc_middle::ty::{
1516
self, Const, FloatTy, GeneratorSubsts, IntTy, ParamEnv, PolyFnSig, Ty, TyKind, TypeAndMut,
@@ -20,12 +21,49 @@ use rustc_span::Span;
2021
use rustc_span::DUMMY_SP;
2122
use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
2223
use rustc_target::abi::{Abi, Align, FieldsShape, Primitive, Scalar, Size, VariantIdx, Variants};
24+
use rustc_target::spec::abi::Abi as SpecAbi;
2325
use std::cell::RefCell;
2426
use std::collections::hash_map::Entry;
2527
use std::fmt;
2628

2729
use num_traits::cast::FromPrimitive;
2830

31+
pub(crate) fn provide(providers: &mut Providers) {
32+
// This is a lil weird: so, we obviously don't support C ABIs at all. However, libcore does declare some extern
33+
// C functions:
34+
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/library/core/src/slice/cmp.rs#L119
35+
// However, those functions will be implemented by compiler-builtins:
36+
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/library/core/src/lib.rs#L23-L27
37+
// This theoretically then should be fine to leave as C, but, there's no backend hook for
38+
// FnAbi::adjust_for_cabi, causing it to panic:
39+
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/compiler/rustc_target/src/abi/call/mod.rs#L603
40+
// So, treat any extern "C" functions as actually being Rust ABI, to be able to compile libcore with arch=spirv.
41+
providers.fn_sig = |tcx, def_id| {
42+
// We can't capture the old fn_sig and just call that, because fn_sig is a `fn`, not a `Fn`, i.e. it can't
43+
// capture variables. Fortunately, the defaults are exposed (thanks rustdoc), so use that instead.
44+
let result = (rustc_interface::DEFAULT_QUERY_PROVIDERS.fn_sig)(tcx, def_id);
45+
result.map_bound(|mut inner| {
46+
if let SpecAbi::C { .. } = inner.abi {
47+
inner.abi = SpecAbi::Rust;
48+
}
49+
inner
50+
})
51+
};
52+
}
53+
54+
pub(crate) fn provide_extern(providers: &mut Providers) {
55+
// See comments in provide(), only this time we use the default *extern* provider.
56+
providers.fn_sig = |tcx, def_id| {
57+
let result = (rustc_interface::DEFAULT_EXTERN_QUERY_PROVIDERS.fn_sig)(tcx, def_id);
58+
result.map_bound(|mut inner| {
59+
if let SpecAbi::C { .. } = inner.abi {
60+
inner.abi = SpecAbi::Rust;
61+
}
62+
inner
63+
})
64+
};
65+
}
66+
2967
/// If a struct contains a pointer to itself, even indirectly, then doing a naiive recursive walk
3068
/// of the fields will result in an infinite loop. Because pointers are the only thing that are
3169
/// allowed to be recursive, keep track of what pointers we've translated, or are currently in the

crates/rustc_codegen_spirv/src/lib.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ use rustc_middle::ty::{self, query, DefIdTree, Instance, InstanceDef, TyCtxt};
176176
use rustc_session::config::{self, OptLevel, OutputFilenames, OutputType};
177177
use rustc_session::Session;
178178
use rustc_span::symbol::{sym, Symbol};
179-
use rustc_target::spec::abi::Abi;
180179
use rustc_target::spec::{Target, TargetTriple};
181180
use std::any::Any;
182181
use std::env;
@@ -286,42 +285,12 @@ impl CodegenBackend for SpirvCodegenBackend {
286285
}
287286

288287
fn provide(&self, providers: &mut query::Providers) {
289-
// This is a lil weird: so, we obviously don't support C ABIs at all. However, libcore does declare some extern
290-
// C functions:
291-
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/library/core/src/slice/cmp.rs#L119
292-
// However, those functions will be implemented by compiler-builtins:
293-
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/library/core/src/lib.rs#L23-L27
294-
// This theoretically then should be fine to leave as C, but, there's no backend hook for
295-
// FnAbi::adjust_for_cabi, causing it to panic:
296-
// https://github.com/rust-lang/rust/blob/5fae56971d8487088c0099c82c0a5ce1638b5f62/compiler/rustc_target/src/abi/call/mod.rs#L603
297-
// So, treat any extern "C" functions as actually being Rust ABI, to be able to compile libcore with arch=spirv.
298-
providers.fn_sig = |tcx, def_id| {
299-
// We can't capture the old fn_sig and just call that, because fn_sig is a `fn`, not a `Fn`, i.e. it can't
300-
// capture variables. Fortunately, the defaults are exposed (thanks rustdoc), so use that instead.
301-
let result = (rustc_interface::DEFAULT_QUERY_PROVIDERS.fn_sig)(tcx, def_id);
302-
result.map_bound(|mut inner| {
303-
if let Abi::C { .. } = inner.abi {
304-
inner.abi = Abi::Rust;
305-
}
306-
inner
307-
})
308-
};
309-
310-
// Extra hooks provided by other parts of `rustc_codegen_spirv`.
288+
crate::abi::provide(providers);
311289
crate::attr::provide(providers);
312290
}
313291

314292
fn provide_extern(&self, providers: &mut query::Providers) {
315-
// See comments in provide(), only this time we use the default *extern* provider.
316-
providers.fn_sig = |tcx, def_id| {
317-
let result = (rustc_interface::DEFAULT_EXTERN_QUERY_PROVIDERS.fn_sig)(tcx, def_id);
318-
result.map_bound(|mut inner| {
319-
if let Abi::C { .. } = inner.abi {
320-
inner.abi = Abi::Rust;
321-
}
322-
inner
323-
})
324-
};
293+
crate::abi::provide_extern(providers);
325294
}
326295

327296
fn codegen_crate(

0 commit comments

Comments
 (0)