diff --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp --- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp +++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp @@ -1802,6 +1802,12 @@ ss << size << "x"; if (failed(appendMangledType(ss, memref.getElementType()))) return failure(); + if (auto as = memref.getMemorySpace()) { + if (auto attr = as.dyn_cast()) + ss << "as" << attr.getInt(); + else + return failure(); + } return success(); } if (auto vec = t.dyn_cast()) { @@ -1821,10 +1827,18 @@ std::string mlir::linalg::generateLibraryCallName(Operation *op) { assert(isa(op)); std::string name(op->getName().getStringRef().str()); + std::string fun = ""; + for (NamedAttribute kv : op->getAttrs()) { + if (UnaryFnAttr ufa = kv.getValue().dyn_cast()) { + fun = stringifyEnum(ufa.getValue()).str() + "_"; + } else if (BinaryFnAttr bfa = kv.getValue().dyn_cast()) { + 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 --- 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 @@ return %C : memref } +// ----- + +#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} + ins(%A: memref<16x8xf32>) outs(%B: memref<16x8xf32>) + linalg.elemwise_unary {fun = #linalg.unary_fn} + 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} + ins(%A: memref<16x8xf32>) outs(%B: memref<16x8xf32>) + linalg.elemwise_unary {fun = #linalg.unary_fn} + 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} + ins(%A, %B: memref<16x8xf32>, memref<16x8xf32>) outs(%C: memref<16x8xf32>) + linalg.elemwise_binary {fun = #linalg.binary_fn} + ins(%D, %E: memref<16xf32>, memref<16xf32>) outs(%F: memref<16xf32>) + return +}