|
| 1 | +use std::iter; |
| 2 | + |
| 3 | +use rustc_abi::ExternAbi; |
| 4 | +use rustc_hir::attrs::AttributeKind; |
| 5 | +use rustc_hir::{self as hir, find_attr}; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 8 | +use rustc_span::Span; |
| 9 | +use rustc_span::def_id::LocalDefId; |
| 10 | + |
| 11 | +use crate::lints::{ImproperGpuKernelArg, MissingGpuKernelExportName}; |
| 12 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 13 | + |
| 14 | +declare_lint! { |
| 15 | + /// The `improper_gpu_kernel_arg` lint detects incorrect use of types in `gpu-kernel` |
| 16 | + /// arguments. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// |
| 20 | + /// ```rust,ignore (fails on non-GPU targets) |
| 21 | + /// #[unsafe(no_mangle)] |
| 22 | + /// extern "gpu-kernel" fn kernel(_: [i32; 10]) {} |
| 23 | + /// ``` |
| 24 | + /// |
| 25 | + /// This will produce: |
| 26 | + /// |
| 27 | + /// ```text |
| 28 | + /// warning: passing type `[i32; 10]` to a function with "gpu-kernel" ABI may have unexpected behavior |
| 29 | + /// --> t.rs:2:34 |
| 30 | + /// | |
| 31 | + /// 2 | extern "gpu-kernel" fn kernel(_: [i32; 10]) {} |
| 32 | + /// | ^^^^^^^^^ |
| 33 | + /// | |
| 34 | + /// = help: use primitive types and raw pointers to get reliable behavior |
| 35 | + /// = note: `#[warn(improper_gpu_kernel_arg)]` on by default |
| 36 | + /// ``` |
| 37 | + /// |
| 38 | + /// ### Explanation |
| 39 | + /// |
| 40 | + /// The compiler has several checks to verify that types used as arguments in `gpu-kernel` |
| 41 | + /// functions follow certain rules to ensure proper compatibility with the foreign interfaces. |
| 42 | + /// This lint is issued when it detects a probable mistake in a signature. |
| 43 | + IMPROPER_GPU_KERNEL_ARG, |
| 44 | + Warn, |
| 45 | + "simple arguments of gpu-kernel functions" |
| 46 | +} |
| 47 | + |
| 48 | +declare_lint! { |
| 49 | + /// The `missing_gpu_kernel_export_name` lint detects `gpu-kernel` functions that have a mangled name. |
| 50 | + /// |
| 51 | + /// ### Example |
| 52 | + /// |
| 53 | + /// ```rust,ignore (fails on non-GPU targets) |
| 54 | + /// extern "gpu-kernel" fn kernel() { } |
| 55 | + /// ``` |
| 56 | + /// |
| 57 | + /// This will produce: |
| 58 | + /// |
| 59 | + /// ```text |
| 60 | + /// warning: function with the "gpu-kernel" ABI has a mangled name |
| 61 | + /// --> t.rs:1:1 |
| 62 | + /// | |
| 63 | + /// 1 | extern "gpu-kernel" fn kernel() {} |
| 64 | + /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 65 | + /// | |
| 66 | + /// = help: use `unsafe(no_mangle)` or `unsafe(export_name = "<name>")` |
| 67 | + /// = note: mangled names make it hard to find the kernel, this is usually not intended |
| 68 | + /// = note: `#[warn(missing_gpu_kernel_export_name)]` on by default |
| 69 | + /// ``` |
| 70 | + /// |
| 71 | + /// ### Explanation |
| 72 | + /// |
| 73 | + /// `gpu-kernel` functions are usually searched by name in the compiled file. |
| 74 | + /// A mangled name is usually unintentional as it would need to be searched by the mangled name. |
| 75 | + /// |
| 76 | + /// To use an unmangled name for the kernel, either `no_mangle` or `export_name` can be used. |
| 77 | + /// ```rust,ignore (fails on non-GPU targets) |
| 78 | + /// // Can be found by the name "kernel" |
| 79 | + /// #[unsafe(no_mangle)] |
| 80 | + /// extern "gpu-kernel" fn kernel() { } |
| 81 | + /// |
| 82 | + /// // Can be found by the name "new_name" |
| 83 | + /// #[unsafe(export_name = "new_name")] |
| 84 | + /// extern "gpu-kernel" fn other_kernel() { } |
| 85 | + /// ``` |
| 86 | + MISSING_GPU_KERNEL_EXPORT_NAME, |
| 87 | + Warn, |
| 88 | + "mangled gpu-kernel function" |
| 89 | +} |
| 90 | + |
| 91 | +declare_lint_pass!(ImproperGpuKernelLint => [ |
| 92 | + IMPROPER_GPU_KERNEL_ARG, |
| 93 | + MISSING_GPU_KERNEL_EXPORT_NAME, |
| 94 | +]); |
| 95 | + |
| 96 | +/// `ImproperGpuKernelLint` checks `gpu-kernel` function definitions: |
| 97 | +/// |
| 98 | +/// - `extern "gpu-kernel" fn` arguments should be simple. |
| 99 | +/// - `extern "gpu-kernel" fn` should have an unmangled name. |
| 100 | +impl<'tcx> LateLintPass<'tcx> for ImproperGpuKernelLint { |
| 101 | + fn check_fn( |
| 102 | + &mut self, |
| 103 | + cx: &LateContext<'tcx>, |
| 104 | + kind: hir::intravisit::FnKind<'tcx>, |
| 105 | + decl: &'tcx hir::FnDecl<'_>, |
| 106 | + _: &'tcx hir::Body<'_>, |
| 107 | + span: Span, |
| 108 | + id: LocalDefId, |
| 109 | + ) { |
| 110 | + use hir::intravisit::FnKind; |
| 111 | + |
| 112 | + let abi = match kind { |
| 113 | + FnKind::ItemFn(_, _, header, ..) => header.abi, |
| 114 | + FnKind::Method(_, sig, ..) => sig.header.abi, |
| 115 | + _ => return, |
| 116 | + }; |
| 117 | + |
| 118 | + if abi != ExternAbi::GpuKernel { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + let sig = cx.tcx.fn_sig(id).instantiate_identity(); |
| 123 | + let sig = cx.tcx.instantiate_bound_regions_with_erased(sig); |
| 124 | + |
| 125 | + for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) { |
| 126 | + let is_valid_arg = match input_ty.kind() { |
| 127 | + ty::Bool |
| 128 | + | ty::Char |
| 129 | + | ty::Int(_) |
| 130 | + | ty::Uint(_) |
| 131 | + | ty::Float(_) |
| 132 | + | ty::RawPtr(_, _) => true, |
| 133 | + |
| 134 | + ty::Adt(_, _) |
| 135 | + | ty::Alias(_, _) |
| 136 | + | ty::Array(_, _) |
| 137 | + | ty::Bound(_, _) |
| 138 | + | ty::Closure(_, _) |
| 139 | + | ty::Coroutine(_, _) |
| 140 | + | ty::CoroutineClosure(_, _) |
| 141 | + | ty::CoroutineWitness(..) |
| 142 | + | ty::Dynamic(_, _) |
| 143 | + | ty::Error(_) |
| 144 | + | ty::FnDef(_, _) |
| 145 | + | ty::FnPtr(..) |
| 146 | + | ty::Foreign(_) |
| 147 | + | ty::Infer(_) |
| 148 | + | ty::Never |
| 149 | + | ty::Param(_) |
| 150 | + | ty::Pat(_, _) |
| 151 | + | ty::Placeholder(_) |
| 152 | + | ty::Ref(_, _, _) |
| 153 | + | ty::Slice(_) |
| 154 | + | ty::Str |
| 155 | + | ty::Tuple(_) |
| 156 | + | ty::UnsafeBinder(_) => false, |
| 157 | + }; |
| 158 | + |
| 159 | + if !is_valid_arg { |
| 160 | + cx.tcx.emit_node_span_lint( |
| 161 | + IMPROPER_GPU_KERNEL_ARG, |
| 162 | + input_hir.hir_id, |
| 163 | + input_hir.span, |
| 164 | + ImproperGpuKernelArg { ty: *input_ty }, |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Check for no_mangle/export_name, so the kernel can be found when querying the compiled object for the kernel function by name |
| 170 | + if !find_attr!( |
| 171 | + cx.tcx.get_all_attrs(id), |
| 172 | + AttributeKind::NoMangle(..) | AttributeKind::ExportName { .. } |
| 173 | + ) { |
| 174 | + cx.emit_span_lint(MISSING_GPU_KERNEL_EXPORT_NAME, span, MissingGpuKernelExportName); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments