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 16, 2023
1 parent 525c91d commit b5d3f7b
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 15 deletions.
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_gcc/src/builder.rs
Expand Up @@ -48,7 +48,7 @@ use rustc_target::abi::{
TargetDataLayout,
WrappingRange,
};
use rustc_target::spec::{HasTargetSpec, Target};
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, WasmCAbi};

use crate::common::{SignType, TypeReflection, type_is_pointer};
use crate::context::CodegenCx;
Expand Down Expand Up @@ -1952,6 +1952,12 @@ impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
}
}

impl<'tcx> HasWasmCAbiOpt for Builder<'_, '_, 'tcx> {
fn wasm_c_abi_opt(&self) -> WasmCAbi {
self.cx.wasm_c_abi_opt()
}
}

pub trait ToGccComp {
fn to_gcc_comparison(&self) -> ComparisonOp;
}
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_gcc/src/context.rs
Expand Up @@ -17,7 +17,7 @@ use rustc_middle::ty::layout::{FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest
use rustc_session::Session;
use rustc_span::{Span, source_map::respan};
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, TlsModel, WasmCAbi};

use crate::callee::get_fn;
use crate::common::SignType;
Expand Down Expand Up @@ -500,6 +500,12 @@ impl<'gcc, 'tcx> HasTargetSpec for CodegenCx<'gcc, 'tcx> {
}
}

impl<'gcc, 'tcx> HasWasmCAbiOpt for CodegenCx<'gcc, 'tcx> {
fn wasm_c_abi_opt(&self) -> WasmCAbi {
self.tcx.sess.opts.unstable_opts.wasm_c_abi
}
}

impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
type LayoutOfResult = TyAndLayout<'tcx>;

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Expand Up @@ -17,7 +17,9 @@ use rustc_session::{build_session, getopts, CompilerIO, EarlyErrorHandler, Sessi
use rustc_span::edition::{Edition, DEFAULT_EDITION};
use rustc_span::symbol::sym;
use rustc_span::{FileName, SourceFileHashAlgorithm};
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
use rustc_target::spec::{
CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel, WasmCAbi,
};
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
use std::collections::{BTreeMap, BTreeSet};
use std::num::NonZeroUsize;
Expand Down Expand Up @@ -832,6 +834,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(verify_llvm_ir, true);
tracked!(virtual_function_elimination, true);
tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));
tracked!(wasm_c_abi, WasmCAbi::Spec);
// tidy-alphabetical-end

macro_rules! tracked_no_crate_hash {
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Expand Up @@ -15,7 +15,9 @@ 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::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};
use rustc_target::spec::{
abi::Abi as SpecAbi, HasTargetSpec, HasWasmCAbiOpt, PanicStrategy, Target, WasmCAbi,
};

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

impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {
fn wasm_c_abi_opt(&self) -> WasmCAbi {
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 +605,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) -> WasmCAbi {
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
3 changes: 2 additions & 1 deletion compiler/rustc_session/src/config.rs
Expand Up @@ -3174,7 +3174,7 @@ pub(crate) mod dep_tracking {
use rustc_feature::UnstableFeatures;
use rustc_span::edition::Edition;
use rustc_span::RealFileName;
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel};
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, RelocModel, WasmCAbi};
use rustc_target::spec::{
RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
};
Expand Down Expand Up @@ -3270,6 +3270,7 @@ pub(crate) mod dep_tracking {
TraitSolver,
Polonius,
InliningThreshold,
WasmCAbi,
);

impl<T1, T2> DepTrackingHash for (T1, T2)
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_session/src/options.rs
Expand Up @@ -7,7 +7,9 @@ use rustc_data_structures::profiling::TimePassesFormat;
use rustc_data_structures::stable_hasher::Hash64;
use rustc_errors::ColorConfig;
use rustc_errors::{LanguageIdentifier, TerminalUrl};
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
use rustc_target::spec::{
CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet, WasmCAbi,
};
use rustc_target::spec::{
RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
};
Expand Down Expand Up @@ -430,6 +432,7 @@ mod desc {
pub const parse_inlining_threshold: &str =
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
pub const parse_llvm_module_flag: &str = "<key>:<type>:<value>:<behavior>. Type must currently be `u32`. Behavior should be one of (`error`, `warning`, `require`, `override`, `append`, `appendunique`, `max`, `min`)";
pub const parse_wasm_c_abi: &str = "`legacy` or `spec`";
}

mod parse {
Expand Down Expand Up @@ -1359,6 +1362,14 @@ mod parse {
slot.push((key.to_string(), value, behavior));
true
}

pub(crate) fn parse_wasm_c_abi(slot: &mut WasmCAbi, v: Option<&str>) -> bool {
match v {
Some("spec") => *slot = WasmCAbi::Spec,
Some("legacy") => *slot = WasmCAbi::Legacy,
_ => return false,
}
}
}

options! {
Expand Down Expand Up @@ -1963,6 +1974,8 @@ written to standard error output)"),
Requires `-Clto[=[fat,yes]]`"),
wasi_exec_model: Option<WasiExecModel> = (None, parse_wasi_exec_model, [TRACKED],
"whether to build a wasi command or reactor"),
wasm_c_abi: WasmCAbi = (WasmCAbi::Legacy, parse_wasm_c_abi, [TRACKED],
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
"whether long type names should be written to files instead of being printed in errors"),
// tidy-alphabetical-end
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_target/src/abi/call/mod.rs
@@ -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
29 changes: 27 additions & 2 deletions compiler/rustc_target/src/spec/mod.rs
Expand Up @@ -1830,6 +1830,19 @@ impl HasTargetSpec for Target {
}
}

/// Which C ABI to use for `wasm32-unknown-unknown`.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum WasmCAbi {
/// Spec-compliant C ABI.
Spec,
/// Legacy ABI. Which is non-spec-compliant.
Legacy,
}

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

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

/// Optional aspects of a target specification.
Expand Down Expand Up @@ -2441,9 +2454,21 @@ 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() == WasmCAbi::Spec
{
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
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
@@ -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.
2 changes: 1 addition & 1 deletion src/llvm-project
Submodule llvm-project updated 55 files
+2 −5 clang/include/clang/Sema/Sema.h
+3 −4 clang/lib/CodeGen/CGExprConstant.cpp
+3 −6 clang/lib/Driver/ToolChains/Solaris.cpp
+0 −6 clang/lib/Format/TokenAnnotator.cpp
+0 −11 clang/lib/Interpreter/CMakeLists.txt
+0 −6 clang/test/CodeGenCXX/const-init-cxx11.cpp
+5 −16 clang/test/Driver/solaris-ld.c
+0 −18 clang/test/Sema/PR69717.cpp
+0 −10 clang/unittests/Format/TokenAnnotatorTest.cpp
+1 −1 libcxx/include/__config
+97 −85 libcxx/include/__expected/expected.h
+0 −8 libcxx/test/std/utilities/expected/expected.expected/assign/emplace.intializer_list.pass.cpp
+0 −7 libcxx/test/std/utilities/expected/expected.expected/assign/emplace.pass.cpp
+2 −9 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.convert.copy.pass.cpp
+2 −9 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.convert.move.pass.cpp
+2 −16 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
+2 −3 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.default.pass.cpp
+8 −10 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.inplace.pass.cpp
+2 −7 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.inplace_init_list.pass.cpp
+2 −18 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.move.pass.cpp
+8 −13 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.u.pass.cpp
+8 −10 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.unexpect.pass.cpp
+2 −7 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.unexpect_init_list.pass.cpp
+4 −4 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.unexpected.copy.pass.cpp
+4 −4 libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.unexpected.move.pass.cpp
+0 −32 libcxx/test/std/utilities/expected/expected.expected/observers/has_value.pass.cpp
+1 −63 libcxx/test/std/utilities/expected/expected.expected/swap/free.swap.pass.cpp
+1 −63 libcxx/test/std/utilities/expected/expected.expected/swap/member.swap.pass.cpp
+2 −9 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.convert.copy.pass.cpp
+2 −9 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.convert.move.pass.cpp
+2 −9 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.copy.pass.cpp
+2 −10 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.move.pass.cpp
+2 −4 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.unexpect.pass.cpp
+2 −7 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.unexpect_init_list.pass.cpp
+2 −2 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.unexpected.copy.pass.cpp
+2 −2 libcxx/test/std/utilities/expected/expected.void/ctor/ctor.unexpected.move.pass.cpp
+0 −12 libcxx/test/std/utilities/expected/expected.void/observers/has_value.pass.cpp
+2 −30 libcxx/test/std/utilities/expected/expected.void/swap/free.swap.pass.cpp
+1 −29 libcxx/test/std/utilities/expected/expected.void/swap/member.swap.pass.cpp
+0 −49 libcxx/test/std/utilities/expected/types.h
+1 −1 llvm/CMakeLists.txt
+6 −0 llvm/lib/CodeGen/BranchFolding.cpp
+0 −1 llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h
+0 −10 llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+6 −8 llvm/lib/Target/Mips/MipsISelLowering.cpp
+8 −22 llvm/lib/Transforms/IPO/GlobalOpt.cpp
+1 −2 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+12 −12 llvm/test/CodeGen/Mips/llvm-ir/ashr.ll
+13 −13 llvm/test/CodeGen/Mips/llvm-ir/lshr.ll
+13 −13 llvm/test/CodeGen/Mips/llvm-ir/shl.ll
+0 −25 llvm/test/CodeGen/PowerPC/pr70208.ll
+0 −44 llvm/test/Transforms/SLPVectorizer/X86/reduction-gather-non-scheduled-extracts.ll
+20 −20 llvm/test/Transforms/SLPVectorizer/X86/reduction-transpose.ll
+1 −1 llvm/utils/gn/secondary/llvm/version.gni
+1 −1 llvm/utils/lit/lit/__init__.py
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 141 files

0 comments on commit b5d3f7b

Please sign in to comment.