[Mlir-commits] [mlir] [MLIR][OpenMP] Add OMP Declare Mapper MLIR Op definition (PR #117045)

Akash Banerjee llvmlistbot at llvm.org
Thu Nov 28 13:18:41 PST 2024


https://github.com/TIFitis updated https://github.com/llvm/llvm-project/pull/117045

>From afcc130f5cff94d8384a759dd3130904b2125d93 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 20 Nov 2024 20:44:23 +0000
Subject: [PATCH 1/3] [MLIR][OpenMP] Add OMP Declare Mapper MLIR Op definition

This patch adds the OMP.DeclareMapperOp to MLIR.
---
 mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 28 +++++++++++++++++++
 mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp  |  8 ++++++
 mlir/test/Dialect/OpenMP/ops.mlir             |  8 ++++++
 3 files changed, 44 insertions(+)

diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 156e6eb371b85d..eabca0b5ac7a25 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1652,6 +1652,34 @@ def CancellationPointOp : OpenMP_Op<"cancellation_point", clauses = [
   let hasVerifier = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// 2.19.7.3 Declare Mapper Directive
+//===----------------------------------------------------------------------===//
+def DeclareMapperOp : OpenMP_Op<"declare_mapper", traits = [
+  Symbol], clauses = [
+    OpenMP_MapClause
+  ]> {
+  let summary = "declare mapper directive";
+  let description = [{
+    The declare mapper directive declares a user-defined mapper for a given
+    type, and may define a mapper-identifier that can be used in a map clause.
+  }] # clausesDescription;
+
+  let arguments = !con((ins SymbolNameAttr:$sym_name,
+                            OpenMP_PointerLikeType:$var_ptr,
+                            TypeAttr:$var_type), clausesArgs);
+
+  let builders = [
+    OpBuilder<(ins CArg<"const DeclareMapperOperands &">:$clauses)>
+  ];
+
+  // Override clause-based assemblyFormat.
+  let assemblyFormat = "$sym_name `:` $var_ptr `:` type($var_ptr) `:` $var_type" # " oilist(" #
+    clausesOptAssemblyFormat # ")  attr-dict";
+
+  let hasVerifier = 1;
+}
+
 //===----------------------------------------------------------------------===//
 // 2.19.5.7 declare reduction Directive
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 94e71e089d4b18..2f40405b54b73c 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -2137,6 +2137,14 @@ LogicalResult DistributeOp::verifyRegions() {
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// DeclareMapperOp
+//===----------------------------------------------------------------------===//
+
+LogicalResult DeclareMapperOp::verify() {
+  return verifyMapClause(*this, getMapVars());
+}
+
 //===----------------------------------------------------------------------===//
 // DeclareReductionOp
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index c25a6ef4b4849b..a3f2c458b6c592 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -879,6 +879,14 @@ cleanup {
   omp.yield
 }
 
+// CHECK: %[[DECL_VAR:.*]] = llvm.alloca %{{.*}}
+// CHECK: %[[DECL_MAP_INFO:.*]] = omp.map.info var_ptr(%[[DECL_VAR]] : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
+// CHECK: omp.declare_mapper @my_mapper : %[[DECL_VAR]] : !llvm.ptr : !llvm.struct<"my_type", (i32)> map_entries(%[[DECL_MAP_INFO]] : !llvm.ptr)
+%decl_c1 = arith.constant 1 : i64
+%decl_var = llvm.alloca %decl_c1 x !llvm.struct<"my_type", (i32)> : (i64) -> !llvm.ptr
+%decl_map_info = omp.map.info var_ptr(%decl_var : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
+omp.declare_mapper @my_mapper : %decl_var : !llvm.ptr : !llvm.struct<"my_type", (i32)> map_entries(%decl_map_info : !llvm.ptr)
+
 // CHECK-LABEL: func @wsloop_reduction
 func.func @wsloop_reduction(%lb : index, %ub : index, %step : index) {
   %c1 = arith.constant 1 : i32

>From 83b19a6fb11c2acd02672b42430c5bd1833d490e Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 22 Nov 2024 12:06:29 +0000
Subject: [PATCH 2/3] Updated description.

---
 mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index eabca0b5ac7a25..870bfd4a14d750 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1662,7 +1662,7 @@ def DeclareMapperOp : OpenMP_Op<"declare_mapper", traits = [
   let summary = "declare mapper directive";
   let description = [{
     The declare mapper directive declares a user-defined mapper for a given
-    type, and may define a mapper-identifier that can be used in a map clause.
+    type, and defines a mapper-identifier that can be used in a map clause.
   }] # clausesDescription;
 
   let arguments = !con((ins SymbolNameAttr:$sym_name,

>From 00c9f3de62a2ca1ded1b86adae40401b3f1c369f Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 27 Nov 2024 19:05:26 +0000
Subject: [PATCH 3/3] Add region to DeclMapperOp. Move map clause to new
 DeclMapperInfoOp.

---
 mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 32 +++++++++++--------
 mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp  |  4 +--
 mlir/test/Dialect/OpenMP/ops.mlir             | 16 ++++++----
 3 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 870bfd4a14d750..efaa9e6fd1de43 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -622,7 +622,7 @@ def DistributeOp : OpenMP_Op<"distribute", traits = [
     will be executed in parallel by threads in the current context. These
     iterations are spread across threads that already exist in the enclosing
     region.
-    
+
     The body region can only contain a single block which must contain a single
     operation. This operation must be another compatible loop wrapper or an
     `omp.loop_nest`.
@@ -1655,28 +1655,32 @@ def CancellationPointOp : OpenMP_Op<"cancellation_point", clauses = [
 //===----------------------------------------------------------------------===//
 // 2.19.7.3 Declare Mapper Directive
 //===----------------------------------------------------------------------===//
-def DeclareMapperOp : OpenMP_Op<"declare_mapper", traits = [
-  Symbol], clauses = [
-    OpenMP_MapClause
-  ]> {
+def DeclareMapperOp : OpenMP_Op<"declare_mapper", singleRegion = 1> {
   let summary = "declare mapper directive";
   let description = [{
     The declare mapper directive declares a user-defined mapper for a given
     type, and defines a mapper-identifier that can be used in a map clause.
   }] # clausesDescription;
 
-  let arguments = !con((ins SymbolNameAttr:$sym_name,
-                            OpenMP_PointerLikeType:$var_ptr,
-                            TypeAttr:$var_type), clausesArgs);
+  let arguments = (ins  SymbolNameAttr:$sym_name,
+                        TypeAttr:$var_type);
+
+  let assemblyFormat = "$sym_name `:` $var_type $region attr-dict";
+}
+
+def DeclareMapperInfoOp : OpenMP_Op<"declare_mapper_info", clauses = [
+    OpenMP_MapClause
+    ]> {
+  let summary = "declare mapper info";
+  let description = [{
+    This Op is used to capture the map information related to it's
+    parent DeclareMapperOp.]>
+  }] # clausesDescription;
 
   let builders = [
-    OpBuilder<(ins CArg<"const DeclareMapperOperands &">:$clauses)>
+    OpBuilder<(ins CArg<"const DeclareMapperInfoOperands &">:$clauses)>
   ];
 
-  // Override clause-based assemblyFormat.
-  let assemblyFormat = "$sym_name `:` $var_ptr `:` type($var_ptr) `:` $var_type" # " oilist(" #
-    clausesOptAssemblyFormat # ")  attr-dict";
-
   let hasVerifier = 1;
 }
 
@@ -1792,7 +1796,7 @@ def MaskedOp : OpenMP_Op<"masked", clauses = [
   ], singleRegion = 1> {
   let summary = "masked construct";
   let description = [{
-    Masked construct allows to specify a structured block to be executed by a subset of 
+    Masked construct allows to specify a structured block to be executed by a subset of
     threads of the current team.
   }] # clausesDescription;
 
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 2f40405b54b73c..65e0ea57028b95 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -2138,10 +2138,10 @@ LogicalResult DistributeOp::verifyRegions() {
 }
 
 //===----------------------------------------------------------------------===//
-// DeclareMapperOp
+// DeclareMapperInfoOp
 //===----------------------------------------------------------------------===//
 
-LogicalResult DeclareMapperOp::verify() {
+LogicalResult DeclareMapperInfoOp::verify() {
   return verifyMapClause(*this, getMapVars());
 }
 
diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index a3f2c458b6c592..39bbe006831c57 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -879,13 +879,15 @@ cleanup {
   omp.yield
 }
 
-// CHECK: %[[DECL_VAR:.*]] = llvm.alloca %{{.*}}
-// CHECK: %[[DECL_MAP_INFO:.*]] = omp.map.info var_ptr(%[[DECL_VAR]] : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
-// CHECK: omp.declare_mapper @my_mapper : %[[DECL_VAR]] : !llvm.ptr : !llvm.struct<"my_type", (i32)> map_entries(%[[DECL_MAP_INFO]] : !llvm.ptr)
-%decl_c1 = arith.constant 1 : i64
-%decl_var = llvm.alloca %decl_c1 x !llvm.struct<"my_type", (i32)> : (i64) -> !llvm.ptr
-%decl_map_info = omp.map.info var_ptr(%decl_var : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
-omp.declare_mapper @my_mapper : %decl_var : !llvm.ptr : !llvm.struct<"my_type", (i32)> map_entries(%decl_map_info : !llvm.ptr)
+// CHECK: omp.declare_mapper @my_mapper : !llvm.struct<"my_type", (i32)>
+omp.declare_mapper @my_mapper : !llvm.struct<"my_type", (i32)> {
+^bb0(%arg: !llvm.ptr):
+  // CHECK: %[[DECL_MAP_INFO:.*]] = omp.map.info var_ptr(%{{.*}} : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
+  %decl_map_info = omp.map.info var_ptr(%arg : !llvm.ptr, !llvm.struct<"my_type", (i32)>) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = ""}
+  // CHECK: omp.declare_mapper_info map_entries(%[[DECL_MAP_INFO]] : !llvm.ptr)
+  omp.declare_mapper_info map_entries(%decl_map_info : !llvm.ptr)
+  omp.terminator
+}
 
 // CHECK-LABEL: func @wsloop_reduction
 func.func @wsloop_reduction(%lb : index, %ub : index, %step : index) {



More information about the Mlir-commits mailing list