[flang-commits] [flang] [flang][openacc] Support variable in equivalence in declare directive (PR #71242)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Fri Nov 3 15:27:08 PDT 2023


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/71242

A variable in equivalence share the storage units with one or more objects. When lowered to FIR, the global created for the equivalence has the name of one of the object. The variable also has an offset in the storage unit.
This patch takes all of this into account for variable part of equivalence used in a declare directive. 

>From 849183e094741f645cf16b4bc3a33673c4864d71 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Fri, 3 Nov 2023 15:21:52 -0700
Subject: [PATCH] [flang][openacc] Support variable in equivalence in declare
 directive

---
 flang/lib/Lower/OpenACC.cpp                   | 86 ++++++++++++++-----
 .../test/Lower/OpenACC/HLFIR/acc-declare.f90  | 58 +++++++++++++
 2 files changed, 122 insertions(+), 22 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 809fd3b3be7cfdf..7808e94ed7c7346 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -27,6 +27,7 @@
 #include "flang/Optimizer/Builder/Todo.h"
 #include "flang/Parser/parse-tree.h"
 #include "flang/Semantics/expression.h"
+#include "flang/Semantics/scope.h"
 #include "flang/Semantics/tools.h"
 #include "llvm/Frontend/OpenACC/ACC.h.inc"
 
@@ -2740,33 +2741,40 @@ static void genACC(Fortran::lower::AbstractConverter &converter,
 
 template <typename GlobalOp, typename EntryOp, typename DeclareOp,
           typename ExitOp>
-static void createDeclareGlobalOp(mlir::OpBuilder &modBuilder,
-                                  fir::FirOpBuilder &builder,
-                                  mlir::Location loc, fir::GlobalOp &globalOp,
-                                  mlir::acc::DataClause clause, bool implicit) {
-  std::stringstream declareGlobalName;
-
-  if constexpr (std::is_same_v<GlobalOp, mlir::acc::GlobalConstructorOp>)
-    declareGlobalName << globalOp.getSymName().str() << "_acc_ctor";
-  else if constexpr (std::is_same_v<GlobalOp, mlir::acc::GlobalDestructorOp>)
-    declareGlobalName << globalOp.getSymName().str() << "_acc_dtor";
-
+static void
+createDeclareGlobalOp(mlir::OpBuilder &modBuilder, fir::FirOpBuilder &builder,
+                      mlir::Location loc, fir::GlobalOp globalOp,
+                      mlir::acc::DataClause clause,
+                      const std::string declareGlobalName, bool implicit,
+                      std::optional<std::size_t> &offset, mlir::Type aliasType,
+                      std::stringstream &asFortran) {
   GlobalOp declareGlobalOp =
-      modBuilder.create<GlobalOp>(loc, declareGlobalName.str());
+      modBuilder.create<GlobalOp>(loc, declareGlobalName);
   builder.createBlock(&declareGlobalOp.getRegion(),
                       declareGlobalOp.getRegion().end(), {}, {});
   builder.setInsertionPointToEnd(&declareGlobalOp.getRegion().back());
+  mlir::Value entryAddr;
 
   fir::AddrOfOp addrOp = builder.create<fir::AddrOfOp>(
       loc, fir::ReferenceType::get(globalOp.getType()), globalOp.getSymbol());
-  addDeclareAttr(builder, addrOp.getOperation(), clause);
+  entryAddr = addrOp.getResTy();
+  // If the variable in the declare clause is part of an equivalence it has an
+  // offset.
+  if (offset) {
+    mlir::Type i8Ptr = builder.getRefType(builder.getI8Type());
+    mlir::Value off =
+        builder.createIntegerConstant(loc, builder.getIndexType(), *offset);
+    mlir::Value aliasAddr = builder.create<fir::CoordinateOp>(
+        loc, i8Ptr, addrOp.getResTy(), mlir::ValueRange{off});
+    entryAddr =
+        builder.createConvert(loc, fir::PointerType::get(aliasType), aliasAddr);
+  }
+  addDeclareAttr(builder, entryAddr.getDefiningOp(), clause);
 
-  std::stringstream asFortran;
-  asFortran << Fortran::lower::mangle::demangleName(globalOp.getSymName());
   llvm::SmallVector<mlir::Value> bounds;
   EntryOp entryOp = createDataEntryOp<EntryOp>(
-      builder, loc, addrOp.getResTy(), asFortran, bounds,
-      /*structured=*/false, implicit, clause, addrOp.getResTy().getType());
+      builder, loc, entryAddr, asFortran, bounds,
+      /*structured=*/false, implicit, clause, entryAddr.getType());
   builder.create<DeclareOp>(loc, mlir::ValueRange(entryOp.getAccPtr()));
   mlir::Value varPtr;
   if constexpr (std::is_same_v<GlobalOp, mlir::acc::GlobalDestructorOp>) {
@@ -2904,8 +2912,39 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
                           designator)) {
                 std::string globalName = converter.mangleName(*name->symbol);
                 fir::GlobalOp globalOp = builder.getNamedGlobal(globalName);
-                if (!globalOp)
-                  llvm::report_fatal_error("could not retrieve global symbol");
+                std::stringstream declareGlobalCtorName;
+                declareGlobalCtorName << globalName << "_acc_ctor";
+                std::stringstream declareGlobalDtorName;
+                declareGlobalDtorName << globalName << "_acc_dtor";
+                std::optional<std::size_t> offset = std::nullopt;
+                mlir::Type aliasType;
+
+                std::stringstream asFortran;
+                asFortran << name->symbol->name().ToString();
+
+                if (!globalOp) {
+                  if (Fortran::semantics::FindEquivalenceSet(*name->symbol)) {
+                    for (Fortran::semantics::EquivalenceObject eqObj :
+                         *Fortran::semantics::FindEquivalenceSet(
+                             *name->symbol)) {
+                      std::string eqName = converter.mangleName(eqObj.symbol);
+                      globalOp = builder.getNamedGlobal(eqName);
+                      if (globalOp)
+                        break;
+                    }
+
+                    if (!globalOp)
+                      llvm::report_fatal_error(
+                          "could not retrieve global symbol");
+
+                    offset = name->symbol->GetUltimate().offset();
+                    aliasType = converter.genType(name->symbol->GetUltimate());
+                  } else {
+                    llvm::report_fatal_error(
+                        "could not retrieve global symbol");
+                  }
+                }
+
                 addDeclareAttr(builder, globalOp.getOperation(), clause);
                 auto crtPos = builder.saveInsertionPoint();
                 modBuilder.setInsertionPointAfter(globalOp);
@@ -2915,7 +2954,8 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
                                         mlir::acc::CopyinOp,
                                         mlir::acc::DeclareEnterOp, ExitOp>(
                       modBuilder, builder, operandLocation, globalOp, clause,
-                      /*implicit=*/true);
+                      declareGlobalCtorName.str(), /*implicit=*/true, offset,
+                      aliasType, asFortran);
                   createDeclareAllocFunc<EntryOp>(
                       modBuilder, builder, operandLocation, globalOp, clause);
                   if constexpr (!std::is_same_v<EntryOp, ExitOp>)
@@ -2925,14 +2965,16 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
                   createDeclareGlobalOp<mlir::acc::GlobalConstructorOp, EntryOp,
                                         mlir::acc::DeclareEnterOp, ExitOp>(
                       modBuilder, builder, operandLocation, globalOp, clause,
-                      /*implicit=*/false);
+                      declareGlobalCtorName.str(), /*implicit=*/false, offset,
+                      aliasType, asFortran);
                 }
                 if constexpr (!std::is_same_v<EntryOp, ExitOp>) {
                   createDeclareGlobalOp<mlir::acc::GlobalDestructorOp,
                                         mlir::acc::GetDevicePtrOp,
                                         mlir::acc::DeclareExitOp, ExitOp>(
                       modBuilder, builder, operandLocation, globalOp, clause,
-                      /*implicit=*/false);
+                      declareGlobalDtorName.str(), /*implicit=*/false, offset,
+                      aliasType, asFortran);
                 }
                 builder.restoreInsertionPoint(crtPos);
               }
diff --git a/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90 b/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
index 0a45967ce82ef3e..6cca57a2504e2b9 100644
--- a/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
@@ -258,3 +258,61 @@ module acc_declare_allocatable_test
 ! ALL:         acc.delete accPtr(%[[DEVICEPTR]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) {dataClause = #acc<data_clause acc_create>, name = "data1", structured = false}
 ! ALL:         acc.terminator
 ! ALL:       }
+
+
+module acc_declare_equivalent
+  integer, parameter :: n = 10
+  real :: v1(n)
+  real :: v2(n)
+  equivalence(v1(1), v2(1))
+  !$acc declare create(v2)
+end module
+
+! ALL-LABEL: acc.global_ctor @_QMacc_declare_equivalentEv2_acc_ctor {
+! ALL-DAG:     %[[C0:.*]] = arith.constant 0 : index
+! ALL-DAG:     %[[ADDR:.*]] = fir.address_of(@_QMacc_declare_equivalentEv1) : !fir.ref<!fir.array<40xi8>>
+! ALL:         %[[COORD:.*]] = fir.coordinate_of %[[ADDR]], %[[C0]] : (!fir.ref<!fir.array<40xi8>>, index) -> !fir.ref<i8>
+! ALL:         %[[CONV:.*]] = fir.convert %[[COORD]] {acc.declare = #acc.declare<dataClause =  acc_create>} : (!fir.ref<i8>) -> !fir.ptr<!fir.array<10xf32>>
+! ALL:         %[[CREATE:.*]] = acc.create varPtr(%[[CONV]] : !fir.ptr<!fir.array<10xf32>>) -> !fir.ptr<!fir.array<10xf32>> {name = "v2", structured = false}
+! ALL:         acc.declare_enter dataOperands(%[[CREATE]] : !fir.ptr<!fir.array<10xf32>>)
+! ALL:         acc.terminator
+! ALL:       }
+
+! ALL-LABEL:  acc.global_dtor @_QMacc_declare_equivalentEv2_acc_dtor {
+! ALL-DAG:      %[[C0:.*]] = arith.constant 0 : index
+! ALL-DAG:      %[[ADDR:.*]] = fir.address_of(@_QMacc_declare_equivalentEv1) : !fir.ref<!fir.array<40xi8>>
+! ALL:          %[[COORD:.*]] = fir.coordinate_of %[[ADDR]], %[[C0]] : (!fir.ref<!fir.array<40xi8>>, index) -> !fir.ref<i8>
+! ALL:          %[[CONV:.*]] = fir.convert %[[COORD]] {acc.declare = #acc.declare<dataClause =  acc_create>} : (!fir.ref<i8>) -> !fir.ptr<!fir.array<10xf32>>
+! ALL:          %[[DEVICEPTR:.*]] = acc.getdeviceptr varPtr(%[[CONV]] : !fir.ptr<!fir.array<10xf32>>) -> !fir.ptr<!fir.array<10xf32>> {dataClause = #acc<data_clause acc_create>, name = "v2", structured = false}
+! ALL:          acc.declare_exit dataOperands(%[[DEVICEPTR]] : !fir.ptr<!fir.array<10xf32>>)
+! ALL:          acc.delete accPtr(%[[DEVICEPTR]] : !fir.ptr<!fir.array<10xf32>>) {dataClause = #acc<data_clause acc_create>, name = "v2", structured = false}
+! ALL:          acc.terminator
+! ALL:        }
+
+module acc_declare_equivalent2
+  real :: v1(10)
+  real :: v2(5)
+  equivalence(v1(6), v2(1))
+  !$acc declare create(v2)
+end module
+
+! ALL-LABEL: acc.global_ctor @_QMacc_declare_equivalent2Ev2_acc_ctor {
+! ALL-DAG:     %[[C20:.*]] = arith.constant 20 : index
+! ALL-DAG:     %[[ADDR:.*]] = fir.address_of(@_QMacc_declare_equivalent2Ev1) : !fir.ref<!fir.array<40xi8>>
+! ALL:         %[[COORD:.*]] = fir.coordinate_of %[[ADDR]], %[[C20]] : (!fir.ref<!fir.array<40xi8>>, index) -> !fir.ref<i8>
+! ALL:         %[[CONV:.*]] = fir.convert %[[COORD]] {acc.declare = #acc.declare<dataClause =  acc_create>} : (!fir.ref<i8>) -> !fir.ptr<!fir.array<5xf32>>
+! ALL:         %[[CREATE:.*]] = acc.create varPtr(%[[CONV]] : !fir.ptr<!fir.array<5xf32>>) -> !fir.ptr<!fir.array<5xf32>> {name = "v2", structured = false}
+! ALL:         acc.declare_enter dataOperands(%[[CREATE]] : !fir.ptr<!fir.array<5xf32>>)
+! ALL:         acc.terminator
+! ALL:       }
+
+! ALL-LABEL: acc.global_dtor @_QMacc_declare_equivalent2Ev2_acc_dtor {
+! ALL-DAG:     %[[C20:.*]] = arith.constant 20 : index
+! ALL-DAG:     %[[ADDR:.*]] = fir.address_of(@_QMacc_declare_equivalent2Ev1) : !fir.ref<!fir.array<40xi8>>
+! ALL:         %[[COORD:.*]] = fir.coordinate_of %[[ADDR]], %[[C20]] : (!fir.ref<!fir.array<40xi8>>, index) -> !fir.ref<i8>
+! ALL:         %[[CONV:.*]] = fir.convert %[[COORD]] {acc.declare = #acc.declare<dataClause =  acc_create>} : (!fir.ref<i8>) -> !fir.ptr<!fir.array<5xf32>>
+! ALL:         %[[DEVICEPTR:.*]] = acc.getdeviceptr varPtr(%[[CONV]] : !fir.ptr<!fir.array<5xf32>>) -> !fir.ptr<!fir.array<5xf32>> {dataClause = #acc<data_clause acc_create>, name = "v2", structured = false}
+! ALL:         acc.declare_exit dataOperands(%[[DEVICEPTR]] : !fir.ptr<!fir.array<5xf32>>)
+! ALL:         acc.delete accPtr(%[[DEVICEPTR]] : !fir.ptr<!fir.array<5xf32>>) {dataClause = #acc<data_clause acc_create>, name = "v2", structured = false}
+! ALL:         acc.terminator
+! ALL:       }



More information about the flang-commits mailing list