Skip to content

Commit a24ed7d

Browse files
authored
[mlir][OpenMP] add attribute for privatization barrier (#140089)
A barrier is needed at the end of initialization/copying of private variables if any of those variables is lastprivate. This ensures that all firstprivate variables receive the original value of the variable before the lastprivate clause overwrites it. Previously this barrier was added by the flang fontend, but there is not a reliable way to put the barrier in the correct place for delayed privatization, and the OpenMP dialect could some day have other users. It is important that there are safe ways to use the constructs available in the dialect. lastprivate is currently not modelled in the OpenMP dialect, and so there is no way to reliably determine whether there were lastprivate variables. Therefore the frontend will have to provide this information through this new attribute. Part of a series of patches to fix #136357
1 parent 03cc50f commit a24ed7d

File tree

5 files changed

+159
-97
lines changed

5 files changed

+159
-97
lines changed

mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,10 @@ class OpenMP_PrivateClauseSkip<
11021102

11031103
let arguments = (ins
11041104
Variadic<AnyType>:$private_vars,
1105-
OptionalAttr<SymbolRefArrayAttr>:$private_syms
1105+
OptionalAttr<SymbolRefArrayAttr>:$private_syms,
1106+
// Set this attribute if a barrier is needed after initialization and
1107+
// copying of lastprivate variables.
1108+
UnitAttr:$private_needs_barrier
11061109
);
11071110

11081111
// TODO: Add description.

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def ParallelOp : OpenMP_Op<"parallel", traits = [
213213

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

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

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

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

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

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

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

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

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

512512
let builders = [
@@ -557,8 +557,8 @@ def WsloopOp : OpenMP_Op<"wsloop", traits = [
557557

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

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

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

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

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

696696
let hasVerifier = 1;
@@ -740,7 +740,7 @@ def TaskOp
740740
custom<InReductionPrivateRegion>(
741741
$region, $in_reduction_vars, type($in_reduction_vars),
742742
$in_reduction_byref, $in_reduction_syms, $private_vars,
743-
type($private_vars), $private_syms) attr-dict
743+
type($private_vars), $private_syms, $private_needs_barrier) attr-dict
744744
}];
745745

746746
let hasVerifier = 1;
@@ -816,8 +816,9 @@ def TaskloopOp : OpenMP_Op<"taskloop", traits = [
816816
custom<InReductionPrivateReductionRegion>(
817817
$region, $in_reduction_vars, type($in_reduction_vars),
818818
$in_reduction_byref, $in_reduction_syms, $private_vars,
819-
type($private_vars), $private_syms, $reduction_mod, $reduction_vars,
820-
type($reduction_vars), $reduction_byref, $reduction_syms) attr-dict
819+
type($private_vars), $private_syms, $private_needs_barrier,
820+
$reduction_mod, $reduction_vars, type($reduction_vars),
821+
$reduction_byref, $reduction_syms) attr-dict
821822
}];
822823

823824
let extraClassDeclaration = [{
@@ -1324,7 +1325,7 @@ def TargetOp : OpenMP_Op<"target", traits = [
13241325
$host_eval_vars, type($host_eval_vars), $in_reduction_vars,
13251326
type($in_reduction_vars), $in_reduction_byref, $in_reduction_syms,
13261327
$map_vars, type($map_vars), $private_vars, type($private_vars),
1327-
$private_syms, $private_maps) attr-dict
1328+
$private_syms, $private_needs_barrier, $private_maps) attr-dict
13281329
}];
13291330

13301331
let hasVerifier = 1;

mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ struct ParallelOpLowering : public OpRewritePattern<scf::ParallelOp> {
450450
/* num_threads = */ numThreadsVar,
451451
/* private_vars = */ ValueRange(),
452452
/* private_syms = */ nullptr,
453+
/* private_needs_barrier = */ nullptr,
453454
/* proc_bind_kind = */ omp::ClauseProcBindKindAttr{},
454455
/* reduction_mod = */ nullptr,
455456
/* reduction_vars = */ llvm::SmallVector<Value>{},

0 commit comments

Comments
 (0)