Skip to content

Commit 28d1529

Browse files
committed
the wasm ABI behavior is a bug
1 parent 243ef31 commit 28d1529

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,12 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
351351
// guarnateeing that we generate ABI-compatible LLVM IR. Things get tricky for
352352
// aggregates...
353353
if matches!(arg.layout.abi, abi::Abi::Aggregate { .. }) {
354-
// This is the most critical case for ABI compatibility, since
355-
// `immediate_llvm_type` will use `layout.fields` to turn this Rust type
356-
// into an LLVM type. ABI-compatible Rust types can have different `fields`,
357-
// so we need to be very sure that LLVM wil treat those different types in
358-
// an ABI-compatible way. Mostly we do this by disallowing
359-
// `PassMode::Direct` for aggregates, but we actually do use that mode on
360-
// wasm. wasm doesn't have aggregate types so we are fairly sure that LLVM
361-
// will treat `{ i32, i32, i32 }` and `{ { i32, i32, i32 } }` the same way
362-
// for ABI purposes.
354+
// This really shouldn't happen, since `immediate_llvm_type` will use
355+
// `layout.fields` to turn this Rust type into an LLVM type. This means all
356+
// sorts of Rust type details leak into the ABI. However wasm sadly *does*
357+
// currently use this mode so we have to allow it -- but we absolutely
358+
// shouldn't let any more targets do that.
359+
// (Also see <https://github.com/rust-lang/rust/issues/115666>.)
363360
assert!(
364361
matches!(&*cx.tcx.sess.target.arch, "wasm32" | "wasm64"),
365362
"`PassMode::Direct` for aggregates only allowed on wasm targets\nProblematic type: {:#?}",

compiler/rustc_target/src/abi/call/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ pub enum PassMode {
3636
Ignore,
3737
/// Pass the argument directly.
3838
///
39-
/// The argument has a layout abi of `Scalar`, `Vector` or in rare cases (e.g. on wasm) `Aggregate`.
39+
/// The argument has a layout abi of `Scalar` or `Vector`.
40+
/// Unfortunately due to past mistakes, in rare cases on wasm, it can also be `Aggregate`.
41+
/// This is bad since it leaks LLVM implementation details into the ABI.
42+
/// (Also see <https://github.com/rust-lang/rust/issues/115666>.)
4043
Direct(ArgAttributes),
4144
/// Pass a pair's elements directly in two arguments.
4245
///
@@ -527,7 +530,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
527530
scalar_attrs(&layout, b, a.size(cx).align_to(b.align(cx).abi)),
528531
),
529532
Abi::Vector { .. } => PassMode::Direct(ArgAttributes::new()),
530-
// The `Aggregate` ABI is almost always adjusted later.
533+
// The `Aggregate` ABI should always be adjusted later.
531534
Abi::Aggregate { .. } => PassMode::Direct(ArgAttributes::new()),
532535
};
533536
ArgAbi { layout, mode }

compiler/rustc_target/src/abi/call/wasm.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ where
6161
/// The purpose of this ABI is for matching the WebAssembly standard. This
6262
/// intentionally diverges from the C ABI and is specifically crafted to take
6363
/// advantage of LLVM's support of multiple returns in WebAssembly.
64+
///
65+
/// This ABI is *bad*! It uses `PassMode::Direct` for `abi::Aggregate` types, which leaks LLVM
66+
/// implementation details into the ABI. It's just hard to fix because ABIs are hard to change.
67+
/// Also see <https://github.com/rust-lang/rust/issues/115666>.
6468
pub fn compute_wasm_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
6569
if !fn_abi.ret.is_ignore() {
6670
classify_ret(&mut fn_abi.ret);

0 commit comments

Comments
 (0)