Skip to content

Commit

Permalink
work around the wasm32-unknown-unknown ABI being broken
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jan 17, 2025
1 parent c637bda commit 1cb81d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 14 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,14 @@ 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
if let PassMode::Pair { .. } = fn_abi.ret.mode {
bug!(
"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 Down Expand Up @@ -346,6 +352,12 @@ fn wasm_type<'tcx>(signature: &mut String, arg_abi: &ArgAbi<'_, Ty<'tcx>>, ptr_t
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
bug!(
"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 @@ -98,7 +98,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 @@ -115,7 +115,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 @@ -138,18 +138,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

0 comments on commit 1cb81d2

Please sign in to comment.