Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions compiler/rustc_target/src/callconv/bpf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
// see https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/BPF/BPFCallingConv.td
use rustc_abi::TyAbiInterface;
use rustc_abi::{Reg, RegKind, Size, TyAbiInterface};

use crate::callconv::{ArgAbi, FnAbi};
use crate::callconv::{ArgAbi, CastTarget, FnAbi, Uniform};

fn classify_aggregate_type<Ty>(arg: &mut ArgAbi<'_, Ty>) {
let size = arg.layout.size;

match size.bits() {
0 => return,
1..=64 => {
arg.cast_to(Reg { kind: RegKind::Integer, size });
}
65..=128 => {
arg.cast_to(CastTarget::from(Uniform::new(Reg::i64(), Size::from_bytes(16))));
}
_ => {
arg.make_indirect();
}
}
}

fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
if !ret.layout.is_sized() {
// Not touching this...
return;
}

if ret.layout.is_aggregate() || ret.layout.size.bits() > 64 {
ret.make_indirect();
classify_aggregate_type(ret);
} else {
ret.extend_integer_width_to(32);
}
Expand All @@ -15,12 +37,16 @@ fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
where
Ty: TyAbiInterface<'a, C> + Copy,
{
if !arg.layout.is_sized() {
// Not touching this...
return;
}
if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
arg.make_indirect();
return;
}
if arg.layout.is_aggregate() || arg.layout.size.bits() > 64 {
arg.make_indirect();
classify_aggregate_type(arg);
} else {
arg.extend_integer_width_to(32);
}
Expand Down
28 changes: 0 additions & 28 deletions tests/codegen-llvm/bpf-abi-indirect-return.rs

This file was deleted.

33 changes: 33 additions & 0 deletions tests/codegen-llvm/bpf-abi/indirect-return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Checks that results larger than one register are returned indirectly
//@ add-minicore
//@ revisions: bpfel bpfeb
//@[bpfel] compile-flags: --target=bpfel-unknown-none
//@[bpfeb] compile-flags: --target=bpfeb-unknown-none
//@ needs-llvm-components: bpf
//@ compile-flags: -Copt-level=3
#![crate_type = "lib"]
#![feature(no_core)]
#![no_core]

extern crate minicore;

struct Big {
a: [u16; 32],
b: u64,
}

// CHECK-LABEL: define{{.*}} @inner_big_rust(
// CHECK-SAME: ptr{{[^,]*}},
// CHECK-SAME: i64{{[^)]*}}
#[unsafe(no_mangle)]
fn inner_big_rust(a: u64) -> Big {
Big { a: [a as u16; 32], b: 42 }
}

// CHECK-LABEL: define{{.*}} @inner_big_c(
// CHECK-SAME: ptr{{[^,]*}},
// CHECK-SAME: i64{{[^)]*}}
#[unsafe(no_mangle)]
extern "C" fn inner_big_c(a: u64) -> Big {
Big { a: [a as u16; 32], b: 42 }
}
107 changes: 107 additions & 0 deletions tests/codegen-llvm/bpf-abi/struct-return-regs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//@ add-minicore
//@ revisions: bpfel bpfeb
//@[bpfel] compile-flags: --target=bpfel-unknown-none
//@[bpfeb] compile-flags: --target=bpfeb-unknown-none
//@ needs-llvm-components: bpf
//@ compile-flags: -Copt-level=3 -Cno-prepopulate-passes
//@ min-llvm-version: 23
#![feature(no_core)]
#![no_core]
#![crate_type = "lib"]

extern crate minicore;
use minicore::*;

#[repr(C)]
struct Foo0 {
a: i8,
}

#[repr(C)]
struct Foo1 {
a: i32,
}

#[repr(C)]
struct Foo2 {
a: i32,
b: i64,
}

#[repr(C)]
struct Foo3 {
a: i32,
b: i32,
c: i64,
}

impl Copy for Foo0 {}
impl Copy for Foo1 {}
impl Copy for Foo2 {}
impl Copy for Foo3 {}

// CHECK-LABEL: define{{.*}} i8 @bar0(
// CHECK: ret i8
#[no_mangle]
extern "C" fn bar0(a: i8) -> Foo0 {
Foo0 { a }
}
Comment on lines +43 to +48

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for BE this is a bit surprising to me, in many targets this would instead pass an i32 or i64 on BE so that the 8 actual bits are in the right spot. But, this is consistent with clang

https://godbolt.org/z/PvMj78bWs


// CHECK-LABEL: define{{.*}} i32 @bar1(
// CHECK: ret i32
#[no_mangle]
extern "C" fn bar1(a: i32) -> Foo1 {
Foo1 { a }
}

// CHECK-LABEL: define{{.*}} [2 x i64] @bar2(
// CHECK: ret [2 x i64]
#[no_mangle]
extern "C" fn bar2(a: i32, b: i32) -> Foo2 {
Foo2 { a, b: b as i64 }
}

// CHECK-LABEL: define{{.*}} [2 x i64] @bar3(
// CHECK: ret [2 x i64]
#[no_mangle]
extern "C" fn bar3(a: i32, b: i32, c: i32) -> Foo3 {
Foo3 { a, b, c: c as i64 }
}

// CHECK-LABEL: define{{.*}} i8 @check0(
// CHECK: %[[C1:.*]] = call i8 @bar0(
// CHECK: store i8 %[[C1]]
#[no_mangle]
extern "C" fn check0(a: i8) -> i8 {
let v = bar0(a);
v.a
}

// CHECK-LABEL: define{{.*}} i32 @check1(
// CHECK: %[[C1:.*]] = call i32 @bar1(
// CHECK: store i32 %[[C1]]
#[no_mangle]
extern "C" fn check1(a: i32) -> i32 {
let v = bar1(a);
v.a
}

// CHECK-LABEL: define{{.*}} i32 @check2(
// CHECK: %[[C2:.*]] = call [2 x i64] @bar2(
// CHECK: store [2 x i64] %[[C2]]
#[no_mangle]
extern "C" fn check2(a: i32, b: i32) -> i32 {
let v = bar2(a, b);
hint::black_box(v);
v.a
}

// CHECK-LABEL: define{{.*}} i32 @check3(
// CHECK: %[[C3:.*]] = call [2 x i64] @bar3(
// CHECK: store [2 x i64] %[[C3]]
#[no_mangle]
extern "C" fn check3(a: i32, b: i32, c: i32) -> i32 {
let v = bar3(a, b, c);
hint::black_box(v);
v.a
}
95 changes: 95 additions & 0 deletions tests/codegen-llvm/bpf-abi/struct-return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//@ add-minicore
//@ revisions: bpfel bpfeb
//@[bpfel] compile-flags: --target=bpfel-unknown-none
//@[bpfeb] compile-flags: --target=bpfeb-unknown-none
//@ needs-llvm-components: bpf
//@ compile-flags: -Copt-level=3 -Cno-prepopulate-passes
//@ min-llvm-version: 23
#![feature(no_core)]
#![no_core]
#![crate_type = "lib"]

extern crate minicore;
use minicore::*;

#[repr(C)]
struct T1 {}

#[repr(C)]
struct T2 {
a: i32,
}
impl Copy for T2 {}

#[repr(C)]
struct T3 {
a: i32,
b: i64,
}

#[repr(C)]
struct T4 {
a: i64,
b: i64,
c: i64,
}

#[repr(C)]
struct T5 {
a: i8,
}

#[repr(C)]
union U1 {
a: i32,
b: i64,
}

// CHECK: define{{.*}} void @foo1()
#[no_mangle]
extern "C" fn foo1() -> T1 {
T1 {}
}

// CHECK: define{{.*}} i32 @foo2()
#[no_mangle]
extern "C" fn foo2() -> T2 {
T2 { a: 0 }
}

// CHECK: define{{.*}} [2 x i64] @foo3()
#[no_mangle]
extern "C" fn foo3() -> T3 {
T3 { a: 0, b: 0 }
}

// CHECK: define{{.*}} void @foo4(ptr{{.*}}sret([24 x i8]){{.*}}align 8
#[no_mangle]
extern "C" fn foo4() -> T4 {
T4 { a: 0, b: 0, c: 0 }
}

// CHECK: define{{.*}} i8 @foo5()
#[no_mangle]
extern "C" fn foo5() -> T5 {
T5 { a: 0 }
}

// CHECK: define{{.*}} i64 @foou()
#[no_mangle]
extern "C" fn foou() -> U1 {
U1 { b: 0 }
}

// CHECK-LABEL: define{{.*}} i32 @bar()
// CHECK: %[[C2:.*]] = call i32 @foo2()
// CHECK: store i32 %[[C2]]
// CHECK: %[[C3:.*]] = call [2 x i64] @foo3()
// CHECK: store [2 x i64] %[[C3]]
#[no_mangle]
extern "C" fn bar() -> i32 {
let a = foo2();
let b = foo3();
hint::black_box((a, b));
a.a
}
Loading