[clang] [Clang][CUDA] Preserve addresses of scalar grid-constant parameters (PR #231567)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 20 23:50:49 PST 2026
wenhu1024 wrote:
> Good point — you're right that we can't distinguish `¶m` from an explicit local copy reliably in NVPTX from the current IR. Once `&x` becomes `alloca` + `store %x`, it's identical to `int copy = x; mutate(©);` (LLVM IR has no `const` on pointers, so redirecting escaping `alloca`s to read-only `.param` space would miscompile mutable copies), and even for a `const` copy, `© != &x` and `__isGridConstant(©)` must remain `false`.
>
> Looking at the alternatives, keeping scalar args direct would require a frontend-emitted intrinsic (e.g., `ptr addrspace(101) @llvm.nvvm.param.addr(i32 immarg)`) living through the middle-end—essentially resurrecting a heavier version of the `nvvm_internal_addrspace_wrap` intrinsic that `#204192` just removed.
>
> Given that, treating `__grid_constant__` kernel parameters as `byval` as a kernel ABI convention (like NVPTX already does for large `_BitInt`, and matching NVCC and `NVPTXUsage.md`) makes sense to me.
>
> We can simplify the Clang side of this patch noticeably using hooks that are already there:
>
> 1. **Derive grid-constant args from `ABIInfoFD` in `arrangeLLVMFunctionInfo`:**
> `CodeGenTypes::arrangeLLVMFunctionInfo` already receives `const FunctionDecl *ABIInfoFD` for decl-dependent ABI info (see `getX86ABIAVXLevel(ABIInfoFD, info)` right above `findOrInsertCGFunctionInfo`). Computing the grid-constant arg indices there (and forwarding from `signature` in `arrangeCall`) avoids plumbing an extra `gridConstantArgs` parameter through `arrangeFunctionDeclaration`, `arrangeCXXMethodDeclaration`, `arrangeCXXMethodType`, and `arrangeLLVMFunctionInfo`.
> 2. **Check `Arg` directly in `EmitFunctionProlog`:**
> `const VarDecl *Arg` is already in scope in `EmitFunctionProlog`, so checking `ArgI.getIndirectByVal() && Arg->hasAttr<CUDAGridConstantAttr>()` keeps `EmitFunctionProlog` decoupled from `CGFunctionInfo` and also covers scalar types that are already classified as `Indirect` `byval` (like `const __grid_constant__ _BitInt(256)`).
> 3. **Edge cases to add tests for:**
>
> * A `__restrict__` grid-constant pointer parameter (`const int * __restrict__ const __grid_constant__ p`) to make sure pointer parameter attribute emission doesn't assert or misbehave when the parameter becomes `byval(ptr)`.
> * `const __grid_constant__ _BitInt(256)` to confirm it also keeps its parameter address without a local copy.
>
> @AlexMaclean, does this direction look good to you on the NVPTX side?
> Thanks for the review! I’ve pushed the update in `77e5d77444fb` and added regression tests for the `__restrict__` pointer and `_BitInt(256)` cases. Both pass locally.
https://github.com/llvm/llvm-project/pull/231567
More information about the cfe-commits
mailing list