[flang][OpenMP] Privatize vars referenced in statement functions by luporl · Pull Request #103390 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-flang-fir-hlfir
@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-openmp

Author: Leandro Lupori (luporl)

Changes

Variables referenced in the body of statement functions need to be
handled as if they are explicitly referenced. Otherwise, they are
skipped during implicit privatization, because statement functions
are represented as procedures in the parse tree.

To avoid missing symbols referenced only in statement functions
during implicit privatization, new symbols, associated with them,
are created and inserted into the context of the directive that
privatizes them. They are later collected and processed in
lowering. To avoid confusing these new symbols with regular ones,
they are tagged with the new OmpFromStmtFunction flag.

Fixes #74273


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

4 Files Affected:

diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h index cf0350735b5b9..b4db6689a9427 100644 --- a/flang/include/flang/Semantics/symbol.h +++ b/flang/include/flang/Semantics/symbol.h @@ -755,7 +755,7 @@ class Symbol { OmpDeclarativeAllocateDirective, OmpExecutableAllocateDirective, OmpDeclareSimd, OmpDeclareTarget, OmpThreadprivate, OmpDeclareReduction, OmpFlushed, OmpCriticalLock, OmpIfSpecified, OmpNone, OmpPreDetermined,

diff --git a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp index e1a193edc004a..1b2f926e21bed 100644 --- a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp +++ b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp @@ -402,6 +402,15 @@ void DataSharingProcessor::collectSymbols( /collectSymbols=/true, /collectHostAssociatedSymbols=/true);

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp index d635a7b8b7874..04642aa4e0279 100644 --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -91,11 +91,12 @@ template class DirectiveAttributeVisitor { void SetContextAssociatedLoopLevel(std::int64_t level) { GetContext().associatedLoopLevel = level; }

+template +Symbol *DirectiveAttributeVisitor::DeclareNewPrivateAccessEntity(

@@ -785,13 +800,7 @@ template Symbol *DirectiveAttributeVisitor::DeclarePrivateAccessEntity( Symbol &object, Symbol::Flag flag, Scope &scope) { if (object.owner() != currScope()) {

} else { object.set(flag); return &object; @@ -2075,13 +2084,30 @@ void OmpAttributeVisitor::Post(const parser::Name &name) { if (found->test(semantics::Symbol::Flag::OmpThreadprivate)) return; }

@@ -2190,7 +2252,7 @@ void OmpAttributeVisitor::Post(const parser::Name &name) { } else { // 7) firstprivate dsa = Symbol::Flag::OmpFirstPrivate;

diff --git a/flang/test/Lower/OpenMP/statement-function.f90 b/flang/test/Lower/OpenMP/statement-function.f90 new file mode 100644 index 0000000000000..6cdbcb6e141c7 --- /dev/null +++ b/flang/test/Lower/OpenMP/statement-function.f90 @@ -0,0 +1,43 @@ +! Test privatization within OpenMP constructs containing statement functions. +! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s + +!CHECK-LABEL: func @_QPtest_implicit_use +!CHECK: %[[IEXP:.]]:2 = hlfir.declare %{{.}} {uniq_name = "_QFtest_implicit_useEiexp"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %[[IIMP:.]]:2 = hlfir.declare %{{.}} {uniq_name = "_QFtest_implicit_useEiimp"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: omp.parallel private({{.firstprivate.}} %[[IEXP]]#0 -> %[[PRIV_IEXP:.]] : !fir.ref, +!CHECK-SAME: {{.firstprivate.}} %[[IIMP]]#0 -> %[[PRIV_IIMP:.]] : !fir.ref) +!CHECK: %{{.}}:2 = hlfir.declare %[[PRIV_IEXP]] {uniq_name = "_QFtest_implicit_useEiexp"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %{{.}}:2 = hlfir.declare %[[PRIV_IIMP]] {uniq_name = "_QFtest_implicit_useEiimp"} : (!fir.ref) -> (!fir.ref, !fir.ref) +subroutine test_implicit_use()