Skip to content

Commit a81098a

Browse files
authored
Rollup merge of #163077 - xkevio:patch-1, r=ZuseZ4
feat: support nested functions with `#[offload_kernel]` Related to #131513. Allows annotating nested functions with `#[offload_kernel]`, similar to autodiff. Also fixes the error message when `#[offload_kernel]` is used on anything other than functions. r? @ZuseZ4 (@Sa4dUs, ping)
2 parents 4985df8 + a6a7cca commit a81098a

7 files changed

Lines changed: 204 additions & 14 deletions

File tree

‎compiler/rustc_builtin_macros/src/diagnostics.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ mod autodiff {
229229
}
230230
}
231231

232+
#[derive(Diagnostic)]
233+
#[diag("offload_kernel must be applied to function")]
234+
pub(crate) struct OffloadKernelInvalidApplication {
235+
#[primary_span]
236+
pub(crate) span: Span,
237+
}
238+
232239
#[derive(Diagnostic)]
233240
#[diag("cannot concatenate {$lit_kind} literals")]
234241
pub(crate) struct ConcatBytesInvalid {

‎compiler/rustc_builtin_macros/src/offload.rs‎

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@ fn outer_normal_attr(normal: &Box<ast::NormalAttr>, id: ast::AttrId, span: Span)
2121
fn extract_fn(
2222
item: &Annotatable,
2323
) -> Option<(ast::Visibility, ast::FnSig, Ident, ast::Generics, Option<Box<ast::Block>>)> {
24-
match item {
25-
Annotatable::Item(iitem) => match &iitem.kind {
26-
ast::ItemKind::Fn(ast::Fn { sig, ident, generics, body, .. }) => {
27-
Some((iitem.vis.clone(), sig.clone(), *ident, generics.clone(), body.clone()))
28-
}
29-
_ => None,
30-
},
24+
let fn_item_kind_and_vis = match item {
25+
Annotatable::Item(iitem) => Some((&iitem.kind, &iitem.vis)),
26+
Annotatable::Stmt(stmt) if let ast::StmtKind::Item(iitem) = &stmt.kind => {
27+
Some((&iitem.kind, &iitem.vis))
28+
}
3129
_ => None,
30+
};
31+
32+
if let Some((item_kind, visibility)) = fn_item_kind_and_vis
33+
&& let ast::ItemKind::Fn(kind) = item_kind
34+
{
35+
Some((
36+
visibility.clone(),
37+
kind.sig.clone(),
38+
kind.ident,
39+
kind.generics.clone(),
40+
kind.body.clone(),
41+
))
42+
} else {
43+
None
3244
}
3345
}
3446

@@ -68,7 +80,7 @@ pub(crate) fn expand_kernel(
6880
let dcx = ecx.sess.dcx();
6981

7082
let Some((vis, sig, ident, generics, body)) = extract_fn(&item) else {
71-
dcx.emit_err(diagnostics::AutoDiffInvalidApplication { span: item.span() });
83+
dcx.emit_err(diagnostics::OffloadKernelInvalidApplication { span: item.span() });
7284
return vec![item];
7385
};
7486

@@ -100,10 +112,19 @@ pub(crate) fn expand_kernel(
100112
);
101113

102114
let device_item = {
103-
let mut item =
115+
let mut device_item_ecx =
104116
ecx.item(span, thin_vec![rustc_offload_kernel.clone()], ast::ItemKind::Fn(device_fn));
105-
item.vis = vis.clone();
106-
Annotatable::Item(item)
117+
device_item_ecx.vis = vis.clone();
118+
119+
match &item {
120+
Annotatable::Item(_) => Annotatable::Item(device_item_ecx),
121+
Annotatable::Stmt(_) => {
122+
Annotatable::Stmt(Box::new(ecx.stmt_item(span, device_item_ecx)))
123+
}
124+
_ => {
125+
unreachable!("item kind checked previously")
126+
}
127+
}
107128
};
108129

109130
// unimplemented! body
@@ -150,13 +171,20 @@ pub(crate) fn expand_kernel(
150171
let inline_never = outer_normal_attr(&inline_never_attr, new_id, span);
151172

152173
let host_item = {
153-
let mut item = ecx.item(
174+
let mut host_item_ecx = ecx.item(
154175
span,
155176
thin_vec![rustc_offload_kernel, inline_never],
156177
ast::ItemKind::Fn(host_fn),
157178
);
158-
item.vis = vis;
159-
Annotatable::Item(item)
179+
host_item_ecx.vis = vis;
180+
181+
match &item {
182+
Annotatable::Item(_) => Annotatable::Item(host_item_ecx),
183+
Annotatable::Stmt(_) => Annotatable::Stmt(Box::new(ecx.stmt_item(span, host_item_ecx))),
184+
_ => {
185+
unreachable!("item kind checked previously")
186+
}
187+
}
160188
};
161189

162190
if compile_for_device(ecx) { vec![device_item] } else { vec![host_item] }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
//@ only-nightly
4+
//@ revisions: host device
5+
6+
//@ pretty-mode:expanded
7+
//@ pretty-compare-only
8+
//@[host] pp-exact:offload_kernel_nested.host.pp
9+
//@[device] pp-exact:offload_kernel_nested.device.pp
10+
11+
//@[device] compile-flags: -Zunstable-options -Zoffload=Device
12+
13+
#![feature(gpu_offload)]
14+
extern crate std;
15+
#[prelude_import]
16+
use ::std::prelude::rust_2015::*;
17+
18+
use std::offload::offload_kernel;
19+
20+
fn kernel() {
21+
#[rustc_offload_kernel]
22+
unsafe extern "gpu-kernel" fn inner_kernel() {}
23+
}
24+
25+
fn main() {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
//@ only-nightly
4+
//@ revisions: host device
5+
6+
//@ pretty-mode:expanded
7+
//@ pretty-compare-only
8+
//@[host] pp-exact:offload_kernel_nested.host.pp
9+
//@[device] pp-exact:offload_kernel_nested.device.pp
10+
11+
//@[device] compile-flags: -Zunstable-options -Zoffload=Device
12+
13+
#![feature(gpu_offload)]
14+
extern crate std;
15+
#[prelude_import]
16+
use ::std::prelude::rust_2015::*;
17+
18+
use std::offload::offload_kernel;
19+
20+
fn kernel() {
21+
#[rustc_offload_kernel]
22+
#[inline(never)]
23+
fn inner_kernel() {
24+
25+
::core::panicking::unimplemented_default_msg()
26+
}
27+
}
28+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ only-nightly
2+
//@ revisions: host device
3+
4+
//@ pretty-mode:expanded
5+
//@ pretty-compare-only
6+
//@[host] pp-exact:offload_kernel_nested.host.pp
7+
//@[device] pp-exact:offload_kernel_nested.device.pp
8+
9+
//@[device] compile-flags: -Zunstable-options -Zoffload=Device
10+
11+
#![feature(gpu_offload)]
12+
13+
use std::offload::offload_kernel;
14+
15+
fn kernel() {
16+
#[offload_kernel]
17+
fn inner_kernel() {}
18+
}
19+
20+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(gpu_offload)]
2+
3+
fn dummy() {
4+
#[core::offload::offload_kernel]
5+
//~^ ERROR macro attributes on statements are unstable
6+
let mut x = 5;
7+
//~^ ERROR offload_kernel must be applied to function
8+
9+
#[core::offload::offload_kernel]
10+
x = x + 3;
11+
//~^^ ERROR attributes on expressions are experimental [E0658]
12+
//~| ERROR macro attributes on expressions are unstable
13+
//~^^^ ERROR offload_kernel must be applied to function
14+
15+
#[core::offload::offload_kernel]
16+
//~^ ERROR macro attributes on statements are unstable
17+
let add_one_v2 = |x: u32| -> u32 { x + 1 };
18+
//~^ ERROR offload_kernel must be applied to function
19+
}
20+
21+
fn main() {}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
error[E0658]: attributes on expressions are experimental
2+
--> $DIR/offload_kernel_illegal.rs:9:5
3+
|
4+
LL | #[core::offload::offload_kernel]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: macro attributes on statements are unstable
12+
--> $DIR/offload_kernel_illegal.rs:4:5
13+
|
14+
LL | #[core::offload::offload_kernel]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #54727 <https://github.com/rust-lang/rust/issues/54727> for more information
18+
= help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: offload_kernel must be applied to function
22+
--> $DIR/offload_kernel_illegal.rs:6:5
23+
|
24+
LL | let mut x = 5;
25+
| ^^^^^^^^^^^^^^
26+
27+
error[E0658]: macro attributes on expressions are unstable
28+
--> $DIR/offload_kernel_illegal.rs:9:5
29+
|
30+
LL | #[core::offload::offload_kernel]
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: see issue #54727 <https://github.com/rust-lang/rust/issues/54727> for more information
34+
= help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable
35+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
36+
37+
error: offload_kernel must be applied to function
38+
--> $DIR/offload_kernel_illegal.rs:10:5
39+
|
40+
LL | x = x + 3;
41+
| ^
42+
43+
error[E0658]: macro attributes on statements are unstable
44+
--> $DIR/offload_kernel_illegal.rs:15:5
45+
|
46+
LL | #[core::offload::offload_kernel]
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
|
49+
= note: see issue #54727 <https://github.com/rust-lang/rust/issues/54727> for more information
50+
= help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable
51+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
52+
53+
error: offload_kernel must be applied to function
54+
--> $DIR/offload_kernel_illegal.rs:17:5
55+
|
56+
LL | let add_one_v2 = |x: u32| -> u32 { x + 1 };
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
59+
error: aborting due to 7 previous errors
60+
61+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)