[mlir][openacc] Add shutdown operation
authorValentin Clement <clementval@gmail.com>
Tue, 29 Sep 2020 17:12:54 +0000 (13:12 -0400)
committerclementval <clementval@gmail.com>
Tue, 29 Sep 2020 17:13:09 +0000 (13:13 -0400)
This patch introduces the acc.shutdown operation that represents an OpenACC shutdown directive.
Clauses are derived from the spec 2.14.2

Reviewed By: ftynse

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

mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
mlir/test/Dialect/OpenACC/invalid.mlir
mlir/test/Dialect/OpenACC/ops.mlir

index 0d8efcc..779f588 100644 (file)
@@ -349,6 +349,36 @@ def OpenACC_InitOp : OpenACC_Op<"init", [AttrSizedOperandSegments]> {
 }
 
 //===----------------------------------------------------------------------===//
+// 2.14.2. Shutdown
+//===----------------------------------------------------------------------===//
+
+def OpenACC_ShutdownOp : OpenACC_Op<"shutdown", [AttrSizedOperandSegments]> {
+  let summary = "shutdown operation";
+
+  let description = [{
+    The "acc.shutdown" operation represents the OpenACC shutdown executable
+    directive.
+
+    Example:
+
+    ```mlir
+    acc.shutdown
+    acc.shutdown device_num(%dev1 : i32)
+    ```
+  }];
+
+  let arguments = (ins Variadic<AnyInteger>:$deviceTypeOperands,
+                       Optional<IntOrIndex>:$deviceNumOperand,
+                       Optional<I1>:$ifCond);
+
+  let assemblyFormat = [{
+    ( `device_type` `(` $deviceTypeOperands^ `:` type($deviceTypeOperands) `)` )?
+    ( `device_num` `(` $deviceNumOperand^ `:` type($deviceNumOperand) `)` )?
+    ( `if` `(` $ifCond^ `)` )? attr-dict-with-keyword
+  }];
+}
+
+//===----------------------------------------------------------------------===//
 // 2.14.4. Update Directive
 //===----------------------------------------------------------------------===//
 
index 515f5a9..7ebba75 100644 (file)
@@ -149,6 +149,10 @@ static OptionalParseResult parserOptionalOperandAndTypeWithPrefix(
   return llvm::None;
 }
 
+static bool isComputeOperation(Operation *op) {
+  return isa<acc::ParallelOp>(op) || isa<acc::LoopOp>(op);
+}
+
 //===----------------------------------------------------------------------===//
 // ParallelOp
 //===----------------------------------------------------------------------===//
@@ -655,13 +659,26 @@ static LogicalResult verify(acc::DataOp dataOp) {
 static LogicalResult verify(acc::InitOp initOp) {
   Operation *currOp = initOp;
   while ((currOp = currOp->getParentOp())) {
-    if (isa<acc::ParallelOp>(currOp) || isa<acc::LoopOp>(currOp))
+    if (isComputeOperation(currOp))
       return initOp.emitOpError("cannot be nested in a compute operation");
   }
   return success();
 }
 
 //===----------------------------------------------------------------------===//
+// ShutdownOp
+//===----------------------------------------------------------------------===//
+
+static LogicalResult verify(acc::ShutdownOp op) {
+  Operation *currOp = op;
+  while ((currOp = currOp->getParentOp())) {
+    if (isComputeOperation(currOp))
+      return op.emitOpError("cannot be nested in a compute operation");
+  }
+  return success();
+}
+
+//===----------------------------------------------------------------------===//
 // UpdateOp
 //===----------------------------------------------------------------------===//
 
index 7a8a07f..c56ccdb 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: mlir-opt -split-input-file -verify-diagnostics %s
+// RUN: mlir-opt -allow-unregistered-dialect -split-input-file -verify-diagnostics %s
 
 // expected-error@+1 {{gang, worker or vector cannot appear with the seq attr}}
 acc.loop gang {
@@ -127,3 +127,29 @@ acc.loop {
   acc.init
   acc.yield
 }
+
+// -----
+
+acc.parallel {
+// expected-error@+1 {{'acc.shutdown' op cannot be nested in a compute operation}}
+  acc.shutdown
+  acc.yield
+}
+
+// -----
+
+acc.loop {
+// expected-error@+1 {{'acc.shutdown' op cannot be nested in a compute operation}}
+  acc.shutdown
+  acc.yield
+}
+
+// -----
+
+acc.loop {
+  "some.op"() ({
+    // expected-error@+1 {{'acc.shutdown' op cannot be nested in a compute operation}}
+    acc.shutdown
+  }) : () -> ()
+  acc.yield
+}
index a4dec5d..7ed4340 100644 (file)
@@ -620,3 +620,31 @@ acc.init if(%ifCond)
 // CHECK: acc.init device_num([[I32VALUE]] : i32)
 // CHECK: acc.init device_num([[IDXVALUE]] : index)
 // CHECK: acc.init if([[IFCOND]])
+
+// -----
+
+%i64Value = constant 1 : i64
+%i32Value = constant 1 : i32
+%i32Value2 = constant 2 : i32
+%idxValue = constant 1 : index
+%ifCond = constant true
+acc.shutdown
+acc.shutdown device_type(%i32Value : i32)
+acc.shutdown device_type(%i32Value, %i32Value2 : i32, i32)
+acc.shutdown device_num(%i64Value : i64)
+acc.shutdown device_num(%i32Value : i32)
+acc.shutdown device_num(%idxValue : index)
+acc.shutdown if(%ifCond)
+
+// CHECK: [[I64VALUE:%.*]] = constant 1 : i64
+// CHECK: [[I32VALUE:%.*]] = constant 1 : i32
+// CHECK: [[I32VALUE2:%.*]] = constant 2 : i32
+// CHECK: [[IDXVALUE:%.*]] = constant 1 : index
+// CHECK: [[IFCOND:%.*]] = constant true
+// CHECK: acc.shutdown
+// CHECK: acc.shutdown device_type([[I32VALUE]] : i32)
+// CHECK: acc.shutdown device_type([[I32VALUE]], [[I32VALUE2]] : i32, i32)
+// CHECK: acc.shutdown device_num([[I64VALUE]] : i64)
+// CHECK: acc.shutdown device_num([[I32VALUE]] : i32)
+// CHECK: acc.shutdown device_num([[IDXVALUE]] : index)
+// CHECK: acc.shutdown if([[IFCOND]])