Skip to content

[flang][OpenMP] Make static duration variables default to shared DSA #142783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/include/flang/Semantics/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ class Symbol {
LocalityShared, // named in SHARED locality-spec
InDataStmt, // initialized in a DATA statement, =>object, or /init/
InNamelist, // in a Namelist group
InCommonBlock, // referenced in a common block
EntryDummyArgument,
CompilerCreated, // A compiler created symbol
// For compiler created symbols that are constant but cannot legally have
Expand Down
17 changes: 16 additions & 1 deletion flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,20 @@ static bool IsPrivatizable(const Symbol *sym) {
misc->kind() != MiscDetails::Kind::ConstructName));
}

static bool IsSymbolStaticStorageDuration(const Symbol &symbol) {
LLVM_DEBUG(llvm::dbgs() << "IsSymbolStaticStorageDuration(" << symbol.name()
<< "):\n");
auto ultSym = symbol.GetUltimate();
// Module-scope variable
return (ultSym.owner().kind() == Scope::Kind::Module) ||
// Data statement variable
(ultSym.flags().test(Symbol::Flag::InDataStmt)) ||
// Save attribute variable
(ultSym.attrs().test(Attr::SAVE)) ||
// Referenced in a common block
(ultSym.flags().test(Symbol::Flag::InCommonBlock));
}

void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) {
if (!IsPrivatizable(symbol)) {
return;
Expand Down Expand Up @@ -2310,6 +2324,7 @@ void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) {
bool targetDir = llvm::omp::allTargetSet.test(dirContext.directive);
bool parallelDir = llvm::omp::allParallelSet.test(dirContext.directive);
bool teamsDir = llvm::omp::allTeamsSet.test(dirContext.directive);
bool isStaticStorageDuration = IsSymbolStaticStorageDuration(*symbol);

if (dsa.any()) {
if (parallelDir || taskGenDir || teamsDir) {
Expand Down Expand Up @@ -2367,7 +2382,7 @@ void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) {
dsa = prevDSA;
} else if (taskGenDir) {
// TODO 5) dummy arg in orphaned taskgen construct -> firstprivate
if (prevDSA.test(Symbol::Flag::OmpShared)) {
if (prevDSA.test(Symbol::Flag::OmpShared) || isStaticStorageDuration) {
// 6) shared in enclosing context -> shared
dsa = {Symbol::Flag::OmpShared};
makeSymbol(dsa);
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6820,6 +6820,9 @@ bool DeclarationVisitor::Pre(const parser::CommonBlockObject &) {

void DeclarationVisitor::Post(const parser::CommonBlockObject &x) {
const auto &name{std::get<parser::Name>(x.t)};
if (auto *symbol{FindSymbol(name)}) {
symbol->set(Symbol::Flag::InCommonBlock);
}
DeclareObjectEntity(name);
auto pair{specPartState_.commonBlockObjects.insert(name.source)};
if (!pair.second) {
Expand Down
6 changes: 3 additions & 3 deletions flang/test/Semantics/OpenMP/common-block.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
! RUN: %flang_fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s

program main
!CHECK: a size=4 offset=0: ObjectEntity type: REAL(4)
!CHECK: b size=8 offset=4: ObjectEntity type: INTEGER(4) shape: 1_8:2_8
!CHECK: c size=4 offset=12: ObjectEntity type: REAL(4)
!CHECK: a (InCommonBlock) size=4 offset=0: ObjectEntity type: REAL(4)
!CHECK: b (InCommonBlock) size=8 offset=4: ObjectEntity type: INTEGER(4) shape: 1_8:2_8
!CHECK: c (InCommonBlock) size=4 offset=12: ObjectEntity type: REAL(4)
!CHECK: blk size=16 offset=0: CommonBlockDetails alignment=4: a b c
real :: a, c
integer :: b(2)
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Semantics/OpenMP/declare-target-common-block.f90
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
! RUN: %flang_fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s

PROGRAM main
!CHECK: one (OmpDeclareTarget) size=4 offset=0: ObjectEntity type: REAL(4)
!CHECK: two (OmpDeclareTarget) size=4 offset=4: ObjectEntity type: REAL(4)
!CHECK: one (InCommonBlock, OmpDeclareTarget) size=4 offset=0: ObjectEntity type: REAL(4)
!CHECK: two (InCommonBlock, OmpDeclareTarget) size=4 offset=4: ObjectEntity type: REAL(4)
!CHECK: numbers size=8 offset=0: CommonBlockDetails alignment=4: one two
REAL :: one, two
COMMON /numbers/ one, two
Expand Down
75 changes: 75 additions & 0 deletions flang/test/Semantics/OpenMP/implicit-dsa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,78 @@ subroutine implicit_dsa_test8
end do
!$omp end task
end subroutine

! Test variables defined in modules default to shared DSA
!DEF: /implicit_dsa_test9_mod Module
module implicit_dsa_test9_mod
!DEF: /implicit_dsa_test9_mod/tm3a PUBLIC (InDataStmt) ObjectEntity COMPLEX(4)
complex tm3a/(0,0)/
!DEF: /implicit_dsa_test9_mod/tm4a PUBLIC ObjectEntity COMPLEX(4)
complex tm4a
contains
!DEF: /implicit_dsa_test9_mod/implict_dsa_test9 PUBLIC (Subroutine) Subprogram
subroutine implict_dsa_test9
!$omp task
!$omp task
!DEF: /implicit_dsa_test9_mod/implict_dsa_test9/OtherConstruct1/OtherConstruct1/tm3a (OmpShared) HostAssoc COMPLEX(4)
tm3a = (1, 2)
!DEF: /implicit_dsa_test9_mod/implict_dsa_test9/OtherConstruct1/OtherConstruct1/tm4a (OmpShared) HostAssoc COMPLEX(4)
tm4a = (3, 4)
!$omp end task
!$omp end task
!$omp taskwait
!REF: /implicit_dsa_test9_mod/tm3a
print *,tm3a
end subroutine
end module

! Test variables in data statement default to shared DSA
!DEF: /implicit_dsa_test10 (Subroutine) Subprogram
subroutine implicit_dsa_test10
!DEF: /implicit_dsa_test10/tm3a (Implicit, InDataStmt) ObjectEntity REAL(4)
data tm3a /3/
!$omp task
!$omp task
!DEF: /implicit_dsa_test10/OtherConstruct1/OtherConstruct1/tm3a (OmpShared) HostAssoc REAL(4)
tm3a = 5
!$omp end task
!$omp end task
!$omp taskwait
!REF: /implicit_dsa_test10/tm3a
print *,tm3a
end subroutine

! Test variables with the SAVE attrtibute default to shared DSA
!DEF: /implicit_dsa_test_11 (Subroutine) Subprogram
subroutine implicit_dsa_test_11
!DEF: /implicit_dsa_test_11/tm3a SAVE ObjectEntity COMPLEX(4)
complex, save :: tm3a
!$omp task
!$omp task
!DEF: /implicit_dsa_test_11/OtherConstruct1/OtherConstruct1/tm3a (OmpShared) HostAssoc COMPLEX(4)
tm3a = (1, 2)
!$omp end task
!$omp end task
!$omp taskwait
!REF: /implicit_dsa_test_11/tm3a
print *,tm3a
end subroutine

! Test variables referenced in a common block default to shared DSA
!DEF: /implicit_dsa_test_12 (Subroutine) Subprogram
subroutine implicit_dsa_test_12
!DEF: /implicit_dsa_test_12/tm3a (InCommonBlock) ObjectEntity COMPLEX(4)
complex tm3a
!DEF: /implicit_dsa_test_12/tcom CommonBlockDetails
!REF: /implicit_dsa_test_12/tm3a
common /tcom/ tm3a
!$omp task
!$omp task
!DEF: /implicit_dsa_test_12/OtherConstruct1/OtherConstruct1/tm3a (OmpShared) HostAssoc COMPLEX(4)
tm3a = (1, 2)
!$omp end task
!$omp end task
!$omp taskwait
!REF: /implicit_dsa_test_12/tm3a
print *,tm3a
end subroutine
4 changes: 2 additions & 2 deletions flang/test/Semantics/OpenMP/symbol01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ program mm
!REF: /md
use :: md
!DEF: /mm/c CommonBlockDetails
!DEF: /mm/x ObjectEntity REAL(4)
!DEF: /mm/y ObjectEntity REAL(4)
!DEF: /mm/x (InCommonBlock) ObjectEntity REAL(4)
!DEF: /mm/y (InCommonBlock) ObjectEntity REAL(4)
common /c/x, y
!REF: /mm/x
!REF: /mm/y
Expand Down
10 changes: 5 additions & 5 deletions flang/test/Semantics/offsets03.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ subroutine mc !CHECK: Subprogram scope: mc size=12 alignment=1
! Common block: objects are in order from COMMON statement and not part of module
module md !CHECK: Module scope: md size=1 alignment=1
integer(1) :: i
integer(2) :: d1 !CHECK: d1, PUBLIC size=2 offset=8:
integer(4) :: d2 !CHECK: d2, PUBLIC size=4 offset=4:
integer(1) :: d3 !CHECK: d3, PUBLIC size=1 offset=0:
real(2) :: d4 !CHECK: d4, PUBLIC size=2 offset=0:
integer(2) :: d1 !CHECK: d1, PUBLIC (InCommonBlock) size=2 offset=8:
integer(4) :: d2 !CHECK: d2, PUBLIC (InCommonBlock) size=4 offset=4:
integer(1) :: d3 !CHECK: d3, PUBLIC (InCommonBlock) size=1 offset=0:
real(2) :: d4 !CHECK: d4, PUBLIC (InCommonBlock) size=2 offset=0:
common /common1/ d3,d2,d1 !CHECK: common1 size=10 offset=0: CommonBlockDetails alignment=4:
common /common2/ d4 !CHECK: common2 size=2 offset=0: CommonBlockDetails alignment=2:
end
Expand Down Expand Up @@ -71,7 +71,7 @@ module me
subroutine host1
contains
subroutine internal
common /b/ x(4) ! CHECK: x (Implicit) size=16 offset=0: ObjectEntity type: REAL(4) shape: 1_8:4_8
common /b/ x(4) ! CHECK: x (Implicit, InCommonBlock) size=16 offset=0: ObjectEntity type: REAL(4) shape: 1_8:4_8
equivalence(x,y) ! CHECK: y (Implicit) size=4 offset=0: ObjectEntity type: REAL(4)
end
end
4 changes: 2 additions & 2 deletions flang/test/Semantics/resolve121.f90
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ subroutine test3()
! CHECK-LABEL: Subprogram scope: test3
! CHECK: i1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4)
! CHECK: j1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4)
! CHECK: k1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4)
! CHECK: k1, SAVE (InCommonBlock) size=4 offset=0: ObjectEntity type: INTEGER(4)
integer :: i1
integer :: j1, k1
common /blk/ k1
Expand All @@ -37,7 +37,7 @@ subroutine test4()
! CHECK-LABEL: Subprogram scope: test4
! CHECK: i1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4) init:1_4
! CHECK: j1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4)
! CHECK: k1, SAVE size=4 offset=0: ObjectEntity type: INTEGER(4)
! CHECK: k1, SAVE (InCommonBlock) size=4 offset=0: ObjectEntity type: INTEGER(4)
integer :: i1 = 1
integer :: j1, k1
common /blk/ k1
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Semantics/symbol33.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
! array element reference still applies implicit typing, &c.
!DEF: /subr (Subroutine) Subprogram
subroutine subr
!DEF: /subr/moo (Implicit) ObjectEntity INTEGER(4)
!DEF: /subr/moo (Implicit, InCommonBlock) ObjectEntity INTEGER(4)
common //moo(1)
!DEF: /subr/a ObjectEntity REAL(4)
!REF: /subr/moo
Expand Down