Skip to content

Commit 7ec3132

Browse files
Rollup merge of #165305 - androm3da:hexagon-abi-stack-aggregates, r=folkertdev
hexagon: pass small aggregates on the stack when registers run out Aggregates of up to 64 bits were always cast to an i32/i64, even when no argument registers were left. LLVM then placed the i64 in an 8-byte aligned stack slot, but the ABI (section 5.1) says stack arguments follow the normal alignment rules of the type, so a `{ i32, i32 }` must be 4-aligned. Track the remaining arg regs. When an aggregate no longer fits in registers, pass it by value on the stack with its natural alignment. Aggregates passed or returned in registers now use the smallest type.
2 parents 5d181d6 + 181719b commit 7ec3132

3 files changed

Lines changed: 197 additions & 20 deletions

File tree

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use rustc_abi::{HasDataLayout, TyAbiInterface};
1+
use rustc_abi::{HasDataLayout, Size, TyAbiInterface};
22

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

55
fn classify_ret<'a, Ty, C>(_cx: &C, ret: &mut ArgAbi<'a, Ty>)
66
where
@@ -21,17 +21,44 @@ where
2121
// - Aggregates 33-64 bits are returned in R1:R0
2222
// - Aggregates > 64 bits are returned indirectly via hidden first argument
2323
let size = ret.layout.size;
24-
let bits = size.bits();
25-
if bits <= 32 {
26-
ret.cast_to(Uniform::new(Reg::i32(), size));
27-
} else if bits <= 64 {
28-
ret.cast_to(Uniform::new(Reg::i64(), size));
24+
if size.bytes() <= 8 {
25+
ret.cast_to(smallest_int_reg(size));
2926
} else {
3027
ret.make_indirect();
3128
}
3229
}
3330

34-
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
31+
/// The number of 32-bit registers (R0-R5) available for passing arguments.
32+
const ARG_REGS: u32 = 6;
33+
34+
/// Account for an argument of the given `size` in the remaining argument registers.
35+
///
36+
/// Returns `true` if the argument is passed entirely in registers. Returns `false` if it does
37+
/// not fit in the registers that are left; it is then passed on the stack, and no registers
38+
/// remain for any subsequent arguments (even ones that would have fit in the skipped registers).
39+
fn adjust_regs_left(size: Size, regs_left: &mut u32) -> bool {
40+
if *regs_left == 0 {
41+
return false;
42+
}
43+
if size.bytes() <= 4 {
44+
*regs_left -= 1;
45+
return true;
46+
}
47+
// Arguments larger than 32 bits are passed in an aligned register pair (R1:R0, R3:R2 or
48+
// R5:R4), so skip a register if the next free one is odd-numbered.
49+
if *regs_left >= 2 && !regs_left.is_multiple_of(2) {
50+
*regs_left -= 1;
51+
}
52+
if *regs_left >= 2 {
53+
*regs_left -= 2;
54+
true
55+
} else {
56+
*regs_left = 0;
57+
false
58+
}
59+
}
60+
61+
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, regs_left: &mut u32)
3562
where
3663
Ty: TyAbiInterface<'a, C> + Copy,
3764
C: HasDataLayout,
@@ -41,29 +68,50 @@ where
4168
}
4269
if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
4370
arg.make_indirect();
71+
// The pointer to the indirectly passed value takes up a register.
72+
adjust_regs_left(Size::from_bytes(4), regs_left);
4473
return;
4574
}
4675

76+
let size = arg.layout.size;
77+
4778
if !arg.layout.is_aggregate() {
79+
if size.bytes() <= 8 {
80+
adjust_regs_left(size, regs_left);
81+
}
4882
arg.extend_integer_width_to(32);
4983
return;
5084
}
5185

52-
// Per the Hexagon ABI:
53-
// - Aggregates up to 32 bits are passed in a single register
54-
// - Aggregates 33-64 bits are passed in a register pair
55-
// - Aggregates > 64 bits are passed on the stack
56-
let size = arg.layout.size;
57-
let bits = size.bits();
58-
if bits <= 32 {
59-
arg.cast_to(Uniform::new(Reg::i32(), size));
60-
} else if bits <= 64 {
61-
arg.cast_to(Uniform::new(Reg::i64(), size));
86+
if size.bytes() > 8 {
87+
arg.pass_by_stack_offset(None);
88+
return;
89+
}
90+
91+
let in_regs = adjust_regs_left(size, regs_left);
92+
// In registers, aggregates have the alignment of the register(s) they are cast to. On the
93+
// stack, they just have their natural alignment.
94+
let align_bytes =
95+
if in_regs { if size.bytes() <= 4 { 4 } else { 8 } } else { arg.layout.align.bytes() };
96+
97+
if size.bytes() <= align_bytes {
98+
arg.cast_to(smallest_int_reg(size));
6299
} else {
63100
arg.pass_by_stack_offset(None);
64101
}
65102
}
66103

104+
/// The smallest integer register that can hold `size` (which is at most 8 bytes).
105+
fn smallest_int_reg(size: Size) -> Reg {
106+
match size.bytes() {
107+
..=1 => Reg::i8(),
108+
..=2 => Reg::i16(),
109+
..=4 => Reg::i32(),
110+
..=8 => Reg::i64(),
111+
_ => unreachable!("aggregate of {} bytes does not fit in a register", size.bytes()),
112+
}
113+
}
114+
67115
pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
68116
where
69117
Ty: TyAbiInterface<'a, C> + Copy,
@@ -73,10 +121,13 @@ where
73121
classify_ret(cx, &mut fn_abi.ret);
74122
}
75123

124+
// Note that, matching clang, the hidden pointer for an indirect return value does not use up
125+
// any of the argument registers.
126+
let mut regs_left = ARG_REGS;
76127
for arg in fn_abi.args.iter_mut() {
77128
if arg.is_ignore() {
78129
continue;
79130
}
80-
classify_arg(cx, arg);
131+
classify_arg(cx, arg, &mut regs_left);
81132
}
82133
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//@ add-minicore
2+
//@ compile-flags: -Copt-level=3 --target hexagon-unknown-linux-musl
3+
//@ needs-llvm-components: hexagon
4+
#![feature(no_core, lang_items)]
5+
#![no_core]
6+
#![crate_type = "lib"]
7+
8+
extern crate minicore;
9+
use minicore::*;
10+
11+
#[repr(C)]
12+
pub struct Pair {
13+
x: i32,
14+
y: i32,
15+
}
16+
17+
#[repr(C)]
18+
pub struct Bytes {
19+
a: i8,
20+
b: i8,
21+
}
22+
23+
// CHECK-LABEL: @pair_in_regs(
24+
// CHECK-SAME: i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i64 %0)
25+
#[no_mangle]
26+
pub extern "C" fn pair_in_regs(a: i32, b: i32, c: i32, d: i32, p: Pair) {}
27+
28+
// CHECK-LABEL: @pair_no_pair_reg(
29+
// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p)
30+
#[no_mangle]
31+
pub extern "C" fn pair_no_pair_reg(a: i32, b: i32, c: i32, d: i32, e: i32, p: Pair) {}
32+
33+
// CHECK-LABEL: @pair_on_stack(
34+
// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p)
35+
#[no_mangle]
36+
pub extern "C" fn pair_on_stack(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32, p: Pair) {}
37+
38+
#[repr(C)]
39+
pub struct Zst;
40+
41+
// A 64-bit argument is passed in an aligned register pair, so R3 is skipped here and no
42+
// registers are left for `s`, even though one register would be free without the alignment.
43+
// CHECK-LABEL: @i64_aligns_regs(
44+
// CHECK-SAME: i32 noundef %c, i64 noundef %d, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s)
45+
#[no_mangle]
46+
pub extern "C" fn i64_aligns_regs(a: i32, b: i32, c: i32, d: i64, s: Bytes) {}
47+
48+
// Here no register is skipped: `c` is in R3:R2 and `s` still fits in R5.
49+
// CHECK-LABEL: @i64_no_skip(
50+
// CHECK-SAME: i32 noundef %b, i64 noundef %c, i32 noundef %d, i16 %0)
51+
#[no_mangle]
52+
pub extern "C" fn i64_no_skip(a: i32, b: i32, c: i64, d: i32, s: Bytes) {}
53+
54+
// A zero-sized argument does not use up any registers, so `p` is in R3:R2 and `s` in R4.
55+
// CHECK-LABEL: @zst_does_not_use_regs(
56+
// CHECK-SAME: i32 noundef %b, i64 %0, i16 %1)
57+
#[no_mangle]
58+
pub extern "C" fn zst_does_not_use_regs(a: i32, b: i32, z: Zst, p: Pair, s: Bytes) {}
59+
60+
// `p` does not completely fit in the one remaining register, so it goes on the stack. That
61+
// exhausts the registers, so `s` is on the stack too, even though it would fit in R5.
62+
// CHECK-LABEL: @pair_then_bytes_on_stack(
63+
// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s)
64+
#[no_mangle]
65+
pub extern "C" fn pair_then_bytes_on_stack(
66+
a: i32,
67+
b: i32,
68+
c: i32,
69+
d: i32,
70+
e: i32,
71+
p: Pair,
72+
s: Bytes,
73+
) {
74+
}
75+
76+
// CHECK-LABEL: @bytes_in_reg(
77+
// CHECK-SAME: i32 noundef %e, i16 %0, i32 noundef %x)
78+
#[no_mangle]
79+
pub extern "C" fn bytes_in_reg(a: i32, b: i32, c: i32, d: i32, e: i32, s: Bytes, x: i32) {}
80+
81+
// CHECK-LABEL: @bytes_on_stack(
82+
// CHECK-SAME: ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s, i32 noundef %x)
83+
#[no_mangle]
84+
pub extern "C" fn bytes_on_stack(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, s: Bytes, x: i32) {
85+
}
86+
87+
// CHECK-LABEL: define{{.*}} i16 @ret_bytes(
88+
#[no_mangle]
89+
pub extern "C" fn ret_bytes() -> Bytes {
90+
Bytes { a: 1, b: 2 }
91+
}
92+
93+
// CHECK-LABEL: @va_list_then_bytes(
94+
// CHECK-SAME: i32 noundef %e, ptr {{.*}}%va, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%t)
95+
#[no_mangle]
96+
pub extern "C" fn va_list_then_bytes(
97+
a: i32,
98+
b: i32,
99+
c: i32,
100+
d: i32,
101+
e: i32,
102+
va: ffi::VaList<'_>,
103+
s: Bytes,
104+
t: Bytes,
105+
) {
106+
}

0 commit comments

Comments
 (0)