[Mlir-commits] [mlir] 6ac632a - [MLIR][Linalg] Generate unique LibraryCallName for LinalgOps.

Prabhdeep Singh Soni llvmlistbot at llvm.org
Mon Mar 20 10:50:56 PDT 2023


Author: Amy Wang
Date: 2023-03-20T13:50:22-04:00
New Revision: 6ac632ad83fb0f1f91cfca2d05e074712aebce79

URL: https://github.com/llvm/llvm-project/commit/6ac632ad83fb0f1f91cfca2d05e074712aebce79
DIFF: https://github.com/llvm/llvm-project/commit/6ac632ad83fb0f1f91cfca2d05e074712aebce79.diff

LOG: [MLIR][Linalg] Generate unique LibraryCallName for LinalgOps.

When lowering LinalgToStandard for named UnaryFn/BinaryFn ops, ensure
the fun name appears in the generated library name. Further, for
linalg.copy to/from different address spaces, ensure the to/from
address spaces are appended onto the library name for uniqueness.
This fixes the lowering error with the linalg.copy testcase shown in
this patch.

Reviewed By: ftynse

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

Added: 
    

Modified: 
    mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
    mlir/test/Dialect/Linalg/library-calls.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
index f766ab6a27fe3..ee4d064d55d36 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
@@ -1802,6 +1802,12 @@ static LogicalResult appendMangledType(llvm::raw_string_ostream &ss, Type t) {
         ss << size << "x";
     if (failed(appendMangledType(ss, memref.getElementType())))
       return failure();
+    if (auto as = memref.getMemorySpace()) {
+      if (auto attr = as.dyn_cast<IntegerAttr>())
+        ss << "as" << attr.getInt();
+      else
+        return failure();
+    }
     return success();
   }
   if (auto vec = t.dyn_cast<VectorType>()) {
@@ -1821,10 +1827,18 @@ static LogicalResult appendMangledType(llvm::raw_string_ostream &ss, Type t) {
 std::string mlir::linalg::generateLibraryCallName(Operation *op) {
   assert(isa<LinalgOp>(op));
   std::string name(op->getName().getStringRef().str());
+  std::string fun = "";
+  for (NamedAttribute kv : op->getAttrs()) {
+    if (UnaryFnAttr ufa = kv.getValue().dyn_cast<UnaryFnAttr>()) {
+      fun = stringifyEnum(ufa.getValue()).str() + "_";
+    } else if (BinaryFnAttr bfa = kv.getValue().dyn_cast<BinaryFnAttr>()) {
+      fun = stringifyEnum(bfa.getValue()).str() + "_";
+    }
+  }
   name.reserve(128);
   std::replace(name.begin(), name.end(), '.', '_');
   llvm::raw_string_ostream ss(name);
-  ss << "_";
+  ss << "_" << fun;
   for (Type t : op->getOperandTypes()) {
     if (failed(appendMangledType(ss, t)))
       return std::string();

diff  --git a/mlir/test/Dialect/Linalg/library-calls.mlir b/mlir/test/Dialect/Linalg/library-calls.mlir
index cbf1c5ed8b878..1fa675d8b4b68 100644
--- a/mlir/test/Dialect/Linalg/library-calls.mlir
+++ b/mlir/test/Dialect/Linalg/library-calls.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -convert-linalg-to-std | FileCheck %s
+// RUN: mlir-opt %s -convert-linalg-to-std -split-input-file | FileCheck %s
 
 func.func private @printMemrefF32(memref<*xf32>)
 
@@ -22,3 +22,80 @@ func.func @matmul(%A: memref<?x?xf32>, %B: memref<?x?xf32>) -> (memref<?x?xf32>)
   return %C : memref<?x?xf32>
 }
 
+// -----
+
+#accesses = [
+        affine_map<(d0) -> (d0)>,
+        affine_map<(d0) -> (d0)>,
+        affine_map<(d0) -> (d0)>
+    ]
+#trait = {
+    doc = "...",
+    indexing_maps = #accesses,
+    library_call = "test",
+    iterator_types = ["parallel"]
+}
+
+// CHECK: func.func private @linalg_copy_view32xf16as1_view32xf16as6(memref<32xf16, strided<[?], offset: ?>, 1>, memref<32xf16, strided<[?], offset: ?>, 6>) attributes {llvm.emit_c_interface}
+// CHECK: func.func private @linalg_copy_view32xf16as6_view32xf16as1(memref<32xf16, strided<[?], offset: ?>, 6>, memref<32xf16, strided<[?], offset: ?>, 1>) attributes {llvm.emit_c_interface}
+
+module {
+  func.func @helper(%arg7: memref<32xf16, 1>, %arg8: memref<32xf16, 1>, %arg9: memref<32xf16, 1>) {
+    %localA = memref.alloca() : memref<32xf16, 6>
+    %localB = memref.alloca() : memref<32xf16, 6>
+    %localOut = memref.alloca() : memref<32xf16, 6>
+    linalg.copy ins(%arg8 : memref<32xf16, 1>) outs(%localA : memref<32xf16, 6>)
+    linalg.copy ins(%arg9 : memref<32xf16, 1>) outs(%localB : memref<32xf16, 6>)
+
+    linalg.generic #trait
+    ins(%localA, %localB : memref<32xf16, 6>, memref<32xf16, 6>)
+    outs(%localOut : memref<32xf16, 6>) {
+      ^bb0(%0: f16, %1: f16, %2: f16) :
+        %e = arith.addf %1, %0: f16
+        linalg.yield %e : f16
+    }
+
+    linalg.copy ins(%localOut : memref<32xf16, 6>) outs(%arg7 : memref<32xf16, 1>)
+    return
+  }
+}
+
+
+// -----
+
+// CHECK: func.func private @linalg_elemwise_unary_negf_view16x8xf32_view16x8xf32(memref<16x8xf32, strided<[?, ?], offset: ?>>, memref<16x8xf32, strided<[?, ?], offset: ?>>) attributes {llvm.emit_c_interface}
+// CHECK: func.func private @linalg_elemwise_unary_negf_view16xf32_view16xf32(memref<16xf32, strided<[?], offset: ?>>, memref<16xf32, strided<[?], offset: ?>>) attributes {llvm.emit_c_interface}
+
+func.func @test_neg(%A : memref<16x8xf32>, %B: memref<16x8xf32>, %C: memref<16xf32>, %D: memref<16xf32>) {
+  linalg.elemwise_unary {fun = #linalg.unary_fn<negf>}
+                              ins(%A: memref<16x8xf32>) outs(%B: memref<16x8xf32>)
+  linalg.elemwise_unary {fun = #linalg.unary_fn<negf>}
+                              ins(%C: memref<16xf32>) outs(%D: memref<16xf32>)
+  return
+}
+
+// -----
+
+// CHECK: func.func private @linalg_elemwise_unary_exp_view16x8xf32_view16x8xf32(memref<16x8xf32, strided<[?, ?], offset: ?>>, memref<16x8xf32, strided<[?, ?], offset: ?>>) attributes {llvm.emit_c_interface}
+// CHECK: func.func private @linalg_elemwise_unary_exp_view16xf32_view16xf32(memref<16xf32, strided<[?], offset: ?>>, memref<16xf32, strided<[?], offset: ?>>) attributes {llvm.emit_c_interface}
+
+func.func @test_exp(%A : memref<16x8xf32>, %B: memref<16x8xf32>, %C: memref<16xf32>, %D: memref<16xf32>) {
+  linalg.elemwise_unary {fun = #linalg.unary_fn<exp>}
+                              ins(%A: memref<16x8xf32>) outs(%B: memref<16x8xf32>)
+  linalg.elemwise_unary {fun = #linalg.unary_fn<exp>}
+                              ins(%C: memref<16xf32>) outs(%D: memref<16xf32>)
+  return
+}
+
+// -----
+
+// CHECK: func.func private @linalg_elemwise_binary_add_view16x8xf32_view16x8xf32_view16x8xf32(memref<16x8xf32, strided<[?, ?], offset: ?>>, memref<16x8xf32, strided<[?, ?], offset: ?>>, memref<16x8xf32, strided<[?, ?], offset: ?>>) attributes {llvm.emit_c_interface}
+// CHECK: func.func private @linalg_elemwise_binary_add_view16xf32_view16xf32_view16xf32(memref<16xf32, strided<[?], offset: ?>>, memref<16xf32, strided<[?], offset: ?>>, memref<16xf32, strided<[?], offset: ?>>) attributes {llvm.emit_c_interface}
+
+func.func @test_add(%A : memref<16x8xf32>, %B: memref<16x8xf32>, %C: memref<16x8xf32>, %D: memref<16xf32>, %E: memref<16xf32>, %F: memref<16xf32>) {
+  linalg.elemwise_binary {fun = #linalg.binary_fn<add>}
+                              ins(%A, %B: memref<16x8xf32>, memref<16x8xf32>) outs(%C: memref<16x8xf32>)
+  linalg.elemwise_binary {fun = #linalg.binary_fn<add>}
+                              ins(%D, %E: memref<16xf32>, memref<16xf32>) outs(%F: memref<16xf32>)
+  return
+}


        


More information about the Mlir-commits mailing list