rust-lang/rust · #165305
hexagon: pass small aggregates on the stack when registers run out
compiler/rustc_target/src/callconv/hexagon.rs70 + / 19 −
@@ -1,6 +1,6 @@-use rustc_abi::{HasDataLayout, TyAbiInterface};+use rustc_abi::{HasDataLayout, Size, TyAbiInterface}; -use crate::callconv::{ArgAbi, FnAbi, Reg, Uniform};+use crate::callconv::{ArgAbi, FnAbi, Reg}; fn classify_ret<'a, Ty, C>(_cx: &C, ret: &mut ArgAbi<'a, Ty>) where@@ -21,17 +21,44 @@ where // - Aggregates 33-64 bits are returned in R1:R0 // - Aggregates > 64 bits are returned indirectly via hidden first argument let size = ret.layout.size;- let bits = size.bits();- if bits <= 32 {- ret.cast_to(Uniform::new(Reg::i32(), size));- } else if bits <= 64 {- ret.cast_to(Uniform::new(Reg::i64(), size));+ if size.bytes() <= 8 {+ ret.cast_to(smallest_int_reg(size)); } else { ret.make_indirect(); } } -fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)+/// The number of 32-bit registers (R0-R5) available for passing arguments.+const ARG_REGS: u32 = 6;++/// Account for an argument of the given `size` in the remaining argument registers.+///+/// Returns `true` if the argument is passed entirely in registers. Returns `false` if it does+/// not fit in the registers that are left; it is then passed on the stack, and no registers+/// remain for any subsequent arguments (even ones that would have fit in the skipped registers).+fn adjust_regs_left(size: Size, regs_left: &mut u32) -> bool {+ if *regs_left == 0 {+ return false;+ }+ if size.bytes() <= 4 {+ *regs_left -= 1;+ return true;+ }+ // Arguments larger than 32 bits are passed in an aligned register pair (R1:R0, R3:R2 or+ // R5:R4), so skip a register if the next free one is odd-numbered.+ if *regs_left >= 2 && !regs_left.is_multiple_of(2) {+ *regs_left -= 1;+ }+ if *regs_left >= 2 {+ *regs_left -= 2;+ true+ } else {+ *regs_left = 0;+ false+ }+}++fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, regs_left: &mut u32) where Ty: TyAbiInterface<'a, C> + Copy, C: HasDataLayout,@@ -41,29 +68,50 @@ where } if arg.layout.pass_indirectly_in_non_rustic_abis(cx) { arg.make_indirect();+ // The pointer to the indirectly passed value takes up a register.+ adjust_regs_left(Size::from_bytes(4), regs_left); return; } + let size = arg.layout.size;+ if !arg.layout.is_aggregate() {+ if size.bytes() <= 8 {+ adjust_regs_left(size, regs_left);+ } arg.extend_integer_width_to(32); return; } - // Per the Hexagon ABI:- // - Aggregates up to 32 bits are passed in a single register- // - Aggregates 33-64 bits are passed in a register pair- // - Aggregates > 64 bits are passed on the stack- let size = arg.layout.size;- let bits = size.bits();- if bits <= 32 {- arg.cast_to(Uniform::new(Reg::i32(), size));- } else if bits <= 64 {- arg.cast_to(Uniform::new(Reg::i64(), size));+ if size.bytes() > 8 {+ arg.pass_by_stack_offset(None);+ return;+ }++ let in_regs = adjust_regs_left(size, regs_left);+ // In registers, aggregates have the alignment of the register(s) they are cast to. On the+ // stack, they just have their natural alignment.+ let align_bytes =+ if in_regs { if size.bytes() <= 4 { 4 } else { 8 } } else { arg.layout.align.bytes() };++ if size.bytes() <= align_bytes {+ arg.cast_to(smallest_int_reg(size)); } else { arg.pass_by_stack_offset(None); } } +/// The smallest integer register that can hold `size` (which is at most 8 bytes).+fn smallest_int_reg(size: Size) -> Reg {+ match size.bytes() {+ ..=1 => Reg::i8(),+ ..=2 => Reg::i16(),+ ..=4 => Reg::i32(),+ ..=8 => Reg::i64(),+ _ => unreachable!("aggregate of {} bytes does not fit in a register", size.bytes()),+ }+}+ pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>) where Ty: TyAbiInterface<'a, C> + Copy,@@ -73,10 +121,13 @@ where classify_ret(cx, &mut fn_abi.ret); } + // Note that, matching clang, the hidden pointer for an indirect return value does not use up+ // any of the argument registers.+ let mut regs_left = ARG_REGS; for arg in fn_abi.args.iter_mut() { if arg.is_ignore() { continue; }- classify_arg(cx, arg);+ classify_arg(cx, arg, &mut regs_left); } }tests/codegen-llvm/hexagon-abi/aggregate-abi.rsadded106 + / 0 −
@@ -0,0 +1,106 @@+//@ add-minicore+//@ compile-flags: -Copt-level=3 --target hexagon-unknown-linux-musl+//@ needs-llvm-components: hexagon+#![feature(no_core, lang_items)]+#![no_core]+#![crate_type = "lib"]++extern crate minicore;+use minicore::*;++#[repr(C)]+pub struct Pair {+ x: i32,+ y: i32,+}++#[repr(C)]+pub struct Bytes {+ a: i8,+ b: i8,+}++// CHECK-LABEL: @pair_in_regs(+// CHECK-SAME: i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i64 %0)+#[no_mangle]+pub extern "C" fn pair_in_regs(a: i32, b: i32, c: i32, d: i32, p: Pair) {}++// CHECK-LABEL: @pair_no_pair_reg(+// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p)+#[no_mangle]+pub extern "C" fn pair_no_pair_reg(a: i32, b: i32, c: i32, d: i32, e: i32, p: Pair) {}++// CHECK-LABEL: @pair_on_stack(+// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p)+#[no_mangle]+pub extern "C" fn pair_on_stack(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32, p: Pair) {}++#[repr(C)]+pub struct Zst;++// A 64-bit argument is passed in an aligned register pair, so R3 is skipped here and no+// registers are left for `s`, even though one register would be free without the alignment.+// CHECK-LABEL: @i64_aligns_regs(+// CHECK-SAME: i32 noundef %c, i64 noundef %d, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s)+#[no_mangle]+pub extern "C" fn i64_aligns_regs(a: i32, b: i32, c: i32, d: i64, s: Bytes) {}++// Here no register is skipped: `c` is in R3:R2 and `s` still fits in R5.+// CHECK-LABEL: @i64_no_skip(+// CHECK-SAME: i32 noundef %b, i64 noundef %c, i32 noundef %d, i16 %0)+#[no_mangle]+pub extern "C" fn i64_no_skip(a: i32, b: i32, c: i64, d: i32, s: Bytes) {}++// A zero-sized argument does not use up any registers, so `p` is in R3:R2 and `s` in R4.+// CHECK-LABEL: @zst_does_not_use_regs(+// CHECK-SAME: i32 noundef %b, i64 %0, i16 %1)+#[no_mangle]+pub extern "C" fn zst_does_not_use_regs(a: i32, b: i32, z: Zst, p: Pair, s: Bytes) {}++// `p` does not completely fit in the one remaining register, so it goes on the stack. That+// exhausts the registers, so `s` is on the stack too, even though it would fit in R5.+// CHECK-LABEL: @pair_then_bytes_on_stack(+// CHECK-SAME: ptr {{.*}}byval([8 x i8]) align 4 {{.*}}%p, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s)+#[no_mangle]+pub extern "C" fn pair_then_bytes_on_stack(+ a: i32,+ b: i32,+ c: i32,+ d: i32,+ e: i32,+ p: Pair,+ s: Bytes,+) {+}++// CHECK-LABEL: @bytes_in_reg(+// CHECK-SAME: i32 noundef %e, i16 %0, i32 noundef %x)+#[no_mangle]+pub extern "C" fn bytes_in_reg(a: i32, b: i32, c: i32, d: i32, e: i32, s: Bytes, x: i32) {}++// CHECK-LABEL: @bytes_on_stack(+// CHECK-SAME: ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s, i32 noundef %x)+#[no_mangle]+pub extern "C" fn bytes_on_stack(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, s: Bytes, x: i32) {+}++// CHECK-LABEL: define{{.*}} i16 @ret_bytes(+#[no_mangle]+pub extern "C" fn ret_bytes() -> Bytes {+ Bytes { a: 1, b: 2 }+}++// CHECK-LABEL: @va_list_then_bytes(+// CHECK-SAME: i32 noundef %e, ptr {{.*}}%va, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%s, ptr {{.*}}byval([2 x i8]) align 1 {{.*}}%t)+#[no_mangle]+pub extern "C" fn va_list_then_bytes(+ a: i32,+ b: i32,+ c: i32,+ d: i32,+ e: i32,+ va: ffi::VaList<'_>,+ s: Bytes,+ t: Bytes,+) {+}tests/codegen-llvm/some-abis-do-extend-params-to-32-bits.rs21 + / 1 −
@@ -1,7 +1,7 @@ //@ add-minicore //@ compile-flags: -Cno-prepopulate-passes -Copt-level=0 -//@ revisions:x86_64-linux x86_64-uefi i686 aarch64-apple aarch64-windows aarch64-linux arm riscv+//@ revisions:x86_64-linux x86_64-uefi i686 aarch64-apple aarch64-windows aarch64-linux arm riscv hexagon //@[x86_64-uefi] compile-flags: --target x86_64-unknown-uefi //@[x86_64-uefi] needs-llvm-components: x86@@ -19,6 +19,8 @@ //@[arm] needs-llvm-components: arm //@[riscv] compile-flags: --target riscv64gc-unknown-none-elf //@[riscv] needs-llvm-components: riscv+//@[hexagon] compile-flags: --target hexagon-unknown-linux-musl+//@[hexagon] needs-llvm-components: hexagon // See bottom of file for a corresponding C source file that is meant to yield // equivalent declarations.@@ -49,6 +51,7 @@ use minicore::*; // aarch64-linux: void @c_arg_bool(i1 %_a) // arm: void @c_arg_bool(i1 zeroext %_a) // riscv: void @c_arg_bool(i1 zeroext %_a)+// hexagon: void @c_arg_bool(i1 zeroext %_a) #[no_mangle] pub extern "C" fn c_arg_bool(_a: bool) {} @@ -60,6 +63,7 @@ pub extern "C" fn c_arg_bool(_a: bool) {} // aarch64-linux: void @c_arg_u8(i8 %_a) // arm: void @c_arg_u8(i8 zeroext %_a) // riscv: void @c_arg_u8(i8 zeroext %_a)+// hexagon: void @c_arg_u8(i8 zeroext %_a) #[no_mangle] pub extern "C" fn c_arg_u8(_a: u8) {} @@ -71,6 +75,7 @@ pub extern "C" fn c_arg_u8(_a: u8) {} // aarch64-linux: void @c_arg_u16(i16 %_a) // arm: void @c_arg_u16(i16 zeroext %_a) // riscv: void @c_arg_u16(i16 zeroext %_a)+// hexagon: void @c_arg_u16(i16 zeroext %_a) #[no_mangle] pub extern "C" fn c_arg_u16(_a: u16) {} @@ -82,6 +87,7 @@ pub extern "C" fn c_arg_u16(_a: u16) {} // aarch64-linux: void @c_arg_u32(i32 %_a) // arm: void @c_arg_u32(i32 %_a) // riscv: void @c_arg_u32(i32 signext %_a)+// hexagon: void @c_arg_u32(i32 %_a) #[no_mangle] pub extern "C" fn c_arg_u32(_a: u32) {} @@ -93,6 +99,7 @@ pub extern "C" fn c_arg_u32(_a: u32) {} // aarch64-linux: void @c_arg_u64(i64 %_a) // arm: void @c_arg_u64(i64 %_a) // riscv: void @c_arg_u64(i64 %_a)+// hexagon: void @c_arg_u64(i64 %_a) #[no_mangle] pub extern "C" fn c_arg_u64(_a: u64) {} @@ -104,6 +111,7 @@ pub extern "C" fn c_arg_u64(_a: u64) {} // aarch64-linux: void @c_arg_i8(i8 %_a) // arm: void @c_arg_i8(i8 signext %_a) // riscv: void @c_arg_i8(i8 signext %_a)+// hexagon: void @c_arg_i8(i8 signext %_a) #[no_mangle] pub extern "C" fn c_arg_i8(_a: i8) {} @@ -115,6 +123,7 @@ pub extern "C" fn c_arg_i8(_a: i8) {} // aarch64-linux: void @c_arg_i16(i16 %_a) // arm: void @c_arg_i16(i16 signext %_a) // riscv: void @c_arg_i16(i16 signext %_a)+// hexagon: void @c_arg_i16(i16 signext %_a) #[no_mangle] pub extern "C" fn c_arg_i16(_a: i16) {} @@ -126,6 +135,7 @@ pub extern "C" fn c_arg_i16(_a: i16) {} // aarch64-linux: void @c_arg_i32(i32 %_a) // arm: void @c_arg_i32(i32 %_a) // riscv: void @c_arg_i32(i32 signext %_a)+// hexagon: void @c_arg_i32(i32 %_a) #[no_mangle] pub extern "C" fn c_arg_i32(_a: i32) {} @@ -137,6 +147,7 @@ pub extern "C" fn c_arg_i32(_a: i32) {} // aarch64-linux: void @c_arg_i64(i64 %_a) // arm: void @c_arg_i64(i64 %_a) // riscv: void @c_arg_i64(i64 %_a)+// hexagon: void @c_arg_i64(i64 %_a) #[no_mangle] pub extern "C" fn c_arg_i64(_a: i64) {} @@ -148,6 +159,7 @@ pub extern "C" fn c_arg_i64(_a: i64) {} // aarch64-linux: define {{(dso_local )?}}i1 @c_ret_bool() // arm: zeroext i1 @c_ret_bool() // riscv: zeroext i1 @c_ret_bool()+// hexagon: zeroext i1 @c_ret_bool() #[no_mangle] pub extern "C" fn c_ret_bool() -> bool { false@@ -161,6 +173,7 @@ pub extern "C" fn c_ret_bool() -> bool { // aarch64-linux: define {{(dso_local )?}}i8 @c_ret_u8() // arm: zeroext i8 @c_ret_u8() // riscv: zeroext i8 @c_ret_u8()+// hexagon: zeroext i8 @c_ret_u8() #[no_mangle] pub extern "C" fn c_ret_u8() -> u8 { 0@@ -174,6 +187,7 @@ pub extern "C" fn c_ret_u8() -> u8 { // aarch64-linux: define {{(dso_local )?}}i16 @c_ret_u16() // arm: zeroext i16 @c_ret_u16() // riscv: zeroext i16 @c_ret_u16()+// hexagon: zeroext i16 @c_ret_u16() #[no_mangle] pub extern "C" fn c_ret_u16() -> u16 { 0@@ -187,6 +201,7 @@ pub extern "C" fn c_ret_u16() -> u16 { // aarch64-linux: define {{(dso_local )?}}i32 @c_ret_u32() // arm: define {{(dso_local )?}}i32 @c_ret_u32() // riscv: signext i32 @c_ret_u32()+// hexagon: define {{(dso_local )?}}i32 @c_ret_u32() #[no_mangle] pub extern "C" fn c_ret_u32() -> u32 { 0@@ -200,6 +215,7 @@ pub extern "C" fn c_ret_u32() -> u32 { // aarch64-linux: define {{(dso_local )?}}i64 @c_ret_u64() // arm: define {{(dso_local )?}}i64 @c_ret_u64() // riscv: define {{(dso_local )?}}i64 @c_ret_u64()+// hexagon: define {{(dso_local )?}}i64 @c_ret_u64() #[no_mangle] pub extern "C" fn c_ret_u64() -> u64 { 0@@ -213,6 +229,7 @@ pub extern "C" fn c_ret_u64() -> u64 { // aarch64-linux: define {{(dso_local )?}}i8 @c_ret_i8() // arm: signext i8 @c_ret_i8() // riscv: signext i8 @c_ret_i8()+// hexagon: signext i8 @c_ret_i8() #[no_mangle] pub extern "C" fn c_ret_i8() -> i8 { 0@@ -226,6 +243,7 @@ pub extern "C" fn c_ret_i8() -> i8 { // aarch64-linux: define {{(dso_local )?}}i16 @c_ret_i16() // arm: signext i16 @c_ret_i16() // riscv: signext i16 @c_ret_i16()+// hexagon: signext i16 @c_ret_i16() #[no_mangle] pub extern "C" fn c_ret_i16() -> i16 { 0@@ -239,6 +257,7 @@ pub extern "C" fn c_ret_i16() -> i16 { // aarch64-linux: define {{(dso_local )?}}i32 @c_ret_i32() // arm: define {{(dso_local )?}}i32 @c_ret_i32() // riscv: signext i32 @c_ret_i32()+// hexagon: define {{(dso_local )?}}i32 @c_ret_i32() #[no_mangle] pub extern "C" fn c_ret_i32() -> i32 { 0@@ -252,6 +271,7 @@ pub extern "C" fn c_ret_i32() -> i32 { // aarch64-linux: define {{(dso_local )?}}i64 @c_ret_i64() // arm: define {{(dso_local )?}}i64 @c_ret_i64() // riscv: define {{(dso_local )?}}i64 @c_ret_i64()+// hexagon: define {{(dso_local )?}}i64 @c_ret_i64() #[no_mangle] pub extern "C" fn c_ret_i64() -> i64 { 0