This is an archive of the discontinued LLVM Phabricator instance.

[mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.
ClosedPublic

Authored by raghavendhra on Apr 14 2022, 6:46 PM.

Diff Detail

Event Timeline

raghavendhra created this revision.Apr 14 2022, 6:46 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 14 2022, 6:46 PM
raghavendhra requested review of this revision.Apr 14 2022, 6:46 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
kiranchandramohan requested changes to this revision.Apr 15 2022, 2:47 AM
kiranchandramohan added inline comments.
flang/lib/Semantics/check-omp-structure.cpp
1809

On a first look, it looks like we have a construct here instead of a clause. Would CancelConstructType be a better name?

mlir/test/Dialect/OpenMP/ops.mlir
1278–1301

There are a few restrictions regarding closely nested constructs. Some of the above might not be valid usage. Could you try implementing them?

This revision now requires changes to proceed.Apr 15 2022, 2:47 AM
raghavendhra requested review of this revision.Apr 15 2022, 12:16 PM
raghavendhra added inline comments.
flang/lib/Semantics/check-omp-structure.cpp
1809

If I change CancelConstruct to CancelConstructType, I am getting

llvm-project/flang/lib/Semantics/check-omp-structure.cpp:1808:21: error: ‘CancelConstructType’ in ‘struct Fortran::parser::OmpClause’ does not name a type
1808 | CHECK_SIMPLE_CLAUSE(CancelConstructType, OMPC_cancel_construct)

Also, it looks like code in flang has been using CancelConstruct in multiple places.

mlir/test/Dialect/OpenMP/ops.mlir
1278–1301

Something like this?

func @omp_cancel(%if_cond : i1) -> () {
  // Test with optional operand; if_expr.
  omp.parallel {
    // CHECK: omp.cancel cancel_construct(parallel) if(%{{.*}})
    omp.cancel cancel_construct(parallel) if(%if_cond)
    // CHECK: omp.terminator
    omp.terminator
  }
  return
}
clementval added inline comments.Apr 15 2022, 1:01 PM
flang/lib/Semantics/check-omp-structure.cpp
1809

You have the error because you have to update the name in OMP.td first.

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

@raghavendhra Apologies for not being clear. The change request was to add a verifier to the operation to issue errors where the closely nested construct restrictions do not hold.

As per the standard, for example, the following usages are not allowed.

!$omp master
!$omp cancel parallel
!$omp end master
!$omp cancel parallel

Could you go through the restrictions of cancel and cancellation point (the restriction on nesting of constructs) and implement these in the verifier? You could use the Reduction Op's verifier for reference. We probably have to check the parent Op and ensure it meets the restrictions specified in the standard.
https://github.com/llvm/llvm-project/blob/76410040b9f391185c7df48c14519860e1cf75e5/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td#L1074
https://github.com/llvm/llvm-project/blob/76410040b9f391185c7df48c14519860e1cf75e5/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp#L713

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.
raghavendhra planned changes to this revision.Apr 20 2022, 2:22 PM

@raghavendhra Apologies for not being clear. The change request was to add a verifier to the operation to issue errors where the closely nested construct restrictions do not hold.

As per the standard, for example, the following usages are not allowed.

!$omp master
!$omp cancel parallel
!$omp end master
!$omp cancel parallel

Could you go through the restrictions of cancel and cancellation point (the restriction on nesting of constructs) and implement these in the verifier? You could use the Reduction Op's verifier for reference. We probably have to check the parent Op and ensure it meets the restrictions specified in the standard.
https://github.com/llvm/llvm-project/blob/76410040b9f391185c7df48c14519860e1cf75e5/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td#L1074
https://github.com/llvm/llvm-project/blob/76410040b9f391185c7df48c14519860e1cf75e5/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp#L713

Thank you so much for more insight. I added verifier and tests to invalid.mlir. Please review my latest changes.

Generally LGTM. A couple nits and minor comments.

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
959

Same as below: please expand auto and can we change the name to parentOp because op is too generic.

967

Same as below: string based matching should be avoided. It would be better to use isa<ParallelOp>(parentOp) instead.

975

do/for is omp.wsloop. We can add check for that too.

983

nit: please expand auto for small type names and when the type is not obvious from the right hand side.
Ref: https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable

984

nit: op is too generic. Can we please change the name to parentOp?

992

string based matching should be avoided. It would be better to use isa<ParallelOp>(parentOp) instead.

mlir/test/Dialect/OpenMP/ops.mlir
1280

Nit: please add tests for cancel construct under omp.sections, omp.section and omp.wsloop also.

raghavendhra marked 7 inline comments as done.

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

dpalermo edited reviewers, added: domada; removed: Dominik_A.Apr 21 2022, 12:02 PM

Thanks for making the changes. This looks mostly good. A few more comments.

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
961

Nit: Might be better to use braces here since the return message is in two lines. Same for cancellation point as well.

962

Nit: an region -> a region

966

Nit: Might be better to use braces in this entire if-else construct since some of them are in two lines. Same for cancellation point as well.

970

Can you add the following two as well?
A worksharing construct that is canceled must not have a nowait clause.
A worksharing-loop construct that is canceled must not have an ordered clause.

Mostly LGTM.

flang/lib/Semantics/check-omp-structure.cpp
1809

Niy: Would CancellationConstructType be better? Both cancel construct and cancellation point construct are cancellation constructs.

llvm/include/llvm/Frontend/OpenMP/OMP.td
176

Nit

raghavendhra planned changes to this revision.Apr 26 2022, 5:21 PM
raghavendhra marked 9 inline comments as done.

Addressed comments

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

peixin added inline comments.Apr 27 2022, 6:11 PM
llvm/include/llvm/Frontend/OpenMP/OMP.td
174

Can you update them all if you use CancellationConstructType for the keyword?
OMPC_CancelConstructType -> OMPC_CancellationConstructType
OMP_CANCEL_CONSTRUCT_Parallel -> OMP_CANCELLATION_CONSTRUCT_Parallel
...

raghavendhra marked an inline comment as done.Apr 28 2022, 2:29 AM

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

LGTM. Please check the CI failure (probably a clang-format issue). Please wait for @shraiysh & @peixin.

This revision is now accepted and ready to land.Apr 28 2022, 4:05 AM
peixin accepted this revision.Apr 28 2022, 4:21 AM
peixin added inline comments.
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
1012

Nit: cancel_construct -> cancellation_construct_type or cancellation_type

1028

Nit: cancel_construct_val -> cancellation_construct_type_val or cancellation_type_val

1029

Nit: cancel_construct -> cancellation_construct_type or cancellation_type

shraiysh accepted this revision.Apr 28 2022, 7:13 AM

LGTM. Thanks!

raghavendhra marked 3 inline comments as done.Apr 28 2022, 9:46 AM

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

Fixed git-clang-format

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

Rebasing with main

Updating D123828: [mlir][OpenMP] Add omp.cancel and omp.cancellationpoint.

Rebasing it to main to land the patch.

Rebasing to main before landing.

This revision was landed with ongoing or failed builds.May 2 2022, 10:24 AM
This revision was automatically updated to reflect the committed changes.

ninja clang had a -Wswitch warning when the host compiler is clang. It was something like:

clang/lib/CodeGen/CGStmtOpenMP.cpp:6166:11: error: enumeration value 'OMPC_cancellation_construct_type' not handled in switch [-Werror,-Wswitch]

I have fixed it in 4d34c4e0e67d321ac6b52f787648a68ea6d267c0 but please pay attention to warnings in the future :)