Skip to content

[flang][openacc] Avoid creation of duplicate global ctor #71846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

clementval
Copy link
Contributor

PR #70698 relax the duplication rule in acc declare clauses. This lead to potential duplicate creation of the global constructor/destructor. This patch make sure to not generate a duplicate ctor/dtor.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir openacc labels Nov 9, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 9, 2023

@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-openacc

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

PR #70698 relax the duplication rule in acc declare clauses. This lead to potential duplicate creation of the global constructor/destructor. This patch make sure to not generate a duplicate ctor/dtor.


Full diff: https://github.com/llvm/llvm-project/pull/71846.diff

2 Files Affected:

  • (modified) flang/lib/Lower/OpenACC.cpp (+6)
  • (modified) flang/test/Lower/OpenACC/HLFIR/acc-declare.f90 (+9)
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 1c045b4273e2607..9c04707f5197f38 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -2906,6 +2906,11 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
                 std::stringstream asFortran;
                 asFortran << name->symbol->name().ToString();
 
+                if (builder.getModule()
+                        .lookupSymbol<mlir::acc::GlobalConstructorOp>(
+                            declareGlobalCtorName.str()))
+                  return;
+
                 if (!globalOp) {
                   if (Fortran::semantics::FindEquivalenceSet(*name->symbol)) {
                     for (Fortran::semantics::EquivalenceObject eqObj :
@@ -2959,6 +2964,7 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
                 }
                 builder.restoreInsertionPoint(crtPos);
               }
+
             },
             [&](const Fortran::parser::Name &name) {
               TODO(operandLocation, "OpenACC Global Ctor from parser::Name");
diff --git a/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90 b/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
index 6ebdd39802fef0f..7670cd654bf6fd7 100644
--- a/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/HLFIR/acc-declare.f90
@@ -304,3 +304,12 @@ module acc_declare_equivalent2
 ! ALL:         acc.delete accPtr(%[[DEVICEPTR]] : !fir.ref<!fir.array<40xi8>>) {dataClause = #acc<data_clause acc_create>, name = "v2", structured = false}
 ! ALL:         acc.terminator
 ! ALL:       }
+
+module acc_declare_allocatable_test2
+  integer, allocatable :: data1(:)
+  integer, allocatable :: data2(:)
+  !$acc declare create(data1, data2, data1)
+end module
+
+! ALL-LABEL: acc.global_ctor @_QMacc_declare_allocatable_test2Edata1_acc_ctor
+! ALL-LABEL: acc.global_ctor @_QMacc_declare_allocatable_test2Edata2_acc_ctor

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -2959,6 +2964,7 @@ static void genGlobalCtors(Fortran::lower::AbstractConverter &converter,
}
builder.restoreInsertionPoint(crtPos);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change

@clementval clementval merged commit 90da688 into llvm:main Nov 9, 2023
@clementval clementval deleted the acc_ctor_duplicate branch November 9, 2023 20:57
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
PR llvm#70698 relax the duplication rule in acc declare clauses. This lead
to potential duplicate creation of the global constructor/destructor.
This patch make sure to not generate a duplicate ctor/dtor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category openacc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants