Skip to content

[flang][cuda] Make sure allocator id is set for pointer allocate #129950

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

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion flang/include/flang/Lower/Cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Fortran::lower {
// for it.
// If the insertion point is inside an OpenACC region op, it is considered
// device context.
static bool isCudaDeviceContext(fir::FirOpBuilder &builder) {
static bool inline isCudaDeviceContext(fir::FirOpBuilder &builder) {
if (builder.getRegion().getParentOfType<cuf::KernelOp>())
return true;
if (builder.getRegion()
Expand All @@ -41,6 +41,23 @@ static bool isCudaDeviceContext(fir::FirOpBuilder &builder) {
}
return false;
}

static inline unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
std::optional<Fortran::common::CUDADataAttr> cudaAttr =
Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate());
if (cudaAttr) {
if (*cudaAttr == Fortran::common::CUDADataAttr::Pinned)
return kPinnedAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Device)
return kDeviceAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Managed)
return kManagedAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Unified)
return kUnifiedAllocatorPos;
}
return kDefaultAllocator;
}

} // end namespace Fortran::lower

#endif // FORTRAN_LOWER_CUDA_H
14 changes: 9 additions & 5 deletions flang/lib/Lower/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ class AllocateStmtHelper {
!alloc.type.IsPolymorphic() &&
!alloc.hasCoarraySpec() && !useAllocateRuntime &&
!box.isPointer();
unsigned allocatorIdx = Fortran::lower::getAllocatorIdx(alloc.getSymbol());

if (inlineAllocation &&
((isCudaSymbol && isCudaDeviceContext) || !isCudaSymbol)) {
Expand All @@ -488,7 +489,7 @@ class AllocateStmtHelper {

// Generate a sequence of runtime calls.
errorManager.genStatCheck(builder, loc);
genAllocateObjectInit(box);
genAllocateObjectInit(box, allocatorIdx);
if (alloc.hasCoarraySpec())
TODO(loc, "coarray: allocation of a coarray object");
if (alloc.type.IsPolymorphic())
Expand Down Expand Up @@ -549,14 +550,16 @@ class AllocateStmtHelper {
TODO(loc, "derived type length parameters in allocate");
}

void genAllocateObjectInit(const fir::MutableBoxValue &box) {
void genAllocateObjectInit(const fir::MutableBoxValue &box,
unsigned allocatorIdx) {
if (box.isPointer()) {
// For pointers, the descriptor may still be uninitialized (see Fortran
// 2018 19.5.2.2). The allocation runtime needs to be given a descriptor
// with initialized rank, types and attributes. Initialize the descriptor
// here to ensure these constraints are fulfilled.
mlir::Value nullPointer = fir::factory::createUnallocatedBox(
builder, loc, box.getBoxTy(), box.nonDeferredLenParams());
builder, loc, box.getBoxTy(), box.nonDeferredLenParams(),
/*typeSourceBox=*/{}, allocatorIdx);
builder.create<fir::StoreOp>(loc, nullPointer, box.getAddr());
} else {
assert(box.isAllocatable() && "must be an allocatable");
Expand Down Expand Up @@ -612,11 +615,12 @@ class AllocateStmtHelper {

void genSourceMoldAllocation(const Allocation &alloc,
const fir::MutableBoxValue &box, bool isSource) {
unsigned allocatorIdx = Fortran::lower::getAllocatorIdx(alloc.getSymbol());
fir::ExtendedValue exv = isSource ? sourceExv : moldExv;
;

// Generate a sequence of runtime calls.
errorManager.genStatCheck(builder, loc);
genAllocateObjectInit(box);
genAllocateObjectInit(box, allocatorIdx);
if (alloc.hasCoarraySpec())
TODO(loc, "coarray: allocation of a coarray object");
// Set length of the allocate object if it has. Otherwise, get the length
Expand Down
19 changes: 2 additions & 17 deletions flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "flang/Lower/ConvertExpr.h"
#include "flang/Lower/ConvertExprToHLFIR.h"
#include "flang/Lower/ConvertProcedureDesignator.h"
#include "flang/Lower/Cuda.h"
#include "flang/Lower/Mangler.h"
#include "flang/Lower/PFTBuilder.h"
#include "flang/Lower/StatementContext.h"
Expand Down Expand Up @@ -1985,22 +1986,6 @@ static void genBoxDeclare(Fortran::lower::AbstractConverter &converter,
replace);
}

static unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
std::optional<Fortran::common::CUDADataAttr> cudaAttr =
Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate());
if (cudaAttr) {
if (*cudaAttr == Fortran::common::CUDADataAttr::Pinned)
return kPinnedAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Device)
return kDeviceAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Managed)
return kManagedAllocatorPos;
if (*cudaAttr == Fortran::common::CUDADataAttr::Unified)
return kUnifiedAllocatorPos;
}
return kDefaultAllocator;
}

/// Lower specification expressions and attributes of variable \p var and
/// add it to the symbol map. For a global or an alias, the address must be
/// pre-computed and provided in \p preAlloc. A dummy argument for the current
Expand Down Expand Up @@ -2091,7 +2076,7 @@ void Fortran::lower::mapSymbolAttributes(
converter, loc, var, boxAlloc, nonDeferredLenParams,
/*alwaysUseBox=*/
converter.getLoweringOptions().getLowerToHighLevelFIR(),
getAllocatorIdx(var.getSymbol()));
Fortran::lower::getAllocatorIdx(var.getSymbol()));
genAllocatableOrPointerDeclare(converter, symMap, var.getSymbol(), box,
replace);
return;
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Lower/CUDA/cuda-pointer.cuf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Test lowering of CUDA pointers.

subroutine allocate_pointer
real, device, pointer :: pr(:)
allocate(pr(10))
end

! CHECK-LABEL: func.func @_QPallocate_pointer()
! CHECK-COUNT-2: fir.embox %{{.*}} {allocator_idx = 2 : i32} : (!fir.ptr<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>