Skip to content

[mlir][OpenMP] allow cancellation to not be directly nested #134084

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
Apr 14, 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
55 changes: 33 additions & 22 deletions mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3101,24 +3101,32 @@ void CancelOp::build(OpBuilder &builder, OperationState &state,
CancelOp::build(builder, state, clauses.cancelDirective, clauses.ifExpr);
}

static Operation *getParentInSameDialect(Operation *thisOp) {
Operation *parent = thisOp->getParentOp();
while (parent) {
if (parent->getDialect() == thisOp->getDialect())
return parent;
parent = parent->getParentOp();
}
return nullptr;
}

LogicalResult CancelOp::verify() {
ClauseCancellationConstructType cct = getCancelDirective();
Operation *parentOp = (*this)->getParentOp();

if (!parentOp) {
return emitOpError() << "must be used within a region supporting "
"cancel directive";
}
// The next OpenMP operation in the chain of parents
Operation *structuralParent = getParentInSameDialect((*this).getOperation());
if (!structuralParent)
return emitOpError() << "Orphaned cancel construct";

if ((cct == ClauseCancellationConstructType::Parallel) &&
!isa<ParallelOp>(parentOp)) {
!mlir::isa<ParallelOp>(structuralParent)) {
return emitOpError() << "cancel parallel must appear "
<< "inside a parallel region";
}
if (cct == ClauseCancellationConstructType::Loop) {
auto loopOp = dyn_cast<LoopNestOp>(parentOp);
auto wsloopOp = llvm::dyn_cast_if_present<WsloopOp>(
loopOp ? loopOp->getParentOp() : nullptr);
// structural parent will be omp.loop_nest, directly nested inside
// omp.wsloop
auto wsloopOp = mlir::dyn_cast<WsloopOp>(structuralParent->getParentOp());

if (!wsloopOp) {
return emitOpError()
Expand All @@ -3134,12 +3142,15 @@ LogicalResult CancelOp::verify() {
}

} else if (cct == ClauseCancellationConstructType::Sections) {
if (!(isa<SectionsOp>(parentOp) || isa<SectionOp>(parentOp))) {
// structural parent will be an omp.section, directly nested inside
// omp.sections
auto sectionsOp =
mlir::dyn_cast<SectionsOp>(structuralParent->getParentOp());
if (!sectionsOp) {
return emitOpError() << "cancel sections must appear "
<< "inside a sections region";
}
if (isa_and_nonnull<SectionsOp>(parentOp->getParentOp()) &&
cast<SectionsOp>(parentOp->getParentOp()).getNowaitAttr()) {
if (sectionsOp.getNowait()) {
return emitError() << "A sections construct that is canceled "
<< "must not have a nowait clause";
}
Expand All @@ -3159,25 +3170,25 @@ void CancellationPointOp::build(OpBuilder &builder, OperationState &state,

LogicalResult CancellationPointOp::verify() {
ClauseCancellationConstructType cct = getCancelDirective();
Operation *parentOp = (*this)->getParentOp();

if (!parentOp) {
return emitOpError() << "must be used within a region supporting "
"cancellation point directive";
}
// The next OpenMP operation in the chain of parents
Operation *structuralParent = getParentInSameDialect((*this).getOperation());
if (!structuralParent)
return emitOpError() << "Orphaned cancellation point";

if ((cct == ClauseCancellationConstructType::Parallel) &&
!(isa<ParallelOp>(parentOp))) {
!mlir::isa<ParallelOp>(structuralParent)) {
return emitOpError() << "cancellation point parallel must appear "
<< "inside a parallel region";
}
// Strucutal parent here will be an omp.loop_nest. Get the parent of that to
// find the wsloop
if ((cct == ClauseCancellationConstructType::Loop) &&
(!isa<LoopNestOp>(parentOp) || !isa<WsloopOp>(parentOp->getParentOp()))) {
!mlir::isa<WsloopOp>(structuralParent->getParentOp())) {
return emitOpError() << "cancellation point loop must appear "
<< "inside a worksharing-loop region";
}
if ((cct == ClauseCancellationConstructType::Sections) &&
!(isa<SectionsOp>(parentOp) || isa<SectionOp>(parentOp))) {
!mlir::isa<omp::SectionOp>(structuralParent)) {
return emitOpError() << "cancellation point sections must appear "
<< "inside a sections region";
}
Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Dialect/OpenMP/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,14 @@ func.func @omp_task(%mem: memref<1xf32>) {

// -----

func.func @omp_cancel() {
// expected-error @below {{Orphaned cancel construct}}
omp.cancel cancellation_construct_type(parallel)
return
}

// -----

func.func @omp_cancel() {
omp.sections {
// expected-error @below {{cancel parallel must appear inside a parallel region}}
Expand Down Expand Up @@ -1789,6 +1797,14 @@ func.func @omp_cancel5() -> () {

// -----

func.func @omp_cancellationpoint() {
// expected-error @below {{Orphaned cancellation point}}
omp.cancellation_point cancellation_construct_type(parallel)
return
}

// -----

func.func @omp_cancellationpoint() {
omp.sections {
// expected-error @below {{cancellation point parallel must appear inside a parallel region}}
Expand Down
82 changes: 82 additions & 0 deletions mlir/test/Dialect/OpenMP/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,48 @@ func.func @omp_cancel_sections() -> () {
return
}

func.func @omp_cancel_parallel_nested(%if_cond : i1) -> () {
omp.parallel {
scf.if %if_cond {
// CHECK: omp.cancel cancellation_construct_type(parallel)
omp.cancel cancellation_construct_type(parallel)
}
// CHECK: omp.terminator
omp.terminator
}
return
}

func.func @omp_cancel_wsloop_nested(%lb : index, %ub : index, %step : index,
%if_cond : i1) {
omp.wsloop {
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
scf.if %if_cond {
// CHECK: omp.cancel cancellation_construct_type(loop)
omp.cancel cancellation_construct_type(loop)
}
// CHECK: omp.yield
omp.yield
}
}
return
}

func.func @omp_cancel_sections_nested(%if_cond : i1) -> () {
omp.sections {
omp.section {
scf.if %if_cond {
// CHECK: omp.cancel cancellation_construct_type(sections)
omp.cancel cancellation_construct_type(sections)
}
omp.terminator
}
// CHECK: omp.terminator
omp.terminator
}
return
}

func.func @omp_cancellationpoint_parallel() -> () {
omp.parallel {
// CHECK: omp.cancellation_point cancellation_construct_type(parallel)
Expand Down Expand Up @@ -2241,6 +2283,46 @@ func.func @omp_cancellationpoint_sections() -> () {
return
}

func.func @omp_cancellationpoint_parallel_nested(%if_cond : i1) -> () {
omp.parallel {
scf.if %if_cond {
// CHECK: omp.cancellation_point cancellation_construct_type(parallel)
omp.cancellation_point cancellation_construct_type(parallel)
}
omp.terminator
}
return
}

func.func @omp_cancellationpoint_wsloop_nested(%lb : index, %ub : index, %step : index, %if_cond : i1) {
omp.wsloop {
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
scf.if %if_cond {
// CHECK: omp.cancellation_point cancellation_construct_type(loop)
omp.cancellation_point cancellation_construct_type(loop)
}
// CHECK: omp.yield
omp.yield
}
}
return
}

func.func @omp_cancellationpoint_sections_nested(%if_cond : i1) -> () {
omp.sections {
omp.section {
scf.if %if_cond {
// CHECK: omp.cancellation_point cancellation_construct_type(sections)
omp.cancellation_point cancellation_construct_type(sections)
}
omp.terminator
}
// CHECK: omp.terminator
omp.terminator
}
return
}

// CHECK-LABEL: @omp_taskgroup_no_tasks
func.func @omp_taskgroup_no_tasks() -> () {

Expand Down