[flang-commits] [flang] [flang][cuda] Allocate descriptors in managed memory only when the device reads them (PR #234579)

via flang-commits flang-commits at lists.llvm.org
Thu Oct 22 04:12:05 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Matsu (khaki3)

<details>
<summary>Changes</summary>

Example:
```fortran
subroutine s(x)
  real, device :: x(:)
end subroutine

real, device, allocatable :: a(:)
allocate(a(8))
do i = 1, n
  call s(a)
end do
```

In this code, each `call s(a)` builds a temporary descriptor for `a`. Codegen stores a temporary descriptor either in an `alloca` or, when `isDeviceAllocation` says the data is device-resident, in managed memory from `CUFAllocDescriptor`. A managed descriptor may be read by an asynchronous kernel, so codegen emits no free for it. This one takes the managed path even though no kernel reads it, so every call leaks one.

Fix: allocate a descriptor with `CUFAllocDescriptor` only when the device reads it, meaning it reaches a `gpu.launch_func` or a device dummy of a BIND(C) procedure. Otherwise it uses an `alloca`.
- `isDeviceAllocation` is removed. Where the data lives doesn't matter, and a kernel launched on a dummy gets its own managed copy from the callee's rebox.
- The OpenACC data-clause check is removed, since a box used only by `acc` data clauses now stays on the stack anyway.
- Lowering records the CUDA data attribute of BIND(C) box arguments on the `fir.call`, so codegen sees it for direct and indirect calls.
- TargetRewrite now keeps a rewritten call's procedure attributes and the attributes of arguments passed unchanged. It used to drop them, which lost the BIND(C) marking when a call also passed, for example, a complex by value.

---
Full diff: https://github.com/llvm/llvm-project/pull/234579.diff


8 Files Affected:

- (modified) flang/include/flang/Lower/CallInterface.h (+5) 
- (modified) flang/lib/Lower/ConvertCall.cpp (+34-2) 
- (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+39-115) 
- (modified) flang/lib/Optimizer/CodeGen/TargetRewrite.cpp (+23-5) 
- (modified) flang/test/Fir/CUDA/cuda-code-gen.mlir (+103-24) 
- (added) flang/test/Fir/target-rewrite-call-attrs.fir (+22) 
- (modified) flang/test/Fir/target-rewrite-indirect-calls.fir (+2-2) 
- (added) flang/test/Lower/CUDA/cuda-bindc-call-data-attr.cuf (+45) 


``````````diff
The server is unavailable at this time. Please wait a few minutes before you try again.
``````````

</details>


https://github.com/llvm/llvm-project/pull/234579


More information about the flang-commits mailing list