Artem-B wrote:

Good point — you're right that we can't distinguish `&param` 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(&copy);` (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, `&copy != 
&x` and `__isGridConstant(&copy)` 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?

https://github.com/llvm/llvm-project/pull/231567
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to