rustc_target/callconv/
nvptx64.rs

1use rustc_abi::{HasDataLayout, Reg, Size, TyAbiInterface};
2
3use super::CastTarget;
4use crate::callconv::{ArgAbi, FnAbi, Uniform};
5
6fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
7    if ret.layout.is_aggregate() && ret.layout.is_sized() {
8        classify_aggregate(ret)
9    } else if ret.layout.size.bits() < 32 && ret.layout.is_sized() {
10        ret.extend_integer_width_to(32);
11    }
12}
13
14fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
15    if arg.layout.is_aggregate() && arg.layout.is_sized() {
16        classify_aggregate(arg)
17    } else if arg.layout.size.bits() < 32 && arg.layout.is_sized() {
18        arg.extend_integer_width_to(32);
19    }
20}
21
22/// the pass mode used for aggregates in arg and ret position
23fn classify_aggregate<Ty>(arg: &mut ArgAbi<'_, Ty>) {
24    let align_bytes = arg.layout.align.abi.bytes();
25    let size = arg.layout.size;
26
27    let reg = match align_bytes {
28        1 => Reg::i8(),
29        2 => Reg::i16(),
30        4 => Reg::i32(),
31        8 => Reg::i64(),
32        16 => Reg::i128(),
33        _ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
34    };
35
36    if align_bytes == size.bytes() {
37        arg.cast_to(CastTarget::prefixed(
38            [Some(reg), None, None, None, None, None, None, None],
39            Uniform::new(Reg::i8(), Size::ZERO),
40        ));
41    } else {
42        arg.cast_to(Uniform::new(reg, size));
43    }
44}
45
46fn classify_arg_kernel<'a, Ty, C>(_cx: &C, arg: &mut ArgAbi<'a, Ty>)
47where
48    Ty: TyAbiInterface<'a, C> + Copy,
49    C: HasDataLayout,
50{
51    match arg.mode {
52        super::PassMode::Ignore | super::PassMode::Direct(_) => return,
53        super::PassMode::Pair(_, _) => {}
54        super::PassMode::Cast { .. } => unreachable!(),
55        super::PassMode::Indirect { .. } => {}
56    }
57
58    // FIXME only allow structs and wide pointers here
59    // panic!(
60    //     "`extern \"ptx-kernel\"` doesn't allow passing types other than primitives and structs"
61    // );
62
63    let align_bytes = arg.layout.align.abi.bytes();
64
65    let unit = match align_bytes {
66        1 => Reg::i8(),
67        2 => Reg::i16(),
68        4 => Reg::i32(),
69        8 => Reg::i64(),
70        16 => Reg::i128(),
71        _ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
72    };
73    if arg.layout.size.bytes() / align_bytes == 1 {
74        // Make sure we pass the struct as array at the LLVM IR level and not as a single integer.
75        arg.cast_to(CastTarget::prefixed(
76            [Some(unit), None, None, None, None, None, None, None],
77            Uniform::new(unit, Size::ZERO),
78        ));
79    } else {
80        arg.cast_to(Uniform::new(unit, arg.layout.size));
81    }
82}
83
84pub(crate) fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
85    if !fn_abi.ret.is_ignore() {
86        classify_ret(&mut fn_abi.ret);
87    }
88
89    for arg in fn_abi.args.iter_mut() {
90        if arg.is_ignore() {
91            continue;
92        }
93        classify_arg(arg);
94    }
95}
96
97pub(crate) fn compute_ptx_kernel_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
98where
99    Ty: TyAbiInterface<'a, C> + Copy,
100    C: HasDataLayout,
101{
102    if !fn_abi.ret.layout.is_unit() && !fn_abi.ret.layout.is_never() {
103        panic!("Kernels should not return anything other than () or !");
104    }
105
106    for arg in fn_abi.args.iter_mut() {
107        if arg.is_ignore() {
108            continue;
109        }
110        classify_arg_kernel(cx, arg);
111    }
112}