Skip to content

[flang][OpenMP] use attribute for delayed privatization barrier #140092

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 4 commits into from
May 22, 2025
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
15 changes: 12 additions & 3 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void DataSharingProcessor::processStep1(

privatize(clauseOps);

insertBarrier();
insertBarrier(clauseOps);
}

void DataSharingProcessor::processStep2(mlir::Operation *op, bool isLoop) {
Expand Down Expand Up @@ -230,9 +230,18 @@ bool DataSharingProcessor::needBarrier() {
return false;
}

void DataSharingProcessor::insertBarrier() {
if (needBarrier())
void DataSharingProcessor::insertBarrier(
mlir::omp::PrivateClauseOps *clauseOps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mlir::omp::PrivateClauseOps *clauseOps) {
mlir::omp::PrivateClauseOps &clauseOps) {

If clauseOps can't be NULL, it would be better to use a reference.

if (!needBarrier())
return;

if (useDelayedPrivatization) {
if (clauseOps)
clauseOps->privateNeedsBarrier =
mlir::UnitAttr::get(&converter.getMLIRContext());
} else {
firOpBuilder.create<mlir::omp::BarrierOp>(converter.getCurrentLocation());
}
}

void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/OpenMP/DataSharingProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class DataSharingProcessor {
const omp::ObjectList &objects,
llvm::SetVector<const semantics::Symbol *> &symbolSet);
void collectSymbolsForPrivatization();
void insertBarrier();
void insertBarrier(mlir::omp::PrivateClauseOps *clauseOps);
void collectDefaultSymbols();
void collectImplicitSymbols();
void collectPreDeterminedSymbols();
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenMP/lastprivate-allocatable.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
! CHECK: fir.store %[[VAL_2]] to %[[VAL_0]] : !fir.ref<!fir.box<!fir.heap<i32>>>
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = {{.*}}<allocatable>, uniq_name = "_QFEa"} : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> (!fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.heap<i32>>>)
! CHECK: omp.parallel {
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %{{.*}}, @{{.*}} %{{.*}} -> %[[VAL_17:.*]] : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<i32>) {
! CHECK: omp.wsloop private(@{{.*}} %{{.*}} -> %{{.*}}, @{{.*}} %{{.*}} -> %[[VAL_17:.*]] : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<i32>) private_barrier {
! CHECK: omp.loop_nest
! CHECK: %[[VAL_16:.*]]:2 = hlfir.declare %{{.*}} {fortran_attrs = {{.*}}<allocatable>, uniq_name = "_QFEa"} : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> (!fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.heap<i32>>>)
! CHECK: %[[VAL_18:.*]]:2 = hlfir.declare %[[VAL_17]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ subroutine firstpriv_lastpriv_int(arg1, arg2)
! Firstprivate update


!CHECK-NEXT: omp.barrier
!CHECK: omp.wsloop private(@{{.*}} %{{.*}}#0 -> %[[CLONE1:.*]], @{{.*}} %{{.*}}#0 -> %[[IV:.*]] : !fir.ref<i32>, !fir.ref<i32>) {
!CHECK: omp.wsloop private(@{{.*}} %{{.*}}#0 -> %[[CLONE1:.*]], @{{.*}} %{{.*}}#0 -> %[[IV:.*]] : !fir.ref<i32>, !fir.ref<i32>) private_barrier {
!CHECK-NEXT: omp.loop_nest (%[[INDX_WS:.*]]) : {{.*}} {
!CHECK: %[[CLONE1_DECL:.*]]:2 = hlfir.declare %[[CLONE1]] {uniq_name = "_QFfirstpriv_lastpriv_int2Earg1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)

Expand Down
3 changes: 1 addition & 2 deletions flang/test/Lower/OpenMP/same_var_first_lastprivate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ subroutine first_and_lastprivate
! CHECK: func.func @{{.*}}first_and_lastprivate()
! CHECK: %[[ORIG_VAR_DECL:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "{{.*}}Evar"}
! CHECK: omp.parallel {
! CHECK: omp.barrier
! CHECK: omp.wsloop private(@{{.*}}var_firstprivate_i32 {{.*}}) {
! CHECK: omp.wsloop private(@{{.*}}var_firstprivate_i32 {{.*}}) private_barrier {
! CHECK: omp.loop_nest {{.*}} {
! CHECK: %[[PRIV_VAR_DECL:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "{{.*}}Evar"}
! CHECK: fir.if %{{.*}} {
Expand Down
5 changes: 4 additions & 1 deletion mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,10 @@ class OpenMP_PrivateClauseSkip<

let arguments = (ins
Variadic<AnyType>:$private_vars,
OptionalAttr<SymbolRefArrayAttr>:$private_syms
OptionalAttr<SymbolRefArrayAttr>:$private_syms,
// Set this attribute if a barrier is needed after initialization and
// copying of lastprivate variables.
UnitAttr:$private_needs_barrier
);

// TODO: Add description.
Expand Down
37 changes: 19 additions & 18 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ def ParallelOp : OpenMP_Op<"parallel", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -258,8 +258,8 @@ def TeamsOp : OpenMP_Op<"teams", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -317,8 +317,8 @@ def SectionsOp : OpenMP_Op<"sections", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -350,7 +350,7 @@ def SingleOp : OpenMP_Op<"single", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateRegion>($region, $private_vars, type($private_vars),
$private_syms) attr-dict
$private_syms, $private_needs_barrier) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -505,8 +505,8 @@ def LoopOp : OpenMP_Op<"loop", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let builders = [
Expand Down Expand Up @@ -557,8 +557,8 @@ def WsloopOp : OpenMP_Op<"wsloop", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -611,8 +611,8 @@ def SimdOp : OpenMP_Op<"simd", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateReductionRegion>($region, $private_vars, type($private_vars),
$private_syms, $reduction_mod, $reduction_vars, type($reduction_vars), $reduction_byref,
$reduction_syms) attr-dict
$private_syms, $private_needs_barrier, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -690,7 +690,7 @@ def DistributeOp : OpenMP_Op<"distribute", traits = [

let assemblyFormat = clausesAssemblyFormat # [{
custom<PrivateRegion>($region, $private_vars, type($private_vars),
$private_syms) attr-dict
$private_syms, $private_needs_barrier) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -740,7 +740,7 @@ def TaskOp
custom<InReductionPrivateRegion>(
$region, $in_reduction_vars, type($in_reduction_vars),
$in_reduction_byref, $in_reduction_syms, $private_vars,
type($private_vars), $private_syms) attr-dict
type($private_vars), $private_syms, $private_needs_barrier) attr-dict
}];

let hasVerifier = 1;
Expand Down Expand Up @@ -816,8 +816,9 @@ def TaskloopOp : OpenMP_Op<"taskloop", traits = [
custom<InReductionPrivateReductionRegion>(
$region, $in_reduction_vars, type($in_reduction_vars),
$in_reduction_byref, $in_reduction_syms, $private_vars,
type($private_vars), $private_syms, $reduction_mod, $reduction_vars,
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
type($private_vars), $private_syms, $private_needs_barrier,
$reduction_mod, $reduction_vars, type($reduction_vars),
$reduction_byref, $reduction_syms) attr-dict
}];

let extraClassDeclaration = [{
Expand Down Expand Up @@ -1324,7 +1325,7 @@ def TargetOp : OpenMP_Op<"target", traits = [
$host_eval_vars, type($host_eval_vars), $in_reduction_vars,
type($in_reduction_vars), $in_reduction_byref, $in_reduction_syms,
$map_vars, type($map_vars), $private_vars, type($private_vars),
$private_syms, $private_maps) attr-dict
$private_syms, $private_needs_barrier, $private_maps) attr-dict
}];

let hasVerifier = 1;
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ struct ParallelOpLowering : public OpRewritePattern<scf::ParallelOp> {
/* num_threads = */ numThreadsVar,
/* private_vars = */ ValueRange(),
/* private_syms = */ nullptr,
/* private_needs_barrier = */ nullptr,
/* proc_bind_kind = */ omp::ClauseProcBindKindAttr{},
/* reduction_mod = */ nullptr,
/* reduction_vars = */ llvm::SmallVector<Value>{},
Expand Down
Loading
Loading