Skip to content

Commit

Permalink
Introduce perma-unstable wasm-c-abi flag
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Nov 14, 2023
1 parent 49b27f4 commit 9021455
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(use_ctors_section, Some(true));
tracked!(verify_llvm_ir, true);
tracked!(virtual_function_elimination, true);
tracked!(wasm_c_abi, true);
tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));

Check failure on line 834 in compiler/rustc_interface/src/tests.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line not in alphabetical order
// tidy-alphabetical-end

Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
use rustc_target::abi::call::FnAbi;
use rustc_target::abi::*;
use rustc_target::spec::HasWasmCAbiOpt;
use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};

use std::cmp;
Expand Down Expand Up @@ -552,6 +553,12 @@ impl<'tcx> HasTargetSpec for TyCtxt<'tcx> {
}
}

impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {
fn wasm_c_abi_opt(&self) -> bool {
self.sess.opts.unstable_opts.wasm_c_abi
}
}

impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
#[inline]
fn tcx(&self) -> TyCtxt<'tcx> {
Expand Down Expand Up @@ -597,6 +604,12 @@ impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> {
}
}

impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> {
fn wasm_c_abi_opt(&self) -> bool {
self.tcx.wasm_c_abi_opt()
}
}

impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx.tcx()
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,8 @@ written to standard error output)"),
virtual_function_elimination: bool = (false, parse_bool, [TRACKED],
"enables dead virtual function elimination optimization. \
Requires `-Clto[=[fat,yes]]`"),
wasm_c_abi: bool = (false, parse_bool, [TRACKED],
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: no)"),
wasi_exec_model: Option<WasiExecModel> = (None, parse_wasi_exec_model, [TRACKED],

Check failure on line 1936 in compiler/rustc_session/src/options.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line not in alphabetical order
"whether to build a wasi command or reactor"),
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::abi::{self, Abi, Align, FieldsShape, Size};
use crate::abi::{HasDataLayout, TyAbiInterface, TyAndLayout};
use crate::spec::{self, HasTargetSpec};
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt};
use rustc_span::Symbol;
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -764,7 +764,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
) -> Result<(), AdjustForForeignAbiError>
where
Ty: TyAbiInterface<'a, C> + Copy,
C: HasDataLayout + HasTargetSpec,
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt,
{
if abi == spec::abi::Abi::X86Interrupt {
if let Some(arg) = self.args.first_mut() {
Expand Down Expand Up @@ -821,7 +821,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
"sparc" => sparc::compute_abi_info(cx, self),
"sparc64" => sparc64::compute_abi_info(cx, self),
"nvptx64" => {
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::PtxKernel {
if cx.target_spec().adjust_abi(cx, abi) == spec::abi::Abi::PtxKernel {
nvptx64::compute_ptx_kernel_abi_info(cx, self)
} else {
nvptx64::compute_abi_info(self)
Expand All @@ -830,7 +830,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
"hexagon" => hexagon::compute_abi_info(self),
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
"wasm32" | "wasm64" => {
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::Wasm {
if cx.target_spec().adjust_abi(cx, abi) == spec::abi::Abi::Wasm {
wasm::compute_wasm_abi_info(self)
} else {
wasm::compute_c_abi_info(cx, self)
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,10 @@ impl HasTargetSpec for Target {
}
}

pub trait HasWasmCAbiOpt {
fn wasm_c_abi_opt(&self) -> bool;
}

type StaticCow<T> = Cow<'static, T>;

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

impl Target {
/// Given a function ABI, turn it into the correct ABI for this target.
pub fn adjust_abi(&self, abi: Abi) -> Abi {
pub fn adjust_abi<C>(&self, cx: &C, abi: Abi) -> Abi
where
C: HasWasmCAbiOpt,
{
match abi {
Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
Abi::C { .. } => {
if self.arch == "wasm32" && self.os == "unknown" && cx.wasm_c_abi_opt() {
abi
} else {
self.default_adjusted_cabi.unwrap_or(abi)
}
}
Abi::System { unwind } if self.is_like_windows && self.arch == "x86" => {
Abi::Stdcall { unwind }
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn fn_sig_for_fn_abi<'tcx>(
#[inline]
fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi) -> Conv {
use rustc_target::spec::abi::Abi::*;
match tcx.sess.target.adjust_abi(abi) {
match tcx.sess.target.adjust_abi(&tcx, abi) {
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => Conv::Rust,

// This is intentionally not using `Conv::Cold`, as that has to preserve
Expand Down
10 changes: 10 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/wasm-c-abi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `wasm-c-abi`

This option controls whether Rust uses the spec-compliant C ABI when compiling
for the `wasm32-unknown-unknown` target.

This makes it possible to be ABI-compatible with all other spec-compliant Wasm
like Rusts `wasm32-wasi`.

This compiler flag is perma-unstable, as it will be enabled by default in the
future with no option to fall back to the old non-spec-compliant ABI.

0 comments on commit 9021455

Please sign in to comment.