Skip to content

Commit

Permalink
Adding masked operation to OpenMP Dialect (#96022)
Browse files Browse the repository at this point in the history
Adding MLIR Op support for omp masked. Omp masked is introduced in 5.2
standard and allows a region to be executed by threads
specified by a programmer. This is achieved with the help of filter
clause which helps to specify thread id expected to execute the region.
  • Loading branch information
anchuraj authored Jul 4, 2024
1 parent 3141c11 commit 7a9ef0f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPClauseOperands.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ struct DoacrossClauseOps {
IntegerAttr doacrossNumLoopsAttr;
};

struct FilterClauseOps {
Value filteredThreadIdVar;
};

struct FinalClauseOps {
Value finalVar;
};
Expand Down Expand Up @@ -254,8 +258,7 @@ using DistributeClauseOps =

using LoopNestClauseOps = detail::Clauses<CollapseClauseOps, LoopRelatedOps>;

// TODO `filter` clause.
using MaskedClauseOps = detail::Clauses<>;
using MaskedClauseOps = detail::Clauses<FilterClauseOps>;

using OrderedOpClauseOps = detail::Clauses<DoacrossClauseOps>;

Expand Down
28 changes: 28 additions & 0 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
Original file line number Diff line number Diff line change
Expand Up @@ -1204,4 +1204,32 @@ class OpenMP_UseDevicePtrClauseSkip<

def OpenMP_UseDevicePtrClause : OpenMP_UseDevicePtrClauseSkip<>;

//===----------------------------------------------------------------------===//
// V5.2: [10.5.1] `filter` clause
//===----------------------------------------------------------------------===//

class OpenMP_FilterClauseSkip<
bit traits = false, bit arguments = false, bit assemblyFormat = false,
bit description = false, bit extraClassDeclaration = false
> : OpenMP_Clause</*isRequired=*/false, traits, arguments, assemblyFormat,
description, extraClassDeclaration> {
let arguments = (ins
Optional<IntLikeType>:$filtered_thread_id
);

let assemblyFormat = [{
`filter` `(` $filtered_thread_id `:` type($filtered_thread_id) `)`
}];

let description = [{
If `filter` is specified, the masked construct masks the execution of
the region to only the thread id filtered. Other threads executing the
parallel region are not expected to execute the region specified within
the `masked` directive. If `filter` is not specified, master thread is
expected to execute the region enclosed within `masked` directive.
}];
}

def OpenMP_FilterClause : OpenMP_FilterClauseSkip<>;

#endif // OPENMP_CLAUSES
17 changes: 17 additions & 0 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1577,4 +1577,21 @@ def DeclareReductionOp : OpenMP_Op<"declare_reduction", [IsolatedFromAbove,
let hasRegionVerifier = 1;
}

//===----------------------------------------------------------------------===//
// [Spec 5.2] 10.5 masked Construct
//===----------------------------------------------------------------------===//
def MaskedOp : OpenMP_Op<"masked", clauses = [
OpenMP_FilterClause
], singleRegion = 1> {
let summary = "masked construct";
let description = [{
Masked construct allows to specify a structured block to be executed by a subset of
threads of the current team.
}] # clausesDescription;

let builders = [
OpBuilder<(ins CArg<"const MaskedClauseOps &">:$clauses)>
];
}

#endif // OPENMP_OPS
9 changes: 9 additions & 0 deletions mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,15 @@ LogicalResult PrivateClauseOp::verify() {
return success();
}

//===----------------------------------------------------------------------===//
// Spec 5.2: Masked construct (10.5)
//===----------------------------------------------------------------------===//

void MaskedOp::build(OpBuilder &builder, OperationState &state,
const MaskedClauseOps &clauses) {
MaskedOp::build(builder, state, clauses.filteredThreadIdVar);
}

#define GET_ATTRDEF_CLASSES
#include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"

Expand Down
18 changes: 18 additions & 0 deletions mlir/test/Dialect/OpenMP/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2358,3 +2358,21 @@ func.func @byref_in_private(%arg0: index) {

return
}

// -----
func.func @masked_arg_type_mismatch(%arg0: f32) {
// expected-error @below {{'omp.masked' op operand #0 must be integer or index, but got 'f32'}}
"omp.masked"(%arg0) ({
omp.terminator
}) : (f32) -> ()
return
}

// -----
func.func @masked_arg_count_mismatch(%arg0: i32, %arg1: i32) {
// expected-error @below {{'omp.masked' op operand group starting at #0 requires 0 or 1 element, but found 2}}
"omp.masked"(%arg0, %arg1) ({
omp.terminator
}) : (i32, i32) -> ()
return
}
14 changes: 14 additions & 0 deletions mlir/test/Dialect/OpenMP/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ func.func @omp_master() -> () {
return
}

// CHECK-LABEL: omp_masked
func.func @omp_masked(%filtered_thread_id : i32) -> () {
// CHECK: omp.masked filter(%{{.*}} : i32)
"omp.masked" (%filtered_thread_id) ({
omp.terminator
}) : (i32) -> ()

// CHECK: omp.masked
"omp.masked" () ({
omp.terminator
}) : () -> ()
return
}

func.func @omp_taskwait() -> () {
// CHECK: omp.taskwait
omp.taskwait
Expand Down

0 comments on commit 7a9ef0f

Please sign in to comment.