Skip to content

[flang][OpenMP] Fix threadprivate common blocks #68739

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

Merged
merged 1 commit into from
Oct 16, 2023
Merged
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
19 changes: 10 additions & 9 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,18 +1944,19 @@ void OmpAttributeVisitor::ResolveOmpNameList(

Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName(
const parser::Name *name) {
if (auto *prev{name
? GetContext().scope.parent().FindCommonBlock(name->source)
: nullptr}) {
if (!name) {
return nullptr;
}
// First check if the Common Block is declared in the current scope
if (auto *cur{GetContext().scope.FindCommonBlock(name->source)}) {
name->symbol = cur;
return cur;
}
// Then check parent scope
if (auto *prev{GetContext().scope.parent().FindCommonBlock(name->source)}) {
name->symbol = prev;
return prev;
}
// Check if the Common Block is declared in the current scope
if (auto *commonBlockSymbol{
name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) {
name->symbol = commonBlockSymbol;
return commonBlockSymbol;
}
return nullptr;
}

Expand Down
30 changes: 30 additions & 0 deletions flang/test/Semantics/OpenMP/threadprivate06.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
! OpenMP Version 5.1
! Check OpenMP construct validity for the following directives:
! 2.21.2 Threadprivate Directive

program main
call sub1()
print *, 'pass'
end program main

subroutine sub1()
common /c/ a
!$omp threadprivate(/c/)
integer :: a

a = 100
call sub2()
if (a .ne. 101) print *, 'err'

contains
subroutine sub2()
common /c/ a
!$omp threadprivate(/c/)
integer :: a

!$omp parallel copyin(/c/)
a = a + 1
!$omp end parallel
end subroutine
end subroutine