[Mlir-commits] [mlir] 667ee52 - [OpenMP][Flang][MLIR] Add MLIR support for OpenMP requires directive

Sergio Afonso llvmlistbot at llvm.org
Mon Jun 12 04:38:32 PDT 2023


Author: Sergio Afonso
Date: 2023-06-12T12:38:04+01:00
New Revision: 667ee52551a7cf31f30b95acdb80cf0ed470a1be

URL: https://github.com/llvm/llvm-project/commit/667ee52551a7cf31f30b95acdb80cf0ed470a1be
DIFF: https://github.com/llvm/llvm-project/commit/667ee52551a7cf31f30b95acdb80cf0ed470a1be.diff

LOG: [OpenMP][Flang][MLIR] Add MLIR support for OpenMP requires directive

This patch introduces an MLIR attribute to the OpenMP dialect
representing the clauses that a 'requires' directive can define.

The `OffloadModuleInterface` is also updated to provide methods to get
and set a new dialect attribute `omp.requires`, to allow storing and using this
information during the lowering stages to LLVM IR.

Differential Revision: https://reviews.llvm.org/D147214

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
    mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
    mlir/test/Dialect/OpenMP/ops.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 878040ebe94ab..ae66c8176a3fe 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1692,4 +1692,35 @@ def ReductionOp : OpenMP_Op<"reduction"> {
   let hasVerifier = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// 8.2 requires directive
+//===----------------------------------------------------------------------===//
+
+// atomic_default_mem_order clause values not defined here because they can be
+// represented by the OMPC_MemoryOrder enumeration instead.
+def ClauseRequiresNone : I32BitEnumAttrCaseNone<"none">;
+def ClauseRequiresReverseOffload : I32BitEnumAttrCaseBit<"reverse_offload", 0>;
+def ClauseRequiresUnifiedAddress : I32BitEnumAttrCaseBit<"unified_address", 1>;
+def ClauseRequiresUnifiedSharedMemory
+    : I32BitEnumAttrCaseBit<"unified_shared_memory", 2>;
+def ClauseRequiresDynamicAllocators
+    : I32BitEnumAttrCaseBit<"dynamic_allocators", 3>;
+
+def ClauseRequires : I32BitEnumAttr<
+    "ClauseRequires",
+    "requires clauses",
+    [
+      ClauseRequiresNone,
+      ClauseRequiresReverseOffload,
+      ClauseRequiresUnifiedAddress,
+      ClauseRequiresUnifiedSharedMemory,
+      ClauseRequiresDynamicAllocators
+    ]> {
+  let genSpecializedAttr = 0;
+  let cppNamespace = "::mlir::omp";
+}
+def ClauseRequiresAttr :
+  EnumAttr<OpenMP_Dialect, ClauseRequires, "clause_requires"> {
+}
+
 #endif // OPENMP_OPS

diff  --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
index 789962999f128..f56fd1377cdb6 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -136,7 +136,7 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
           mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.is_device"}),
             mlir::BoolAttr::get($_op->getContext(), isDevice));
       }]>,
-      InterfaceMethod<
+    InterfaceMethod<
       /*description=*/[{
         Get the attribute on the current module if it exists and
         return its value, if it doesn't exist it returns false by default.
@@ -149,7 +149,7 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
            return isDevice.dyn_cast<BoolAttr>().getValue();
         return false;
       }]>,
-      InterfaceMethod<
+    InterfaceMethod<
       /*description=*/[{
         Get the FlagsAttr attribute on the current module if it exists
         and return the attribute, if it doesn't exit it returns a nullptr
@@ -207,7 +207,7 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
                                              targetCPU.str(),
                                              targetFeatures.str()));
       }]>,
-      InterfaceMethod<
+    InterfaceMethod<
       /*description=*/[{
         Set a StringAttr on the current module containing the host IR file path. This
         file path is used in two-phase compilation during the device phase to generate
@@ -220,7 +220,7 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
           mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
             mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
        }]>,
-      InterfaceMethod<
+    InterfaceMethod<
       /*description=*/[{
         Find the host-ir file path StringAttr from the current module if it exists and
         return its contained value, if it doesn't exist it returns an empty string. This
@@ -234,6 +234,30 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
           if (::llvm::isa<mlir::StringAttr>(filepath))
             return ::llvm::dyn_cast<mlir::StringAttr>(filepath).getValue();
         return {};
+      }]>,
+    InterfaceMethod<
+      /*description=*/[{
+        Get the omp.requires attribute on the operator if it's present and
+        return its value. If it doesn't exist, return `ClauseRequires::none` by
+        default.
+      }],
+      /*retTy=*/"::mlir::omp::ClauseRequires",
+      /*methodName=*/"getRequires",
+      (ins), [{}], [{
+        if (Attribute requiresAttr = $_op->getAttr("omp.requires"))
+          if (auto requiresVal = requiresAttr.dyn_cast<mlir::omp::ClauseRequiresAttr>())
+            return requiresVal.getValue();
+        return mlir::omp::ClauseRequires::none;
+      }]>,
+    InterfaceMethod<
+      /*description=*/[{
+        Set the omp.requires attribute on the operator to the specified clauses.
+      }],
+      /*retTy=*/"void",
+      /*methodName=*/"setRequires",
+      (ins "::mlir::omp::ClauseRequires":$clauses), [{}], [{
+        $_op->setAttr(mlir::StringAttr::get($_op->getContext(), "omp.requires"),
+          mlir::omp::ClauseRequiresAttr::get($_op->getContext(), clauses));
       }]>
   ];
 }

diff  --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index c9832f80369f8..544da36cca845 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -1881,6 +1881,20 @@ func.func @omp_taskloop(%lb: i32, %ub: i32, %step: i32) -> () {
   return
 }
 
+// CHECK: func.func @omp_requires_one
+// CHECK-SAME: omp.requires = #omp<clause_requires reverse_offload>
+func.func @omp_requires_one() -> ()
+    attributes {omp.requires = #omp<clause_requires reverse_offload>} {
+  return
+}
+
+// CHECK: func.func @omp_requires_multiple
+// CHECK-SAME: omp.requires = #omp<clause_requires unified_address|dynamic_allocators>
+func.func @omp_requires_multiple() -> ()
+    attributes {omp.requires = #omp<clause_requires unified_address|dynamic_allocators>} {
+  return
+}
+
 // -----
 
 // CHECK-LABEL: @opaque_pointers_atomic_rwu


        


More information about the Mlir-commits mailing list