Skip to content

Rollup of 10 pull requests #135825

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cf34545
CI: build FreeBSD artifacts on FreeBSD 13.4
asomers Oct 27, 2024
0454d79
Fixup: fix clang command lines in another file
asomers Jan 12, 2025
b2b12ae
Add an example of using `carrying_mul_add` to write wider multiplication
scottmcm Jan 20, 2025
8dec09f
support wasm inline assembly in naked functions
folkertdev Jan 15, 2025
bcf478b
work around the `wasm32-unknown-unknown` ABI being broken
folkertdev Jan 17, 2025
a175e8d
CI: free disk on linux arm runner
marcoieni Jan 20, 2025
9d6a1c9
Shorten linker output even more when `--verbose` is not present
jyn514 Jan 18, 2025
9d88b82
Ignore `mermaid.min.js`
jyn514 Jan 20, 2025
51af4d6
Add Kobzol on vacation
Kobzol Jan 21, 2025
00381ea
Make it possible to build GCC on CI
Kobzol Jan 2, 2025
cd9dcfc
ci: use ghcr buildkit image
marcoieni Jan 21, 2025
587b9c6
[cfg_match] Document the use of expressions
c410-f3r Jan 21, 2025
047e679
Rollup merge of #132232 - asomers:fbsd-13.4, r=Mark-Simulacrum
jieyouxu Jan 21, 2025
b4e2fb5
Rollup merge of #135625 - c410-f3r:cfg-match-foo-bar-baz, r=tgross35,…
jieyouxu Jan 21, 2025
3753fbf
Rollup merge of #135638 - Kobzol:gcc-ci, r=onur-ozkan
jieyouxu Jan 21, 2025
8395924
Rollup merge of #135648 - folkertdev:naked-asm-wasm, r=bjorn3
jieyouxu Jan 21, 2025
89d5c81
Rollup merge of #135707 - jyn514:linker-messages-2, r=bjorn3
jieyouxu Jan 21, 2025
dbc4adb
Rollup merge of #135750 - scottmcm:cma-example, r=cuviper
jieyouxu Jan 21, 2025
8d3906b
Rollup merge of #135779 - marcoieni:free-disk-arm-runner, r=Kobzol
jieyouxu Jan 21, 2025
e1b5523
Rollup merge of #135793 - jyn514:gitignore, r=jieyouxu
jieyouxu Jan 21, 2025
3370a20
Rollup merge of #135810 - Kobzol:kobzol-parental-leave, r=Kobzol
jieyouxu Jan 21, 2025
fde77e9
Rollup merge of #135814 - marcoieni:use-buildkit-ghcr, r=Kobzol
jieyouxu Jan 21, 2025
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
Prev Previous commit
Next Next commit
work around the wasm32-unknown-unknown ABI being broken
  • Loading branch information
folkertdev committed Jan 20, 2025
commit bcf478b7a6915a8ce14009934f2893ddcce8052c
47 changes: 39 additions & 8 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind};
use rustc_attr_parsing::InstructionSetAttr;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
use rustc_middle::mir::{Body, InlineAsmOperand};
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, LayoutOf};
use rustc_middle::ty::{Instance, Ty, TyCtxt};
use rustc_middle::{bug, ty};
use rustc_middle::{bug, span_bug, ty};
use rustc_span::sym;
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
use rustc_target::spec::WasmCAbi;

use crate::common;
use crate::traits::{AsmCodegenMethods, BuilderMethods, GlobalAsmOperandRef, MiscCodegenMethods};
Expand Down Expand Up @@ -285,7 +287,12 @@ fn prefix_and_suffix<'tcx>(
writeln!(begin, "{}", arch_prefix).unwrap();
}
writeln!(begin, "{asm_name}:").unwrap();
writeln!(begin, ".functype {asm_name} {}", wasm_functype(tcx, fn_abi)).unwrap();
writeln!(
begin,
".functype {asm_name} {}",
wasm_functype(tcx, fn_abi, instance.def_id())
)
.unwrap();

writeln!(end).unwrap();
// .size is ignored for function symbols, so we can skip it
Expand All @@ -299,7 +306,7 @@ fn prefix_and_suffix<'tcx>(
/// The webassembly type signature for the given function.
///
/// Used by the `.functype` directive on wasm targets.
fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> String {
fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, def_id: DefId) -> String {
let mut signature = String::with_capacity(64);

let ptr_type = match tcx.data_layout.pointer_size.bits() {
Expand All @@ -308,8 +315,18 @@ fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Str
other => bug!("wasm pointer size cannot be {other} bits"),
};

let hidden_return =
matches!(fn_abi.ret.mode, PassMode::Indirect { .. } | PassMode::Pair { .. });
// FIXME: remove this once the wasm32-unknown-unknown ABI is fixed
// please also add `wasm32-unknown-unknown` back in `tests/assembly/wasm32-naked-fn.rs`
// basically the commit introducing this comment should be reverted
if let PassMode::Pair { .. } = fn_abi.ret.mode {
let _ = WasmCAbi::Legacy;
span_bug!(
tcx.def_span(def_id),
"cannot return a pair (the wasm32-unknown-unknown ABI is broken, see https://github.com/rust-lang/rust/issues/115666"
);
}

let hidden_return = matches!(fn_abi.ret.mode, PassMode::Indirect { .. });

signature.push('(');

Expand All @@ -322,7 +339,7 @@ fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Str

let mut it = fn_abi.args.iter().peekable();
while let Some(arg_abi) = it.next() {
wasm_type(&mut signature, arg_abi, ptr_type);
wasm_type(tcx, &mut signature, arg_abi, ptr_type, def_id);
if it.peek().is_some() {
signature.push_str(", ");
}
Expand All @@ -331,21 +348,35 @@ fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Str
signature.push_str(") -> (");

if !hidden_return {
wasm_type(&mut signature, &fn_abi.ret, ptr_type);
wasm_type(tcx, &mut signature, &fn_abi.ret, ptr_type, def_id);
}

signature.push(')');

signature
}

fn wasm_type<'tcx>(signature: &mut String, arg_abi: &ArgAbi<'_, Ty<'tcx>>, ptr_type: &'static str) {
fn wasm_type<'tcx>(
tcx: TyCtxt<'tcx>,
signature: &mut String,
arg_abi: &ArgAbi<'_, Ty<'tcx>>,
ptr_type: &'static str,
def_id: DefId,
) {
match arg_abi.mode {
PassMode::Ignore => { /* do nothing */ }
PassMode::Direct(_) => {
let direct_type = match arg_abi.layout.backend_repr {
BackendRepr::Scalar(scalar) => wasm_primitive(scalar.primitive(), ptr_type),
BackendRepr::Vector { .. } => "v128",
BackendRepr::Memory { .. } => {
// FIXME: remove this branch once the wasm32-unknown-unknown ABI is fixed
let _ = WasmCAbi::Legacy;
span_bug!(
tcx.def_span(def_id),
"cannot use memory args (the wasm32-unknown-unknown ABI is broken, see https://github.com/rust-lang/rust/issues/115666"
);
}
other => unreachable!("unexpected BackendRepr: {:?}", other),
};

Expand Down
17 changes: 9 additions & 8 deletions tests/assembly/wasm32-naked-fn.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//@ revisions: wasm32-unknown wasm64-unknown wasm32-wasip1
// FIXME: add wasm32-unknown when the wasm32-unknown-unknown ABI is fixed
// see https://github.com/rust-lang/rust/issues/115666
//@ revisions: wasm64-unknown wasm32-wasip1
//@ add-core-stubs
//@ assembly-output: emit-asm
//@ [wasm32-unknown] compile-flags: --target wasm32-unknown-unknown
//@ [wasm64-unknown] compile-flags: --target wasm64-unknown-unknown
//@ [wasm32-wasip1] compile-flags: --target wasm32-wasip1
//@ [wasm32-unknown] needs-llvm-components: webassembly
//@ [wasm64-unknown] needs-llvm-components: webassembly
//@ [wasm32-wasip1] needs-llvm-components: webassembly

Expand Down Expand Up @@ -97,7 +97,7 @@ unsafe extern "C" fn fn_i64_i64(num: i64) -> i64 {
}

// CHECK-LABEL: fn_i128_i128:
// wasm32-unknown,wasm32-wasip1: .functype fn_i128_i128 (i32, i64, i64) -> ()
// wasm32-wasip1: .functype fn_i128_i128 (i32, i64, i64) -> ()
// wasm64-unknown: .functype fn_i128_i128 (i64, i64, i64) -> ()
#[allow(improper_ctypes_definitions)]
#[no_mangle]
Expand All @@ -114,7 +114,7 @@ unsafe extern "C" fn fn_i128_i128(num: i128) -> i128 {
}

// CHECK-LABEL: fn_f128_f128:
// wasm32-unknown,wasm32-wasip1: .functype fn_f128_f128 (i32, i64, i64) -> ()
// wasm32-wasip1: .functype fn_f128_f128 (i32, i64, i64) -> ()
// wasm64-unknown: .functype fn_f128_f128 (i64, i64, i64) -> ()
#[no_mangle]
#[naked]
Expand All @@ -137,18 +137,19 @@ struct Compound {

// CHECK-LABEL: fn_compound_compound:
// wasm32-wasip1: .functype fn_compound_compound (i32, i32) -> ()
// wasm32-unknown: .functype fn_compound_compound (i32, i32, i64) -> ()
// wasm64-unknown: .functype fn_compound_compound (i64, i64) -> ()
#[no_mangle]
#[naked]
unsafe extern "C" fn fn_compound_compound(_: Compound) -> Compound {
// this is the wasm32-unknown-unknown assembly
// this is the wasm32-wasip1 assembly
naked_asm!(
"local.get 0",
"local.get 2",
"local.get 1",
"i64.load 8",
"i64.store 8",
"local.get 0",
"local.get 1",
"i32.load16_u 0",
"i32.store16 0",
)
}
Expand Down
Loading