Skip to content

Conversation

@Flakebi
Copy link
Contributor

@Flakebi Flakebi commented Dec 14, 2025

The gpu-kernel calling convention has several restrictions that were not enforced by the compiler until now.
Add the following restrictions:

  1. Cannot be async
  2. Cannot be called
  3. Cannot return values, return type must be () or !
  4. Arguments should be simple, i.e. passed by value. More complicated types can work when you know what you are doing, but it is rather unintuitive, one needs to know ABI/compiler internals.
  5. Export name should be unmangled, either through no_mangle or export_name. Kernels are searched by name on the CPU side, having a mangled name makes it hard to find and probably almost always unintentional.

Tracking issue: #135467
amdgpu target tracking issue: #135024

@workingjubilee, these should be all the restrictions we talked about a year ago.

cc @RDambrosio016 @kjetilkjeka for nvptx

@rustbot
Copy link
Collaborator

rustbot commented Dec 14, 2025

r? @WaffleLapkin

rustbot has assigned @WaffleLapkin.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 14, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin
Copy link
Member

r? workingjubilee

As I'm completely missing context

@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2025

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST-level code looks good.

Some details on messaging here. I'm not committed to a precise message on these, which is why it's a bit "multiple choice" here, just wondering if these could be improved. In one or two cases it is a must-change.

Should we be enforcing a maximum number of arguments, also? Probably not if there's no cross-driver consensus on that, but maybe?

View changes since this review

/// This lint is issued when it detects a probable mistake in a signature.
IMPROPER_GPU_KERNEL_ARG,
Warn,
"simple arguments of gpu-kernel functions"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should capture the reason for the lint, not what it is checking for, so something like

Suggested change
"simple arguments of gpu-kernel functions"
"GPU kernel entry points have a limited calling convention"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I’ll call it ABI instead of calling convention, it seems like Rust calls it ABI in most places. (Unless there’s a difference I’m missing)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically ABI is a superset of calling convention, notionally.

I'm cool with people abusing the term a bit when it's clear what it means from context and is more concise, such as in diagnostic messaging here.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 16, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 16, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Copy link
Contributor Author

@Flakebi Flakebi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

Some context on the internal workings:

  1. On the CPU side, a program passes arguments to a kernel
  2. The “API” takes these arguments and writes them into GPU memory
  3. The kernel on the GPU gets a pointer to this memory
  4. When the kernel accesses arguments, it reads from this memory

(I think nvidia and amd work the same here. I’m not too familiar with nvidia, but this seems to suggest so: https://github.com/Rust-GPU/rust-cuda/blob/44c44baf6fb738d5ffec25aac5db8af02514e890/crates/rustc_codegen_nvvm/src/abi.rs#L60)

So, number of arguments or size of arguments doesn’t really matter, it’s all memory anyways.
And, we could make struct arguments work (maybe, I didn’t look into the details), but Rust would need to take them by value, currently it changes them to pass by pointer.

View changes since this review

/// This lint is issued when it detects a probable mistake in a signature.
IMPROPER_GPU_KERNEL_ARG,
Warn,
"simple arguments of gpu-kernel functions"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I’ll call it ABI instead of calling convention, it seems like Rust calls it ABI in most places. (Unless there’s a difference I’m missing)

@rust-log-analyzer

This comment has been minimized.

@Flakebi Flakebi force-pushed the gpu-kernel-cc branch 2 times, most recently from 6e7c9a0 to 6868f66 Compare December 20, 2025 15:06
Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some context on the internal workings:

  1. On the CPU side, a program passes arguments to a kernel
  2. The “API” takes these arguments and writes them into GPU memory
  3. The kernel on the GPU gets a pointer to this memory
  4. When the kernel accesses arguments, it reads from this memory

Ah, yeah. I know of the general idea here, though I am foggy on specifics in many cases.

And even in some hypothetical where the driver isn't writing them into the GPU memory, it would have to invoke some dark magic on the GPU to put it directly into registers anyways, which is just saying "did you know: some computer hardware has memory-mapped registers?"

( I only am making that comment because I vaguely remember one GPU driver involving something similar. )

At some point we are left with doing codegen to accept arguments and we can expect that to have some target-specific nuances, even if it's just stack alignment.

For structs, I'd rather we avoid thinking too hard about the struct question for cases where they aren't just repr(transparent) primitives until we have tinkered with things a bit more. If repr(C) structs are widely supported and fully intentionally that could be worth tackling.

View changes since this review

| ty::Slice(_)
| ty::Str
| ty::Tuple(_)
| ty::UnsafeBinder(_) => false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the answer for ty::UnsafeBinder should be the recursive answer to this question by default (I think the implied result is quite useless given the answer to other things, but that's fine).

Thus this should... probably use an impl of TypeFolder to apply the recursive traversal, I think? I do not consider that required, but I thought I should note it because this seems like a sorta-classic ..super_fold.. case, where we want to only consider the "true" type instead of binders and such: https://rustc-dev-guide.rust-lang.org/ty-fold.html

| ty::CoroutineClosure(_, _)
| ty::CoroutineWitness(..)
| ty::Dynamic(_, _)
| ty::Error(_)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either return or continue if the type is so erroneous.

Comment on lines 147 to 149
| ty::Infer(_)
| ty::Never
| ty::Param(_)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalizing should have handled Infer and Param cases I think?

The `gpu-kernel` calling convention has several restrictions that were
not enforced by the compiler until now.
Add the following restrictions:

1. Cannot be async
2. Cannot be called
3. Cannot return values, return type must be `()` or `!`
4. Arguments should be primitives, i.e. passed by value. More complicated
   types can work when you know what you are doing, but it is rather
   unintuitive, one needs to know ABI/compiler internals.
5. Export name should be unmangled, either through `no_mangle` or
   `export_name`. Kernels are searched by name on the CPU side, having
   a mangled name makes it hard to find and probably almost always
   unintentional.
@Flakebi
Copy link
Contributor Author

Flakebi commented Jan 1, 2026

I tried rewriting the lint pass using TypeFolder (plus fixing the other comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants