[Mlir-commits] [mlir] 3028bca - [mlir] Move AllocationOpInterface to Bufferize/IR/AllocationOpInterface.td.

Alexander Belyaev llvmlistbot at llvm.org
Mon Nov 22 12:01:09 PST 2021


Author: Alexander Belyaev
Date: 2021-11-22T21:00:59+01:00
New Revision: 3028bca6a987e424365ca67f6dc29e037e52ea11

URL: https://github.com/llvm/llvm-project/commit/3028bca6a987e424365ca67f6dc29e037e52ea11
DIFF: https://github.com/llvm/llvm-project/commit/3028bca6a987e424365ca67f6dc29e037e52ea11.diff

LOG: [mlir] Move AllocationOpInterface to Bufferize/IR/AllocationOpInterface.td.

Remove the interface from op defs in MemRefOps.td and make it an external model.

This is the first PR of many that will move bufferization-related ops, interfaces, passes to Dialect/Bufferize.
RFC: https://llvm.discourse.group/t/rfc-dialect-for-bufferization-related-ops/4712
It is still debated if the comprehensive bufferization has to be moved there as well, so for now I am just moving the "gradual" bufferization.

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

Added: 
    mlir/include/mlir/Dialect/Bufferization/CMakeLists.txt
    mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h
    mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td
    mlir/include/mlir/Dialect/Bufferization/IR/CMakeLists.txt
    mlir/lib/Dialect/Bufferization/CMakeLists.txt
    mlir/lib/Dialect/Bufferization/IR/AllocationOpInterface.cpp
    mlir/lib/Dialect/Bufferization/IR/CMakeLists.txt

Modified: 
    mlir/include/mlir/Dialect/CMakeLists.txt
    mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
    mlir/include/mlir/Interfaces/SideEffectInterfaces.td
    mlir/lib/Dialect/CMakeLists.txt
    mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
    mlir/lib/Transforms/BufferDeallocation.cpp
    mlir/lib/Transforms/CMakeLists.txt
    utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Bufferization/CMakeLists.txt b/mlir/include/mlir/Dialect/Bufferization/CMakeLists.txt
new file mode 100644
index 0000000000000..f33061b2d87cf
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Bufferization/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(IR)

diff  --git a/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h b/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h
new file mode 100644
index 0000000000000..cfba581568f17
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h
@@ -0,0 +1,21 @@
+//===- AllocationOpInterface.h - Allocation op interface ------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the operation interface for allocation ops.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_BUFFERIZATION_IR_ALLOCATIONOPINTERFACE_H_
+#define MLIR_DIALECT_BUFFERIZATION_IR_ALLOCATIONOPINTERFACE_H_
+
+#include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/IR/Builders.h"
+
+#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h.inc"
+
+#endif // MLIR_DIALECT_BUFFERIZATION_IR_ALLOCATIONOPINTERFACE_H_

diff  --git a/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td b/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td
new file mode 100644
index 0000000000000..87531951ea577
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td
@@ -0,0 +1,61 @@
+//===-- AllocationOpInterface.td - Allocation op interface -*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines the interface with allocation-related methods. It is used by the
+// buffer deallocation pass.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef ALLOCATION_OP_INTERFACE
+#define ALLOCATION_OP_INTERFACE
+
+include "mlir/IR/OpBase.td"
+
+//===----------------------------------------------------------------------===//
+// AllocationOpInterface
+//===----------------------------------------------------------------------===//
+
+def AllocationOpInterface : OpInterface<"AllocationOpInterface"> {
+  let description = [{
+    This interface provides general allocation-related methods that are
+    designed for allocation operations. For example, it offers the ability to
+    construct associated deallocation and clone operations that are compatible
+    with the current allocation operation.
+  }];
+  let cppNamespace = "::mlir::bufferization";
+
+  let methods = [
+    StaticInterfaceMethod<[{
+        Builds a deallocation operation using the provided builder and the
+        current allocation value (which refers to the current Op implementing
+        this interface). The allocation value is a result of the current
+        operation implementing this interface. If there is no compatible
+        deallocation operation, this method can return ::llvm::None.
+      }],
+      "::mlir::Optional<::mlir::Operation*>", "buildDealloc",
+      (ins "::mlir::OpBuilder&":$builder, "::mlir::Value":$alloc), [{}],
+      /*defaultImplementation=*/[{ return llvm::None; }]
+    >,
+    StaticInterfaceMethod<[{
+        Builds a clone operation using the provided builder and the current
+        allocation value (which refers to the current Op implementing this
+        interface). The allocation value is a result of the current operation
+        implementing this interface. If there is no compatible clone operation,
+        this method can return ::llvm::None.
+      }],
+      "::mlir::Optional<::mlir::Value>", "buildClone",
+      (ins "::mlir::OpBuilder&":$builder, "::mlir::Value":$alloc), [{}],
+      /*defaultImplementation=*/[{
+          return builder.create<memref::CloneOp>(alloc.getLoc(), alloc)
+            .getResult();
+      }]
+    >
+  ];
+}
+
+#endif  // ALLOCATION_OP_INTERFACE

diff  --git a/mlir/include/mlir/Dialect/Bufferization/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Bufferization/IR/CMakeLists.txt
new file mode 100644
index 0000000000000..ab8eefd6c6105
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Bufferization/IR/CMakeLists.txt
@@ -0,0 +1 @@
+add_mlir_interface(AllocationOpInterface)

diff  --git a/mlir/include/mlir/Dialect/CMakeLists.txt b/mlir/include/mlir/Dialect/CMakeLists.txt
index 80153b6eba6f9..40fc101862e0e 100644
--- a/mlir/include/mlir/Dialect/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/CMakeLists.txt
@@ -4,6 +4,7 @@ add_subdirectory(Async)
 add_subdirectory(ArmNeon)
 add_subdirectory(ArmSVE)
 add_subdirectory(AMX)
+add_subdirectory(Bufferization)
 add_subdirectory(Complex)
 add_subdirectory(DLTI)
 add_subdirectory(EmitC)

diff  --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index dac7b756b4f73..bc74b4d3ada5f 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -120,10 +120,7 @@ def AssumeAlignmentOp : MemRef_Op<"assume_alignment"> {
 // AllocOp
 //===----------------------------------------------------------------------===//
 
-def MemRef_AllocOp : AllocLikeOp<"alloc", DefaultResource, [
-    DeclareOpInterfaceMethods<AllocationOpInterface,
-      ["buildDealloc", "buildClone"]>]
-  > {
+def MemRef_AllocOp : AllocLikeOp<"alloc", DefaultResource, []> {
   let summary = "memory allocation operation";
   let description = [{
     The `alloc` operation allocates a region of memory, as specified by its
@@ -418,8 +415,6 @@ def MemRef_CastOp : MemRef_Op<"cast", [
 def CloneOp : MemRef_Op<"clone", [
     CopyOpInterface,
     DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
-    DeclareOpInterfaceMethods<AllocationOpInterface,
-      ["buildDealloc", "buildClone"]>
   ]> {
   let builders = [
     OpBuilder<(ins "Value":$value), [{

diff  --git a/mlir/include/mlir/Interfaces/SideEffectInterfaces.td b/mlir/include/mlir/Interfaces/SideEffectInterfaces.td
index 09cb5c0cdc6a1..1c12a5a56e7f7 100644
--- a/mlir/include/mlir/Interfaces/SideEffectInterfaces.td
+++ b/mlir/include/mlir/Interfaces/SideEffectInterfaces.td
@@ -16,45 +16,6 @@
 
 include "mlir/Interfaces/SideEffectInterfaceBase.td"
 
-//===----------------------------------------------------------------------===//
-// AllocationOpInterface
-//===----------------------------------------------------------------------===//
-
-def AllocationOpInterface : OpInterface<"AllocationOpInterface"> {
-  let description = [{
-    This interface provides general allocation-related methods that are
-    designed for allocation operations. For example, it offers the ability to
-    construct associated deallocation and clone operations that are compatible
-    with the current allocation operation.
-  }];
-  let cppNamespace = "::mlir";
-
-  let methods = [
-    StaticInterfaceMethod<[{
-        Builds a deallocation operation using the provided builder and the
-        current allocation value (which refers to the current Op implementing
-        this interface). The allocation value is a result of the current
-        operation implementing this interface. If there is no compatible
-        deallocation operation, this method can return ::llvm::None.
-      }],
-      "::mlir::Optional<::mlir::Operation*>", "buildDealloc",
-      (ins "::mlir::OpBuilder&":$opBuilder, "::mlir::Value":$alloc), [{}],
-      /*defaultImplementation=*/[{ return llvm::None; }]
-    >,
-    StaticInterfaceMethod<[{
-        Builds a clone operation using the provided builder and the current
-        allocation value (which refers to the current Op implementing this
-        interface). The allocation value is a result of the current operation
-        implementing this interface. If there is no compatible clone operation,
-        this method can return ::llvm::None.
-      }],
-      "::mlir::Optional<::mlir::Value>", "buildClone",
-      (ins "::mlir::OpBuilder&":$opBuilder, "::mlir::Value":$alloc), [{}],
-      /*defaultImplementation=*/[{ return llvm::None; }]
-    >
-  ];
-}
-
 //===----------------------------------------------------------------------===//
 // MemoryEffects
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Bufferization/CMakeLists.txt b/mlir/lib/Dialect/Bufferization/CMakeLists.txt
new file mode 100644
index 0000000000000..f33061b2d87cf
--- /dev/null
+++ b/mlir/lib/Dialect/Bufferization/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(IR)

diff  --git a/mlir/lib/Dialect/Bufferization/IR/AllocationOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/AllocationOpInterface.cpp
new file mode 100644
index 0000000000000..76fc1fe72ada7
--- /dev/null
+++ b/mlir/lib/Dialect/Bufferization/IR/AllocationOpInterface.cpp
@@ -0,0 +1,10 @@
+//===- AllocationOpInterface.cpp -  Allocation op interface ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
+#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.cpp.inc"

diff  --git a/mlir/lib/Dialect/Bufferization/IR/CMakeLists.txt b/mlir/lib/Dialect/Bufferization/IR/CMakeLists.txt
new file mode 100644
index 0000000000000..9931ea4e48f41
--- /dev/null
+++ b/mlir/lib/Dialect/Bufferization/IR/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_mlir_library(MLIRAllocationOpInterface
+  AllocationOpInterface.cpp
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Bufferization/IR
+
+  DEPENDS
+  MLIRAllocationOpInterfaceIncGen
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  )

diff  --git a/mlir/lib/Dialect/CMakeLists.txt b/mlir/lib/Dialect/CMakeLists.txt
index 13cb4a418db35..da2be111e2083 100644
--- a/mlir/lib/Dialect/CMakeLists.txt
+++ b/mlir/lib/Dialect/CMakeLists.txt
@@ -4,6 +4,7 @@ add_subdirectory(ArmNeon)
 add_subdirectory(ArmSVE)
 add_subdirectory(Async)
 add_subdirectory(AMX)
+add_subdirectory(Bufferization)
 add_subdirectory(Complex)
 add_subdirectory(DLTI)
 add_subdirectory(EmitC)

diff  --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 0837bd2f888ec..13f10a7d509e5 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -196,15 +196,6 @@ struct SimplifyDeadAlloc : public OpRewritePattern<T> {
 };
 } // end anonymous namespace.
 
-Optional<Operation *> AllocOp::buildDealloc(OpBuilder &builder, Value alloc) {
-  return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
-      .getOperation();
-}
-
-Optional<Value> AllocOp::buildClone(OpBuilder &builder, Value alloc) {
-  return builder.create<memref::CloneOp>(alloc.getLoc(), alloc).getResult();
-}
-
 void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
                                           MLIRContext *context) {
   results.add<SimplifyAllocConst<AllocOp>, SimplifyDeadAlloc<AllocOp>>(context);
@@ -653,15 +644,6 @@ OpFoldResult CloneOp::fold(ArrayRef<Attribute> operands) {
   return succeeded(foldMemRefCast(*this)) ? getResult() : Value();
 }
 
-Optional<Operation *> CloneOp::buildDealloc(OpBuilder &builder, Value alloc) {
-  return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
-      .getOperation();
-}
-
-Optional<Value> CloneOp::buildClone(OpBuilder &builder, Value alloc) {
-  return builder.create<memref::CloneOp>(alloc.getLoc(), alloc).getResult();
-}
-
 //===----------------------------------------------------------------------===//
 // DeallocOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Transforms/BufferDeallocation.cpp b/mlir/lib/Transforms/BufferDeallocation.cpp
index 9e1f2123382e3..061f22128b145 100644
--- a/mlir/lib/Transforms/BufferDeallocation.cpp
+++ b/mlir/lib/Transforms/BufferDeallocation.cpp
@@ -51,6 +51,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "PassDetail.h"
+
+#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
 #include "mlir/Dialect/MemRef/IR/MemRef.h"
 #include "mlir/Dialect/StandardOps/IR/Ops.h"
 #include "mlir/IR/Operation.h"
@@ -193,7 +195,8 @@ class Backedges {
 /// introduce clones that in turn leads to additional deallocations.
 class BufferDeallocation : public BufferPlacementTransformationBase {
 public:
-  using AliasAllocationMapT = llvm::DenseMap<Value, AllocationOpInterface>;
+  using AliasAllocationMapT =
+      llvm::DenseMap<Value, bufferization::AllocationOpInterface>;
 
   BufferDeallocation(Operation *op)
       : BufferPlacementTransformationBase(op), dominators(op),
@@ -208,7 +211,8 @@ class BufferDeallocation : public BufferPlacementTransformationBase {
     for (const BufferPlacementAllocs::AllocEntry &entry : allocs) {
       // Get the defining allocation operation.
       Value alloc = std::get<0>(entry);
-      auto allocationInterface = alloc.getDefiningOp<AllocationOpInterface>();
+      auto allocationInterface =
+          alloc.getDefiningOp<bufferization::AllocationOpInterface>();
       // If there is no existing deallocation operation and no implementation of
       // the AllocationOpInterface, we cannot apply the BufferDeallocation pass.
       if (!std::get<1>(entry) && !allocationInterface) {
@@ -614,10 +618,26 @@ class BufferDeallocation : public BufferPlacementTransformationBase {
 // BufferDeallocationPass
 //===----------------------------------------------------------------------===//
 
+template <typename T>
+struct DefaultAllocationInterface
+    : public bufferization::AllocationOpInterface::FallbackModel<T> {
+  static Optional<Operation *> buildDealloc(OpBuilder &builder, Value alloc) {
+    return builder.create<memref::DeallocOp>(alloc.getLoc(), alloc)
+        .getOperation();
+  }
+};
+
 /// The actual buffer deallocation pass that inserts and moves dealloc nodes
 /// into the right positions. Furthermore, it inserts additional clones if
 /// necessary. It uses the algorithm described at the top of the file.
 struct BufferDeallocationPass : BufferDeallocationBase<BufferDeallocationPass> {
+  void getDependentDialects(DialectRegistry &registry) const override {
+    registry.insert<memref::MemRefDialect>();
+    registry.addOpInterface<memref::AllocOp,
+                            DefaultAllocationInterface<memref::AllocOp>>();
+    registry.addOpInterface<memref::CloneOp,
+                            DefaultAllocationInterface<memref::CloneOp>>();
+  }
 
   void runOnFunction() override {
     // Ensure that there are supported loops only.

diff  --git a/mlir/lib/Transforms/CMakeLists.txt b/mlir/lib/Transforms/CMakeLists.txt
index 54f3693c89c69..b75b2a45ea312 100644
--- a/mlir/lib/Transforms/CMakeLists.txt
+++ b/mlir/lib/Transforms/CMakeLists.txt
@@ -32,6 +32,7 @@ add_mlir_library(MLIRTransforms
   LINK_LIBS PUBLIC
   MLIRAffine
   MLIRAnalysis
+  MLIRAllocationOpInterface
   MLIRCopyOpInterface
   MLIRLoopLikeInterface
   MLIRMemRef

diff  --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index b29c59e3a0f1f..fe4d90e92724f 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -4432,6 +4432,7 @@ cc_library(
     includes = ["include"],
     deps = [
         ":Affine",
+        ":AllocationOpInterface",
         ":Analysis",
         ":ArithmeticDialect",
         ":ControlFlowInterfaces",
@@ -7488,6 +7489,43 @@ cc_library(
     ],
 )
 
+td_library(
+    name = "AllocationOpInterfaceTdFiles",
+    srcs = ["include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td"],
+    includes = ["include"],
+    deps = [":OpBaseTdFiles"],
+)
+
+gentbl_cc_library(
+    name = "AllocationOpInterfaceIncGen",
+    strip_include_prefix = "include",
+    tbl_outs = [
+        (
+            ["-gen-op-interface-decls"],
+            "include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h.inc",
+        ),
+        (
+            ["-gen-op-interface-defs"],
+            "include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.cpp.inc",
+        ),
+    ],
+    tblgen = ":mlir-tblgen",
+    td_file = "include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.td",
+    deps = [":AllocationOpInterfaceTdFiles"],
+)
+
+cc_library(
+    name = "AllocationOpInterface",
+    srcs = ["lib/Dialect/Bufferization/IR/AllocationOpInterface.cpp"],
+    hdrs = ["include/mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"],
+    includes = ["include"],
+    deps = [
+        ":AllocationOpInterfaceIncGen",
+        ":IR",
+        ":MemRefDialect",
+    ],
+)
+
 td_library(
     name = "DLTIDialectTdFiles",
     srcs = [


        


More information about the Mlir-commits mailing list