-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[Flang][OpenMP] Make implicitly captured scalars fully firstprivatized #147442
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
#include "flang/Semantics/openmp-modifiers.h" | ||
#include "flang/Semantics/symbol.h" | ||
#include "flang/Semantics/tools.h" | ||
#include "flang/Support/Flags.h" | ||
#include "llvm/Frontend/OpenMP/OMP.h.inc" | ||
#include "llvm/Support/Debug.h" | ||
#include <list> | ||
|
@@ -2297,6 +2298,34 @@ static bool IsSymbolStaticStorageDuration(const Symbol &symbol) { | |
(ultSym.flags().test(Symbol::Flag::InCommonBlock)); | ||
} | ||
|
||
static bool IsTargetCaptureImplicitlyFirstPrivatizeable( | ||
const Symbol &symbol, const Symbol::Flags &dsa) { | ||
// if we're associated with any other flags we skip implicit privitization | ||
// for now. If we're an allocatable, pointer or declare target, we're not | ||
// implicitly firstprivitizeable under OpenMP restrictions. | ||
// TODO: Relax restriction as we progress privitization and further | ||
// investigate the flags we can intermix with. | ||
if (!dsa.none() || !symbol.flags().none() || | ||
semantics::IsAllocatableOrPointer(symbol)) { | ||
return false; | ||
} | ||
|
||
// It is default firstprivatizeable as far as the OpenMP specification is | ||
// concerned if it is a non-array scalar type that has been implicitly | ||
// captured in a target region | ||
const auto *type{symbol.GetType()}; | ||
if ((!symbol.GetShape() || symbol.GetShape()->empty()) && | ||
(type->category() == | ||
Fortran::semantics::DeclTypeSpec::Category::Numeric || | ||
type->category() == | ||
Fortran::semantics::DeclTypeSpec::Category::Logical || | ||
type->category() == | ||
Fortran::semantics::DeclTypeSpec::Category::Character)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) { | ||
if (!IsPrivatizable(symbol)) { | ||
return; | ||
|
@@ -2444,7 +2473,15 @@ void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) { | |
useLastDeclSymbol(); | ||
PRINT_IMPLICIT_RULE("3) enclosing context"); | ||
} else if (targetDir) { | ||
// TODO 4) not mapped target variable -> firstprivate | ||
// 4) not mapped target variable -> firstprivate | ||
// - i.e. implicit, but meets OpenMP specification rules for | ||
// firstprivate "promotion" | ||
if (enableDelayedPrivatizationStaging && symbol && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
IsTargetCaptureImplicitlyFirstPrivatizeable(*symbol, prevDSA)) { | ||
prevDSA.set(Symbol::Flag::OmpImplicit); | ||
prevDSA.set(Symbol::Flag::OmpFirstPrivate); | ||
makeSymbol(prevDSA); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add this line, to keep debug messages consistent: |
||
dsa = prevDSA; | ||
} else if (taskGenDir) { | ||
// TODO 5) dummy arg in orphaned taskgen construct -> firstprivate | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
! Tests delayed privatization works for implicit capture of scalars similarly to | ||
! the way it works for explicitly firstprivitized scalars. | ||
|
||
! RUN: %flang_fc1 -emit-mlir -fopenmp -mmlir --enable-delayed-privatization-staging \ | ||
! RUN: -o - %s 2>&1 | FileCheck %s | ||
|
||
! CHECK-LABEL: omp.private {type = firstprivate} @_QFExdgfx_firstprivate_i32 : i32 copy { | ||
! CHECK: ^bb0(%{{.*}}: !fir.ref<i32>, %{{.*}}: !fir.ref<i32>): | ||
! CHECK: %{{.*}} = fir.load %{{.*}} : !fir.ref<i32> | ||
! CHECK: fir.store %{{.*}} to %{{.*}} : !fir.ref<i32> | ||
! CHECK: omp.yield(%{{.*}} : !fir.ref<i32>) | ||
! CHECK: } | ||
|
||
! CHECK-LABEL: omp.private {type = firstprivate} @_QFExfpvx_firstprivate_i32 : i32 copy { | ||
! CHECK: ^bb0(%{{.*}}: !fir.ref<i32>, %{{.*}}: !fir.ref<i32>): | ||
! CHECK: %{{.*}} = fir.load %{{.*}} : !fir.ref<i32> | ||
! CHECK: fir.store %{{.*}} to %{{.*}} : !fir.ref<i32> | ||
! CHECK: omp.yield(%{{.*}} : !fir.ref<i32>) | ||
! CHECK: } | ||
|
||
! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "xdgfx", uniq_name = "_QFExdgfx"} | ||
! CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "_QFExdgfx"} : (!fir.ref<i32>) -> !fir.ref<i32> | ||
! CHECK: %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "xfpvx", uniq_name = "_QFExfpvx"} | ||
! CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_2]] {uniq_name = "_QFExfpvx"} : (!fir.ref<i32>) -> !fir.ref<i32> | ||
! CHECK: %[[VAL_4:.*]] = omp.map.info var_ptr(%[[VAL_3]] : !fir.ref<i32>, i32) map_clauses(to) capture(ByRef) -> !fir.ref<i32> | ||
! CHECK: %[[VAL_5:.*]] = omp.map.info var_ptr(%[[VAL_1]] : !fir.ref<i32>, i32) map_clauses(to) capture(ByRef) -> !fir.ref<i32> | ||
|
||
! CHECK: omp.target map_entries(%[[VAL_4]] -> %{{.*}}, %[[VAL_5]] -> %{{.*}} : !fir.ref<i32>, !fir.ref<i32>) private(@_QFExfpvx_firstprivate_i32 %[[VAL_3]] -> %[[VAL_6:.*]] [map_idx=0], @_QFExdgfx_firstprivate_i32 %[[VAL_1]] -> %[[VAL_7:.*]] [map_idx=1] : !fir.ref<i32>, !fir.ref<i32>) { | ||
! CHECK: %{{.*}} = fir.declare %[[VAL_6]] {uniq_name = "_QFExfpvx"} : (!fir.ref<i32>) -> !fir.ref<i32> | ||
! CHECK: %{{.*}} = fir.declare %[[VAL_7]] {uniq_name = "_QFExdgfx"} : (!fir.ref<i32>) -> !fir.ref<i32> | ||
|
||
program test_default_implicit_firstprivate | ||
implicit none | ||
integer :: xdgfx, xfpvx | ||
xdgfx = 1 | ||
xfpvx = 2 | ||
!$omp target firstprivate(xfpvx) | ||
xdgfx = 42 | ||
xfpvx = 43 | ||
!$omp end target | ||
write(*,*) xdgfx, xfpvx | ||
end program |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible that other flags not related to DSA may be set.
If you want to consider only DSA and data-mapping related flags, you can use:
(dsa & (dataSharingAttributeFlags | dataMappingAttributeFlags)).none()
Note that
dataMappingAttributeFlags
would need to be moved from OmpAttributeVisitor::ResolveOmpObject() to OmpAttributeVisitor.