Skip to content

Commit 1241096

Browse files
committed
Implement workaround for LLVM runtime error when deallocate() is called on pointer member inside ESMF objects returned from C++.
1 parent ee2798f commit 1241096

File tree

6 files changed

+58
-35
lines changed

6 files changed

+58
-35
lines changed

src/Infrastructure/Field/interface/ESMF_Field_C.F90

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,27 +736,30 @@ subroutine f_esmf_fieldcollectgarbage(field, rc)
736736

737737
type(ESMF_Field) :: field
738738
integer, intent(out) :: rc
739-
739+
740+
type(ESMF_FieldType), pointer :: ftypepp
740741
integer :: localrc
741-
742+
742743
! initialize return code; assume routine not implemented
743744
localrc = ESMF_RC_NOT_IMPL
744745
rc = ESMF_RC_NOT_IMPL
745-
746+
746747
!print *, "collecting Field garbage"
747748

748-
if (associated(field%ftypep)) then
749+
ftypepp => field%ftypep ! LLVM workaround for deallocate() runtime error!
750+
751+
if (associated(ftypepp)) then
749752
! destruct internal data allocations
750-
call ESMF_FieldDestruct(field%ftypep, rc=localrc)
753+
call ESMF_FieldDestruct(ftypepp, rc=localrc)
751754
if (ESMF_LogFoundError(localrc, &
752755
ESMF_ERR_PASSTHRU, &
753756
ESMF_CONTEXT, rcToReturn=rc)) return
754757
! deallocate actual FieldType allocation
755-
!print *, "deallocate(field%ftypep)"
756-
deallocate(field%ftypep, stat=localrc)
758+
deallocate(ftypepp, stat=localrc)
757759
if (ESMF_LogFoundDeallocError(localrc, msg="Deallocating Field", &
758760
ESMF_CONTEXT, rcToReturn=rc)) return
759761
endif
762+
760763
nullify(field%ftypep)
761764

762765
! return successfully

src/Infrastructure/FieldBundle/interface/ESMF_FieldBundle_C.F90

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,32 @@ subroutine f_esmf_fbundlecollectgarbage(fb, rc)
7878

7979
type(ESMF_FieldBundle):: fb
8080
integer, intent(out) :: rc
81-
81+
82+
type(ESMF_FieldBundleType), pointer :: fbpp
8283
integer :: localrc
83-
84+
8485
! initialize return code; assume routine not implemented
8586
localrc = ESMF_RC_NOT_IMPL
8687
rc = ESMF_RC_NOT_IMPL
8788

8889
!print *, "collecting FieldBundle garbage"
89-
90-
if (associated(fb%this)) then
90+
91+
fbpp => fb%this ! LLVM workaround for deallocate() runtime error!
92+
93+
if (associated(fbpp)) then
9194
! destruct internal data allocations
92-
call ESMF_FieldBundleDestruct(fb%this, localrc)
95+
call ESMF_FieldBundleDestruct(fbpp, rc=localrc)
9396
if (ESMF_LogFoundError(localrc, &
9497
ESMF_ERR_PASSTHRU, &
9598
ESMF_CONTEXT, &
9699
rcToReturn=rc)) return
97100
! deallocate actual FieldBundleType allocation
98-
deallocate(fb%this, stat=localrc)
101+
deallocate(fbpp, stat=localrc)
99102
if (ESMF_LogFoundDeallocError(localrc, msg="Deallocating FieldBundle", &
100103
ESMF_CONTEXT, &
101104
rcToReturn=rc)) return
102105
endif
106+
103107
nullify(fb%this)
104108

105109
! return successfully

src/Infrastructure/LocStream/interface/ESMF_LocStream_C.F90

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,32 @@ subroutine f_esmf_locstreamcollectgarbage(locstream, rc)
195195
implicit none
196196

197197
type(ESMF_LocStream) :: locstream
198-
integer, intent(out) :: rc
199-
200-
integer :: localrc
201-
198+
integer, intent(out) :: rc
199+
200+
type(ESMF_LocStreamType), pointer :: lspp
201+
integer :: localrc
202+
202203
! initialize return code; assume routine not implemented
203204
localrc = ESMF_RC_NOT_IMPL
204205
rc = ESMF_RC_NOT_IMPL
205-
206+
206207
!print *, "collecting LocStream garbage"
207-
208+
209+
lspp => locstream%lstypep ! LLVM workaround for deallocate() runtime error!
210+
208211
! destruct internal data allocations
209-
call ESMF_LocStreamDestruct(locstream%lstypep, rc=localrc)
212+
call ESMF_LocStreamDestruct(lspp, rc=localrc)
210213
if (ESMF_LogFoundError(localrc, &
211214
ESMF_ERR_PASSTHRU, &
212215
ESMF_CONTEXT, rcToReturn=rc)) return
213216

214217
! deallocate actual LocStreamType allocation
215-
if (associated(locstream%lstypep)) then
216-
deallocate(locstream%lstypep, stat=localrc)
218+
if (associated(lspp)) then
219+
deallocate(lspp, stat=localrc)
217220
if (ESMF_LogFoundDeallocError(localrc, msg="Deallocating LocStream", &
218221
ESMF_CONTEXT, rcToReturn=rc)) return
219222
endif
223+
220224
nullify(locstream%lstypep)
221225

222226
! return successfully

src/Infrastructure/XGridGeomBase/src/ESMF_XGridGeomBase.F90

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ subroutine f_esmf_xgridgeombasecolgarbage(gb, rc)
13621362
type(ESMF_XGridGeomBase) :: gb
13631363
integer, intent(out) :: rc
13641364

1365+
type(ESMF_XGridGeomBaseClass), pointer :: gbcpp
13651366
integer :: localrc
13661367

13671368
! initialize return code; assume routine not implemented
@@ -1370,12 +1371,15 @@ subroutine f_esmf_xgridgeombasecolgarbage(gb, rc)
13701371

13711372
!print *, "collecting GeomBase garbage"
13721373

1374+
gbcpp => gb%gbcp ! LLVM workaround for deallocate() runtime error!
1375+
13731376
! deallocate actual GeomBaseClass allocation
1374-
if (associated(gb%gbcp)) then
1375-
deallocate(gb%gbcp, stat=localrc)
1377+
if (associated(gbcpp)) then
1378+
deallocate(gbcpp, stat=localrc)
13761379
if (ESMF_LogFoundAllocError(localrc, msg="Deallocating GeomBase", &
13771380
ESMF_CONTEXT, rcToReturn=rc)) return
13781381
endif
1382+
13791383
nullify(gb%gbcp)
13801384

13811385
! return successfully

src/Superstructure/Component/interface/ESMF_Comp_C.F90

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ recursive subroutine f_esmf_compcollectgarbage2(comp, rc)
491491
type(ESMF_CWrap) :: comp
492492
integer, intent(out) :: rc
493493

494+
type(ESMF_CompClass), pointer :: comppp
494495
integer :: localrc
495496
integer :: timeout
496497
logical :: timeoutFlag
@@ -501,22 +502,25 @@ recursive subroutine f_esmf_compcollectgarbage2(comp, rc)
501502

502503
!print *, "collecting Component garbage #2"
503504

505+
comppp => comp%compp ! LLVM workaround for deallocate() runtime error!
506+
504507
! destruct internal data allocations and perform full shut down, making this
505508
! call collective on some MPI implementations
506509
timeout = 10 ! allow for 10s timeout
507510
! calling with 'timeoutFlag' prevents timeout to propagate as error condition
508-
call ESMF_CompDestruct(comp%compp, interCompComm=.false., &
511+
call ESMF_CompDestruct(comppp, interCompComm=.false., &
509512
fullShutdown=.true., timeout=timeout, timeoutFlag=timeoutFlag, rc=localrc)
510513
if (ESMF_LogFoundError(localrc, &
511514
ESMF_ERR_PASSTHRU, &
512515
ESMF_CONTEXT, rcToReturn=rc)) return
513516

514517
! deallocate actual CompClass allocation
515-
if (associated(comp%compp)) then
516-
deallocate(comp%compp, stat=localrc)
518+
if (associated(comppp)) then
519+
deallocate(comppp, stat=localrc)
517520
if (ESMF_LogFoundDeallocError(localrc, msg="Deallocating Comp", &
518521
ESMF_CONTEXT, rcToReturn=rc)) return
519522
endif
523+
520524
nullify(comp%compp)
521525

522526
! return successfully

src/Superstructure/State/interface/ESMF_State_C.F90

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,34 +455,38 @@ subroutine f_esmf_statecollectgarbage(state, rc)
455455
use ESMF_LogErrMod
456456
use ESMF_StateTypesMod
457457
use ESMF_StateMod
458-
458+
459459
implicit none
460-
460+
461461
type(ESMF_State) :: state
462462
integer, intent(out) :: rc
463-
463+
464+
type(ESMF_StateClass), pointer :: statepp
464465
integer :: localrc
465-
466+
466467
! initialize return code; assume routine not implemented
467468
localrc = ESMF_RC_NOT_IMPL
468469
rc = ESMF_RC_NOT_IMPL
469-
470+
470471
!print *, "collecting State garbage"
471472

472-
if (associated(state%statep)) then
473+
statepp => state%statep ! LLVM workaround for deallocate() runtime error!
474+
475+
if (associated(statepp)) then
473476
! destruct internal data allocations
474-
call ESMF_StateDestruct(state%statep, rc=localrc)
477+
call ESMF_StateDestruct(statepp, rc=localrc)
475478
if (ESMF_LogFoundError(localrc, &
476479
ESMF_ERR_PASSTHRU, &
477480
ESMF_CONTEXT, &
478481
rcToReturn=rc)) return
479482
! deallocate actual StateClass allocation
480-
deallocate(state%statep, stat=localrc)
483+
deallocate(statepp, stat=localrc)
481484
localrc = merge (ESMF_SUCCESS, ESMF_RC_MEM_DEALLOCATE, localrc == 0)
482485
if (ESMF_LogFoundDeallocError(localrc, msg="Deallocating State", &
483486
ESMF_CONTEXT, &
484487
rcToReturn=rc)) return
485488
endif
489+
486490
nullify(state%statep)
487491

488492
! return successfully

0 commit comments

Comments
 (0)