Move the definitions of AllocOp and DeallocOp to tablegen.
authorRiver Riddle <riverriddle@google.com>
Thu, 9 May 2019 05:38:01 +0000 (22:38 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 11 May 2019 02:25:03 +0000 (19:25 -0700)
--

PiperOrigin-RevId: 247359472

mlir/include/mlir/StandardOps/Ops.h
mlir/include/mlir/StandardOps/Ops.td
mlir/lib/StandardOps/Ops.cpp

index 8d2ee87..81d3614 100644 (file)
@@ -46,46 +46,6 @@ public:
 #define GET_OP_CLASSES
 #include "mlir/StandardOps/Ops.h.inc"
 
-/// The "alloc" operation allocates a region of memory, as specified by its
-/// memref type. For example:
-///
-///   %0 = alloc() : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
-///
-/// The optional list of dimension operands are bound to the dynamic dimensions
-/// specified in its memref type. In the example below, the ssa value '%d' is
-/// bound to the second dimension of the memref (which is dynamic).
-///
-///   %0 = alloc(%d) : memref<8x?xf32, (d0, d1) -> (d0, d1), 1>
-///
-/// The optional list of symbol operands are bound to the symbols of the
-/// memrefs affine map. In the example below, the ssa value '%s' is bound to
-/// the symbol 's0' in the affine map specified in the allocs memref type.
-///
-///   %0 = alloc()[%s] : memref<8x64xf32, (d0, d1)[s0] -> ((d0 + s0), d1), 1>
-///
-/// This operation returns a single ssa value of memref type, which can be used
-/// by subsequent load and store operations.
-class AllocOp
-    : public Op<AllocOp, OpTrait::VariadicOperands, OpTrait::OneResult> {
-public:
-  friend Operation;
-  using Op::Op;
-
-  /// The result of an alloc is always a MemRefType.
-  MemRefType getType() { return getResult()->getType().cast<MemRefType>(); }
-
-  static StringRef getOperationName() { return "std.alloc"; }
-
-  // Hooks to customize behavior of this op.
-  static void build(Builder *builder, OperationState *result,
-                    MemRefType memrefType, ArrayRef<Value *> operands = {});
-  LogicalResult verify();
-  static ParseResult parse(OpAsmParser *parser, OperationState *result);
-  void print(OpAsmPrinter *p);
-  static void getCanonicalizationPatterns(OwningRewritePatternList &results,
-                                          MLIRContext *context);
-};
-
 /// The "br" operation represents a branch operation in a function.
 /// The operation takes variable number of operands and produces no results.
 /// The operand number and types for each successor must match the
@@ -497,36 +457,6 @@ public:
   static bool isClassFor(Operation *op);
 };
 
-/// The "dealloc" operation frees the region of memory referenced by a memref
-/// which was originally created by the "alloc" operation.
-/// The "dealloc" operation should not be called on memrefs which alias an
-//  alloc'd memref (i.e. memrefs returned by the "view" and "reshape"
-/// operations).
-///
-///   %0 = alloc() : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
-///
-///   dealloc %0 : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
-///
-class DeallocOp
-    : public Op<DeallocOp, OpTrait::OneOperand, OpTrait::ZeroResult> {
-public:
-  friend Operation;
-  using Op::Op;
-
-  Value *getMemRef() { return getOperand(); }
-  void setMemRef(Value *value) { setOperand(value); }
-
-  static StringRef getOperationName() { return "std.dealloc"; }
-
-  // Hooks to customize behavior of this op.
-  static void build(Builder *builder, OperationState *result, Value *memref);
-  LogicalResult verify();
-  static ParseResult parse(OpAsmParser *parser, OperationState *result);
-  void print(OpAsmPrinter *p);
-  static void getCanonicalizationPatterns(OwningRewritePatternList &results,
-                                          MLIRContext *context);
-};
-
 /// The "dim" operation takes a memref or tensor operand and returns an
 /// "index".  It requires a single integer attribute named "index".  It
 /// returns the size of the specified dimension.  For example:
index 12424f5..237730c 100644 (file)
@@ -84,6 +84,50 @@ def AddIOp : IntArithmeticOp<"addi", [Commutative]> {
   let hasConstantFolder = 0b1;
 }
 
+def AllocOp : Op<Standard_Dialect, "alloc"> {
+  let summary = "memory allocation operation";
+  let description = [{
+    The "alloc" operation allocates a region of memory, as specified by its
+    memref type. For example:
+
+      %0 = alloc() : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
+
+    The optional list of dimension operands are bound to the dynamic dimensions
+    specified in its memref type. In the example below, the ssa value '%d' is
+    bound to the second dimension of the memref (which is dynamic).
+
+      %0 = alloc(%d) : memref<8x?xf32, (d0, d1) -> (d0, d1), 1>
+
+    The optional list of symbol operands are bound to the symbols of the
+    memrefs affine map. In the example below, the ssa value '%s' is bound to
+    the symbol 's0' in the affine map specified in the allocs memref type.
+
+      %0 = alloc()[%s] : memref<8x64xf32, (d0, d1)[s0] -> ((d0 + s0), d1), 1>
+
+    This operation returns a single ssa value of memref type, which can be used
+    by subsequent load and store operations.
+  }];
+
+  let arguments = (ins Variadic<Index>:$value);
+  let results = (outs MemRef<AnyType>);
+
+  let builders = [OpBuilder<
+    "Builder *builder, OperationState *result, MemRefType memrefType", [{
+       result->types.push_back(memrefType);
+     }]
+  >];
+
+  let parser = [{ return parseAllocOp(parser, result); }];
+  let printer = [{ return printAllocOp(p, *this); }];
+  let verifier = [{ return ::verify(*this); }];
+
+  let extraClassDeclaration = [{
+    MemRefType getType() { return getResult()->getType().cast<MemRefType>(); }
+  }];
+
+  let hasCanonicalizer = 0b1;
+}
+
 def AndOp : IntArithmeticOp<"and", [Commutative]> {
   let summary = "integer binary and";
   let hasConstantFolder = 0b1;
@@ -111,6 +155,28 @@ def ConstantOp : Op<Standard_Dialect, "constant", [NoSideEffect]> {
   let hasConstantFolder = 1;
 }
 
+def DeallocOp : Op<Standard_Dialect, "dealloc"> {
+  let summary = "memory deallocation operation";
+  let description = [{
+    The "dealloc" operation frees the region of memory referenced by a memref
+    which was originally created by the "alloc" operation.
+    The "dealloc" operation should not be called on memrefs which alias an
+    alloc'd memref (i.e. memrefs returned by the "view" and "reshape"
+    operations).
+
+      %0 = alloc() : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
+      dealloc %0 : memref<8x64xf32, (d0, d1) -> (d0, d1), 1>
+  }];
+
+  let arguments = (ins MemRef<AnyType>:$memref);
+
+  let parser = [{ return parseDeallocOp(parser, result); }];
+  let printer = [{ return printDeallocOp(p, *this); }];
+  let verifier = [{ return ::verify(*this); }];
+
+  let hasCanonicalizer = 0b1;
+}
+
 def DivFOp : FloatArithmeticOp<"divf"> {
   let summary = "floating point division operation";
 }
index 64e7b57..9883b02 100644 (file)
@@ -61,10 +61,9 @@ void detail::printStandardBinaryOp(Operation *op, OpAsmPrinter *p) {
 
 StandardOpsDialect::StandardOpsDialect(MLIRContext *context)
     : Dialect(/*name=*/"std", context) {
-  addOperations<AllocOp, BranchOp, CallOp, CallIndirectOp, CmpFOp, CmpIOp,
-                CondBranchOp, DeallocOp, DimOp, DmaStartOp, DmaWaitOp,
-                ExtractElementOp, LoadOp, MemRefCastOp, ReturnOp, SelectOp,
-                StoreOp, TensorCastOp,
+  addOperations<BranchOp, CallOp, CallIndirectOp, CmpFOp, CmpIOp, CondBranchOp,
+                DimOp, DmaStartOp, DmaWaitOp, ExtractElementOp, LoadOp,
+                MemRefCastOp, ReturnOp, SelectOp, StoreOp, TensorCastOp,
 #define GET_OP_LIST
 #include "mlir/StandardOps/Ops.cpp.inc"
                 >();
@@ -207,23 +206,18 @@ Value *AddIOp::fold() {
 // AllocOp
 //===----------------------------------------------------------------------===//
 
-void AllocOp::build(Builder *builder, OperationState *result,
-                    MemRefType memrefType, ArrayRef<Value *> operands) {
-  result->addOperands(operands);
-  result->types.push_back(memrefType);
-}
-
-void AllocOp::print(OpAsmPrinter *p) {
-  MemRefType type = getType();
+static void printAllocOp(OpAsmPrinter *p, AllocOp op) {
   *p << "alloc";
+
   // Print dynamic dimension operands.
-  printDimAndSymbolList(operand_begin(), operand_end(),
+  MemRefType type = op.getType();
+  printDimAndSymbolList(op.operand_begin(), op.operand_end(),
                         type.getNumDynamicDims(), p);
-  p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/{"map"});
+  p->printOptionalAttrDict(op.getAttrs(), /*elidedAttrs=*/{"map"});
   *p << " : " << type;
 }
 
-ParseResult AllocOp::parse(OpAsmParser *parser, OperationState *result) {
+static ParseResult parseAllocOp(OpAsmParser *parser, OperationState *result) {
   MemRefType type;
 
   // Parse the dimension operands and optional symbol operands, followed by a
@@ -240,19 +234,18 @@ ParseResult AllocOp::parse(OpAsmParser *parser, OperationState *result) {
   // Verification still checks that the total number of operands matches
   // the number of symbols in the affine map, plus the number of dynamic
   // dimensions in the memref.
-  if (numDimOperands != type.getNumDynamicDims()) {
-    return parser->emitError(parser->getNameLoc(),
-                             "dimension operand count does not equal memref "
-                             "dynamic dimension count");
-  }
+  if (numDimOperands != type.getNumDynamicDims())
+    return parser->emitError(parser->getNameLoc())
+           << "dimension operand count does not equal memref dynamic dimension "
+              "count";
   result->types.push_back(type);
   return success();
 }
 
-LogicalResult AllocOp::verify() {
-  auto memRefType = getResult()->getType().dyn_cast<MemRefType>();
+static LogicalResult verify(AllocOp op) {
+  auto memRefType = op.getResult()->getType().dyn_cast<MemRefType>();
   if (!memRefType)
-    return emitOpError("result must be a memref");
+    return op.emitOpError("result must be a memref");
 
   unsigned numSymbols = 0;
   if (!memRefType.getAffineMaps().empty()) {
@@ -266,20 +259,21 @@ LogicalResult AllocOp::verify() {
     //
     // Verify that the layout affine map matches the rank of the memref.
     if (affineMap.getNumDims() != memRefType.getRank())
-      return emitOpError("affine map dimension count must equal memref rank");
+      return op.emitOpError(
+          "affine map dimension count must equal memref rank");
   }
   unsigned numDynamicDims = memRefType.getNumDynamicDims();
   // Check that the total number of operands matches the number of symbols in
   // the affine map, plus the number of dynamic dimensions specified in the
   // memref type.
-  if (getOperation()->getNumOperands() != numDynamicDims + numSymbols)
-    return emitOpError(
+  if (op.getOperation()->getNumOperands() != numDynamicDims + numSymbols)
+    return op.emitOpError(
         "operand count does not equal dimension plus symbol operand count");
 
   // Verify that all operands are of type Index.
-  for (auto *operand : getOperands())
+  for (auto *operand : op.getOperands())
     if (!operand->getType().isIndex())
-      return emitOpError("requires operands to be of type Index");
+      return op.emitOpError("requires operands to be of type Index");
   return success();
 }
 
@@ -1256,7 +1250,7 @@ struct SimplifyDeadDealloc : public RewritePattern {
     auto dealloc = op->cast<DeallocOp>();
 
     // Check that the memref operand's defining operation is an AllocOp.
-    Value *memref = dealloc.getMemRef();
+    Value *memref = dealloc.memref();
     Operation *defOp = memref->getDefiningOp();
     if (!isa_and_nonnull<AllocOp>(defOp))
       return matchFailure();
@@ -1273,15 +1267,11 @@ struct SimplifyDeadDealloc : public RewritePattern {
 };
 } // end anonymous namespace.
 
-void DeallocOp::build(Builder *builder, OperationState *result, Value *memref) {
-  result->addOperands(memref);
-}
-
-void DeallocOp::print(OpAsmPrinter *p) {
-  *p << "dealloc " << *getMemRef() << " : " << getMemRef()->getType();
+static void printDeallocOp(OpAsmPrinter *p, DeallocOp op) {
+  *p << "dealloc " << *op.memref() << " : " << op.memref()->getType();
 }
 
-ParseResult DeallocOp::parse(OpAsmParser *parser, OperationState *result) {
+static ParseResult parseDeallocOp(OpAsmParser *parser, OperationState *result) {
   OpAsmParser::OperandType memrefInfo;
   MemRefType type;
 
@@ -1290,9 +1280,9 @@ ParseResult DeallocOp::parse(OpAsmParser *parser, OperationState *result) {
                  parser->resolveOperand(memrefInfo, type, result->operands));
 }
 
-LogicalResult DeallocOp::verify() {
-  if (!getMemRef()->getType().isa<MemRefType>())
-    return emitOpError("operand must be a memref");
+static LogicalResult verify(DeallocOp op) {
+  if (!op.memref()->getType().isa<MemRefType>())
+    return op.emitOpError("operand must be a memref");
   return success();
 }