Skip to content
Closed
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
66 changes: 32 additions & 34 deletions flang/lib/Lower/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,21 @@ class AllocateStmtHelper {
void genSimpleAllocation(const Allocation &alloc,
const fir::MutableBoxValue &box) {
bool isCudaSymbol = Fortran::semantics::HasCUDAAttr(alloc.getSymbol());
bool isCudaDeviceContext = cuf::isCUDADeviceContext(builder.getRegion());
bool inlineAllocation = !box.isDerived() && !errorManager.hasStatSpec() &&
!alloc.type.IsPolymorphic() &&
!alloc.hasCoarraySpec() && !useAllocateRuntime &&
!box.isPointer();

unsigned allocatorIdx = Fortran::lower::getAllocatorIdx(alloc.getSymbol());

if (inlineAllocation &&
((isCudaSymbol && isCudaDeviceContext) || !isCudaSymbol)) {
// Pointers must use PointerAllocate so that their deallocations
// can be validated.
genInlinedAllocation(alloc, box);
postAllocationAction(alloc);
setPinnedToFalse();
return;
if (isCudaSymbol) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want want inline allocation for non cuda symbol as well.

bool inlineAllocation = !box.isDerived() && !errorManager.hasStatSpec() &&
!alloc.type.IsPolymorphic() &&
!alloc.hasCoarraySpec() && !useAllocateRuntime &&
!box.isPointer();
bool isCudaDeviceContext = cuf::isCUDADeviceContext(builder.getRegion());
if (inlineAllocation && isCudaDeviceContext) {
genInlinedAllocation(alloc, box);
postAllocationAction(alloc);
setPinnedToFalse();
return;
}
}

// Generate a sequence of runtime calls.
Expand Down Expand Up @@ -863,29 +863,27 @@ genDeallocate(fir::FirOpBuilder &builder,
const Fortran::semantics::Symbol *symbol = nullptr) {
bool isCudaSymbol = symbol && Fortran::semantics::HasCUDAAttr(*symbol);
bool isCudaDeviceContext = cuf::isCUDADeviceContext(builder.getRegion());
bool inlineDeallocation =
!box.isDerived() && !box.isPolymorphic() && !box.hasAssumedRank() &&
!box.isUnlimitedPolymorphic() && !errorManager.hasStatSpec() &&
!useAllocateRuntime && !box.isPointer();
// Deallocate intrinsic types inline.
if (inlineDeallocation &&
((isCudaSymbol && isCudaDeviceContext) || !isCudaSymbol)) {
// Pointers must use PointerDeallocate so that their deallocations
// can be validated.
mlir::Value ret = fir::factory::genFreemem(builder, loc, box);
if (symbol)
postDeallocationAction(converter, builder, *symbol);
return ret;
}
// Use runtime calls to deallocate descriptor cases. Sync MutableBoxValue
// with its descriptor before and after calls if needed.
errorManager.genStatCheck(builder, loc);
mlir::Value stat;
if (!isCudaSymbol)
mlir::Value stat = nullptr;
if (!isCudaSymbol) {
// For non-CUDA symbols, always use runtime deallocation.
errorManager.genStatCheck(builder, loc);
stat =
genRuntimeDeallocate(builder, loc, box, errorManager, declaredTypeDesc);
else
stat = genCudaDeallocate(builder, loc, box, errorManager, *symbol);
} else {
bool inlineDeallocation =
!box.isDerived() && !box.isPolymorphic() && !box.hasAssumedRank() &&
!box.isUnlimitedPolymorphic() && !errorManager.hasStatSpec() &&
!useAllocateRuntime && !box.isPointer();

if (inlineDeallocation && isCudaDeviceContext) {
// Inline deallocation for CUDA when conditions hold.
stat = fir::factory::genFreemem(builder, loc, box);
} else {
// Otherwise, use the CUDA-specific runtime deallocation.
errorManager.genStatCheck(builder, loc);
stat = genCudaDeallocate(builder, loc, box, errorManager, *symbol);
}
}
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
if (symbol)
postDeallocationAction(converter, builder, *symbol);
Expand Down
4 changes: 2 additions & 2 deletions flang/test/HLFIR/dummy_deallocation.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
! is not deallocated in entry SUB_B.

! CHECK-LABEL: func.func @_QPsub_a
! CHECK: fir.freemem
! CHECK: fir.call @_FortranAAllocatableDeallocate

! CHECK-LABEL: func.func @_QPsub_b
! CHECK-NOT: fir.freemem
! CHECK-NOT: fir.call @_FortranAAllocatableDeallocate
SUBROUTINE SUB_A(A)
INTEGER, INTENT(out), ALLOCATABLE, DIMENSION (:) :: A
RETURN
Expand Down
3 changes: 2 additions & 1 deletion flang/test/Lower/CUDA/cuda-allocatable.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end subroutine
! CHECK: fir.call @_FortranAAllocatableSetBounds
! CHECK: %{{.*}} = cuf.allocate %[[BOX_A_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> source(%[[LOAD_B]] : !fir.box<!fir.heap<!fir.array<?xf32>>>) {data_attr = #cuf.cuda<device>} -> i32
! CHECK: fir.if
! CHECK: fir.freemem
! CHECK: fir.call @_FortranAAllocatableDeallocate
! CHECK: fir.if %{{.*}} {
! CHECK: %{{.*}} = cuf.deallocate %[[BOX_A_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {data_attr = #cuf.cuda<device>} -> i32
! CHECK: }
Expand Down Expand Up @@ -206,6 +206,7 @@ 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: fir.call @_FortranAAllocatableAllocate
! CHECK: %[[FALSE:.*]] = arith.constant false
! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#0 : !fir.ref<!fir.logical<4>>
Expand Down
Loading
Loading