Skip to content

Commit 5198923

Browse files
authored
[flang][OpenMP] Allow common blocks in nested directives (#88430)
COMMON block names must be declared in the same scoping unit in which the OpenMP directive or clause appears, but OpenMP constructs must not be considered as scoping units. Instead, consider only program units and block constructs as such.
1 parent fe28a0e commit 5198923

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,15 +2096,10 @@ Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName(
20962096
if (!name) {
20972097
return nullptr;
20982098
}
2099-
// First check if the Common Block is declared in the current scope
2100-
if (auto *cur{GetContext().scope.FindCommonBlock(name->source)}) {
2101-
name->symbol = cur;
2102-
return cur;
2103-
}
2104-
// Then check parent scope
2105-
if (auto *prev{GetContext().scope.parent().FindCommonBlock(name->source)}) {
2106-
name->symbol = prev;
2107-
return prev;
2099+
if (auto *cb{GetProgramUnitOrBlockConstructContaining(GetContext().scope)
2100+
.FindCommonBlock(name->source)}) {
2101+
name->symbol = cb;
2102+
return cb;
21082103
}
21092104
return nullptr;
21102105
}

flang/test/Semantics/OpenMP/resolve03.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
common /c/ a, b
1010
integer a(3), b
11+
common /tc/ x
12+
integer x
13+
!$omp threadprivate(/tc/)
1114

1215
A = 1
1316
B = 2
@@ -19,4 +22,26 @@
1922
!$omp end parallel
2023
end block
2124
print *, a, b
25+
26+
!$omp parallel
27+
block
28+
!$omp single
29+
x = 18
30+
!ERROR: COMMON block must be declared in the same scoping unit in which the OpenMP directive or clause appears
31+
!$omp end single copyprivate(/tc/)
32+
end block
33+
!$omp end parallel
34+
35+
! Common block names may be used inside nested OpenMP directives.
36+
!$omp parallel
37+
!$omp parallel copyin(/tc/)
38+
x = x + 10
39+
!$omp end parallel
40+
!$omp end parallel
41+
42+
!$omp parallel
43+
!$omp single
44+
x = 18
45+
!$omp end single copyprivate(/tc/)
46+
!$omp end parallel
2247
end

0 commit comments

Comments
 (0)