[flang-commits] [flang] 980808d - [Flang][OpenMP] Fix loop index privatisation with HLFIR

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Tue Aug 1 07:50:23 PDT 2023


Author: Kiran Chandramohan
Date: 2023-08-01T14:45:52Z
New Revision: 980808dc5b598dd6d2f85746bd1fdeb36d4c6a1c

URL: https://github.com/llvm/llvm-project/commit/980808dc5b598dd6d2f85746bd1fdeb36d4c6a1c
DIFF: https://github.com/llvm/llvm-project/commit/980808dc5b598dd6d2f85746bd1fdeb36d4c6a1c.diff

LOG: [Flang][OpenMP] Fix loop index privatisation with HLFIR

Loop index variables are privatised for a worksharing loop.
The alloca ops of the privatised index are hoisted to the
entry block of the outlineable region or parent function.
With HLFIR, the hlfir.declare should be created in the same
place as the alloca op. To achieve this the alloc and the
hflir.declare should be created in the same place. A new
function is created in OpenMP.cpp for this purpose.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D156719

Added: 
    flang/test/Lower/OpenMP/hlfir-wsloop.f90

Modified: 
    flang/lib/Lower/OpenMP.cpp
    flang/test/Lower/OpenMP/lastprivate-commonblock.f90
    flang/test/Lower/OpenMP/stop-stmt-in-region.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp
index a61f8069513bce..fa3779b2d93d74 100644
--- a/flang/lib/Lower/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP.cpp
@@ -1934,6 +1934,27 @@ static void resetBeforeTerminator(fir::FirOpBuilder &firOpBuilder,
     firOpBuilder.setInsertionPointToStart(&block);
 }
 
+static mlir::Operation *
+createAndSetPrivatizedLoopVar(Fortran::lower::AbstractConverter &converter,
+                              mlir::Location loc, mlir::Type loopVarType,
+                              mlir::Value indexVal,
+                              const Fortran::semantics::Symbol *sym) {
+  fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+  mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
+  firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock());
+
+  mlir::Value temp = firOpBuilder.create<fir::AllocaOp>(
+      loc, loopVarType, /*pinned=*/true, /*lengthParams=*/mlir::ValueRange{},
+      /*shapeParams*/ mlir::ValueRange{},
+      llvm::ArrayRef<mlir::NamedAttribute>{
+          Fortran::lower::getAdaptToByRefAttr(firOpBuilder)});
+  converter.bindSymbol(*sym, temp);
+  firOpBuilder.restoreInsertionPoint(insPt);
+  mlir::Operation *storeOp = firOpBuilder.create<fir::StoreOp>(
+      loc, indexVal, converter.getSymbolAddress(*sym));
+  return storeOp;
+}
+
 /// Create the body (block) for an OpenMP Operation.
 ///
 /// \param [in]    op - the operation the body belongs to.
@@ -1974,14 +1995,10 @@ static void createBodyOfOp(
     // The argument is not currently in memory, so make a temporary for the
     // argument, and store it there, then bind that location to the argument.
     for (const Fortran::semantics::Symbol *arg : args) {
-      mlir::Value val =
+      mlir::Value indexVal =
           fir::getBase(op.getRegion().front().getArgument(argIndex));
-      mlir::Value temp = firOpBuilder.createTemporary(
-          loc, loopVarType,
-          llvm::ArrayRef<mlir::NamedAttribute>{
-              Fortran::lower::getAdaptToByRefAttr(firOpBuilder)});
-      storeOp = firOpBuilder.create<fir::StoreOp>(loc, val, temp);
-      converter.bindSymbol(*arg, temp);
+      storeOp = createAndSetPrivatizedLoopVar(converter, loc, loopVarType,
+                                              indexVal, arg);
       argIndex++;
     }
   } else {

diff  --git a/flang/test/Lower/OpenMP/hlfir-wsloop.f90 b/flang/test/Lower/OpenMP/hlfir-wsloop.f90
new file mode 100644
index 00000000000000..b6be77fe3016d1
--- /dev/null
+++ b/flang/test/Lower/OpenMP/hlfir-wsloop.f90
@@ -0,0 +1,27 @@
+! This test checks lowering of OpenMP DO Directive with HLFIR.
+
+! RUN: bbc -hlfir -fopenmp -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir -flang-experimental-hlfir -fopenmp %s -o - | FileCheck %s
+
+!CHECK-LABEL: func @_QPsimple_loop()
+subroutine simple_loop
+  integer :: i
+  ! CHECK-DAG:     %[[WS_ST:.*]] = arith.constant 1 : i32
+  ! CHECK-DAG:     %[[WS_END:.*]] = arith.constant 9 : i32
+  ! CHECK:  omp.parallel
+  !$OMP PARALLEL
+  ! CHECK-DAG:     %[[ALLOCA_IV:.*]] = fir.alloca i32 {{{.*}}, pinned}
+  ! CHECK:     %[[IV:.*]]    = fir.declare %[[ALLOCA_IV]] {uniq_name = "_QFsimple_loopEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
+  ! CHECK:     omp.wsloop for (%[[I:.*]]) : i32 = (%[[WS_ST]]) to (%[[WS_END]]) inclusive step (%[[WS_ST]])
+  !$OMP DO
+  do i=1, 9
+  ! CHECK:             fir.store %[[I]] to %[[IV:.*]] : !fir.ref<i32>
+  ! CHECK:             %[[LOAD_IV:.*]] = fir.load %[[IV]] : !fir.ref<i32>
+  ! CHECK:    fir.call @_FortranAioOutputInteger32({{.*}}, %[[LOAD_IV]]) {{.*}}: (!fir.ref<i8>, i32) -> i1
+    print*, i
+  end do
+  ! CHECK:       omp.yield
+  !$OMP END DO
+  ! CHECK:       omp.terminator
+  !$OMP END PARALLEL
+end subroutine

diff  --git a/flang/test/Lower/OpenMP/lastprivate-commonblock.f90 b/flang/test/Lower/OpenMP/lastprivate-commonblock.f90
index d0d9cca7ff21f1..73175cbd45623f 100644
--- a/flang/test/Lower/OpenMP/lastprivate-commonblock.f90
+++ b/flang/test/Lower/OpenMP/lastprivate-commonblock.f90
@@ -1,7 +1,7 @@
 ! RUN: %flang_fc1 -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s 
 
 !CHECK: func.func @_QPlastprivate_common() {
-!CHECK: %[[val_0:.*]] = fir.alloca i32 {adapt.valuebyref}
+!CHECK: %[[val_0:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
 !CHECK: %[[val_1:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFlastprivate_commonEi"}
 !CHECK: %[[val_2:.*]] = fir.address_of(@_QCc) : !fir.ref<!fir.array<8xi8>>
 !CHECK: %[[val_3:.*]] = fir.convert %[[val_2]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>

diff  --git a/flang/test/Lower/OpenMP/stop-stmt-in-region.f90 b/flang/test/Lower/OpenMP/stop-stmt-in-region.f90
index 75a5c736d2d2e3..2f73fb31966ec4 100644
--- a/flang/test/Lower/OpenMP/stop-stmt-in-region.f90
+++ b/flang/test/Lower/OpenMP/stop-stmt-in-region.f90
@@ -71,7 +71,7 @@ subroutine test_stop_in_region3()
 end
 
 ! CHECK-LABEL: func.func @_QPtest_stop_in_region4() {
-! CHECK:         %[[VAL_0:.*]] = fir.alloca i32 {adapt.valuebyref}
+! CHECK:         %[[VAL_0:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
 ! CHECK:         %[[VAL_1:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_stop_in_region4Ei"}
 ! CHECK:         %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region4Ex"}
 ! CHECK:         %[[VAL_3:.*]] = arith.constant 1 : i32


        


More information about the flang-commits mailing list