Skip to content

Commit f1fbc71

Browse files
Rollup merge of rust-lang#94703 - kjetilkjeka:nvptx-kernel-args-abi2, r=nagisa
Fix codegen bug in "ptx-kernel" abi related to arg passing I found a codegen bug in the nvptx abi related to that args are passed as ptrs ([see comment](rust-lang#38788 (comment))), this is not as specified in the [ptx-interoperability doc](https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/) or how C/C++ does it. It will also almost always fail in practice since device/host uses different memory spaces for most hardware. This PR fixes the bug and add tests for passing structs to ptx kernels. I observed that all nvptx assembly tests had been marked as [ignore a long time ago](rust-lang#59752 (comment)). I'm not sure if the new one should be marked as ignore, it passed on my computer but it might fail if ptx-linker is missing on the server? I guess this is outside scope for this PR and should be looked at in a different issue/PR. I only fixed the nvptx64-nvidia-cuda target and not the potential code paths for the non-existing 32bit target. Even though 32bit nvptx is not a supported target there are still some code under the hood supporting codegen for 32 bit ptx. I was advised to create an MCP to find out if this code should be removed or updated. Perhaps `@RDambrosio016` would have interest in taking a quick look at this.
2 parents 24dcda0 + 5bf5acc commit f1fbc71

File tree

6 files changed

+352
-9
lines changed

6 files changed

+352
-9
lines changed

compiler/rustc_middle/src/ty/layout.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,22 @@ where
25922592

25932593
pointee_info
25942594
}
2595+
2596+
fn is_adt(this: TyAndLayout<'tcx>) -> bool {
2597+
matches!(this.ty.kind(), ty::Adt(..))
2598+
}
2599+
2600+
fn is_never(this: TyAndLayout<'tcx>) -> bool {
2601+
this.ty.kind() == &ty::Never
2602+
}
2603+
2604+
fn is_tuple(this: TyAndLayout<'tcx>) -> bool {
2605+
matches!(this.ty.kind(), ty::Tuple(..))
2606+
}
2607+
2608+
fn is_unit(this: TyAndLayout<'tcx>) -> bool {
2609+
matches!(this.ty.kind(), ty::Tuple(list) if list.len() == 0)
2610+
}
25952611
}
25962612

25972613
impl<'tcx> ty::Instance<'tcx> {

compiler/rustc_middle/src/ty/list.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ impl<T> List<T> {
6161
static EMPTY_SLICE: InOrder<usize, MaxAlign> = InOrder(0, MaxAlign);
6262
unsafe { &*(&EMPTY_SLICE as *const _ as *const List<T>) }
6363
}
64+
65+
pub fn len(&self) -> usize {
66+
self.len
67+
}
6468
}
6569

6670
impl<T: Copy> List<T> {

compiler/rustc_target/src/abi/call/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,13 @@ impl<'a, Ty> FnAbi<'a, Ty> {
696696
"sparc" => sparc::compute_abi_info(cx, self),
697697
"sparc64" => sparc64::compute_abi_info(cx, self),
698698
"nvptx" => nvptx::compute_abi_info(self),
699-
"nvptx64" => nvptx64::compute_abi_info(self),
699+
"nvptx64" => {
700+
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::PtxKernel {
701+
nvptx64::compute_ptx_kernel_abi_info(cx, self)
702+
} else {
703+
nvptx64::compute_abi_info(self)
704+
}
705+
}
700706
"hexagon" => hexagon::compute_abi_info(self),
701707
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
702708
"wasm32" | "wasm64" => {
+39-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
// Reference: PTX Writer's Guide to Interoperability
2-
// https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability
3-
4-
use crate::abi::call::{ArgAbi, FnAbi};
1+
use crate::abi::call::{ArgAbi, FnAbi, PassMode, Reg, Size, Uniform};
2+
use crate::abi::{HasDataLayout, TyAbiInterface};
53

64
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
75
if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 {
86
ret.make_indirect();
9-
} else {
10-
ret.extend_integer_width_to(64);
117
}
128
}
139

1410
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
1511
if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 {
1612
arg.make_indirect();
17-
} else {
18-
arg.extend_integer_width_to(64);
13+
}
14+
}
15+
16+
fn classify_arg_kernel<'a, Ty, C>(_cx: &C, arg: &mut ArgAbi<'a, Ty>)
17+
where
18+
Ty: TyAbiInterface<'a, C> + Copy,
19+
C: HasDataLayout,
20+
{
21+
if matches!(arg.mode, PassMode::Pair(..)) && (arg.layout.is_adt() || arg.layout.is_tuple()) {
22+
let align_bytes = arg.layout.align.abi.bytes();
23+
24+
let unit = match align_bytes {
25+
1 => Reg::i8(),
26+
2 => Reg::i16(),
27+
4 => Reg::i32(),
28+
8 => Reg::i64(),
29+
16 => Reg::i128(),
30+
_ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
31+
};
32+
arg.cast_to(Uniform { unit, total: Size::from_bytes(2 * align_bytes) });
1933
}
2034
}
2135

@@ -31,3 +45,20 @@ pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
3145
classify_arg(arg);
3246
}
3347
}
48+
49+
pub fn compute_ptx_kernel_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
50+
where
51+
Ty: TyAbiInterface<'a, C> + Copy,
52+
C: HasDataLayout,
53+
{
54+
if !fn_abi.ret.layout.is_unit() && !fn_abi.ret.layout.is_never() {
55+
panic!("Kernels should not return anything other than () or !");
56+
}
57+
58+
for arg in &mut fn_abi.args {
59+
if arg.is_ignore() {
60+
continue;
61+
}
62+
classify_arg_kernel(cx, arg);
63+
}
64+
}

compiler/rustc_target/src/abi/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,10 @@ pub trait TyAbiInterface<'a, C>: Sized {
13551355
cx: &C,
13561356
offset: Size,
13571357
) -> Option<PointeeInfo>;
1358+
fn is_adt(this: TyAndLayout<'a, Self>) -> bool;
1359+
fn is_never(this: TyAndLayout<'a, Self>) -> bool;
1360+
fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
1361+
fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
13581362
}
13591363

13601364
impl<'a, Ty> TyAndLayout<'a, Ty> {
@@ -1396,6 +1400,34 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
13961400
_ => false,
13971401
}
13981402
}
1403+
1404+
pub fn is_adt<C>(self) -> bool
1405+
where
1406+
Ty: TyAbiInterface<'a, C>,
1407+
{
1408+
Ty::is_adt(self)
1409+
}
1410+
1411+
pub fn is_never<C>(self) -> bool
1412+
where
1413+
Ty: TyAbiInterface<'a, C>,
1414+
{
1415+
Ty::is_never(self)
1416+
}
1417+
1418+
pub fn is_tuple<C>(self) -> bool
1419+
where
1420+
Ty: TyAbiInterface<'a, C>,
1421+
{
1422+
Ty::is_tuple(self)
1423+
}
1424+
1425+
pub fn is_unit<C>(self) -> bool
1426+
where
1427+
Ty: TyAbiInterface<'a, C>,
1428+
{
1429+
Ty::is_unit(self)
1430+
}
13991431
}
14001432

14011433
impl<'a, Ty> TyAndLayout<'a, Ty> {

0 commit comments

Comments
 (0)