-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -133,6 +133,10 @@ hir_typeck_fru_suggestion = | |||||||||
| hir_typeck_functional_record_update_on_non_struct = | ||||||||||
| functional record update syntax requires a struct | ||||||||||
|
|
||||||||||
| hir_typeck_gpu_kernel_abi_cannot_be_called = | ||||||||||
| functions with the "gpu-kernel" ABI cannot be called | ||||||||||
| .note = an `extern "gpu-kernel"` function can only be launched on the GPU through an API | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure "can" is right? Maybe "should"? And "an API" is somewhat vague, what kind of API? Surely not a standard library one, for instance...
Suggested change
Or "must"? ...I don't know if "kernel loader" is a real term for the thing that the GPU driver "does", it just came up intuitively while trying to grasp for words for the thing.
Suggested change
|
||||||||||
|
|
||||||||||
| hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml` | ||||||||||
| hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc` | ||||||||||
|
|
||||||||||
|
|
||||||||||
| 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, | ||||||
| "simple arguments of gpu-kernel functions" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
| } | ||||||
|
|
||||||
| declare_lint! { | ||||||
| /// The `missing_gpu_kernel_export_name` lint detects `gpu-kernel` functions that have a mangled name. | ||||||
|
Comment on lines
+48
to
+49
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we can't simply use an unmangled name by default because that would be |
||||||
| /// | ||||||
| /// ### 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 simple. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid "simple", even in a concise format like this: we mean "primitive types".
Suggested change
|
||||||
| /// - `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); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
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?