[flang-commits] [flang] [flang][OpenMP] Make lastprivate work with reallocated variables (PR #106559)

Leandro Lupori via flang-commits flang-commits at lists.llvm.org
Mon Sep 2 14:33:00 PDT 2024


================
@@ -36,3 +24,45 @@ program lastprivate_allocatable
 ! CHECK-NEXT:            }
 ! CHECK-NEXT:            omp.yield
 ! CHECK-NEXT:          }
+program lastprivate_allocatable
+  integer, allocatable :: a
+  integer :: i
+  ! a is unallocated here
+  !$omp parallel do lastprivate(a)
+  do i=1,1
+    a = 42
+  enddo
+  !$omp end parallel do
+  ! a should be allocated here
+end program
+
+! CHECK-LABEL:  func @_QPlastprivate_realloc()
+! CHECK:          %[[A:.*]]:2 = hlfir.declare %{{.*}} {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFlastprivate_reallocEa"} :
+! CHECK-SAME:       (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>) ->
+! CHECK-SAME:       (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>)
+! CHECK:          omp.parallel {
+! CHECK:            %[[A_PRIV:.*]]:2 = hlfir.declare %{{.*}} {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFlastprivate_reallocEa"} :
+! CHECK-SAME:         (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>) ->
+! CHECK-SAME:         (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.complex<4>>>>>)
+! CHECK:            omp.sections {
+! CHECK:              omp.section {
+! CHECK:                fir.if %{{.*}} {
----------------
luporl wrote:

OK, I'll remove this `if` and adjust the tests.

BTW, according to the sentence that you have quoted above, when there is a `type bound procedure as a deļ¬ned assignment` it should be used, but it's not, because we set `AssignOp`'s `temporary_lhs` flag to false in `Bridge.cpp`.

There is a comment in the code saying that `temporary_lhs` also avoids finalization calls, but it seems to me we shouldn't avoid them:

> Finalization of a list item of a finalizable type or subobjects of a list item of a finalizable type occurs at the end of the region. The order in which any final subroutines for different variables of a finalizable type are called is unspecified.
(OMP 5.2, 5.3 List Item Privatization)

So I intend to also change `temporary_lhs` to false to fix these nonconformities. This is already being done in `DataSharingProcessor::copyLastPrivateSymbol`. Please let me know if you disagree.

https://github.com/llvm/llvm-project/pull/106559


More information about the flang-commits mailing list