Skip to content

Commit 076eb5d

Browse files
Rollup merge of #162177 - Flakebi:amdgpu-kernel-cc, r=bjorn3
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/e4e18dba3d77f4a3eea58bcc9ccae5a5498ede7c/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). This adds two members to `PassMode::Indirect`. `address_space` specifies the address space of an on_stack/byval or by_ref pointer argument. `by_ref` translates to LLVM’s byref, which is similar to on_stack/byval, however, there is no extra copy made, the pointer may not point to the stack but can point to some other address space, and the passed argument should not be modified. Both are used by the amdgpu target to implement the `gpu-kernel` ABI. Tracking issue for the `gpu-kernel` ABI: #135467 Tracking issue for the amdgpu target: #135024
2 parents 52d0fd8 + be988e9 commit 076eb5d

38 files changed

Lines changed: 661 additions & 169 deletions

File tree

‎compiler/rustc_abi/src/layout/ty.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
233233
}
234234

235235
/// If this method returns `true`, then this type should always have a `PassMode` of
236-
/// `Indirect { on_stack: false, .. }` when being used as the argument type of a function with a
237-
/// non-Rustic ABI (this is true for structs annotated with the
236+
/// `Indirect { mode: IndirectMode::Pointer, .. }` when being used as the argument type of a
237+
/// function with a non-Rustic ABI (this is true for structs annotated with the
238238
/// `#[rustc_pass_indirectly_in_non_rustic_abis]` attribute).
239239
///
240240
/// This is used to replicate some of the behaviour of C array-to-pointer decay; however unlike

‎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);

‎compiler/rustc_codegen_cranelift/src/abi/pass_mode.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use cranelift_codegen::ir::ArgumentPurpose;
44
use rustc_abi::{Reg, RegKind};
55
use rustc_target::callconv::{
6-
ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, PassMode,
6+
ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, IndirectMode, PassMode,
77
};
88
use smallvec::{SmallVec, smallvec};
99

@@ -126,8 +126,12 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
126126
assert_eq!(pad_i32_count, 0, "padding support not yet implemented");
127127
cast_target_to_abi_params(cast).into_iter().map(|(_, param)| param).collect()
128128
}
129-
PassMode::Indirect { attrs, meta_attrs: None, on_stack } => {
130-
if on_stack {
129+
PassMode::Indirect { attrs, meta_attrs: None, address_space: _, mode } => {
130+
assert!(
131+
mode != IndirectMode::AmdgpuKernelArg,
132+
"unsupported amdgpu kernel argument"
133+
);
134+
if mode == IndirectMode::OnStack {
131135
// Abi requires aligning struct size to pointer size
132136
let size = self.layout.size.align_to(tcx.data_layout.pointer_align().abi);
133137
let size = u32::try_from(size.bytes()).unwrap();
@@ -139,8 +143,8 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
139143
smallvec![apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)]
140144
}
141145
}
142-
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
143-
assert!(!on_stack);
146+
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), address_space: _, mode } => {
147+
assert!(mode == IndirectMode::Pointer);
144148
smallvec![
145149
apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs),
146150
apply_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), meta_attrs),
@@ -184,8 +188,8 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
184188
None,
185189
cast_target_to_abi_params(cast).into_iter().map(|(_, param)| param).collect(),
186190
),
187-
PassMode::Indirect { attrs, meta_attrs: None, on_stack } => {
188-
assert!(!on_stack);
191+
PassMode::Indirect { attrs, meta_attrs: None, address_space: _, mode } => {
192+
assert!(mode == IndirectMode::Pointer);
189193
(
190194
Some(apply_attrs_to_abi_param(
191195
AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn),
@@ -194,7 +198,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
194198
vec![],
195199
)
196200
}
197-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
201+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
198202
unreachable!("unsized return value")
199203
}
200204
}
@@ -324,7 +328,7 @@ pub(super) fn cvalue_for_param<'tcx>(
324328
PassMode::Cast { ref cast, .. } => {
325329
from_casted_value(fx, &block_params, arg_abi.layout, cast)
326330
}
327-
PassMode::Indirect { attrs, meta_attrs: None, on_stack: _ } => {
331+
PassMode::Indirect { attrs, meta_attrs: None, address_space: _, mode: _ } => {
328332
assert_eq!(block_params.len(), 1, "{:?}", block_params);
329333
if let Some(pointee_align) = attrs.pointee_align
330334
&& pointee_align < arg_abi.layout.align.abi
@@ -342,7 +346,7 @@ pub(super) fn cvalue_for_param<'tcx>(
342346
CValue::by_ref(Pointer::new(block_params[0]), arg_abi.layout)
343347
}
344348
}
345-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
349+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
346350
assert_eq!(block_params.len(), 2, "{:?}", block_params);
347351
CValue::by_ref_unsized(Pointer::new(block_params[0]), block_params[1], arg_abi.layout)
348352
}

‎compiler/rustc_codegen_cranelift/src/abi/returning.rs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ pub(super) fn codegen_return_param<'tcx>(
1717
let is_ssa = ssa_analyzed[RETURN_PLACE].is_ssa(fx, fx.fn_abi.ret.layout.ty);
1818
(super::make_local_place(fx, RETURN_PLACE, fx.fn_abi.ret.layout, is_ssa), smallvec![])
1919
}
20-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
20+
PassMode::Indirect { attrs: _, meta_attrs: None, address_space: _, mode: _ } => {
2121
let ret_param = block_params_iter.next().unwrap();
2222
assert_eq!(fx.bcx.func.dfg.value_type(ret_param), fx.pointer_type);
2323
(CPlace::for_ptr(Pointer::new(ret_param), fx.fn_abi.ret.layout), smallvec![ret_param])
2424
}
25-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
25+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
2626
unreachable!("unsized return value")
2727
}
2828
};
@@ -50,7 +50,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
5050
) {
5151
let (ret_temp_place, return_ptr) = match ret_arg_abi.mode {
5252
PassMode::Ignore => (None, None),
53-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
53+
PassMode::Indirect { attrs: _, meta_attrs: None, address_space: _, mode: _ } => {
5454
if let Some(ret_ptr) = ret_place.try_to_ptr() {
5555
// This is an optimization to prevent unnecessary copies of the return value when
5656
// the return place is already a memory place as opposed to a register.
@@ -61,7 +61,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
6161
(Some(place), Some(place.to_ptr().get_addr(fx)))
6262
}
6363
}
64-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
64+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
6565
unreachable!("unsized return value")
6666
}
6767
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast { .. } => (None, None),
@@ -86,14 +86,14 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
8686
super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
8787
ret_place.write_cvalue(fx, result);
8888
}
89-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
89+
PassMode::Indirect { attrs: _, meta_attrs: None, address_space: _, mode: _ } => {
9090
if let Some(ret_temp_place) = ret_temp_place {
9191
// If ret_temp_place is None, it is not necessary to copy the return value.
9292
let ret_temp_value = ret_temp_place.to_cvalue(fx);
9393
ret_place.write_cvalue(fx, ret_temp_value);
9494
}
9595
}
96-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
96+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
9797
unreachable!("unsized return value")
9898
}
9999
}
@@ -102,10 +102,11 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
102102
/// Codegen a return instruction with the right return value(s) if any.
103103
pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
104104
match fx.fn_abi.ret.mode {
105-
PassMode::Ignore | PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
105+
PassMode::Ignore
106+
| PassMode::Indirect { attrs: _, meta_attrs: None, address_space: _, mode: _ } => {
106107
fx.bcx.ins().return_(&[]);
107108
}
108-
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
109+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), address_space: _, mode: _ } => {
109110
unreachable!("unsized return value")
110111
}
111112
PassMode::Direct(_) => {

‎compiler/rustc_codegen_gcc/src/abi.rs‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::layout::LayoutOf;
1111
#[cfg(feature = "master")]
1212
use rustc_session::{Session, config};
1313
use rustc_span::bug;
14-
use rustc_target::callconv::{ArgAttributes, CastTarget, FnAbi, PassMode};
14+
use rustc_target::callconv::{ArgAttributes, CastTarget, FnAbi, IndirectMode, PassMode};
1515
#[cfg(feature = "master")]
1616
use rustc_target::spec::Arch;
1717

@@ -189,7 +189,12 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
189189
let ty = cast.gcc_type(cx);
190190
apply_attrs(ty, &cast.attrs, argument_tys.len())
191191
}
192-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: true } => {
192+
PassMode::Indirect {
193+
attrs: _,
194+
meta_attrs: None,
195+
address_space: _,
196+
mode: IndirectMode::OnStack,
197+
} => {
193198
let x86_interrupt_first_arg = {
194199
#[cfg(feature = "master")]
195200
{
@@ -216,14 +221,32 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
216221
arg.layout.gcc_type(cx)
217222
}
218223
}
224+
PassMode::Indirect {
225+
attrs: _,
226+
meta_attrs: None,
227+
address_space: _,
228+
mode: IndirectMode::AmdgpuKernelArg,
229+
} => {
230+
unimplemented!("unsupported amdgpu kernel argument")
231+
}
219232
PassMode::Direct(attrs) => {
220233
apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len())
221234
}
222-
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
235+
PassMode::Indirect {
236+
attrs,
237+
meta_attrs: None,
238+
address_space: _,
239+
mode: IndirectMode::Pointer,
240+
} => {
223241
apply_attrs(cx.type_ptr_to(arg.layout.gcc_type(cx)), &attrs, argument_tys.len())
224242
}
225-
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
226-
assert!(!on_stack);
243+
PassMode::Indirect {
244+
attrs,
245+
meta_attrs: Some(meta_attrs),
246+
address_space: _,
247+
mode,
248+
} => {
249+
assert!(mode == IndirectMode::Pointer);
227250
// Construct the type of a (wide) pointer to `ty`, and pass its two fields.
228251
// Any two ABI-compatible unsized types have the same metadata type and
229252
// moreover the same metadata value leads to the same dynamic size and

0 commit comments

Comments
 (0)