https://github.com/kparzysz updated https://github.com/llvm/llvm-project/pull/243868
>From a70249a37451b49b3f0c43db2a6950e7fa0d97d3 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek <[email protected]> Date: Wed, 18 Nov 2026 06:35:56 -0600 Subject: [PATCH] [flang][OpenMP] Do not register DECLARE_REDUCTIONs in METADIRECTIVEs Since declarative METADIRECTIVEs are not handled yet, they shouldn't produce any side-effects. Specifically DECLARE_REDUCTION directives should not actually declare anything. This PR eliminates the actual declaration of the reduction-identifier associated with a DECLARE_REDUCTION in a METADIRECTIVE. --- flang/lib/Semantics/resolve-names.cpp | 52 ++++++++++++++----- .../OpenMP/declare-reduction-modfile.f90 | 1 - ...etadirective-declare-reduction-ignored.f90 | 16 ++++++ 3 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 flang/test/Semantics/OpenMP/metadirective-declare-reduction-ignored.f90 diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp index 180818b193aaf6..1199ef2b245613 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -188,6 +188,7 @@ class BaseVisitor { // Make a placeholder symbol for a Name that otherwise wouldn't have one. // It is not in any scope and always has MiscDetails. + Symbol *MakePlaceholder(parser::CharBlock name, MiscDetails::Kind kind); void MakePlaceholder(const parser::Name &, MiscDetails::Kind); template <typename T> common::IfNoLvalue<T, T> FoldExpr(T &&expr) { @@ -2194,6 +2195,22 @@ void OmpVisitor::ProcessMapperSpecifier(const parser::OmpMapperSpecifier &spec, void OmpVisitor::ProcessReductionSpecifier( const parser::OmpReductionSpecifier &spec, const parser::OmpClauseList &clauses) { + // Even if this directive is invalid or ignored for any reason, there is + // still some processing that needs to happen in order for all Name's + // embedded in the reduction specifier and the clauses to have symbols. + bool ignore{false}; + + // Ignore declarative METADIRECTIVEs for now. Those are not handled yet. + // Also, the top of the declaratives_ stack can be a nullptr if this + // directive is (incorrectly) placed in the execution part. + const parser::OpenMPDeclarativeConstruct *ompDecl{declaratives_.back()}; + auto dirId = ompDecl + ? parser::omp::GetOmpDirectiveSpecification(*ompDecl).DirId() + : llvm::omp::Directive::OMPD_nothing; + if (dirId != llvm::omp::OMPD_declare_reduction) { + ignore = true; + } + const parser::Name *name{nullptr}; parser::CharBlock mangledName; UserReductionDetails reductionDetailsTemp; @@ -2224,7 +2241,7 @@ void OmpVisitor::ProcessReductionSpecifier( UserReductionDetails *reductionDetails{&reductionDetailsTemp}; // Use scope-local lookup to avoid false matches from parent scopes. Symbol *symbol{FindInScope(currScope(), mangledName)}; - if (symbol) { + if (!ignore && symbol) { if (symbol->detailsIf<UseDetails>()) { // USE-associated reduction: shadow it with a new local declaration. EraseSymbol(*symbol); @@ -2243,22 +2260,29 @@ void OmpVisitor::ProcessReductionSpecifier( } } - reductionDetails->AddDecl(declaratives_.back()); - // Do not walk OmpTypeNameList. The types on the list will be visited - // during procesing of OmpCombinerExpression. + // during procesing of OmpCombinerExpression. The traversal of + // OmpCombinerExpression will also create symbols for omp_* variables, + // so it has to run regardless of whether this directive is valid + // or not. Walk(std::get<std::optional<parser::OmpCombinerExpression>>(spec.t)); Walk(clauses); - for (auto &type : std::get<parser::OmpTypeNameList>(spec.t).v) { - // The declTypeSpec can be null if there is some semantic error. - if (type.declTypeSpec) { - reductionDetails->AddType(*type.declTypeSpec); + if (!ignore) { + reductionDetails->AddDecl(declaratives_.back()); + + for (auto &type : std::get<parser::OmpTypeNameList>(spec.t).v) { + // The declTypeSpec can be null if there is some semantic error. + if (type.declTypeSpec) { + reductionDetails->AddType(*type.declTypeSpec); + } } } - if (!symbol) { - symbol = &MakeSymbol(mangledName, Attrs{}, std::move(*reductionDetails)); + if (ignore || !symbol) { + symbol = ignore + ? MakePlaceholder(mangledName, MiscDetails::Kind::None) + : &MakeSymbol(mangledName, Attrs{}, std::move(*reductionDetails)); } if (name) { name->symbol = symbol; @@ -2570,11 +2594,15 @@ template <typename T> void BaseVisitor::Walk(const T &x) { parser::Walk(x, *this_); } +Symbol *BaseVisitor::MakePlaceholder( + parser::CharBlock name, MiscDetails::Kind kind) { + return &context_->globalScope().MakeSymbol(name, Attrs{}, MiscDetails{kind}); +} + void BaseVisitor::MakePlaceholder( const parser::Name &name, MiscDetails::Kind kind) { if (!name.symbol) { - name.symbol = &context_->globalScope().MakeSymbol( - name.source, Attrs{}, MiscDetails{kind}); + name.symbol = MakePlaceholder(name.source, kind); } } diff --git a/flang/test/Semantics/OpenMP/declare-reduction-modfile.f90 b/flang/test/Semantics/OpenMP/declare-reduction-modfile.f90 index 763179cb52a13f..1d4a7a6be20251 100644 --- a/flang/test/Semantics/OpenMP/declare-reduction-modfile.f90 +++ b/flang/test/Semantics/OpenMP/declare-reduction-modfile.f90 @@ -8,7 +8,6 @@ !endtype !!$OMP DECLARE REDUCTION(*:t1: omp_out=omp_out*omp_in) INITIALIZER(omp_priv=t1(& !!$OMP&1)) -!!$OMP METADIRECTIVE OTHERWISE(DECLARE REDUCTION(+:INTEGER)) !!$OMP DECLARE REDUCTION(.fluffy.:t1: omp_out=omp_out.fluffy.omp_in) INITIALIZE& !!$OMP&R(omp_priv=t1(0)) !!$OMP DECLARE REDUCTION(.mul.:t1: omp_out=omp_out.mul.omp_in) INITIALIZER(omp_& diff --git a/flang/test/Semantics/OpenMP/metadirective-declare-reduction-ignored.f90 b/flang/test/Semantics/OpenMP/metadirective-declare-reduction-ignored.f90 new file mode 100644 index 00000000000000..9bfe70ec438bb4 --- /dev/null +++ b/flang/test/Semantics/OpenMP/metadirective-declare-reduction-ignored.f90 @@ -0,0 +1,16 @@ +!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=60 -Werror -Wno-experimental-option + +! Check that we don't process the DECLARE_REDUCTION inside the METADIRECTIVE. +! The way that this test detects it is that it tries to trigger a conflict +! between a variable FOO and the reduction named FOO. + +! Declarative METADIRECTIVEs are not handled yet. Once that's implemented +! this test will have to go away. At the moment it's just the check that +! unhandled situations don't interfere with valid code. + +subroutine f00 + integer :: foo + !WARNING: The declarative form of the METADIRECTIVE directive is not implemented yet and will be ignored [-Wopenmp-usage] + !$omp metadirective when(user={condition(.false.)}: & + !$omp & declare_reduction(foo : integer) combiner(omp_out = omp_in)) +end _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
