[flang-commits] [flang] 1f9c53c - [flang][cuda] Emit better error when subprogram attribute is absent or bad (#176501)
via flang-commits
flang-commits at lists.llvm.org
Fri Jan 16 16:26:39 PST 2026
Author: Valentin Clement (バレンタイン クレメン)
Date: 2026-01-16T16:26:33-08:00
New Revision: 1f9c53c6a35ecfdf81a4f80169e31860c5158d51
URL: https://github.com/llvm/llvm-project/commit/1f9c53c6a35ecfdf81a4f80169e31860c5158d51
DIFF: https://github.com/llvm/llvm-project/commit/1f9c53c6a35ecfdf81a4f80169e31860c5158d51.diff
LOG: [flang][cuda] Emit better error when subprogram attribute is absent or bad (#176501)
this patch update the parser for CUDA Fortran subprogram attribute to
emit more precise error.
Instead of having error like:
```
error: expected 'END'
attributes(managed) integer function fooj()
^
```
The parser will emit:
```
expected DEVICE, GLOBAL, GRID_GLOBAL, or HOST attribute
attributes(managed) integer function fooj()
^
```
Added:
flang/test/Semantics/cuf-proc-attr-error.cuf
Modified:
flang/lib/Parser/program-parsers.cpp
Removed:
################################################################################
diff --git a/flang/lib/Parser/program-parsers.cpp b/flang/lib/Parser/program-parsers.cpp
index 303335934a37a..36a9c00a4f30d 100644
--- a/flang/lib/Parser/program-parsers.cpp
+++ b/flang/lib/Parser/program-parsers.cpp
@@ -533,10 +533,12 @@ TYPE_PARSER(construct<AltReturnSpec>(star >> label))
// NON_RECURSIVE | PURE | RECURSIVE |
// (CUDA) ATTRIBUTES ( (DEVICE | GLOBAL | GRID_GLOBAL | HOST)... ) |
// LAUNCH_BOUNDS(expr-list) | CLUSTER_DIMS(expr-list)
-TYPE_PARSER(first("DEVICE" >> pure(common::CUDASubprogramAttrs::Device),
- "GLOBAL" >> pure(common::CUDASubprogramAttrs::Global),
- "GRID_GLOBAL" >> pure(common::CUDASubprogramAttrs::Grid_Global),
- "HOST" >> pure(common::CUDASubprogramAttrs::Host)))
+TYPE_PARSER(withMessage(
+ "expected DEVICE, GLOBAL, GRID_GLOBAL, or HOST attribute"_err_en_US,
+ first("DEVICE" >> pure(common::CUDASubprogramAttrs::Device),
+ "GLOBAL" >> pure(common::CUDASubprogramAttrs::Global),
+ "GRID_GLOBAL" >> pure(common::CUDASubprogramAttrs::Grid_Global),
+ "HOST" >> pure(common::CUDASubprogramAttrs::Host))))
TYPE_PARSER(first(construct<PrefixSpec>(declarationTypeSpec),
construct<PrefixSpec>(construct<PrefixSpec::Elemental>("ELEMENTAL"_tok)),
construct<PrefixSpec>(construct<PrefixSpec::Impure>("IMPURE"_tok)),
@@ -546,9 +548,12 @@ TYPE_PARSER(first(construct<PrefixSpec>(declarationTypeSpec),
construct<PrefixSpec>(construct<PrefixSpec::Pure>("PURE"_tok)),
construct<PrefixSpec>(construct<PrefixSpec::Recursive>("RECURSIVE"_tok)),
extension<LanguageFeature::CUDA>(
- construct<PrefixSpec>(construct<PrefixSpec::Attributes>("ATTRIBUTES" >>
- parenthesized(
- optionalList(Parser<common::CUDASubprogramAttrs>{}))))),
+ construct<PrefixSpec>(construct<PrefixSpec::Attributes>(
+ localRecovery("expected valid ATTRIBUTES specification"_err_en_US,
+ "ATTRIBUTES" >> parenthesized(nonemptyList(
+ Parser<common::CUDASubprogramAttrs>{})),
+ "ATTRIBUTES" >> SkipTo<')'>{} >> ")"_ch >>
+ pure<std::list<common::CUDASubprogramAttrs>>())))),
extension<LanguageFeature::CUDA>(construct<PrefixSpec>(
construct<PrefixSpec::Launch_Bounds>("LAUNCH_BOUNDS" >>
parenthesized(nonemptyList(
diff --git a/flang/test/Semantics/cuf-proc-attr-error.cuf b/flang/test/Semantics/cuf-proc-attr-error.cuf
new file mode 100644
index 0000000000000..54bed74c27687
--- /dev/null
+++ b/flang/test/Semantics/cuf-proc-attr-error.cuf
@@ -0,0 +1,9 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Test conflicting CUDA subprogram attributes
+module m1
+ contains
+ !ERROR: expected DEVICE, GLOBAL, GRID_GLOBAL, or HOST attribute
+ attributes() subroutine empty1; end
+ !ERROR: expected DEVICE, GLOBAL, GRID_GLOBAL, or HOST attribute
+ attributes(managed) subroutine badAttr1; end
+end
More information about the flang-commits
mailing list