Skip to content

Commit 903c553

Browse files
committed
Wasm-bindgen abi compat using cast_to
1 parent ecbc661 commit 903c553

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,7 @@ where
28602860
return;
28612861
}
28622862

2863-
if let Err(msg) = self.adjust_for_cabi(cx, abi) {
2863+
if let Err(msg) = self.adjust_for_cabi(cx, &cx.tcx().sess.target_features, abi) {
28642864
cx.tcx().sess.fatal(&msg);
28652865
}
28662866
}

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub enum PassMode {
5353
// Hack to disable non_upper_case_globals only for the bitflags! and not for the rest
5454
// of this module
5555
pub use attr_impl::ArgAttribute;
56+
use rustc_data_structures::stable_set::FxHashSet;
57+
use rustc_span::Symbol;
5658

5759
#[allow(non_upper_case_globals)]
5860
#[allow(unused)]
@@ -592,7 +594,12 @@ pub struct FnAbi<'a, Ty> {
592594
}
593595

594596
impl<'a, Ty> FnAbi<'a, Ty> {
595-
pub fn adjust_for_cabi<C>(&mut self, cx: &C, abi: spec::abi::Abi) -> Result<(), String>
597+
pub fn adjust_for_cabi<C>(
598+
&mut self,
599+
cx: &C,
600+
target_features: &FxHashSet<Symbol>,
601+
abi: spec::abi::Abi,
602+
) -> Result<(), String>
596603
where
597604
Ty: TyAndLayoutMethods<'a, C> + Copy,
598605
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout + HasTargetSpec,
@@ -633,7 +640,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
633640
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
634641
"wasm32" => match cx.target_spec().os.as_str() {
635642
"emscripten" | "wasi" => wasm32::compute_abi_info(cx, self),
636-
_ => wasm32_bindgen_compat::compute_abi_info(self),
643+
_ => wasm32_bindgen_compat::compute_abi_info(cx, target_features, self),
637644
},
638645
"asmjs" => wasm32::compute_abi_info(cx, self),
639646
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),

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

+39-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,58 @@
55
// can be fixed to work with the correct ABI. See #63649 for further
66
// discussion.
77

8-
use crate::abi::call::{ArgAbi, FnAbi};
8+
use rustc_data_structures::stable_set::FxHashSet;
9+
use rustc_span::Symbol;
910

10-
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
11+
use crate::abi::call::{ArgAbi, FnAbi, Uniform};
12+
use crate::abi::{HasDataLayout, LayoutOf, TyAndLayout, TyAndLayoutMethods};
13+
14+
fn classify_ret<'a, Ty, C>(cx: &C, target_features: &FxHashSet<Symbol>, ret: &mut ArgAbi<'a, Ty>)
15+
where
16+
Ty: TyAndLayoutMethods<'a, C> + Copy,
17+
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
18+
{
19+
if ret.layout.is_aggregate() {
20+
if let Some(unit) = ret.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()) {
21+
let size = ret.layout.size;
22+
if unit.size == size || target_features.contains(&Symbol::intern("multivalue")) {
23+
ret.cast_to(Uniform { unit, total: size });
24+
}
25+
}
26+
}
1127
ret.extend_integer_width_to(32);
1228
}
1329

14-
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
30+
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
31+
where
32+
Ty: TyAndLayoutMethods<'a, C> + Copy,
33+
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
34+
{
35+
if arg.layout.is_aggregate() {
36+
if let Some(unit) = arg.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()) {
37+
let size = arg.layout.size;
38+
arg.cast_to(Uniform { unit, total: size });
39+
}
40+
}
1541
arg.extend_integer_width_to(32);
1642
}
1743

18-
pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
44+
pub fn compute_abi_info<'a, Ty, C>(
45+
cx: &C,
46+
target_features: &FxHashSet<Symbol>,
47+
fn_abi: &mut FnAbi<'a, Ty>,
48+
) where
49+
Ty: TyAndLayoutMethods<'a, C> + Copy,
50+
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
51+
{
1952
if !fn_abi.ret.is_ignore() {
20-
classify_ret(&mut fn_abi.ret);
53+
classify_ret(cx, target_features, &mut fn_abi.ret);
2154
}
2255

2356
for arg in &mut fn_abi.args {
2457
if arg.is_ignore() {
2558
continue;
2659
}
27-
classify_arg(arg);
60+
classify_arg(cx, arg);
2861
}
2962
}

0 commit comments

Comments
 (0)