-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Add checks for gpu-kernel calling conv #149991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Flakebi
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
Flakebi:gpu-kernel-cc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| use std::iter; | ||
|
|
||
| use rustc_abi::ExternAbi; | ||
| use rustc_hir::attrs::AttributeKind; | ||
| use rustc_hir::{self as hir, find_attr}; | ||
| use rustc_middle::ty; | ||
| use rustc_session::{declare_lint, declare_lint_pass}; | ||
| use rustc_span::Span; | ||
| use rustc_span::def_id::LocalDefId; | ||
|
|
||
| use crate::lints::{ImproperGpuKernelArg, MissingGpuKernelExportName}; | ||
| use crate::{LateContext, LateLintPass, LintContext}; | ||
|
|
||
| declare_lint! { | ||
| /// The `improper_gpu_kernel_arg` lint detects incorrect use of types in `gpu-kernel` | ||
| /// arguments. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,ignore (fails on non-GPU targets) | ||
| /// #[unsafe(no_mangle)] | ||
| /// extern "gpu-kernel" fn kernel(_: [i32; 10]) {} | ||
| /// ``` | ||
| /// | ||
| /// This will produce: | ||
| /// | ||
| /// ```text | ||
| /// warning: passing type `[i32; 10]` to a function with "gpu-kernel" ABI may have unexpected behavior | ||
| /// --> t.rs:2:34 | ||
| /// | | ||
| /// 2 | extern "gpu-kernel" fn kernel(_: [i32; 10]) {} | ||
| /// | ^^^^^^^^^ | ||
| /// | | ||
| /// = help: use primitive types and raw pointers to get reliable behavior | ||
| /// = note: `#[warn(improper_gpu_kernel_arg)]` on by default | ||
| /// ``` | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// The compiler has several checks to verify that types used as arguments in `gpu-kernel` | ||
| /// functions follow certain rules to ensure proper compatibility with the foreign interfaces. | ||
| /// This lint is issued when it detects a probable mistake in a signature. | ||
| IMPROPER_GPU_KERNEL_ARG, | ||
| Warn, | ||
| "GPU kernel entry points have a limited ABI" | ||
| } | ||
|
|
||
| declare_lint! { | ||
| /// The `missing_gpu_kernel_export_name` lint detects `gpu-kernel` functions that have a mangled name. | ||
Flakebi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,ignore (fails on non-GPU targets) | ||
| /// extern "gpu-kernel" fn kernel() { } | ||
| /// ``` | ||
| /// | ||
| /// This will produce: | ||
| /// | ||
| /// ```text | ||
| /// warning: function with the "gpu-kernel" ABI has a mangled name | ||
| /// --> t.rs:1:1 | ||
| /// | | ||
| /// 1 | extern "gpu-kernel" fn kernel() {} | ||
| /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| /// | | ||
| /// = help: use `unsafe(no_mangle)` or `unsafe(export_name = "<name>")` | ||
| /// = note: mangled names make it hard to find the kernel, this is usually not intended | ||
| /// = note: `#[warn(missing_gpu_kernel_export_name)]` on by default | ||
| /// ``` | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// `gpu-kernel` functions are usually searched by name in the compiled file. | ||
| /// A mangled name is usually unintentional as it would need to be searched by the mangled name. | ||
| /// | ||
| /// To use an unmangled name for the kernel, either `no_mangle` or `export_name` can be used. | ||
| /// ```rust,ignore (fails on non-GPU targets) | ||
| /// // Can be found by the name "kernel" | ||
| /// #[unsafe(no_mangle)] | ||
| /// extern "gpu-kernel" fn kernel() { } | ||
| /// | ||
| /// // Can be found by the name "new_name" | ||
| /// #[unsafe(export_name = "new_name")] | ||
| /// extern "gpu-kernel" fn other_kernel() { } | ||
| /// ``` | ||
| MISSING_GPU_KERNEL_EXPORT_NAME, | ||
| Warn, | ||
| "mangled gpu-kernel function" | ||
| } | ||
|
|
||
| declare_lint_pass!(ImproperGpuKernelLint => [ | ||
| IMPROPER_GPU_KERNEL_ARG, | ||
| MISSING_GPU_KERNEL_EXPORT_NAME, | ||
| ]); | ||
|
|
||
| /// `ImproperGpuKernelLint` checks `gpu-kernel` function definitions: | ||
| /// | ||
| /// - `extern "gpu-kernel" fn` arguments should be primitive types. | ||
| /// - `extern "gpu-kernel" fn` should have an unmangled name. | ||
| impl<'tcx> LateLintPass<'tcx> for ImproperGpuKernelLint { | ||
| fn check_fn( | ||
| &mut self, | ||
| cx: &LateContext<'tcx>, | ||
| kind: hir::intravisit::FnKind<'tcx>, | ||
| decl: &'tcx hir::FnDecl<'_>, | ||
| _: &'tcx hir::Body<'_>, | ||
| span: Span, | ||
| id: LocalDefId, | ||
| ) { | ||
| use hir::intravisit::FnKind; | ||
|
|
||
| let abi = match kind { | ||
| FnKind::ItemFn(_, _, header, ..) => header.abi, | ||
| FnKind::Method(_, sig, ..) => sig.header.abi, | ||
| _ => return, | ||
| }; | ||
|
|
||
| if abi != ExternAbi::GpuKernel { | ||
| return; | ||
| } | ||
|
|
||
| let sig = cx.tcx.fn_sig(id).instantiate_identity(); | ||
| let sig = cx.tcx.instantiate_bound_regions_with_erased(sig); | ||
|
|
||
| for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) { | ||
| let is_valid_arg = match input_ty.kind() { | ||
| ty::Bool | ||
| | ty::Char | ||
| | ty::Int(_) | ||
| | ty::Uint(_) | ||
| | ty::Float(_) | ||
| | ty::RawPtr(_, _) => true, | ||
|
|
||
| ty::Adt(_, _) | ||
| | ty::Alias(_, _) | ||
| | ty::Array(_, _) | ||
| | ty::Bound(_, _) | ||
| | ty::Closure(_, _) | ||
| | ty::Coroutine(_, _) | ||
| | ty::CoroutineClosure(_, _) | ||
| | ty::CoroutineWitness(..) | ||
| | ty::Dynamic(_, _) | ||
| | ty::Error(_) | ||
| | ty::FnDef(_, _) | ||
| | ty::FnPtr(..) | ||
| | ty::Foreign(_) | ||
| | ty::Infer(_) | ||
| | ty::Never | ||
| | ty::Param(_) | ||
| | ty::Pat(_, _) | ||
| | ty::Placeholder(_) | ||
| | ty::Ref(_, _, _) | ||
| | ty::Slice(_) | ||
| | ty::Str | ||
| | ty::Tuple(_) | ||
| | ty::UnsafeBinder(_) => false, | ||
| }; | ||
|
|
||
| if !is_valid_arg { | ||
| cx.tcx.emit_node_span_lint( | ||
| IMPROPER_GPU_KERNEL_ARG, | ||
| input_hir.hir_id, | ||
| input_hir.span, | ||
| ImproperGpuKernelArg { ty: *input_ty }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Check for no_mangle/export_name, so the kernel can be found when querying the compiled object for the kernel function by name | ||
| if !find_attr!( | ||
| cx.tcx.get_all_attrs(id), | ||
| AttributeKind::NoMangle(..) | AttributeKind::ExportName { .. } | ||
| ) { | ||
| cx.emit_span_lint(MISSING_GPU_KERNEL_EXPORT_NAME, span, MissingGpuKernelExportName); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we be more specific?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn’t be called from C or the CPU either ;)
See also my other comment, I think nobody can call a gpu-kernel, it can only be “launched” (which is a different thing!).