Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm64 support #80525

Merged
merged 1 commit into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
// Note that currently the `wasm-import-module` doesn't do anything, but
// eventually LLVM 7 should read this and ferry the appropriate import
// module to the output file.
if cx.tcx.sess.target.arch == "wasm32" {
if cx.tcx.sess.target.is_like_wasm {
if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
llvm::AddFunctionAttrStringValue(
llfn,
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ pub fn target_machine_factory(
// On the wasm target once the `atomics` feature is enabled that means that
// we're no longer single-threaded, or otherwise we don't want LLVM to
// lower atomic operations to single-threaded operations.
if singlethread
&& sess.target.llvm_target.contains("wasm32")
&& sess.target_features.contains(&sym::atomics)
{
if singlethread && sess.target.is_like_wasm && sess.target_features.contains(&sym::atomics) {
singlethread = false;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,9 @@ pub fn compile_unit_metadata(
);
}

// Insert `llvm.ident` metadata on the wasm32 targets since that will
// Insert `llvm.ident` metadata on the wasm targets since that will
// get hooked up to the "producer" sections `processed-by` information.
if tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
if tcx.sess.target.is_like_wasm {
let name_metadata = llvm::LLVMMDStringInContext(
debug_context.llcontext,
rustc_producer.as_ptr().cast(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'a> GccLinker<'a> {
// * On OSX they have their own linker, not binutils'
// * For WebAssembly the only functional linker is LLD, which doesn't
// support hint flags
!self.sess.target.is_like_osx && self.sess.target.arch != "wasm32"
!self.sess.target.is_like_osx && !self.sess.target.is_like_wasm
}

// Some platforms take hints about whether a library is static or dynamic.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Opt
"mips" | "mips64" => MIPS_ALLOWED_FEATURES,
"powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
"riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
"wasm32" => WASM_ALLOWED_FEATURES,
"wasm32" | "wasm64" => WASM_ALLOWED_FEATURES,
_ => &[],
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,9 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
}
}
ret.insert((sym::target_arch, Some(Symbol::intern(arch))));
if sess.target.is_like_wasm {
ret.insert((sym::wasm, None));
}
ret.insert((sym::target_endian, Some(Symbol::intern(end.as_str()))));
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
ret.insert((sym::target_env, Some(Symbol::intern(env))));
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 @@ -1291,6 +1291,7 @@ symbols! {
vreg,
vreg_low16,
warn,
wasm,
wasm_import_module,
wasm_target_feature,
while_let,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn compute_symbol_name(
//
// [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
if is_foreign
&& (tcx.sess.target.arch != "wasm32"
&& (!tcx.sess.target.is_like_wasm
|| !tcx.wasm_import_module_map(def_id.krate).contains_key(&def_id))
{
if let Some(name) = attrs.link_name {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod sparc;
mod sparc64;
mod wasm32;
mod wasm32_bindgen_compat;
mod wasm64;
mod x86;
mod x86_64;
mod x86_win64;
Expand Down Expand Up @@ -652,6 +653,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
_ => wasm32_bindgen_compat::compute_abi_info(self),
},
"asmjs" => wasm32::compute_abi_info(cx, self),
"wasm64" => wasm64::compute_abi_info(cx, self),
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
}

Expand Down
58 changes: 58 additions & 0 deletions compiler/rustc_target/src/abi/call/wasm64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::abi::call::{ArgAbi, FnAbi, Uniform};
use crate::abi::{HasDataLayout, LayoutOf, TyAndLayout, TyAndLayoutMethods};

fn unwrap_trivial_aggregate<'a, Ty, C>(cx: &C, val: &mut ArgAbi<'a, Ty>) -> bool
where
Ty: TyAndLayoutMethods<'a, C> + Copy,
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
{
if val.layout.is_aggregate() {
if let Some(unit) = val.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()) {
let size = val.layout.size;
if unit.size == size {
val.cast_to(Uniform { unit, total: size });
return true;
}
}
}
false
}

fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
where
Ty: TyAndLayoutMethods<'a, C> + Copy,
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
{
ret.extend_integer_width_to(64);
if ret.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, ret) {
ret.make_indirect();
}
}

fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
where
Ty: TyAndLayoutMethods<'a, C> + Copy,
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
{
arg.extend_integer_width_to(64);
if arg.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, arg) {
arg.make_indirect_byval();
}
}

pub fn compute_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,
{
if !fn_abi.ret.is_ignore() {
classify_ret(cx, &mut fn_abi.ret);
}

for arg in &mut fn_abi.args {
if arg.is_ignore() {
continue;
}
classify_arg(cx, arg);
}
}
8 changes: 7 additions & 1 deletion compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod solaris_base;
mod thumb_base;
mod uefi_msvc_base;
mod vxworks_base;
mod wasm32_base;
mod wasm_base;
mod windows_gnu_base;
mod windows_msvc_base;
mod windows_uwp_gnu_base;
Expand Down Expand Up @@ -762,6 +762,7 @@ supported_targets! {
("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
("wasm32-unknown-unknown", wasm32_unknown_unknown),
("wasm32-wasi", wasm32_wasi),
("wasm64-unknown-unknown", wasm64_unknown_unknown),

("thumbv6m-none-eabi", thumbv6m_none_eabi),
("thumbv7m-none-eabi", thumbv7m_none_eabi),
Expand Down Expand Up @@ -996,6 +997,8 @@ pub struct TargetOptions {
pub is_like_emscripten: bool,
/// Whether the target toolchain is like Fuchsia's.
pub is_like_fuchsia: bool,
/// Whether a target toolchain is like WASM.
pub is_like_wasm: bool,
/// Version of DWARF to use if not using the default.
/// Useful because some platforms (osx, bsd) only want up to DWARF2.
pub dwarf_version: Option<u32>,
Expand Down Expand Up @@ -1204,6 +1207,7 @@ impl Default for TargetOptions {
is_like_emscripten: false,
is_like_msvc: false,
is_like_fuchsia: false,
is_like_wasm: false,
dwarf_version: None,
linker_is_gnu: false,
allows_weak_linkage: true,
Expand Down Expand Up @@ -1678,6 +1682,7 @@ impl Target {
key!(is_like_msvc, bool);
key!(is_like_emscripten, bool);
key!(is_like_fuchsia, bool);
key!(is_like_wasm, bool);
key!(dwarf_version, Option<u32>);
key!(linker_is_gnu, bool);
key!(allows_weak_linkage, bool);
Expand Down Expand Up @@ -1914,6 +1919,7 @@ impl ToJson for Target {
target_option_val!(is_like_msvc);
target_option_val!(is_like_emscripten);
target_option_val!(is_like_fuchsia);
target_option_val!(is_like_wasm);
target_option_val!(dwarf_version);
target_option_val!(linker_is_gnu);
target_option_val!(allows_weak_linkage);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/tests/tests_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Target {
// and you certainly want "unknown" for the OS name.
fn can_use_os_unknown(&self) -> bool {
self.llvm_target == "wasm32-unknown-unknown"
|| self.llvm_target == "wasm64-unknown-unknown"
|| (self.env == "sgx" && self.vendor == "fortanix")
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::wasm32_base;
use super::wasm_base;
use super::{LinkArgs, LinkerFlavor, PanicStrategy, Target, TargetOptions};

pub fn target() -> Target {
let mut options = wasm32_base::options();
let mut options = wasm_base::options();

let clang_args = options.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_target/src/spec/wasm32_unknown_unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//! This target is more or less managed by the Rust and WebAssembly Working
//! Group nowadays at <https://github.com/rustwasm>.

use super::wasm32_base;
use super::wasm_base;
use super::{LinkerFlavor, LldFlavor, Target};

pub fn target() -> Target {
let mut options = wasm32_base::options();
let mut options = wasm_base::options();
options.os = "unknown".to_string();
options.linker_flavor = LinkerFlavor::Lld(LldFlavor::Wasm);
let clang_args = options.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_target/src/spec/wasm32_wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
//! best we can with this target. Don't start relying on too much here unless
//! you know what you're getting in to!

use super::wasm32_base;
use super::wasm_base;
use super::{crt_objects, LinkerFlavor, LldFlavor, Target};

pub fn target() -> Target {
let mut options = wasm32_base::options();
let mut options = wasm_base::options();

options.os = "wasi".to_string();
options.linker_flavor = LinkerFlavor::Lld(LldFlavor::Wasm);
Expand Down
39 changes: 39 additions & 0 deletions compiler/rustc_target/src/spec/wasm64_unknown_unknown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! A "bare wasm" target representing a WebAssembly output that makes zero
//! assumptions about its environment.
//!
//! The `wasm64-unknown-unknown` target is intended to encapsulate use cases
//! that do not rely on any imported functionality. The binaries generated are
//! entirely self-contained by default when using the standard library. Although
//! the standard library is available, most of it returns an error immediately
//! (e.g. trying to create a TCP stream or something like that).

use super::wasm_base;
use super::{LinkerFlavor, LldFlavor, Target};

pub fn target() -> Target {
let mut options = wasm_base::options();
options.os = "unknown".to_string();
options.linker_flavor = LinkerFlavor::Lld(LldFlavor::Wasm);
let clang_args = options.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap();

// Make sure clang uses LLD as its linker and is configured appropriately
// otherwise
clang_args.push("--target=wasm64-unknown-unknown".to_string());

// For now this target just never has an entry symbol no matter the output
// type, so unconditionally pass this.
clang_args.push("-Wl,--no-entry".to_string());
options
.pre_link_args
.get_mut(&LinkerFlavor::Lld(LldFlavor::Wasm))
.unwrap()
.push("--no-entry".to_string());

Target {
llvm_target: "wasm64-unknown-unknown".to_string(),
pointer_width: 64,
data_layout: "e-m:e-p:64:64-i64:64-n32:64-S128".to_string(),
arch: "wasm64".to_string(),
options,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub fn options() -> TargetOptions {
pre_link_args.insert(LinkerFlavor::Gcc, clang_args);

TargetOptions {
is_like_wasm: true,

// we allow dynamic linking, but only cdylibs. Basically we allow a
// final library artifact that exports some symbols (a wasm module) but
// we don't allow intermediate `dylib` crate types
Expand Down
1 change: 1 addition & 0 deletions src/doc/rustc/src/platform-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ target | std | host | notes
`thumbv7a-uwp-windows-msvc` | ✓ | |
`thumbv7neon-unknown-linux-musleabihf` | ? | | Thumb2-mode ARMv7a Linux with NEON, MUSL
`thumbv4t-none-eabi` | * | | ARMv4T T32
`wasm64-unknown-unknown` | * | | WebAssembly
`x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst on x86_64
`x86_64-apple-tvos` | * | | x86 64-bit tvOS
`x86_64-unknown-none-linuxkernel` | * | | Linux kernel modules
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ impl<'a> fmt::Display for Display<'a> {
"windows" => "Windows",
_ => "",
},
(sym::wasm, None) => "WebAssembly",
(sym::target_arch, Some(arch)) => match &*arch.as_str() {
"aarch64" => "AArch64",
"arm" => "ARM",
Expand All @@ -498,7 +499,7 @@ impl<'a> fmt::Display for Display<'a> {
"powerpc64" => "PowerPC-64",
"s390x" => "s390x",
"sparc64" => "SPARC64",
"wasm32" => "WebAssembly",
"wasm32" | "wasm64" => "WebAssembly",
"x86" => "x86",
"x86_64" => "x86-64",
_ => "",
Expand Down