Skip to content

Commit f09c19a

Browse files
committed
Introduce perma-unstable wasm-c-abi flag
1 parent 5bc7b9a commit f09c19a

File tree

10 files changed

+94
-13
lines changed

10 files changed

+94
-13
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_span::Span;
3131
use rustc_target::abi::{
3232
self, call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout, WrappingRange,
3333
};
34-
use rustc_target::spec::{HasTargetSpec, Target};
34+
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, WasmCAbi};
3535

3636
use crate::common::{type_is_pointer, SignType, TypeReflection};
3737
use crate::context::CodegenCx;
@@ -2349,6 +2349,12 @@ impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
23492349
}
23502350
}
23512351

2352+
impl<'tcx> HasWasmCAbiOpt for Builder<'_, '_, 'tcx> {
2353+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
2354+
self.cx.wasm_c_abi_opt()
2355+
}
2356+
}
2357+
23522358
pub trait ToGccComp {
23532359
fn to_gcc_comparison(&self) -> ComparisonOp;
23542360
}

compiler/rustc_codegen_gcc/src/context.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::{source_map::respan, Span};
2020
use rustc_target::abi::{
2121
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
2222
};
23-
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
23+
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, TlsModel, WasmCAbi};
2424

2525
use crate::callee::get_fn;
2626
use crate::common::SignType;
@@ -555,6 +555,12 @@ impl<'gcc, 'tcx> HasTargetSpec for CodegenCx<'gcc, 'tcx> {
555555
}
556556
}
557557

558+
impl<'gcc, 'tcx> HasWasmCAbiOpt for CodegenCx<'gcc, 'tcx> {
559+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
560+
self.tcx.sess.opts.unstable_opts.wasm_c_abi
561+
}
562+
}
563+
558564
impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
559565
type LayoutOfResult = TyAndLayout<'tcx>;
560566

compiler/rustc_interface/src/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use rustc_session::{build_session, getopts, CompilerIO, EarlyDiagCtxt, Session};
1717
use rustc_span::edition::{Edition, DEFAULT_EDITION};
1818
use rustc_span::symbol::sym;
1919
use rustc_span::{FileName, SourceFileHashAlgorithm};
20-
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
20+
use rustc_target::spec::{
21+
CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel, WasmCAbi,
22+
};
2123
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
2224
use std::collections::{BTreeMap, BTreeSet};
2325
use std::num::NonZero;
@@ -836,6 +838,7 @@ fn test_unstable_options_tracking_hash() {
836838
tracked!(verify_llvm_ir, true);
837839
tracked!(virtual_function_elimination, true);
838840
tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));
841+
tracked!(wasm_c_abi, WasmCAbi::Spec);
839842
// tidy-alphabetical-end
840843

841844
macro_rules! tracked_no_crate_hash {

compiler/rustc_middle/src/ty/layout.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_span::symbol::{sym, Symbol};
1515
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
1616
use rustc_target::abi::call::FnAbi;
1717
use rustc_target::abi::*;
18-
use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};
18+
use rustc_target::spec::{
19+
abi::Abi as SpecAbi, HasTargetSpec, HasWasmCAbiOpt, PanicStrategy, Target, WasmCAbi,
20+
};
1921

2022
use std::borrow::Cow;
2123
use std::cmp;
@@ -539,6 +541,12 @@ impl<'tcx> HasTargetSpec for TyCtxt<'tcx> {
539541
}
540542
}
541543

544+
impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {
545+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
546+
self.sess.opts.unstable_opts.wasm_c_abi
547+
}
548+
}
549+
542550
impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
543551
#[inline]
544552
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -584,6 +592,12 @@ impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> {
584592
}
585593
}
586594

595+
impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> {
596+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
597+
self.tcx.wasm_c_abi_opt()
598+
}
599+
}
600+
587601
impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
588602
fn tcx(&self) -> TyCtxt<'tcx> {
589603
self.tcx.tcx()

compiler/rustc_session/src/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3227,7 +3227,7 @@ pub(crate) mod dep_tracking {
32273227
use rustc_feature::UnstableFeatures;
32283228
use rustc_span::edition::Edition;
32293229
use rustc_span::RealFileName;
3230-
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel};
3230+
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel, WasmCAbi};
32313231
use rustc_target::spec::{
32323232
RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
32333233
};
@@ -3324,6 +3324,7 @@ pub(crate) mod dep_tracking {
33243324
Polonius,
33253325
InliningThreshold,
33263326
FunctionReturn,
3327+
WasmCAbi,
33273328
);
33283329

33293330
impl<T1, T2> DepTrackingHash for (T1, T2)

compiler/rustc_session/src/options.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_data_structures::profiling::TimePassesFormat;
88
use rustc_data_structures::stable_hasher::Hash64;
99
use rustc_errors::ColorConfig;
1010
use rustc_errors::{LanguageIdentifier, TerminalUrl};
11-
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
11+
use rustc_target::spec::{
12+
CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet, WasmCAbi,
13+
};
1214
use rustc_target::spec::{
1315
RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
1416
};
@@ -437,6 +439,7 @@ mod desc {
437439
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
438440
pub const parse_llvm_module_flag: &str = "<key>:<type>:<value>:<behavior>. Type must currently be `u32`. Behavior should be one of (`error`, `warning`, `require`, `override`, `append`, `appendunique`, `max`, `min`)";
439441
pub const parse_function_return: &str = "`keep` or `thunk-extern`";
442+
pub const parse_wasm_c_abi: &str = "`legacy` or `spec`";
440443
}
441444

442445
mod parse {
@@ -1402,6 +1405,15 @@ mod parse {
14021405
}
14031406
true
14041407
}
1408+
1409+
pub(crate) fn parse_wasm_c_abi(slot: &mut WasmCAbi, v: Option<&str>) -> bool {
1410+
match v {
1411+
Some("spec") => *slot = WasmCAbi::Spec,
1412+
Some("legacy") => *slot = WasmCAbi::Legacy,
1413+
_ => return false,
1414+
}
1415+
true
1416+
}
14051417
}
14061418

14071419
options! {
@@ -2021,6 +2033,8 @@ written to standard error output)"),
20212033
Requires `-Clto[=[fat,yes]]`"),
20222034
wasi_exec_model: Option<WasiExecModel> = (None, parse_wasi_exec_model, [TRACKED],
20232035
"whether to build a wasi command or reactor"),
2036+
wasm_c_abi: WasmCAbi = (WasmCAbi::Legacy, parse_wasm_c_abi, [TRACKED],
2037+
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
20242038
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
20252039
"whether long type names should be written to files instead of being printed in errors"),
20262040
// tidy-alphabetical-end

compiler/rustc_target/src/abi/call/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::abi::{self, Abi, Align, FieldsShape, Size};
22
use crate::abi::{HasDataLayout, TyAbiInterface, TyAndLayout};
3-
use crate::spec::{self, HasTargetSpec};
3+
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt};
44
use rustc_span::Symbol;
55
use std::fmt;
66
use std::str::FromStr;
@@ -782,7 +782,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
782782
) -> Result<(), AdjustForForeignAbiError>
783783
where
784784
Ty: TyAbiInterface<'a, C> + Copy,
785-
C: HasDataLayout + HasTargetSpec,
785+
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt,
786786
{
787787
if abi == spec::abi::Abi::X86Interrupt {
788788
if let Some(arg) = self.args.first_mut() {
@@ -839,7 +839,9 @@ impl<'a, Ty> FnAbi<'a, Ty> {
839839
"sparc" => sparc::compute_abi_info(cx, self),
840840
"sparc64" => sparc64::compute_abi_info(cx, self),
841841
"nvptx64" => {
842-
if cx.target_spec().adjust_abi(abi, self.c_variadic) == spec::abi::Abi::PtxKernel {
842+
if cx.target_spec().adjust_abi(cx, abi, self.c_variadic)
843+
== spec::abi::Abi::PtxKernel
844+
{
843845
nvptx64::compute_ptx_kernel_abi_info(cx, self)
844846
} else {
845847
nvptx64::compute_abi_info(self)
@@ -848,7 +850,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
848850
"hexagon" => hexagon::compute_abi_info(self),
849851
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
850852
"wasm32" | "wasm64" => {
851-
if cx.target_spec().adjust_abi(abi, self.c_variadic) == spec::abi::Abi::Wasm {
853+
if cx.target_spec().adjust_abi(cx, abi, self.c_variadic) == spec::abi::Abi::Wasm {
852854
wasm::compute_wasm_abi_info(self)
853855
} else {
854856
wasm::compute_c_abi_info(cx, self)

compiler/rustc_target/src/spec/mod.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,19 @@ impl HasTargetSpec for Target {
18071807
}
18081808
}
18091809

1810+
/// Which C ABI to use for `wasm32-unknown-unknown`.
1811+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1812+
pub enum WasmCAbi {
1813+
/// Spec-compliant C ABI.
1814+
Spec,
1815+
/// Legacy ABI. Which is non-spec-compliant.
1816+
Legacy,
1817+
}
1818+
1819+
pub trait HasWasmCAbiOpt {
1820+
fn wasm_c_abi_opt(&self) -> WasmCAbi;
1821+
}
1822+
18101823
type StaticCow<T> = Cow<'static, T>;
18111824

18121825
/// Optional aspects of a target specification.
@@ -2417,9 +2430,21 @@ impl DerefMut for Target {
24172430

24182431
impl Target {
24192432
/// Given a function ABI, turn it into the correct ABI for this target.
2420-
pub fn adjust_abi(&self, abi: Abi, c_variadic: bool) -> Abi {
2433+
pub fn adjust_abi<C>(&self, cx: &C, abi: Abi, c_variadic: bool) -> Abi
2434+
where
2435+
C: HasWasmCAbiOpt,
2436+
{
24212437
match abi {
2422-
Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
2438+
Abi::C { .. } => {
2439+
if self.arch == "wasm32"
2440+
&& self.os == "unknown"
2441+
&& cx.wasm_c_abi_opt() == WasmCAbi::Spec
2442+
{
2443+
abi
2444+
} else {
2445+
self.default_adjusted_cabi.unwrap_or(abi)
2446+
}
2447+
}
24232448

24242449
// On Windows, `extern "system"` behaves like msvc's `__stdcall`.
24252450
// `__stdcall` only applies on x86 and on non-variadic functions:

compiler/rustc_ty_utils/src/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ fn fn_sig_for_fn_abi<'tcx>(
311311
#[inline]
312312
fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi, c_variadic: bool) -> Conv {
313313
use rustc_target::spec::abi::Abi::*;
314-
match tcx.sess.target.adjust_abi(abi, c_variadic) {
314+
match tcx.sess.target.adjust_abi(&tcx, abi, c_variadic) {
315315
RustIntrinsic | Rust | RustCall => Conv::Rust,
316316

317317
// This is intentionally not using `Conv::Cold`, as that has to preserve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `wasm-c-abi`
2+
3+
This option controls whether Rust uses the spec-compliant C ABI when compiling
4+
for the `wasm32-unknown-unknown` target.
5+
6+
This makes it possible to be ABI-compatible with all other spec-compliant Wasm
7+
like Rusts `wasm32-wasi`.
8+
9+
This compiler flag is perma-unstable, as it will be enabled by default in the
10+
future with no option to fall back to the old non-spec-compliant ABI.

0 commit comments

Comments
 (0)