[flang-commits] [clang] [flang] [Flang][OpenMP] Add -fopenmp-default-none command line flag (PR #120287)

Michael Klemm via flang-commits flang-commits at lists.llvm.org
Wed Dec 18 04:55:00 PST 2024


https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/120287

>From ef9b76ad1dfb4b6c99bbdf6391fe57b944ffbfa2 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Tue, 17 Dec 2024 18:34:06 +0100
Subject: [PATCH 1/4] Implement -fopenmp-default-none compiler flag

---
 clang/include/clang/Driver/Options.td         | 3 +++
 clang/lib/Driver/ToolChains/Flang.cpp         | 3 +++
 flang/include/flang/Common/Fortran-features.h | 2 +-
 flang/lib/Common/Fortran-features.cpp         | 1 +
 flang/lib/Frontend/CompilerInvocation.cpp     | 4 ++++
 flang/lib/Semantics/resolve-directives.cpp    | 8 +++++++-
 6 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index bed2a56b003512..acbe93fe72a24e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3596,6 +3596,9 @@ def fopenmp : Flag<["-"], "fopenmp">, Group<f_Group>,
   Flags<[NoArgumentUnused]>,
   Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   HelpText<"Parse OpenMP pragmas and generate parallel code.">;
+def fopenmp_default_none : Flag<["-"], "fopenmp-default-none">, Group<f_Group>,
+  Visibility<[FlangOption, FC1Option]>,
+  HelpText<"Add DEFAULT(NONE) to all OpenMP directives that allow data-sharing clauses.">;
 def fno_openmp : Flag<["-"], "fno-openmp">, Group<f_Group>,
   Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   Flags<[NoArgumentUnused]>;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index c98fdbd157bac8..521ae90e0bd164 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -855,6 +855,9 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
       // is experimental.
       D.Diag(diag::warn_openmp_experimental);
 
+      if (Args.hasArg((options::OPT_fopenmp_default_none)))
+        CmdArgs.push_back("-fopenmp-default-none");
+
       // FIXME: Clang supports a whole bunch more flags here.
       break;
     default:
diff --git a/flang/include/flang/Common/Fortran-features.h b/flang/include/flang/Common/Fortran-features.h
index b04f6117ae9656..218e05e53aee01 100644
--- a/flang/include/flang/Common/Fortran-features.h
+++ b/flang/include/flang/Common/Fortran-features.h
@@ -54,7 +54,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     PolymorphicActualAllocatableOrPointerToMonomorphicDummy, RelaxedPureDummy,
     UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram, PrintCptr,
     SavedLocalInSpecExpr, PrintNamelist, AssumedRankPassedToNonAssumedRank,
-    IgnoreIrrelevantAttributes)
+    IgnoreIrrelevantAttributes, OpenMPDefaultNone)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Common/Fortran-features.cpp b/flang/lib/Common/Fortran-features.cpp
index c86432874b8d83..c6f818478b065c 100644
--- a/flang/lib/Common/Fortran-features.cpp
+++ b/flang/lib/Common/Fortran-features.cpp
@@ -17,6 +17,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
   disable_.set(LanguageFeature::OldDebugLines);
   disable_.set(LanguageFeature::OpenACC);
   disable_.set(LanguageFeature::OpenMP);
+  disable_.set(LanguageFeature::OpenMPDefaultNone);
   disable_.set(LanguageFeature::CUDA); // !@cuf
   disable_.set(LanguageFeature::CudaManaged);
   disable_.set(LanguageFeature::CudaUnified);
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 648b88e84051c2..648ad224a26f66 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1023,6 +1023,10 @@ static bool parseOpenMPArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
           res.getLangOpts().OpenMPVersion, diags)) {
     res.getLangOpts().OpenMPVersion = Version;
   }
+  if (args.hasArg(clang::driver::options::OPT_fopenmp_default_none)) {
+    res.getFrontendOpts().features.Enable(
+        Fortran::common::LanguageFeature::OpenMPDefaultNone);
+  }
   if (args.hasArg(clang::driver::options::OPT_fopenmp_force_usm)) {
     res.getLangOpts().OpenMPForceUSM = 1;
   }
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 39478b58a9070d..9cefc36d987b15 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2268,6 +2268,11 @@ void OmpAttributeVisitor::CreateImplicitSymbols(
 void OmpAttributeVisitor::Post(const parser::Name &name) {
   auto *symbol{name.symbol};
 
+  // if -fopenmp-default-none was given on the command line, act as if
+  // DEFAULT(NONE) was present at the directive.
+  bool HaveOpenMPDefaultNone = context_.languageFeatures().IsEnabled(
+      common::LanguageFeature::OpenMPDefaultNone);
+
   if (symbol && !dirContext_.empty() && GetContext().withinConstruct) {
     if (IsPrivatizable(symbol) && !IsObjectWithDSA(*symbol)) {
       // TODO: create a separate function to go through the rules for
@@ -2276,7 +2281,8 @@ void OmpAttributeVisitor::Post(const parser::Name &name) {
       if (Symbol * found{currScope().FindSymbol(name.source)}) {
         if (symbol != found) {
           name.symbol = found; // adjust the symbol within region
-        } else if (GetContext().defaultDSA == Symbol::Flag::OmpNone &&
+        } else if ((HaveOpenMPDefaultNone ||
+                       GetContext().defaultDSA == Symbol::Flag::OmpNone) &&
             !symbol->test(Symbol::Flag::OmpThreadprivate) &&
             // Exclude indices of sequential loops that are privatised in
             // the scope of the parallel region, and not in this scope.

>From e5a71fc4bb47160085fa04948b93bd1ece11b37f Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Tue, 17 Dec 2024 18:42:41 +0100
Subject: [PATCH 2/4] Add test for -fopenmp-default-none

---
 flang/test/Semantics/OpenMP/resolve07.f90 | 34 +++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 flang/test/Semantics/OpenMP/resolve07.f90

diff --git a/flang/test/Semantics/OpenMP/resolve07.f90 b/flang/test/Semantics/OpenMP/resolve07.f90
new file mode 100644
index 00000000000000..4a898903fae431
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/resolve07.f90
@@ -0,0 +1,34 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-default-none
+
+
+! Test that -fopenmp-default-none shows the same errors as DEFAULT(NONE)
+subroutine default_none()
+  integer a(3)
+  integer, parameter :: D=10
+  A = 1
+  B = 2
+  !$omp parallel private(c)
+  !ERROR: The DEFAULT(NONE) clause requires that 'a' must be listed in a data-sharing attribute clause
+  A(1:2) = 3
+  !ERROR: The DEFAULT(NONE) clause requires that 'b' must be listed in a data-sharing attribute clause
+  B = 4
+  C = 5 + D
+  !$omp end parallel
+end subroutine default_none
+
+! Test that indices of sequential loops are privatised and hence do not error
+! for -fopenmp-default-none.
+subroutine default_none_seq_loop
+  integer :: i
+
+  !$omp parallel do
+  do i = 1, 10
+     do j = 1, 20
+    enddo
+  enddo
+end subroutine
+
+program mm
+  call default_none()
+  call default_none_seq_loop()
+end

>From 4dbad748a8b89ba06a64b9f79ca1b06fd4d5427e Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 18 Dec 2024 13:53:18 +0100
Subject: [PATCH 3/4] Move -fopenmp-default-none to the end of OpenMP options

---
 clang/include/clang/Driver/Options.td | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index acbe93fe72a24e..b12cd6098e7aa2 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3596,9 +3596,6 @@ def fopenmp : Flag<["-"], "fopenmp">, Group<f_Group>,
   Flags<[NoArgumentUnused]>,
   Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   HelpText<"Parse OpenMP pragmas and generate parallel code.">;
-def fopenmp_default_none : Flag<["-"], "fopenmp-default-none">, Group<f_Group>,
-  Visibility<[FlangOption, FC1Option]>,
-  HelpText<"Add DEFAULT(NONE) to all OpenMP directives that allow data-sharing clauses.">;
 def fno_openmp : Flag<["-"], "fno-openmp">, Group<f_Group>,
   Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   Flags<[NoArgumentUnused]>;
@@ -3651,6 +3648,9 @@ def fopenmp_cuda_blocks_per_sm_EQ : Joined<["-"], "fopenmp-cuda-blocks-per-sm=">
   Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>;
 def fopenmp_cuda_teams_reduction_recs_num_EQ : Joined<["-"], "fopenmp-cuda-teams-reduction-recs-num=">, Group<f_Group>,
   Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>;
+def fopenmp_default_none : Flag<["-"], "fopenmp-default-none">, Group<f_Group>,
+  Visibility<[FlangOption, FC1Option]>,
+  HelpText<"Add DEFAULT(NONE) to all OpenMP directives that allow data-sharing clauses.">;
 
 //===----------------------------------------------------------------------===//
 // Shared cc1 + fc1 OpenMP Target Options

>From ffafbfdc48692f41aa1e2bc9f4c465a48fcf7cab Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 18 Dec 2024 13:53:47 +0100
Subject: [PATCH 4/4] Rename variable

---
 flang/lib/Semantics/resolve-directives.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 9cefc36d987b15..602e8584038825 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2270,7 +2270,7 @@ void OmpAttributeVisitor::Post(const parser::Name &name) {
 
   // if -fopenmp-default-none was given on the command line, act as if
   // DEFAULT(NONE) was present at the directive.
-  bool HaveOpenMPDefaultNone = context_.languageFeatures().IsEnabled(
+  bool haveOpenMPDefaultNone = context_.languageFeatures().IsEnabled(
       common::LanguageFeature::OpenMPDefaultNone);
 
   if (symbol && !dirContext_.empty() && GetContext().withinConstruct) {
@@ -2281,7 +2281,7 @@ void OmpAttributeVisitor::Post(const parser::Name &name) {
       if (Symbol * found{currScope().FindSymbol(name.source)}) {
         if (symbol != found) {
           name.symbol = found; // adjust the symbol within region
-        } else if ((HaveOpenMPDefaultNone ||
+        } else if ((haveOpenMPDefaultNone ||
                        GetContext().defaultDSA == Symbol::Flag::OmpNone) &&
             !symbol->test(Symbol::Flag::OmpThreadprivate) &&
             // Exclude indices of sequential loops that are privatised in



More information about the flang-commits mailing list