llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: Andre Kuhlenschmidt (akuhlens)

<details>
<summary>Changes</summary>

Adds the flag, `-facc-allow-default-none-scalars`. When this flag is enabled, 
Flang reverts to the pre-3.2 behavior: scalar variables referenced inside a 
`default(none)` compute region without an explicit data clause do not produce 
an error.  Instead, Flang infers implicit data attributes for those scalars via 
the same implicit-copy logic applied in regions without `default(none)`.

- Adds tests and documentation.
- Makes an explicit extensions doc for OpenACC to mirror the OpenMP extensions 
doc.
- Moves the intentional deviations to from the standard to the extensions doc.

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


9 Files Affected:

- (modified) clang/include/clang/Driver/Options.td (+2) 
- (modified) clang/lib/Driver/ToolChains/Flang.cpp (+2) 
- (added) flang/docs/OpenACC-extensions.md (+57) 
- (modified) flang/docs/OpenACC.md (+3-12) 
- (modified) flang/include/flang/Support/Fortran-features.h (+4-2) 
- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+8) 
- (modified) flang/lib/Semantics/resolve-directives.cpp (+24-4) 
- (modified) flang/lib/Support/Fortran-features.cpp (+2) 
- (added) flang/test/Semantics/OpenACC/acc-default-none-scalars.f90 (+16) 


``````````diff
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 77379f1130149..bac2cddcf6e83 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7132,6 +7132,8 @@ defm ppc_native_vec_elem_order: 
BoolOptionWithoutMarshalling<"f", "ppc-native-ve
   PosFlag<SetTrue, [], [ClangOption], "Specifies PowerPC native vector element 
order (default)">,
   NegFlag<SetFalse, [], [ClangOption], "Specifies PowerPC non-native vector 
element order">>;
 defm unsigned : OptInFC1FFlag<"unsigned", "Enables UNSIGNED type">;
+defm acc_allow_default_none_scalars : 
OptInFC1FFlag<"acc-allow-default-none-scalars",
+  "Allow scalar variables in OpenACC default(none) regions without explicit 
data clauses (pre-OpenACC-3.2 behavior)">;
 
 def fno_automatic : Flag<["-"], "fno-automatic">, Group<f_Group>,
   HelpText<"Implies the SAVE attribute for non-automatic local objects in 
subprograms unless RECURSIVE">;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index 1edb83f7255eb..ed8d30f2dce14 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -128,6 +128,8 @@ void Flang::addOtherOptions(const ArgList &Args, 
ArgStringList &CmdArgs) const {
                    options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ,
                    options::OPT_funderscoring, options::OPT_fno_underscoring,
                    options::OPT_funsigned, options::OPT_fno_unsigned,
+                   options::OPT_facc_allow_default_none_scalars,
+                   options::OPT_fno_acc_allow_default_none_scalars,
                    options::OPT_finstrument_functions});
 
   llvm::codegenoptions::DebugInfoKind DebugInfoKind;
diff --git a/flang/docs/OpenACC-extensions.md b/flang/docs/OpenACC-extensions.md
new file mode 100644
index 0000000000000..732e82a03045d
--- /dev/null
+++ b/flang/docs/OpenACC-extensions.md
@@ -0,0 +1,57 @@
+<!--===- docs/OpenACC-extensions.md
+
+   Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+   See https://llvm.org/LICENSE.txt for license information.
+   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+-->
+
+# OpenACC Extensions in Flang
+
+```{contents}
+---
+local:
+---
+```
+
+The items below are intentional deviations from the OpenACC specification
+accepted by Flang.  They are also listed as deviations in
+[OpenACC.md](OpenACC.md).
+
+## Extensions always active
+
+These extensions require no flag.
+
+* The end directive for combined constructs can omit the `loop` keyword.
+* An `!$acc routine` with no parallelism clause is treated as if the `seq`
+  clause were present.
+* `!$acc end loop` does not trigger a parsing error and is silently ignored.
+* The restriction on required clauses for `!$acc data` is emitted as a
+  portability warning rather than an error, matching the behavior of other
+  compilers.
+* The `if` clause accepts scalar integer expressions in addition to scalar
+  logical expressions.
+* `!$acc routine` directives can be placed at the top level.
+* `!$acc cache` directives accept scalar variables.
+* The `!$acc declare` directive accepts assumed-size array arguments for
+  `deviceptr` and `present` clauses.
+
+## Extensions enabled by flag
+
+### `-facc-allow-default-none-scalars` — pre-OpenACC-3.2 scalar behavior under 
`DEFAULT(NONE)`
+
+OpenACC version 3.2 (section 1.16, change 733) clarified that the
+`default(none)` clause applies to scalar variables.  Prior to version 3.2,
+`default(none)` did not impose a data-clause requirement on scalar variables.
+
+When this flag is enabled, Flang reverts to the pre-3.2 behavior: scalar
+variables referenced inside a `default(none)` compute region without an
+explicit data clause do not produce an error.  Instead, Flang infers implicit
+data attributes for those scalars via the same implicit-copy logic applied
+in regions without `default(none)`.
+
+Array variables always require an explicit data clause under `default(none)`
+regardless of this flag.
+
+When a scalar is implicitly attributed under this extension, a warning is
+emitted at `-pedantic` level (or explicitly via `-Wacc-implicit-scalar`).
diff --git a/flang/docs/OpenACC.md b/flang/docs/OpenACC.md
index 87f30ccd953b6..47ce31c0bebaa 100644
--- a/flang/docs/OpenACC.md
+++ b/flang/docs/OpenACC.md
@@ -15,18 +15,9 @@ local:
 ```
 
 ## Intentional deviation from the specification
-* The end directive for combined construct can omit the `loop` keyword.
-* An `!$acc routine` with no parallelism clause is treated as if the `seq`
-  clause was present.
-* `!$acc end loop` does not trigger a parsing error and is just ignored.
-* The restriction on `!$acc data` required clauses is emitted as a portability
-  warning instead of an error as other compiler accepts it.
-* The `if` clause accepts scalar integer expression in addition to scalar
-  logical expression.
-* `!$acc routine` directive can be placed at the top level. 
-* `!$acc cache` directive accepts scalar variable.
-* The `!$acc declare` directive accepts assumed size array arguments for 
-  `deviceptr` and `present` clauses.
+
+See [OpenACC-extensions.md](OpenACC-extensions.md) for the full list of
+intentional deviations and opt-in extensions supported by Flang.
 
 ## Remarks about incompatibilities with other implementations
 * Array element references in the data clauses are equivalent to array sections
diff --git a/flang/include/flang/Support/Fortran-features.h 
b/flang/include/flang/Support/Fortran-features.h
index 857de9479e4e5..9effbb4db21d3 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -55,7 +55,8 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     SavedLocalInSpecExpr, PrintNamelist, AssumedRankPassedToNonAssumedRank,
     IgnoreIrrelevantAttributes, Unsigned, AmbiguousStructureConstructor,
     ContiguousOkForSeqAssociation, ForwardRefExplicitTypeDummy,
-    InaccessibleDeferredOverride, CudaWarpMatchFunction, DoConcurrentOffload)
+    InaccessibleDeferredOverride, CudaWarpMatchFunction, DoConcurrentOffload,
+    AccDefaultNoneScalars)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
@@ -77,7 +78,8 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     MismatchingDummyProcedure, SubscriptedEmptyArray, 
UnsignedLiteralTruncation,
     CompatibleDeclarationsFromDistinctModules,
     NullActualForDefaultIntentAllocatable, 
UseAssociationIntoSameNameSubprogram,
-    HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile)
+    HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
+    AccImplicitScalar)
 
 using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
 using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index 5455efde1a81f..7638bc7f51196 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -844,6 +844,14 @@ static bool parseFrontendArgs(FrontendOptions &opts, 
llvm::opt::ArgList &args,
                                     clang::driver::options::OPT_fno_unsigned,
                                     false));
 
+  // -f{no-}acc-allow-default-none-scalars
+  opts.features.Enable(
+      Fortran::common::LanguageFeature::AccDefaultNoneScalars,
+      args.hasFlag(
+          clang::driver::options::OPT_facc_allow_default_none_scalars,
+          clang::driver::options::OPT_fno_acc_allow_default_none_scalars,
+          false));
+
   // -f{no-}xor-operator
   opts.features.Enable(
       Fortran::common::LanguageFeature::XOROperator,
diff --git a/flang/lib/Semantics/resolve-directives.cpp 
b/flang/lib/Semantics/resolve-directives.cpp
index 299bb6ff876e7..14a253f301c48 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1500,10 +1500,30 @@ void AccAttributeVisitor::Post(const parser::Name 
&name) {
           name.symbol = found; // adjust the symbol within region
         } else if (GetContext().defaultDSA == Symbol::Flag::AccNone) {
           // 2.5.14.
-          context_.Say(name.source,
-              "The DEFAULT(NONE) clause requires that '%s' must be listed in "
-              "a data-mapping clause"_err_en_US,
-              symbol->name());
+          bool isScalarExtensionActive{false};
+          if (context_.IsEnabled(
+                  common::LanguageFeature::AccDefaultNoneScalars)) {
+            if (const auto *det{symbol->detailsIf<ObjectEntityDetails>()}) {
+              if (!det->IsArray()) {
+                // Pre-OpenACC-3.2: scalars without explicit data clauses get
+                // implicit attributes (firstprivate/reduction) via normal
+                // inference. Warn so the user knows they are using legacy
+                // behavior (-Wacc-implicit-scalar to suppress).
+                isScalarExtensionActive = true;
+                context_.Warn(common::UsageWarning::AccImplicitScalar,
+                    name.source,
+                    "'%s' has no data clause under DEFAULT(NONE); implicit "
+                    "attribute inferred (-facc-allow-default-none-scalars "
+                    "enables pre-OpenACC-3.2 behavior)"_warn_en_US,
+                    symbol->name());
+              }
+            }
+          }
+          if (!isScalarExtensionActive) {
+            context_.Say(name.source,
+                "The DEFAULT(NONE) clause requires that '%s' must be listed in 
a data-mapping clause"_err_en_US,
+                symbol->name());
+          }
         }
       }
     }
diff --git a/flang/lib/Support/Fortran-features.cpp 
b/flang/lib/Support/Fortran-features.cpp
index fc69fc638eda1..fcd4c29d0afef 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -90,6 +90,8 @@ LanguageFeatureControl::LanguageFeatureControl() {
   disable_.set(LanguageFeature::OldStyleParameter);
   // Possibly an accidental "feature" of nvfortran.
   disable_.set(LanguageFeature::AssumedRankPassedToNonAssumedRank);
+  // Pre-OpenACC-3.2 scalar behavior under DEFAULT(NONE): disabled by default.
+  disable_.set(LanguageFeature::AccDefaultNoneScalars);
   // These warnings are enabled by default, but only because they used
   // to be unconditional.  TODO: prune this list
   warnLanguage_.set(LanguageFeature::ExponentMatchingKindParam);
diff --git a/flang/test/Semantics/OpenACC/acc-default-none-scalars.f90 
b/flang/test/Semantics/OpenACC/acc-default-none-scalars.f90
new file mode 100644
index 0000000000000..329bfdebd8d3d
--- /dev/null
+++ b/flang/test/Semantics/OpenACC/acc-default-none-scalars.f90
@@ -0,0 +1,16 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenacc 
-facc-allow-default-none-scalars -Wacc-implicit-scalar
+
+! With -facc-allow-default-none-scalars, scalar variables without explicit
+! data clauses under DEFAULT(NONE) are allowed and get a warning
+! (-Wacc-implicit-scalar) instead of an error.  Arrays continue to error.
+
+subroutine default_none_scalars()
+  integer :: a
+  integer :: b(10)
+  !$acc parallel default(none)
+  !WARNING: 'a' has no data clause under DEFAULT(NONE); implicit attribute 
inferred (-facc-allow-default-none-scalars enables pre-OpenACC-3.2 behavior) 
[-Wacc-implicit-scalar]
+  a = 1
+  !ERROR: The DEFAULT(NONE) clause requires that 'b' must be listed in a 
data-mapping clause
+  b(1) = 2
+  !$acc end parallel
+end subroutine

``````````

</details>


https://github.com/llvm/llvm-project/pull/197718
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to