Skip to content

Commit be988e9

Browse files
committed
Properly implement the gpu-kernel ABI for amdgpu
Add support to pass structs, arrays and vectors to amdgpu kernels. Scalars and vectors are taken by value, aggregates are passed by byref pointers. Structs containing a single scalar/vector are handled like a scalar. Judging from clang tests, nvptx seems to do somewhat the same, just using byval instead of byref: https://github.com/llvm/llvm-project/blob/3a8affeef4da19d39191aac316e189eca3214a8c/clang/test/CodeGenCUDA/kernel-args.cu I tested a couple of the lit test signatures on real hardware and it seems to work fine. Given the relatively simple implementation, I hope this amount of testing is enough (the C calling convention seems like a worse fit for Rust’s current ABI code, it’s still giving me headaches).
1 parent b8c747f commit be988e9

3 files changed

Lines changed: 139 additions & 35 deletions

File tree

‎compiler/rustc_abi/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,10 @@ pub struct AddressSpace(pub u32);
17781778
impl AddressSpace {
17791779
/// LLVM's `0` address space.
17801780
pub const ZERO: Self = AddressSpace(0);
1781+
/// The address space for constant memory on nvptx and amdgpu.
1782+
/// This address space is used e.g. for kernel arguments that are constant throughout the
1783+
/// execution.
1784+
pub const GPU_CONSTANT: Self = AddressSpace(4);
17811785
/// The address space for workgroup memory on nvptx and amdgpu.
17821786
/// See e.g. the `gpu_launch_sized_workgroup_mem` intrinsic for details.
17831787
pub const GPU_WORKGROUP: Self = AddressSpace(3);
Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,86 @@
1-
use rustc_abi::{HasDataLayout, TyAbiInterface};
1+
use rustc_abi::{
2+
AddressSpace, BackendRepr, CanonAbi, HasDataLayout, Reg, RegKind, TyAbiInterface, TyAndLayout,
3+
};
24

3-
use crate::callconv::{ArgAbi, FnAbi};
5+
use crate::callconv::{FnAbi, Uniform};
46

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-
ret.extend_integer_width_to(32);
11-
}
7+
// For reference, see llvm-project/clang/lib/CodeGen/Targets/AMDGPU.cpp
128

13-
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
9+
/// If the given type is a (potentially nested) struct containing a single scalar, return
10+
/// a `Uniform` for the contained, single element.
11+
fn single_element_struct_to_reg<'a, Ty, C>(cx: &C, ty: TyAndLayout<'a, Ty>) -> Option<Uniform>
1412
where
1513
Ty: TyAbiInterface<'a, C> + Copy,
1614
C: HasDataLayout,
1715
{
18-
if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
19-
arg.make_indirect();
20-
return;
16+
assert!(ty.is_aggregate(), "Only handles aggregate types");
17+
if ty.layout.fields.count() != 1 {
18+
return None;
19+
}
20+
let field = ty.field(cx, 0);
21+
match field.backend_repr {
22+
BackendRepr::SimdScalableVector { .. } => panic!("scalable vectors are unsupported"),
23+
BackendRepr::Scalar(_) => {
24+
// Check that the size is the same as the size for ty, so no extra padding
25+
let size = field.layout.size.bytes();
26+
if ty.layout.size.bytes() != size {
27+
return None;
28+
}
29+
30+
// clang passes the inner type directly, we emulate it with fitting integer types
31+
match size {
32+
1 => Some(Uniform::new(Reg::i8(), field.layout.size)),
33+
2 => Some(Uniform::new(Reg::i16(), field.layout.size)),
34+
4 => Some(Uniform::new(Reg::i32(), field.layout.size)),
35+
8 => Some(Uniform::new(Reg::i64(), field.layout.size)),
36+
16 => Some(Uniform::new(Reg::i128(), field.layout.size)),
37+
s => panic!("Unhandled scalar of size {s} in amdgpu gpu-kernel ABI"),
38+
}
39+
}
40+
BackendRepr::SimdVector { element, .. } => {
41+
// Check that the size is the same as the size for ty, so no extra padding
42+
let size = field.layout.size.bytes();
43+
if ty.layout.size.bytes() != size {
44+
return None;
45+
}
46+
47+
// clang passes the inner type directly, we emulate it with a vector of the same type.
48+
// The size is rounded up to the size of the complete type (including alignment).
49+
let reg = Reg {
50+
kind: RegKind::Vector { hint_vector_elem: element.primitive() },
51+
size: field.layout.size,
52+
};
53+
Some(Uniform::new(reg, field.layout.size))
54+
}
55+
BackendRepr::Memory { .. } => single_element_struct_to_reg(cx, field),
56+
BackendRepr::ScalarPair { .. } => None,
2157
}
22-
arg.extend_integer_width_to(32);
2358
}
2459

2560
pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
2661
where
2762
Ty: TyAbiInterface<'a, C> + Copy,
2863
C: HasDataLayout,
2964
{
30-
if !fn_abi.ret.is_ignore() {
31-
classify_ret(cx, &mut fn_abi.ret);
32-
}
65+
// Kernels cannot return values, so do not handle return types
3366

67+
// Try to fill first registers with values and pass by_ref pointers for later indirect arguments
3468
for arg in fn_abi.args.iter_mut() {
3569
if arg.is_ignore() {
3670
continue;
3771
}
38-
classify_arg(cx, arg);
72+
if fn_abi.conv == CanonAbi::GpuKernel {
73+
if arg.layout.is_aggregate() {
74+
if let Some(uniform) = single_element_struct_to_reg(cx, arg.layout) {
75+
// Single element structs are passed directly as the inner type
76+
arg.cast_to(uniform);
77+
} else {
78+
// All other aggregates are passed as by_ref pointer in the constant address space
79+
arg.pass_amdgpu_kernel_arg(Some(AddressSpace::GPU_CONSTANT));
80+
}
81+
}
82+
} else {
83+
// FIXME: C ABI is not yet implemented
84+
}
3985
}
4086
}

‎tests/codegen-llvm/amdgpu-abi/struct-abi.rs‎

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ add-minicore
22
//@ compile-flags: --crate-type=rlib --target=amdgcn-amd-amdhsa -Ctarget-cpu=gfx900 -Copt-level=3
33
//@ needs-llvm-components: amdgpu
4-
#![feature(no_core, abi_gpu_kernel)]
4+
#![feature(no_core, abi_gpu_kernel, repr_simd)]
55
#![no_core]
66
#![allow(improper_gpu_kernel_arg)]
77

@@ -10,14 +10,32 @@ use minicore::num::Complex;
1010

1111
// Tests from llvm-project/clang/test/CodeGenOpenCL/amdgpu-abi-struct-coerce.cl
1212

13+
#[repr(simd)]
14+
pub struct I8X2([i8; 2]);
15+
16+
#[repr(simd)]
17+
pub struct I16X2([i16; 2]);
18+
19+
#[repr(simd)]
20+
pub struct I16X3([i16; 3]);
21+
22+
#[repr(simd)]
23+
pub struct I16X4([i16; 4]);
24+
25+
#[repr(simd)]
26+
pub struct I32X3([i32; 3]);
27+
28+
#[repr(simd)]
29+
pub struct I32X4([i32; 4]);
30+
1331
#[repr(C)]
14-
pub struct SingleElementStructArg {
15-
i: i32,
32+
pub struct SingleElementStructArg<T> {
33+
i: T,
1634
}
1735

1836
#[repr(C)]
1937
pub struct NestedSingleElementStructArg {
20-
i: SingleElementStructArg,
38+
i: SingleElementStructArg<i32>,
2139
}
2240

2341
#[repr(C)]
@@ -78,56 +96,92 @@ pub struct DifferentSizeTypePair {
7896
i: i32,
7997
}
8098

81-
// CHECK: define amdgpu_kernel void @kernel_single_element_struct_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(4) {{%.+}})
99+
// CHECK: define amdgpu_kernel void @kernel_single_element_struct_arg(i32 %0)
82100
#[no_mangle]
83-
pub extern "gpu-kernel" fn kernel_single_element_struct_arg(_: SingleElementStructArg) {}
101+
pub extern "gpu-kernel" fn kernel_single_element_struct_arg(_: SingleElementStructArg<i32>) {}
84102

85-
// CHECK: define amdgpu_kernel void @kernel_nested_single_element_struct_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(4) {{%.+}})
103+
// CHECK: define amdgpu_kernel void @kernel_nested_single_element_struct_arg(i32 %0)
86104
#[no_mangle]
87105
pub extern "gpu-kernel" fn kernel_nested_single_element_struct_arg(
88106
_: NestedSingleElementStructArg,
89107
) {
90108
}
91109

92-
// CHECK: define amdgpu_kernel void @kernel_struct_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(12) {{%.+}})
110+
// CHECK: define amdgpu_kernel void @kernel_struct_arg(ptr addrspace(4) noalias nofree noundef readnone byref([12 x i8]) align 4 captures(none) dereferenceable(12) {{%.+}})
93111
#[no_mangle]
94112
pub extern "gpu-kernel" fn kernel_struct_arg(_: StructArg) {}
95113

96-
// CHECK: define amdgpu_kernel void @kernel_struct_padding_arg(i8 noundef {{%.+}}, i64 noundef {{%.+}})
114+
// CHECK: define amdgpu_kernel void @kernel_struct_padding_arg(ptr addrspace(4) noalias nofree noundef readnone byref([16 x i8]) align 8 captures(none) dereferenceable(16) {{%.+}})
97115
#[no_mangle]
98116
pub extern "gpu-kernel" fn kernel_struct_padding_arg(_: StructPaddingArg) {}
99117

100-
// CHECK: define amdgpu_kernel void @kernel_struct_of_arrays_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(44) {{%.+}})
118+
// CHECK: define amdgpu_kernel void @kernel_struct_of_arrays_arg(ptr addrspace(4) noalias nofree noundef readnone byref([44 x i8]) align 4 captures(none) dereferenceable(44) {{%.+}})
101119
#[no_mangle]
102120
pub extern "gpu-kernel" fn kernel_struct_of_arrays_arg(_: StructOfArraysArg) {}
103121

104-
// CHECK: define amdgpu_kernel void @kernel_struct_of_structs_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(24) {{%.+}})
122+
// CHECK: define amdgpu_kernel void @kernel_struct_of_structs_arg(ptr addrspace(4) noalias nofree noundef readnone byref([24 x i8]) align 4 captures(none) dereferenceable(24) {{%.+}})
105123
#[no_mangle]
106124
pub extern "gpu-kernel" fn kernel_struct_of_structs_arg(_: StructOfStructsArg) {}
107125

108-
// CHECK: define amdgpu_kernel void @test_kernel_union_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(4) {{%.+}})
126+
// CHECK: define amdgpu_kernel void @test_kernel_union_arg(ptr addrspace(4) noalias nofree noundef readnone byref([4 x i8]) align 4 captures(none) dereferenceable(4) {{%.+}})
109127
#[no_mangle]
110128
pub extern "gpu-kernel" fn test_kernel_union_arg(_: U) {}
111129

112-
// CHECK: define amdgpu_kernel void @kernel_single_array_element_struct_arg(ptr noalias nofree noundef readnone align 4 captures(none) dead_on_return dereferenceable(16) {{%.+}})
130+
// CHECK: define amdgpu_kernel void @kernel_single_array_element_struct_arg(ptr addrspace(4) noalias nofree noundef readnone byref([16 x i8]) align 4 captures(none) dereferenceable(16) {{%.+}})
113131
#[no_mangle]
114132
pub extern "gpu-kernel" fn kernel_single_array_element_struct_arg(_: SingleArrayElementStructArg) {}
115133

116-
// CHECK: define amdgpu_kernel void @kernel_single_struct_element_struct_arg(i32 noundef {{%.+}}, i64 noundef {{%.+}})
134+
// CHECK: define amdgpu_kernel void @kernel_single_struct_element_struct_arg(ptr addrspace(4) noalias nofree noundef readnone byref([16 x i8]) align 8 captures(none) dereferenceable(16) {{%.+}})
117135
#[no_mangle]
118136
pub extern "gpu-kernel" fn kernel_single_struct_element_struct_arg(
119137
_: SingleStructElementStructArg,
120138
) {
121139
}
122140

123-
// CHECK: define amdgpu_kernel void @kernel_different_size_type_pair_arg(i64 noundef {{%.+}}, i32 noundef {{%.+}})
141+
// CHECK: define amdgpu_kernel void @kernel_different_size_type_pair_arg(ptr addrspace(4) noalias nofree noundef readnone byref([16 x i8]) align 8 captures(none) dereferenceable(16) {{%.+}})
124142
#[no_mangle]
125143
pub extern "gpu-kernel" fn kernel_different_size_type_pair_arg(_: DifferentSizeTypePair) {}
126144

127-
// CHECK: define amdgpu_kernel void @kernel_complex(float noundef {{%.+}}, float noundef {{%.+}})
145+
// CHECK: define amdgpu_kernel void @kernel_complex(ptr addrspace(4) noalias nofree noundef readnone byref([8 x i8]) align 4 captures(none) dereferenceable(8) {{%.+}})
128146
#[no_mangle]
129147
pub extern "gpu-kernel" fn kernel_complex(_: Complex<f32>) {}
130148

131-
// CHECK: define amdgpu_kernel void @kernel_slice(ptr noalias nofree noundef nonnull readonly align 4 captures(none) {{%.+}}, i64 noundef range(i64 0, 2305843009213693952) {{%.+}})
149+
// CHECK: define amdgpu_kernel void @kernel_slice(ptr addrspace(4) noalias nofree noundef readnone byref([16 x i8]) align 8 captures(none) dereferenceable(16) {{%.+}})
132150
#[no_mangle]
133151
pub extern "gpu-kernel" fn kernel_slice(_: &[u32]) {}
152+
153+
// CHECK: define amdgpu_kernel void @kernel_i64(i64 noundef {{%.+}})
154+
#[no_mangle]
155+
pub extern "gpu-kernel" fn kernel_i64(_: i64) {}
156+
157+
// CHECK: define amdgpu_kernel void @kernel_i64_struct(i64 {{%.+}})
158+
#[no_mangle]
159+
pub extern "gpu-kernel" fn kernel_i64_struct(_: SingleElementStructArg<i64>) {}
160+
161+
// CHECK: define amdgpu_kernel void @kernel_i128_struct(i128 {{%.+}})
162+
#[no_mangle]
163+
pub extern "gpu-kernel" fn kernel_i128_struct(_: SingleElementStructArg<i128>) {}
164+
165+
// CHECK: define amdgpu_kernel void @kernel_i8x2_struct(<2 x i8> {{%.+}})
166+
#[no_mangle]
167+
pub extern "gpu-kernel" fn kernel_i8x2_struct(_: SingleElementStructArg<I8X2>) {}
168+
169+
// CHECK: define amdgpu_kernel void @kernel_i16x2_struct(<2 x i16> {{%.+}})
170+
#[no_mangle]
171+
pub extern "gpu-kernel" fn kernel_i16x2_struct(_: SingleElementStructArg<I16X2>) {}
172+
173+
// CHECK: define amdgpu_kernel void @kernel_i16x3_struct(<4 x i16> {{%.+}})
174+
#[no_mangle]
175+
pub extern "gpu-kernel" fn kernel_i16x3_struct(_: SingleElementStructArg<I16X3>) {}
176+
177+
// CHECK: define amdgpu_kernel void @kernel_i16x4_struct(<4 x i16> {{%.+}})
178+
#[no_mangle]
179+
pub extern "gpu-kernel" fn kernel_i16x4_struct(_: SingleElementStructArg<I16X4>) {}
180+
181+
// CHECK: define amdgpu_kernel void @kernel_i32x3_struct(<4 x i32> {{%.+}})
182+
#[no_mangle]
183+
pub extern "gpu-kernel" fn kernel_i32x3_struct(_: SingleElementStructArg<I32X3>) {}
184+
185+
// CHECK: define amdgpu_kernel void @kernel_i32x4_struct(<4 x i32> {{%.+}})
186+
#[no_mangle]
187+
pub extern "gpu-kernel" fn kernel_i32x4_struct(_: SingleElementStructArg<I32X4>) {}

0 commit comments

Comments
 (0)