Skip to content

Commit 499d41c

Browse files
authored
[flang][OpenMP] Fix threadprivate common blocks (#68739)
Using a threadprivate common block within a nested scope resulted in compilation errors. This happened because common block names were being first resolved to those in the parent scope. Because of this, in a nested scope, an inner threadprivate directive would be applied to the outter common block. This caused a 'common_block appears in more than one data-sharing clause' error. Also, when a copyin clause in a parallel region tried to use the common block, getting the inner version of it, their objects would be missing the threadprivate attribute, causing a 'Non-THREADPRIVATE object in COPYIN clause' error. Fixes #61200
1 parent de9b3c5 commit 499d41c

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,18 +1947,19 @@ void OmpAttributeVisitor::ResolveOmpNameList(
19471947

19481948
Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName(
19491949
const parser::Name *name) {
1950-
if (auto *prev{name
1951-
? GetContext().scope.parent().FindCommonBlock(name->source)
1952-
: nullptr}) {
1950+
if (!name) {
1951+
return nullptr;
1952+
}
1953+
// First check if the Common Block is declared in the current scope
1954+
if (auto *cur{GetContext().scope.FindCommonBlock(name->source)}) {
1955+
name->symbol = cur;
1956+
return cur;
1957+
}
1958+
// Then check parent scope
1959+
if (auto *prev{GetContext().scope.parent().FindCommonBlock(name->source)}) {
19531960
name->symbol = prev;
19541961
return prev;
19551962
}
1956-
// Check if the Common Block is declared in the current scope
1957-
if (auto *commonBlockSymbol{
1958-
name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) {
1959-
name->symbol = commonBlockSymbol;
1960-
return commonBlockSymbol;
1961-
}
19621963
return nullptr;
19631964
}
19641965

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
2+
! OpenMP Version 5.1
3+
! Check OpenMP construct validity for the following directives:
4+
! 2.21.2 Threadprivate Directive
5+
6+
program main
7+
call sub1()
8+
print *, 'pass'
9+
end program main
10+
11+
subroutine sub1()
12+
common /c/ a
13+
!$omp threadprivate(/c/)
14+
integer :: a
15+
16+
a = 100
17+
call sub2()
18+
if (a .ne. 101) print *, 'err'
19+
20+
contains
21+
subroutine sub2()
22+
common /c/ a
23+
!$omp threadprivate(/c/)
24+
integer :: a
25+
26+
!$omp parallel copyin(/c/)
27+
a = a + 1
28+
!$omp end parallel
29+
end subroutine
30+
end subroutine

0 commit comments

Comments
 (0)