Skip to content

Commit

Permalink
Rollup merge of rust-lang#83763 - alexcrichton:wasm-multivalue-abi, r…
Browse files Browse the repository at this point in the history
…=nagisa

rustc: Add a new `wasm` ABI

This commit implements the idea of a new ABI for the WebAssembly target,
one called `"wasm"`. This ABI is entirely of my own invention
and has no current precedent, but I think that the addition of this ABI
might help solve a number of issues with the WebAssembly targets.

When `wasm32-unknown-unknown` was first added to Rust I naively
"implemented an abi" for the target. I then went to write `wasm-bindgen`
which accidentally relied on details of this ABI. Turns out the ABI
definition didn't match C, which is causing issues for C/Rust interop.
Currently the compiler has a "wasm32 bindgen compat" ABI which is the
original implementation I added, and it's purely there for, well,
`wasm-bindgen`.

Another issue with the WebAssembly target is that it's not clear to me
when and if the default C ABI will change to account for WebAssembly's
multi-value feature (a feature that allows functions to return multiple
values). Even if this does happen, though, it seems like the C ABI will
be guided based on the performance of WebAssembly code and will likely
not match even what the current wasm-bindgen-compat ABI is today. This
leaves a hole in Rust's expressivity in binding WebAssembly where given
a particular import type, Rust may not be able to import that signature
with an updated C ABI for multi-value.

To fix these issues I had the idea of a new ABI for WebAssembly, one
called `wasm`. The definition of this ABI is "what you write
maps straight to wasm". The goal here is that whatever you write down in
the parameter list or in the return values goes straight into the
function's signature in the WebAssembly file. This special ABI is for
intentionally matching the ABI of an imported function from the
environment or exporting a function with the right signature.

With the addition of a new ABI, this enables rustc to:

* Eventually remove the "wasm-bindgen compat hack". Once this multivalue
  ABI is stable wasm-bindgen can switch to using it everywhere.
  Afterwards the wasm32-unknown-unknown target can have its default ABI
  updated to match C.

* Expose the ability to precisely match an ABI signature for a
  WebAssembly function, regardless of what the C ABI that clang chooses
  turns out to be.

* Continue to evolve the definition of the default C ABI to match what
  clang does on all targets, since the purpose of that ABI will be
  explicitly matching C rather than generating particular function
  imports/exports.

Naturally this is implemented as an unstable feature initially, but it
would be nice for this to get stabilized (if it works) in the near-ish
future to remove the wasm32-unknown-unknown incompatibility with the C
ABI. Doing this, however, requires the feature to be on stable because
wasm-bindgen works with stable Rust.
  • Loading branch information
Dylan-DPC authored Apr 3, 2021
2 parents f1d4bfb + 0665766 commit ef60832
Show file tree
Hide file tree
Showing 21 changed files with 388 additions and 120 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ impl<'a> PostExpansionVisitor<'a> {
"thiscall-unwind ABI is experimental and subject to change"
);
}
"wasm" => {
gate_feature_post!(
&self,
wasm_abi,
span,
"wasm ABI is experimental and subject to change"
);
}
abi => self
.sess
.parse_sess
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::{OptLevel, SanitizerSet};
use rustc_session::Session;
use rustc_target::spec::abi::Abi;
use rustc_target::spec::StackProbeType;

use crate::attributes;
Expand Down Expand Up @@ -289,7 +290,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
// The target doesn't care; the subtarget reads our attribute.
apply_tune_cpu_attr(cx, llfn);

let function_features = codegen_fn_attrs
let mut function_features = codegen_fn_attrs
.target_features
.iter()
.map(|f| {
Expand All @@ -301,6 +302,18 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
}))
.collect::<Vec<String>>();

// The `"wasm"` abi on wasm targets automatically enables the `+multivalue`
// feature because the purpose of the wasm abi is to match the WebAssembly
// specification, which has this feature. This won't be needed when LLVM
// enables this `multivalue` feature by default.
if cx.tcx.sess.target.arch == "wasm32" && !cx.tcx.is_closure(instance.def_id()) {
let abi = cx.tcx.fn_sig(instance.def_id()).abi();
if abi == Abi::Wasm {
function_features.push("+multivalue".to_string());
}
}

if !function_features.is_empty() {
let mut global_features = llvm_util::llvm_global_features(cx.tcx.sess);
global_features.extend(function_features.into_iter());
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@ declare_features! (
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries.
(active, c_unwind, "1.52.0", Some(74990), None),

/// Allows `extern "wasm" fn`
(active, wasm_abi, "1.53.0", Some(83788), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,7 @@ fn fn_can_unwind(
| AvrInterrupt
| AvrNonBlockingInterrupt
| CCmseNonSecureCall
| Wasm
| RustIntrinsic
| PlatformIntrinsic
| Unadjusted => false,
Expand Down Expand Up @@ -2712,6 +2713,7 @@ where
AmdGpuKernel => Conv::AmdGpuKernel,
AvrInterrupt => Conv::AvrInterrupt,
AvrNonBlockingInterrupt => Conv::AvrNonBlockingInterrupt,
Wasm => Conv::C,

// These API constants ought to be more specific...
Cdecl => Conv::C,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, abi: Abi) -> bo
| AvrInterrupt
| AvrNonBlockingInterrupt
| CCmseNonSecureCall
| Wasm
| RustIntrinsic
| PlatformIntrinsic
| Unadjusted => true,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ symbols! {
vreg,
vreg_low16,
warn,
wasm_abi,
wasm_import_module,
wasm_target_feature,
while_let,
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod s390x;
mod sparc;
mod sparc64;
mod wasm32;
mod wasm32_bindgen_compat;
mod x86;
mod x86_64;
mod x86_win64;
Expand Down Expand Up @@ -647,11 +646,14 @@ impl<'a, Ty> FnAbi<'a, Ty> {
"nvptx64" => nvptx64::compute_abi_info(self),
"hexagon" => hexagon::compute_abi_info(self),
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
"wasm32" => match cx.target_spec().os.as_str() {
"emscripten" | "wasi" => wasm32::compute_abi_info(cx, self),
_ => wasm32_bindgen_compat::compute_abi_info(self),
},
"asmjs" => wasm32::compute_abi_info(cx, self),
"wasm32" => {
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::Wasm {
wasm32::compute_wasm_abi_info(self)
} else {
wasm32::compute_c_abi_info(cx, self)
}
}
"asmjs" => wasm32::compute_c_abi_info(cx, self),
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
}

Expand Down
27 changes: 26 additions & 1 deletion compiler/rustc_target/src/abi/call/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ where
}
}

pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
/// The purpose of this ABI is to match the C ABI (aka clang) exactly.
pub fn compute_c_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
where
Ty: TyAndLayoutMethods<'a, C> + Copy,
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
Expand All @@ -56,3 +57,27 @@ where
classify_arg(cx, arg);
}
}

/// The purpose of this ABI is for matching the WebAssembly standard. This
/// intentionally diverges from the C ABI and is specifically crafted to take
/// advantage of LLVM's support of multiple returns in WebAssembly.
pub fn compute_wasm_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
if !fn_abi.ret.is_ignore() {
classify_ret(&mut fn_abi.ret);
}

for arg in &mut fn_abi.args {
if arg.is_ignore() {
continue;
}
classify_arg(arg);
}

fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
ret.extend_integer_width_to(32);
}

fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
arg.extend_integer_width_to(32);
}
}
29 changes: 0 additions & 29 deletions compiler/rustc_target/src/abi/call/wasm32_bindgen_compat.rs

This file was deleted.

15 changes: 9 additions & 6 deletions compiler/rustc_target/src/spec/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum Abi {
AvrInterrupt,
AvrNonBlockingInterrupt,
CCmseNonSecureCall,
Wasm,

// Multiplatform / generic ABIs
System { unwind: bool },
Expand Down Expand Up @@ -83,6 +84,7 @@ const AbiDatas: &[AbiData] = &[
generic: false,
},
AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call", generic: false },
AbiData { abi: Abi::Wasm, name: "wasm", generic: false },
// Cross-platform ABIs
AbiData { abi: Abi::System { unwind: false }, name: "system", generic: true },
AbiData { abi: Abi::System { unwind: true }, name: "system-unwind", generic: true },
Expand Down Expand Up @@ -131,13 +133,14 @@ impl Abi {
AvrInterrupt => 18,
AvrNonBlockingInterrupt => 19,
CCmseNonSecureCall => 20,
Wasm => 21,
// Cross-platform ABIs
System { unwind: false } => 21,
System { unwind: true } => 22,
RustIntrinsic => 23,
RustCall => 24,
PlatformIntrinsic => 25,
Unadjusted => 26,
System { unwind: false } => 22,
System { unwind: true } => 23,
RustIntrinsic => 24,
RustCall => 25,
PlatformIntrinsic => 26,
Unadjusted => 27,
};
debug_assert!(
AbiDatas
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ pub struct TargetOptions {
/// How to handle split debug information, if at all. Specifying `None` has
/// target-specific meaning.
pub split_debuginfo: SplitDebuginfo,

/// If present it's a default value to use for adjusting the C ABI.
pub default_adjusted_cabi: Option<Abi>,
}

impl Default for TargetOptions {
Expand Down Expand Up @@ -1265,6 +1268,7 @@ impl Default for TargetOptions {
eh_frame_header: true,
has_thumb_interworking: false,
split_debuginfo: SplitDebuginfo::Off,
default_adjusted_cabi: None,
}
}
}
Expand Down Expand Up @@ -1316,6 +1320,9 @@ impl Target {
Abi::C { unwind: false }
}
}

Abi::C { unwind } => self.default_adjusted_cabi.unwrap_or(Abi::C { unwind }),

abi => abi,
}
}
Expand Down Expand Up @@ -1632,6 +1639,16 @@ impl Target {
}
}
} );
($key_name:ident, Option<Abi>) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
match lookup_abi(s) {
Some(abi) => base.$key_name = Some(abi),
_ => return Some(Err(format!("'{}' is not a valid value for abi", s))),
}
Some(Ok(()))
})).unwrap_or(Ok(()))
} );
}

if let Some(s) = obj.find("target-endian").and_then(Json::as_string) {
Expand Down Expand Up @@ -1729,6 +1746,7 @@ impl Target {
key!(eh_frame_header, bool);
key!(has_thumb_interworking, bool);
key!(split_debuginfo, SplitDebuginfo)?;
key!(default_adjusted_cabi, Option<Abi>)?;

// NB: The old name is deprecated, but support for it is retained for
// compatibility.
Expand Down Expand Up @@ -1967,6 +1985,10 @@ impl ToJson for Target {
target_option_val!(has_thumb_interworking);
target_option_val!(split_debuginfo);

if let Some(abi) = self.default_adjusted_cabi {
d.insert("default-adjusted-cabi".to_string(), Abi::name(abi).to_json());
}

if default.unsupported_abis != self.unsupported_abis {
d.insert(
"unsupported-abis".to_string(),
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_target/src/spec/wasm32_unknown_unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@

use super::wasm32_base;
use super::{LinkerFlavor, LldFlavor, Target};
use crate::spec::abi::Abi;

pub fn target() -> Target {
let mut options = wasm32_base::options();
options.os = "unknown".to_string();
options.linker_flavor = LinkerFlavor::Lld(LldFlavor::Wasm);

// This is a default for backwards-compatibility with the original
// definition of this target oh-so-long-ago. Once the "wasm" ABI is
// stable and the wasm-bindgen project has switched to using it then there's
// no need for this and it can be removed.
//
// Currently this is the reason that this target's ABI is mismatched with
// clang's ABI. This means that, in the limit, you can't merge C and Rust
// code on this target due to this ABI mismatch.
options.default_adjusted_cabi = Some(Abi::Wasm);

let clang_args = options.pre_link_args.entry(LinkerFlavor::Gcc).or_default();

// Make sure clang uses LLD as its linker and is configured appropriately
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/wasm-abi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../../run-make-fulldeps/tools.mk

# only-wasm32-bare

all:
$(RUSTC) foo.rs --target wasm32-unknown-unknown
$(NODE) foo.js $(TMPDIR)/foo.wasm
22 changes: 22 additions & 0 deletions src/test/run-make/wasm-abi/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fs = require('fs');
const process = require('process');
const assert = require('assert');
const buffer = fs.readFileSync(process.argv[2]);

const m = new WebAssembly.Module(buffer);
const i = new WebAssembly.Instance(m, {
host: {
two_i32: () => [100, 101],
two_i64: () => [102n, 103n],
two_f32: () => [104, 105],
two_f64: () => [106, 107],
mishmash: () => [108, 109, 110, 111n, 112, 113],
}
});

assert.deepEqual(i.exports.return_two_i32(), [1, 2])
assert.deepEqual(i.exports.return_two_i64(), [3, 4])
assert.deepEqual(i.exports.return_two_f32(), [5, 6])
assert.deepEqual(i.exports.return_two_f64(), [7, 8])
assert.deepEqual(i.exports.return_mishmash(), [9, 10, 11, 12, 13, 14])
i.exports.call_imports();
Loading

0 comments on commit ef60832

Please sign in to comment.