Skip to content

Commit f1d5fde

Browse files
committed
Fix Hexagon ABI calling convention for small aggregates
Small structs (<= 64 bits) were being passed with their fields split into separate arguments instead of being packed into register-sized chunks. This caused ABI mismatches. The fix properly casts small aggregates to consecutive register-sized chunks using Uniform::consecutive(), matching the Hexagon C ABI where small structs are packed into R1:0 register pair. This fixes tests like extern-pass-TwoU16s.rs and extern-pass-TwoU8s.rs.
1 parent d222ddc commit f1d5fde

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

compiler/rustc_target/src/callconv/hexagon.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
1-
use rustc_abi::TyAbiInterface;
1+
use rustc_abi::{HasDataLayout, TyAbiInterface};
22

3-
use crate::callconv::{ArgAbi, FnAbi};
3+
use crate::callconv::{ArgAbi, FnAbi, Reg, Uniform};
44

5-
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
6-
if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 {
7-
ret.make_indirect();
8-
} else {
5+
fn classify_ret<'a, Ty, C>(_cx: &C, ret: &mut ArgAbi<'a, Ty>)
6+
where
7+
Ty: TyAbiInterface<'a, C> + Copy,
8+
C: HasDataLayout,
9+
{
10+
if !ret.layout.is_sized() {
11+
return;
12+
}
13+
if !ret.layout.is_aggregate() {
914
ret.extend_integer_width_to(32);
15+
return;
16+
}
17+
18+
let size = ret.layout.size;
19+
let bits = size.bits();
20+
21+
// Aggregates larger than 64 bits are returned indirectly
22+
if bits > 64 {
23+
ret.make_indirect();
24+
return;
1025
}
26+
27+
// Small aggregates are returned in registers
28+
// Cast to appropriate register type to ensure proper ABI
29+
let align = ret.layout.align.bytes();
30+
ret.cast_to(Uniform::consecutive(if align <= 4 { Reg::i32() } else { Reg::i64() }, size));
1131
}
1232

1333
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
1434
where
1535
Ty: TyAbiInterface<'a, C> + Copy,
36+
C: HasDataLayout,
1637
{
38+
if !arg.layout.is_sized() {
39+
return;
40+
}
1741
if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
1842
arg.make_indirect();
1943
return;
2044
}
21-
if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 {
22-
arg.make_indirect();
23-
} else {
45+
if !arg.layout.is_aggregate() {
2446
arg.extend_integer_width_to(32);
47+
return;
2548
}
49+
50+
let size = arg.layout.size;
51+
let bits = size.bits();
52+
53+
// Aggregates larger than 64 bits are passed indirectly
54+
if bits > 64 {
55+
arg.make_indirect();
56+
return;
57+
}
58+
59+
// Small aggregates are passed in registers
60+
// Cast to consecutive register-sized chunks to match the C ABI
61+
let align = arg.layout.align.bytes();
62+
arg.cast_to(Uniform::consecutive(if align <= 4 { Reg::i32() } else { Reg::i64() }, size));
2663
}
2764

2865
pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
2966
where
3067
Ty: TyAbiInterface<'a, C> + Copy,
68+
C: HasDataLayout,
3169
{
3270
if !fn_abi.ret.is_ignore() {
33-
classify_ret(&mut fn_abi.ret);
71+
classify_ret(cx, &mut fn_abi.ret);
3472
}
3573

3674
for arg in fn_abi.args.iter_mut() {

0 commit comments

Comments
 (0)