Skip to content

Commit 571d6df

Browse files
committed
Introduce perma-unstable wasm-c-abi flag
1 parent 49b27f4 commit 571d6df

File tree

10 files changed

+92
-13
lines changed

10 files changed

+92
-13
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_target::abi::{
4848
TargetDataLayout,
4949
WrappingRange,
5050
};
51-
use rustc_target::spec::{HasTargetSpec, Target};
51+
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, WasmCAbi};
5252

5353
use crate::common::{SignType, TypeReflection, type_is_pointer};
5454
use crate::context::CodegenCx;
@@ -1952,6 +1952,12 @@ impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
19521952
}
19531953
}
19541954

1955+
impl<'tcx> HasWasmCAbiOpt for Builder<'_, '_, 'tcx> {
1956+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
1957+
self.cx.wasm_c_abi_opt()
1958+
}
1959+
}
1960+
19551961
pub trait ToGccComp {
19561962
fn to_gcc_comparison(&self) -> ComparisonOp;
19571963
}

compiler/rustc_codegen_gcc/src/context.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::layout::{FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest
1717
use rustc_session::Session;
1818
use rustc_span::{Span, source_map::respan};
1919
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
20-
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
20+
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, TlsModel, WasmCAbi};
2121

2222
use crate::callee::get_fn;
2323
use crate::common::SignType;
@@ -500,6 +500,12 @@ impl<'gcc, 'tcx> HasTargetSpec for CodegenCx<'gcc, 'tcx> {
500500
}
501501
}
502502

503+
impl<'gcc, 'tcx> HasWasmCAbiOpt for CodegenCx<'gcc, 'tcx> {
504+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
505+
self.tcx.sess.opts.unstable_opts.wasm_c_abi
506+
}
507+
}
508+
503509
impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
504510
type LayoutOfResult = TyAndLayout<'tcx>;
505511

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, EarlyErrorHandler, Sessi
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::NonZeroUsize;
@@ -831,6 +833,7 @@ fn test_unstable_options_tracking_hash() {
831833
tracked!(verify_llvm_ir, true);
832834
tracked!(virtual_function_elimination, true);
833835
tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));
836+
tracked!(wasm_c_abi, WasmCAbi::Spec);
834837
// tidy-alphabetical-end
835838

836839
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::cmp;
2123
use std::fmt;
@@ -552,6 +554,12 @@ impl<'tcx> HasTargetSpec for TyCtxt<'tcx> {
552554
}
553555
}
554556

557+
impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {
558+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
559+
self.sess.opts.unstable_opts.wasm_c_abi
560+
}
561+
}
562+
555563
impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
556564
#[inline]
557565
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -597,6 +605,12 @@ impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> {
597605
}
598606
}
599607

608+
impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> {
609+
fn wasm_c_abi_opt(&self) -> WasmCAbi {
610+
self.tcx.wasm_c_abi_opt()
611+
}
612+
}
613+
600614
impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
601615
fn tcx(&self) -> TyCtxt<'tcx> {
602616
self.tcx.tcx()

compiler/rustc_session/src/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3174,7 +3174,7 @@ pub(crate) mod dep_tracking {
31743174
use rustc_feature::UnstableFeatures;
31753175
use rustc_span::edition::Edition;
31763176
use rustc_span::RealFileName;
3177-
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel};
3177+
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel, WasmCAbi};
31783178
use rustc_target::spec::{
31793179
RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
31803180
};
@@ -3270,6 +3270,7 @@ pub(crate) mod dep_tracking {
32703270
TraitSolver,
32713271
Polonius,
32723272
InliningThreshold,
3273+
WasmCAbi,
32733274
);
32743275

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

compiler/rustc_session/src/options.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_data_structures::profiling::TimePassesFormat;
77
use rustc_data_structures::stable_hasher::Hash64;
88
use rustc_errors::ColorConfig;
99
use rustc_errors::{LanguageIdentifier, TerminalUrl};
10-
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
10+
use rustc_target::spec::{
11+
CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet, WasmCAbi,
12+
};
1113
use rustc_target::spec::{
1214
RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
1315
};
@@ -429,6 +431,7 @@ mod desc {
429431
pub const parse_remap_path_scope: &str = "comma separated list of scopes: `macro`, `diagnostics`, `unsplit-debuginfo`, `split-debuginfo`, `split-debuginfo-path`, `object`, `all`";
430432
pub const parse_inlining_threshold: &str =
431433
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
434+
pub const parse_wasm_c_abi: &str = "`legacy` or `spec`";
432435
}
433436

434437
mod parse {
@@ -1331,6 +1334,15 @@ mod parse {
13311334
}
13321335
true
13331336
}
1337+
1338+
pub(crate) fn parse_wasm_c_abi(slot: &mut WasmCAbi, v: Option<&str>) -> bool {
1339+
match v {
1340+
Some("spec") => *slot = WasmCAbi::Spec,
1341+
Some("legacy") => *slot = WasmCAbi::Legacy,
1342+
_ => return false,
1343+
}
1344+
true
1345+
}
13341346
}
13351347

13361348
options! {
@@ -1933,6 +1945,8 @@ written to standard error output)"),
19331945
Requires `-Clto[=[fat,yes]]`"),
19341946
wasi_exec_model: Option<WasiExecModel> = (None, parse_wasi_exec_model, [TRACKED],
19351947
"whether to build a wasi command or reactor"),
1948+
wasm_c_abi: WasmCAbi = (WasmCAbi::Legacy, parse_wasm_c_abi, [TRACKED],
1949+
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
19361950
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
19371951
"whether long type names should be written to files instead of being printed in errors"),
19381952
// tidy-alphabetical-end

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

+4-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;
@@ -764,7 +764,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
764764
) -> Result<(), AdjustForForeignAbiError>
765765
where
766766
Ty: TyAbiInterface<'a, C> + Copy,
767-
C: HasDataLayout + HasTargetSpec,
767+
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt,
768768
{
769769
if abi == spec::abi::Abi::X86Interrupt {
770770
if let Some(arg) = self.args.first_mut() {
@@ -821,7 +821,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
821821
"sparc" => sparc::compute_abi_info(cx, self),
822822
"sparc64" => sparc64::compute_abi_info(cx, self),
823823
"nvptx64" => {
824-
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::PtxKernel {
824+
if cx.target_spec().adjust_abi(cx, abi) == spec::abi::Abi::PtxKernel {
825825
nvptx64::compute_ptx_kernel_abi_info(cx, self)
826826
} else {
827827
nvptx64::compute_abi_info(self)
@@ -830,7 +830,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
830830
"hexagon" => hexagon::compute_abi_info(self),
831831
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
832832
"wasm32" | "wasm64" => {
833-
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::Wasm {
833+
if cx.target_spec().adjust_abi(cx, abi) == spec::abi::Abi::Wasm {
834834
wasm::compute_wasm_abi_info(self)
835835
} else {
836836
wasm::compute_c_abi_info(cx, self)

compiler/rustc_target/src/spec/mod.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,19 @@ impl HasTargetSpec for Target {
18301830
}
18311831
}
18321832

1833+
/// Which C ABI to use for `wasm32-unknown-unknown`.
1834+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1835+
pub enum WasmCAbi {
1836+
/// Spec-compliant C ABI.
1837+
Spec,
1838+
/// Legacy ABI. Which is non-spec-compliant.
1839+
Legacy,
1840+
}
1841+
1842+
pub trait HasWasmCAbiOpt {
1843+
fn wasm_c_abi_opt(&self) -> WasmCAbi;
1844+
}
1845+
18331846
type StaticCow<T> = Cow<'static, T>;
18341847

18351848
/// Optional aspects of a target specification.
@@ -2441,9 +2454,21 @@ impl DerefMut for Target {
24412454

24422455
impl Target {
24432456
/// Given a function ABI, turn it into the correct ABI for this target.
2444-
pub fn adjust_abi(&self, abi: Abi) -> Abi {
2457+
pub fn adjust_abi<C>(&self, cx: &C, abi: Abi) -> Abi
2458+
where
2459+
C: HasWasmCAbiOpt,
2460+
{
24452461
match abi {
2446-
Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
2462+
Abi::C { .. } => {
2463+
if self.arch == "wasm32"
2464+
&& self.os == "unknown"
2465+
&& cx.wasm_c_abi_opt() == WasmCAbi::Spec
2466+
{
2467+
abi
2468+
} else {
2469+
self.default_adjusted_cabi.unwrap_or(abi)
2470+
}
2471+
}
24472472
Abi::System { unwind } if self.is_like_windows && self.arch == "x86" => {
24482473
Abi::Stdcall { unwind }
24492474
}

compiler/rustc_ty_utils/src/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn fn_sig_for_fn_abi<'tcx>(
169169
#[inline]
170170
fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi) -> Conv {
171171
use rustc_target::spec::abi::Abi::*;
172-
match tcx.sess.target.adjust_abi(abi) {
172+
match tcx.sess.target.adjust_abi(&tcx, abi) {
173173
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => Conv::Rust,
174174

175175
// 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)