[flang-commits] [clang] [flang] [Flang][OpenMP] Add -fopenmp-default-none command line flag (PR #120287)
via flang-commits
flang-commits at lists.llvm.org
Tue Dec 17 10:53:45 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-driver
Author: Michael Klemm (mjklemm)
<details>
<summary>Changes</summary>
This PR adds the `-fopenmp-default-none` command line flag to the Flang compiler. Similarly, to `-fimplicit-none` it provides error checking for OpenMP directives by behaving as if `DEFAULT(NONE)` was specified at all directives that support data-sharing attribute clauses.
---
Full diff: https://github.com/llvm/llvm-project/pull/120287.diff
7 Files Affected:
- (modified) clang/include/clang/Driver/Options.td (+3)
- (modified) clang/lib/Driver/ToolChains/Flang.cpp (+3)
- (modified) flang/include/flang/Common/Fortran-features.h (+1-1)
- (modified) flang/lib/Common/Fortran-features.cpp (+1)
- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+4)
- (modified) flang/lib/Semantics/resolve-directives.cpp (+7-1)
- (added) flang/test/Semantics/OpenMP/resolve07.f90 (+34)
``````````diff
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.
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/120287
More information about the flang-commits
mailing list