@@ -799,17 +799,40 @@ static void postDeallocationAction(Fortran::lower::AbstractConverter &converter,
799
799
Fortran::lower::attachDeclarePostDeallocAction (converter, builder, sym);
800
800
}
801
801
802
+ static mlir::Value genCudaDeallocate (fir::FirOpBuilder &builder,
803
+ mlir::Location loc,
804
+ const fir::MutableBoxValue &box,
805
+ ErrorManager &errorManager,
806
+ const Fortran::semantics::Symbol &sym) {
807
+ fir::CUDADataAttributeAttr cudaAttr =
808
+ Fortran::lower::translateSymbolCUDADataAttribute (builder.getContext (),
809
+ sym);
810
+ mlir::Value errmsg =
811
+ mlir::isa<fir::AbsentOp>(errorManager.errMsgAddr .getDefiningOp ())
812
+ ? nullptr
813
+ : errorManager.errMsgAddr ;
814
+
815
+ // Keep return type the same as a standard AllocatableAllocate call.
816
+ mlir::Type retTy = fir::runtime::getModel<int >()(builder.getContext ());
817
+ return builder
818
+ .create <fir::CUDADeallocateOp>(
819
+ loc, retTy, box.getAddr (), errmsg, cudaAttr,
820
+ errorManager.hasStatSpec () ? builder.getUnitAttr () : nullptr )
821
+ .getResult ();
822
+ }
823
+
802
824
// Generate deallocation of a pointer/allocatable.
803
825
static mlir::Value
804
826
genDeallocate (fir::FirOpBuilder &builder,
805
827
Fortran::lower::AbstractConverter &converter, mlir::Location loc,
806
828
const fir::MutableBoxValue &box, ErrorManager &errorManager,
807
829
mlir::Value declaredTypeDesc = {},
808
830
const Fortran::semantics::Symbol *symbol = nullptr ) {
831
+ bool isCudaSymbol = symbol && Fortran::semantics::HasCUDAAttr (*symbol);
809
832
// Deallocate intrinsic types inline.
810
833
if (!box.isDerived () && !box.isPolymorphic () &&
811
834
!box.isUnlimitedPolymorphic () && !errorManager.hasStatSpec () &&
812
- !useAllocateRuntime && !box.isPointer ()) {
835
+ !useAllocateRuntime && !box.isPointer () && !isCudaSymbol ) {
813
836
// Pointers must use PointerDeallocate so that their deallocations
814
837
// can be validated.
815
838
mlir::Value ret = fir::factory::genFreemem (builder, loc, box);
@@ -820,8 +843,12 @@ genDeallocate(fir::FirOpBuilder &builder,
820
843
// Use runtime calls to deallocate descriptor cases. Sync MutableBoxValue
821
844
// with its descriptor before and after calls if needed.
822
845
errorManager.genStatCheck (builder, loc);
823
- mlir::Value stat =
824
- genRuntimeDeallocate (builder, loc, box, errorManager, declaredTypeDesc);
846
+ mlir::Value stat;
847
+ if (!isCudaSymbol)
848
+ stat =
849
+ genRuntimeDeallocate (builder, loc, box, errorManager, declaredTypeDesc);
850
+ else
851
+ stat = genCudaDeallocate (builder, loc, box, errorManager, *symbol);
825
852
fir::factory::syncMutableBoxFromIRBox (builder, loc, box);
826
853
if (symbol)
827
854
postDeallocationAction (converter, builder, *symbol);
0 commit comments