Skip to content

[flang][cuda] Set PINNED variable to false in ALLOCATE #121593

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
Jan 3, 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
33 changes: 26 additions & 7 deletions flang/lib/Lower/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,19 @@ class AllocateStmtHelper {
alloc.getSymbol());
}

void setPinnedToFalse() {
if (!pinnedExpr)
return;
Fortran::lower::StatementContext stmtCtx;
mlir::Value pinned =
fir::getBase(converter.genExprAddr(loc, *pinnedExpr, stmtCtx));
mlir::Location loc = pinned.getLoc();
mlir::Value falseValue = builder.createBool(loc, false);
mlir::Value falseConv = builder.createConvert(
loc, fir::unwrapRefType(pinned.getType()), falseValue);
builder.create<fir::StoreOp>(loc, falseConv, pinned);
}

void genSimpleAllocation(const Allocation &alloc,
const fir::MutableBoxValue &box) {
bool isCudaSymbol = Fortran::semantics::HasCUDAAttr(alloc.getSymbol());
Expand All @@ -469,6 +482,7 @@ class AllocateStmtHelper {
// can be validated.
genInlinedAllocation(alloc, box);
postAllocationAction(alloc);
setPinnedToFalse();
return;
}

Expand All @@ -482,11 +496,13 @@ class AllocateStmtHelper {
genSetDeferredLengthParameters(alloc, box);
genAllocateObjectBounds(alloc, box);
mlir::Value stat;
if (!isCudaSymbol)
if (!isCudaSymbol) {
stat = genRuntimeAllocate(builder, loc, box, errorManager);
else
setPinnedToFalse();
} else {
stat =
genCudaAllocate(builder, loc, box, errorManager, alloc.getSymbol());
}
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
postAllocationAction(alloc);
errorManager.assignStat(builder, loc, stat);
Expand Down Expand Up @@ -616,13 +632,16 @@ class AllocateStmtHelper {
genSetDeferredLengthParameters(alloc, box);
genAllocateObjectBounds(alloc, box);
mlir::Value stat;
if (Fortran::semantics::HasCUDAAttr(alloc.getSymbol()))
if (Fortran::semantics::HasCUDAAttr(alloc.getSymbol())) {
stat =
genCudaAllocate(builder, loc, box, errorManager, alloc.getSymbol());
else if (isSource)
stat = genRuntimeAllocateSource(builder, loc, box, exv, errorManager);
else
stat = genRuntimeAllocate(builder, loc, box, errorManager);
} else {
if (isSource)
stat = genRuntimeAllocateSource(builder, loc, box, exv, errorManager);
else
stat = genRuntimeAllocate(builder, loc, box, errorManager);
setPinnedToFalse();
}
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
postAllocationAction(alloc);
errorManager.assignStat(builder, loc, stat);
Expand Down
27 changes: 27 additions & 0 deletions flang/test/Lower/CUDA/cuda-allocatable.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,30 @@ end subroutine
! CHECK: %[[BOX:.*]] = fir.load %[[A]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
! CHECK: %[[BOXADDR:.*]] = fir.box_addr %[[BOX]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
! CHECK: fir.freemem %[[BOXADDR]] : !fir.heap<!fir.array<?xf32>>

subroutine setpinned()
integer, allocatable :: i(:)
logical :: plog
allocate(i(10), pinned=plog)
end

! CHECK-LABEL: func.func @_QPsetpinned()
! CHECK: %[[PLOG:.*]] = fir.alloca !fir.logical<4> {bindc_name = "plog", uniq_name = "_QFsetpinnedEplog"}
! CHECK: %[[PLOG_DECL:.*]]:2 = hlfir.declare %[[PLOG]] {uniq_name = "_QFsetpinnedEplog"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
! CHECK: %[[FALSE:.*]] = arith.constant false
! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#1 : !fir.ref<!fir.logical<4>>

subroutine setpinnedpointer()
integer, pointer :: i(:)
logical :: plog
allocate(i(10), pinned=plog)
end

! CHECK-LABEL: func.func @_QPsetpinnedpointer()
! CHECK: %[[PLOG:.*]] = fir.alloca !fir.logical<4> {bindc_name = "plog", uniq_name = "_QFsetpinnedpointerEplog"}
! CHECK: %[[PLOG_DECL:.*]]:2 = hlfir.declare %[[PLOG]] {uniq_name = "_QFsetpinnedpointerEplog"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
! CHECK: fir.call @_FortranAPointerAllocate
! CHECK: %[[FALSE:.*]] = arith.constant false
! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#1 : !fir.ref<!fir.logical<4>>
Loading