[Mlir-commits] [mlir] 4397a1b - [mlir][linalg][bufferize] Remove remaining linalg dependencies

Matthias Springer llvmlistbot at llvm.org
Thu Nov 11 02:05:00 PST 2021


Author: Matthias Springer
Date: 2021-11-11T19:04:41+09:00
New Revision: 4397a1baefdb0e8c83329acd31866890199856b7

URL: https://github.com/llvm/llvm-project/commit/4397a1baefdb0e8c83329acd31866890199856b7
DIFF: https://github.com/llvm/llvm-project/commit/4397a1baefdb0e8c83329acd31866890199856b7.diff

LOG: [mlir][linalg][bufferize] Remove remaining linalg dependencies

* Move "linalg.inplaceable" attr name literals to BufferizableOpInterface.
* Use `memref.copy` by default. Override to `linalg.copy` in ComprehensiveBufferizePass.

These are the last remaining code dependencies on Linalg in Comprehensive Bufferize. The next commit will make ComprehensiveBufferize independent of the Linalg dialect.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.td
    mlir/include/mlir/Dialect/Linalg/IR/LinalgBase.td
    mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ComprehensiveBufferize.cpp
    mlir/lib/Dialect/Linalg/IR/CMakeLists.txt
    mlir/lib/Dialect/Linalg/IR/LinalgTypes.cpp
    mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
    mlir/lib/Dialect/SparseTensor/Transforms/CMakeLists.txt
    mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp
    utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.td b/mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.td
index 66e26b3e6e611..4742b51623e10 100644
--- a/mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.td
+++ b/mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.td
@@ -233,6 +233,18 @@ def BufferizableOpInterface : OpInterface<"BufferizableOpInterface"> {
           && static_cast<bool>(
               bufferizableOp.getAliasingOpResult(opOperand));
     }
+
+    // TODO: The following two attributes should belong to the tensor dialect.
+    // The corresponding verifier should also be in the tensor dialect.
+    /// Attribute name used to mark region arguments that can be bufferized
+    /// in-place during linalg comprehensive bufferization.
+    constexpr const static ::llvm::StringLiteral
+      kInplaceableAttrName = "linalg.inplaceable";
+
+    /// Attribute name used to mark the bufferization layout for region
+    /// arguments during linalg comprehensive bufferization.
+    constexpr const static ::llvm::StringLiteral
+      kBufferLayoutAttrName = "linalg.buffer_layout";
   }];
 }
 

diff  --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgBase.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgBase.td
index 206b5a35d641e..de3703b71acb0 100644
--- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgBase.td
+++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgBase.td
@@ -48,16 +48,6 @@ def Linalg_Dialect : Dialect {
     constexpr const static ::llvm::StringLiteral
         kMemoizedIndexingMapsAttrName = "linalg.memoized_indexing_maps";
 
-    /// Attribute name used to mark region arguments that can be bufferized
-    /// in-place during linalg comprehensive bufferization.
-    constexpr const static ::llvm::StringLiteral
-      kInplaceableAttrName = "linalg.inplaceable";
-
-    /// Attribute name used to mark the bufferization layout for region
-    /// arguments during linalg comprehensive bufferization.
-    constexpr const static ::llvm::StringLiteral
-      kBufferLayoutAttrName = "linalg.buffer_layout";
-
     using RegionBuilderFunType =
       llvm::function_ref<void(ImplicitLocOpBuilder &b, Block &)>;
     RegionBuilderFunType getRegionBuilder(StringRef name) {

diff  --git a/mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ComprehensiveBufferize.cpp b/mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ComprehensiveBufferize.cpp
index 60b0e86d86287..cbfbef467b6f3 100644
--- a/mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ComprehensiveBufferize.cpp
+++ b/mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ComprehensiveBufferize.cpp
@@ -209,7 +209,8 @@ static void setInPlaceOpResult(OpResult opResult, bool inPlace) {
 /// `bbArg`.
 static void setInPlaceFuncArgument(BlockArgument bbArg, bool inPlace) {
   auto funcOp = cast<FuncOp>(bbArg.getOwner()->getParentOp());
-  funcOp.setArgAttr(bbArg.getArgNumber(), LinalgDialect::kInplaceableAttrName,
+  funcOp.setArgAttr(bbArg.getArgNumber(),
+                    BufferizableOpInterface::kInplaceableAttrName,
                     BoolAttr::get(bbArg.getContext(), inPlace));
 }
 
@@ -218,9 +219,9 @@ static void setInPlaceFuncArgument(BlockArgument bbArg, bool inPlace) {
 static void removeBufferizationFuncArguments(BlockArgument bbArg) {
   auto funcOp = cast<FuncOp>(bbArg.getOwner()->getParentOp());
   funcOp.removeArgAttr(bbArg.getArgNumber(),
-                       LinalgDialect::kBufferLayoutAttrName);
+                       BufferizableOpInterface::kBufferLayoutAttrName);
   funcOp.removeArgAttr(bbArg.getArgNumber(),
-                       LinalgDialect::kInplaceableAttrName);
+                       BufferizableOpInterface::kInplaceableAttrName);
 }
 
 //===----------------------------------------------------------------------===//
@@ -1146,7 +1147,7 @@ inPlaceAnalysisFuncOpBody(FuncOp funcOp, BufferizationAliasInfo &aliasInfo,
   // bufferizing to a writeable memory.
   for (BlockArgument bbArg : funcOp.getArguments()) {
     BoolAttr inplaceAttr = funcOp.getArgAttrOfType<BoolAttr>(
-        bbArg.getArgNumber(), LinalgDialect::kInplaceableAttrName);
+        bbArg.getArgNumber(), BufferizableOpInterface::kInplaceableAttrName);
     if (inplaceAttr && inplaceAttr.getValue())
       aliasInfo.setBufferizesToWritableMemory(bbArg);
   }
@@ -1179,7 +1180,7 @@ void mlir::linalg::comprehensive_bufferize::defaultMemCpyFn(OpBuilder &b,
                                                             Location loc,
                                                             Value from,
                                                             Value to) {
-  b.create<CopyOp>(loc, from, to);
+  b.create<memref::CopyOp>(loc, from, to);
 }
 
 LogicalResult mlir::linalg::comprehensive_bufferize::bufferizeOp(
@@ -1532,7 +1533,7 @@ static void layoutPostProcessing(ModuleOp moduleOp) {
       Type inputType = it.value();
       auto memrefType = inputType.dyn_cast<MemRefType>();
       auto layoutAttr = funcOp.getArgAttrOfType<AffineMapAttr>(
-          argNumber, LinalgDialect::kBufferLayoutAttrName);
+          argNumber, BufferizableOpInterface::kBufferLayoutAttrName);
       AffineMap desiredLayoutMap =
           layoutAttr ? layoutAttr.getValue() : AffineMap();
       AffineMap currentLayoutMap =

diff  --git a/mlir/lib/Dialect/Linalg/IR/CMakeLists.txt b/mlir/lib/Dialect/Linalg/IR/CMakeLists.txt
index 98ecbebfa1f2a..d063c43f3d44a 100644
--- a/mlir/lib/Dialect/Linalg/IR/CMakeLists.txt
+++ b/mlir/lib/Dialect/Linalg/IR/CMakeLists.txt
@@ -14,6 +14,7 @@ add_mlir_dialect_library(MLIRLinalg
   LINK_LIBS PUBLIC
   MLIRAffine
   MLIRArithmetic
+  MLIRBufferizableOpInterface
   MLIRDialectUtils
   MLIRInferTypeOpInterface
   MLIRIR

diff  --git a/mlir/lib/Dialect/Linalg/IR/LinalgTypes.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgTypes.cpp
index 0cc170ebeb823..685ec000e4f39 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgTypes.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgTypes.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Dialect/Linalg/IR/LinalgTypes.h"
+#include "mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.h"
 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
 #include "mlir/IR/BuiltinTypes.h"
 #include "mlir/IR/Dialect.h"
@@ -64,11 +65,13 @@ constexpr const ::llvm::StringLiteral
 
 /// Attribute name used to mark the bufferization layout for region
 /// arguments during linalg comprehensive bufferization.
-constexpr const ::llvm::StringLiteral LinalgDialect::kBufferLayoutAttrName;
+constexpr const ::llvm::StringLiteral
+    comprehensive_bufferize::BufferizableOpInterface::kBufferLayoutAttrName;
 
 /// Attribute name used to mark region arguments that can be bufferized
 /// in-place during linalg comprehensive bufferization.
-constexpr const ::llvm::StringLiteral LinalgDialect::kInplaceableAttrName;
+constexpr const ::llvm::StringLiteral
+    comprehensive_bufferize::BufferizableOpInterface::kInplaceableAttrName;
 
 /// Trait to check if T provides a `regionBuilder` method.
 template <typename T, typename... Args>
@@ -147,20 +150,24 @@ void mlir::linalg::LinalgDialect::printType(Type type,
 
 LogicalResult LinalgDialect::verifyOperationAttribute(Operation *op,
                                                       NamedAttribute attr) {
-  if (attr.first == LinalgDialect::kInplaceableAttrName) {
+  using comprehensive_bufferize::BufferizableOpInterface;
+
+  if (attr.first == BufferizableOpInterface::kInplaceableAttrName) {
     if (!attr.second.isa<BoolAttr>()) {
-      return op->emitError() << "'" << LinalgDialect::kInplaceableAttrName
-                             << "' is expected to be a boolean attribute";
+      return op->emitError()
+             << "'" << BufferizableOpInterface::kInplaceableAttrName
+             << "' is expected to be a boolean attribute";
     }
     if (!op->hasTrait<OpTrait::FunctionLike>())
       return op->emitError() << "expected " << attr.first
                              << " to be used on function-like operations";
     return success();
   }
-  if (attr.first == LinalgDialect::kBufferLayoutAttrName) {
+  if (attr.first == BufferizableOpInterface::kBufferLayoutAttrName) {
     if (!attr.second.isa<AffineMapAttr>()) {
-      return op->emitError() << "'" << LinalgDialect::kBufferLayoutAttrName
-                             << "' is expected to be a affine map attribute";
+      return op->emitError()
+             << "'" << BufferizableOpInterface::kBufferLayoutAttrName
+             << "' is expected to be a affine map attribute";
     }
     if (!op->hasTrait<OpTrait::FunctionLike>())
       return op->emitError() << "expected " << attr.first

diff  --git a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
index b9fbfd727adfb..f14ab5af50a6a 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
@@ -61,6 +61,12 @@ void LinalgComprehensiveModuleBufferize::runOnOperation() {
     options.allocationFns->deallocationFn = [](OpBuilder &b, Location loc,
                                                Value v) {};
   }
+  // TODO: Change to memref::CopyOp (default memCpyFn).
+  options.allocationFns->memCpyFn = [](OpBuilder &b, Location loc, Value from,
+                                       Value to) {
+    b.create<linalg::CopyOp>(loc, from, to);
+  };
+
   options.allowReturnMemref = allowReturnMemref;
   options.analysisFuzzerSeed = analysisFuzzerSeed;
   options.testAnalysisOnly = testAnalysisOnly;

diff  --git a/mlir/lib/Dialect/SparseTensor/Transforms/CMakeLists.txt b/mlir/lib/Dialect/SparseTensor/Transforms/CMakeLists.txt
index 0d27a884642aa..6b72cb1b3fceb 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/CMakeLists.txt
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/CMakeLists.txt
@@ -11,6 +11,7 @@ add_mlir_dialect_library(MLIRSparseTensorTransforms
 
   LINK_LIBS PUBLIC
   MLIRArithmetic
+  MLIRBufferizableOpInterface
   MLIRIR
   MLIRLLVMIR
   MLIRLinalg

diff  --git a/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp
index f8db7eb00319a..8dda6c991d5cc 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp
@@ -12,6 +12,7 @@
 
 #include "mlir/Dialect/Affine/IR/AffineOps.h"
 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
+#include "mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.h"
 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
 #include "mlir/Dialect/Linalg/Utils/Utils.h"
 #include "mlir/Dialect/MemRef/IR/MemRef.h"
@@ -266,7 +267,9 @@ static bool isInPlace(Value val) {
   if (auto arg = val.dyn_cast<BlockArgument>())
     if (auto funcOp = dyn_cast<FuncOp>(arg.getOwner()->getParentOp()))
       if (auto attr = funcOp.getArgAttrOfType<BoolAttr>(
-              arg.getArgNumber(), linalg::LinalgDialect::kInplaceableAttrName))
+              arg.getArgNumber(),
+              linalg::comprehensive_bufferize::BufferizableOpInterface::
+                  kInplaceableAttrName))
         return attr.getValue();
   return false;
 }

diff  --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 1c8a45c7e9b74..28ae1131718ee 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -1716,6 +1716,7 @@ cc_library(
     deps = [
         ":Affine",
         ":ArithmeticDialect",
+        ":BufferizableOpInterface",
         ":IR",
         ":LLVMDialect",
         ":LinalgOps",
@@ -6303,6 +6304,7 @@ cc_library(
     deps = [
         ":Affine",
         ":ArithmeticDialect",
+        ":BufferizableOpInterface",
         ":CopyOpInterface",
         ":DialectUtils",
         ":IR",


        


More information about the Mlir-commits mailing list