[flang-commits] [flang] 2f0047a - [MLIR][OpenMP] Allow map operands to be not specified

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Wed Aug 2 05:06:12 PDT 2023


Author: Kiran Chandramohan
Date: 2023-08-02T12:04:36Z
New Revision: 2f0047aad1327b364aeda58b6ce747c2cc969792

URL: https://github.com/llvm/llvm-project/commit/2f0047aad1327b364aeda58b6ce747c2cc969792
DIFF: https://github.com/llvm/llvm-project/commit/2f0047aad1327b364aeda58b6ce747c2cc969792.diff

LOG: [MLIR][OpenMP] Allow map operands to be not specified

This patch permits map operands to be not specified for the target
data operation. Also emit an error if none of the map, use_device_addr,
or use_device_ptr operands are specified.

Reviewed By: TIFitis

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

Added: 
    

Modified: 
    flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
    mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
    mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
    mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
    mlir/test/Dialect/OpenMP/invalid.mlir

Removed: 
    


################################################################################
diff  --git a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
index bf03c24fee75c0..61df6d8e16c349 100644
--- a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
+++ b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
@@ -321,6 +321,19 @@ func.func @_QPopenmp_target_data_region() {
 
 // -----
 
+func.func @_QPomp_target_data_empty() {
+  %0 = fir.alloca !fir.array<1024xi32> {bindc_name = "a", uniq_name = "_QFomp_target_data_emptyEa"}
+  omp.target_data use_device_addr(%0 : !fir.ref<!fir.array<1024xi32>>) {
+  }
+  return
+}
+
+// CHECK-LABEL:   llvm.func @_QPomp_target_data_empty
+// CHECK: omp.target_data   use_device_addr(%1 : !llvm.ptr<array<1024 x i32>>) {
+// CHECK: }
+
+// -----
+
 func.func @_QPomp_target() {
   %0 = fir.alloca !fir.array<512xi32> {bindc_name = "a", uniq_name = "_QFomp_targetEa"}
   %c64_i32 = arith.constant 64 : i32

diff  --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index aa142ed6aab3eb..d096791490cce9 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1041,15 +1041,16 @@ def Target_DataOp: OpenMP_Op<"target_data", [AttrSizedOperandSegments]>{
                        Variadic<OpenMP_PointerLikeType>:$use_device_ptr,
                        Variadic<OpenMP_PointerLikeType>:$use_device_addr,
                        Variadic<OpenMP_PointerLikeType>:$map_operands,
-                       I64ArrayAttr:$map_types);
+                       OptionalAttr<I64ArrayAttr>:$map_types);
 
   let regions = (region AnyRegion:$region);
 
   let assemblyFormat = [{
     oilist(`if` `(` $if_expr `:` type($if_expr) `)`
-    | `device` `(` $device `:` type($device) `)`)
-    `map` `(` custom<MapClause>($map_operands, type($map_operands), $map_types) `)`
-    oilist(`use_device_ptr` `(` $use_device_ptr `:` type($use_device_ptr) `)`
+    | `device` `(` $device `:` type($device) `)`
+    | `map`
+      `(` custom<MapClause>($map_operands, type($map_operands), $map_types) `)`
+    | `use_device_ptr` `(` $use_device_ptr `:` type($use_device_ptr) `)`
     | `use_device_addr` `(` $use_device_addr `:` type($use_device_addr) `)`)
     $region attr-dict
   }];

diff  --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 3695414a84c0f0..2ba5f1aca9cf6b 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -800,11 +800,22 @@ static LogicalResult verifyMapClause(Operation *op, OperandRange map_operands,
                std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
                flag);
   };
-  if (!map_types.has_value())
-    return success();
+  if (!map_types) {
+    if (!map_operands.empty())
+      return emitError(op->getLoc(), "missing mapTypes");
+    else
+      return success();
+  }
+
+  if (map_operands.empty() && !map_types->empty())
+    return emitError(op->getLoc(), "missing mapOperands");
+
+  if (map_types->empty() && !map_operands.empty())
+    return emitError(op->getLoc(), "missing mapTypes");
 
   if (map_operands.size() != map_types->size())
-    return failure();
+    return emitError(op->getLoc(),
+                     "mismatch in number of mapOperands and mapTypes");
 
   for (const auto &mapTypeOp : *map_types) {
     int64_t mapTypeBits = 0x00;
@@ -835,6 +846,11 @@ static LogicalResult verifyMapClause(Operation *op, OperandRange map_operands,
 }
 
 LogicalResult DataOp::verify() {
+  if (getMapOperands().empty() && getUseDevicePtr().empty() &&
+      getUseDeviceAddr().empty()) {
+    return ::emitError(this->getLoc(), "At least one of map, useDevicePtr, or "
+                                       "useDeviceAddr operand must be present");
+  }
   return verifyMapClause(*this, getMapOperands(), getMapTypes());
 }
 

diff  --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index f4f7b5f4c6a082..42545f276298c1 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1437,7 +1437,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
                   deviceID = intAttr.getInt();
 
             mapOperands = dataOp.getMapOperands();
-            mapTypes = dataOp.getMapTypes();
+            if (dataOp.getMapTypes())
+              mapTypes = dataOp.getMapTypes().value();
             return success();
           })
           .Case([&](omp::EnterDataOp enterDataOp) {

diff  --git a/mlir/test/Dialect/OpenMP/invalid.mlir b/mlir/test/Dialect/OpenMP/invalid.mlir
index fc65fb77ffc88f..e7bdd0bcf27ab7 100644
--- a/mlir/test/Dialect/OpenMP/invalid.mlir
+++ b/mlir/test/Dialect/OpenMP/invalid.mlir
@@ -1630,6 +1630,14 @@ func.func @omp_target_data(%map1: memref<?xi32>) {
 
 // -----
 
+func.func @omp_target_data() {
+  // expected-error @below {{At least one of map, useDevicePtr, or useDeviceAddr operand must be present}}
+  omp.target_data {}
+  return
+}
+
+// -----
+
 func.func @omp_target_enter_data(%map1: memref<?xi32>) {
   // expected-error @below {{to and alloc map types are permitted}}
   omp.target_enter_data map((from -> %map1 : memref<?xi32>)){}


        


More information about the flang-commits mailing list