Skip to content

Commit 72b6505

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

File tree

9 files changed

+62
-10
lines changed

9 files changed

+62
-10
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};
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) -> bool {
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};
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) -> bool {
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

+1
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ fn test_unstable_options_tracking_hash() {
831831
tracked!(verify_llvm_ir, true);
832832
tracked!(virtual_function_elimination, true);
833833
tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));
834+
tracked!(wasm_c_abi, true);
834835
// tidy-alphabetical-end
835836

836837
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,
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) -> bool {
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) -> bool {
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/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,8 @@ written to standard error output)"),
19331933
Requires `-Clto[=[fat,yes]]`"),
19341934
wasi_exec_model: Option<WasiExecModel> = (None, parse_wasi_exec_model, [TRACKED],
19351935
"whether to build a wasi command or reactor"),
1936+
wasm_c_abi: bool = (false, parse_bool, [TRACKED],
1937+
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: no)"),
19361938
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
19371939
"whether long type names should be written to files instead of being printed in errors"),
19381940
// 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

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

1833+
pub trait HasWasmCAbiOpt {
1834+
fn wasm_c_abi_opt(&self) -> bool;
1835+
}
1836+
18331837
type StaticCow<T> = Cow<'static, T>;
18341838

18351839
/// Optional aspects of a target specification.
@@ -2441,9 +2445,18 @@ impl DerefMut for Target {
24412445

24422446
impl Target {
24432447
/// Given a function ABI, turn it into the correct ABI for this target.
2444-
pub fn adjust_abi(&self, abi: Abi) -> Abi {
2448+
pub fn adjust_abi<C>(&self, cx: &C, abi: Abi) -> Abi
2449+
where
2450+
C: HasWasmCAbiOpt,
2451+
{
24452452
match abi {
2446-
Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
2453+
Abi::C { .. } => {
2454+
if self.arch == "wasm32" && self.os == "unknown" && cx.wasm_c_abi_opt() {
2455+
abi
2456+
} else {
2457+
self.default_adjusted_cabi.unwrap_or(abi)
2458+
}
2459+
}
24472460
Abi::System { unwind } if self.is_like_windows && self.arch == "x86" => {
24482461
Abi::Stdcall { unwind }
24492462
}

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)