[flang-commits] [flang] [flang][openacc] Ignore bare acc routine in module subprogram part instead of erroring out (PR #205450)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Tue Jun 23 16:55:11 PDT 2026


https://github.com/clementval updated https://github.com/llvm/llvm-project/pull/205450

>From eaced713de403fed0784efb16078d97dcee50190 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Tue, 23 Jun 2026 16:15:14 -0700
Subject: [PATCH 1/2] [flang][openacc] Ignore bare acc routine in module
 subprogram part instead of erroring out

---
 flang/lib/Parser/program-parsers.cpp         |  8 ++++++++
 flang/test/Lower/OpenACC/acc-routine-bad.f90 | 11 +++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 flang/test/Lower/OpenACC/acc-routine-bad.f90

diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index da8cd6fa27b65..a73dde8f5c81c 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -88,6 +88,13 @@ static constexpr auto globalOpenACCCompilerDirective{
     construct<ProgramUnit>(indirect(skipStuffBeforeStatement >>
         "!$ACC "_sptok >> Parser<OpenACCRoutineConstruct>{} / endOfLine))};
 
+// In a module-subprogram-part, a bare `!$ACC ROUTINE` line (without a name)
+// cannot be attached to a specific routine. Accept and ignore it so parsing
+// can continue to the following subprogram.
+static constexpr auto ignoredBareRoutineOpenACCDirective{
+    (skipStuffBeforeStatement >> "!$ACC "_sptok >> "ROUTINE"_tok / endOfLine) >>
+    construct<CompilerDirective>(pure<CompilerDirective::Unrecognized>())};
+
 // R501 program -> program-unit [program-unit]...
 // This is the top-level production for the Fortran language.
 TYPE_PARSER(construct<Program>(skipStuffBeforeStatement >>
@@ -294,6 +301,7 @@ TYPE_CONTEXT_PARSER("module subprogram part"_en_US,
 TYPE_PARSER(construct<ModuleSubprogram>(indirect(functionSubprogram)) ||
     construct<ModuleSubprogram>(indirect(subroutineSubprogram)) ||
     construct<ModuleSubprogram>(indirect(Parser<SeparateModuleSubprogram>{})) ||
+    construct<ModuleSubprogram>(indirect(ignoredBareRoutineOpenACCDirective)) ||
     construct<ModuleSubprogram>(indirect(compilerDirective)))
 
 // R1410 module-nature -> INTRINSIC | NON_INTRINSIC
diff --git a/flang/test/Lower/OpenACC/acc-routine-bad.f90 b/flang/test/Lower/OpenACC/acc-routine-bad.f90
new file mode 100644
index 0000000000000..1d9b73c12ac7d
--- /dev/null
+++ b/flang/test/Lower/OpenACC/acc-routine-bad.f90
@@ -0,0 +1,11 @@
+! RUN: bbc -fopenacc -emit-hlfir %s -o - 2>&1 | FileCheck %s
+
+module acc_routine_bad
+
+contains
+!$acc routine
+subroutine acc_routine20()
+end subroutine
+end module
+
+!CHECK: {{.*}}warning: Compiler directive ignored here{{.*}}

>From bd271e03b9c8e5b90825df4136493845c7f40ddf Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Tue, 23 Jun 2026 16:54:43 -0700
Subject: [PATCH 2/2] Add more precise warning

---
 flang/lib/Parser/program-parsers.cpp         | 15 ++++++++++++++-
 flang/test/Lower/OpenACC/acc-routine-bad.f90 |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index a73dde8f5c81c..dcef0a6742db7 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -88,11 +88,24 @@ static constexpr auto globalOpenACCCompilerDirective{
     construct<ProgramUnit>(indirect(skipStuffBeforeStatement >>
         "!$ACC "_sptok >> Parser<OpenACCRoutineConstruct>{} / endOfLine))};
 
+struct WarnUnnamedOpenACCRoutineDirective {
+  using resultType = Success;
+  std::optional<Success> Parse(ParseState &state) const {
+    state.Say("OpenACC routine directive without name must be placed in a "
+              "subroutine or function"_warn_en_US);
+    return {Success{}};
+  }
+};
+static constexpr WarnUnnamedOpenACCRoutineDirective
+    warnUnnamedOpenACCRoutineDirective;
+
 // In a module-subprogram-part, a bare `!$ACC ROUTINE` line (without a name)
 // cannot be attached to a specific routine. Accept and ignore it so parsing
 // can continue to the following subprogram.
 static constexpr auto ignoredBareRoutineOpenACCDirective{
-    (skipStuffBeforeStatement >> "!$ACC "_sptok >> "ROUTINE"_tok / endOfLine) >>
+    ((skipStuffBeforeStatement >> "!$ACC "_sptok >>
+         "ROUTINE"_tok / endOfLine) >>
+        warnUnnamedOpenACCRoutineDirective) >>
     construct<CompilerDirective>(pure<CompilerDirective::Unrecognized>())};
 
 // R501 program -> program-unit [program-unit]...
diff --git a/flang/test/Lower/OpenACC/acc-routine-bad.f90 b/flang/test/Lower/OpenACC/acc-routine-bad.f90
index 1d9b73c12ac7d..f1cead6633a65 100644
--- a/flang/test/Lower/OpenACC/acc-routine-bad.f90
+++ b/flang/test/Lower/OpenACC/acc-routine-bad.f90
@@ -8,4 +8,4 @@ subroutine acc_routine20()
 end subroutine
 end module
 
-!CHECK: {{.*}}warning: Compiler directive ignored here{{.*}}
+!CHECK: {{.*}}warning: OpenACC routine directive without name must be placed in a subroutine or function



More information about the flang-commits mailing list