[Mlir-commits] [mlir] 921c6f0 - [mlir][FuncToLLVM] Drop llvm.linkage attribute
Tobias Gysi
llvmlistbot at llvm.org
Thu Jan 19 01:29:36 PST 2023
Author: Christian Ulmann
Date: 2023-01-19T10:28:29+01:00
New Revision: 921c6f070714233ab707808354520b3f6e0ff4d6
URL: https://github.com/llvm/llvm-project/commit/921c6f070714233ab707808354520b3f6e0ff4d6
DIFF: https://github.com/llvm/llvm-project/commit/921c6f070714233ab707808354520b3f6e0ff4d6.diff
LOG: [mlir][FuncToLLVM] Drop llvm.linkage attribute
This commit ensures that all functions produced by `FuncToLLVM` drop the
llvm.linkage attribute. Furthermore, it adds a small test that checks if
the readnone attribute is preserved.
Reviewed By: gysit
Differential Revision: https://reviews.llvm.org/D142002
Added:
Modified:
mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir
mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-callers.mlir
mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-functions.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
index c905fd567768e..552bb1e8841ea 100644
--- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
+++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
@@ -56,6 +56,9 @@ using namespace mlir;
#define PASS_NAME "convert-func-to-llvm"
+static constexpr StringRef varargsAttrName = "func.varargs";
+static constexpr StringRef linkageAttrName = "llvm.linkage";
+
/// Only retain those attributes that are not constructed by
/// `LLVMFuncOp::build`. If `filterArgAttrs` is set, also filter out argument
/// attributes.
@@ -64,7 +67,8 @@ static void filterFuncAttributes(func::FuncOp func, bool filterArgAndResAttrs,
for (const NamedAttribute &attr : func->getAttrs()) {
if (attr.getName() == SymbolTable::getSymbolAttrName() ||
attr.getName() == func.getFunctionTypeAttrName() ||
- attr.getName() == "func.varargs" ||
+ attr.getName() == linkageAttrName ||
+ attr.getName() == varargsAttrName ||
(filterArgAndResAttrs &&
(attr.getName() == func.getArgAttrsAttrName() ||
attr.getName() == func.getResAttrsAttrName())))
@@ -290,7 +294,7 @@ struct FuncOpConversionBase : public ConvertOpToLLVMPattern<func::FuncOp> {
ConversionPatternRewriter &rewriter) const {
// Convert the original function arguments. They are converted using the
// LLVMTypeConverter provided to this legalization pattern.
- auto varargsAttr = funcOp->getAttrOfType<BoolAttr>("func.varargs");
+ auto varargsAttr = funcOp->getAttrOfType<BoolAttr>(varargsAttrName);
TypeConverter::SignatureConversion result(funcOp.getNumArguments());
auto llvmType = getTypeConverter()->convertFunctionSignature(
funcOp.getFunctionType(), varargsAttr && varargsAttr.getValue(),
@@ -356,22 +360,16 @@ struct FuncOpConversionBase : public ConvertOpToLLVMPattern<func::FuncOp> {
attributes.push_back(rewriter.getNamedAttr(
funcOp.getArgAttrsAttrName(), rewriter.getArrayAttr(newArgAttrs)));
}
- for (const auto &pair : llvm::enumerate(attributes)) {
- if (pair.value().getName() == "llvm.linkage") {
- attributes.erase(attributes.begin() + pair.index());
- break;
- }
- }
// Create an LLVM function, use external linkage by default until MLIR
// functions have linkage.
LLVM::Linkage linkage = LLVM::Linkage::External;
- if (funcOp->hasAttr("llvm.linkage")) {
+ if (funcOp->hasAttr(linkageAttrName)) {
auto attr =
- funcOp->getAttr("llvm.linkage").dyn_cast<mlir::LLVM::LinkageAttr>();
+ funcOp->getAttr(linkageAttrName).dyn_cast<mlir::LLVM::LinkageAttr>();
if (!attr) {
- funcOp->emitError()
- << "Contains llvm.linkage attribute not of type LLVM::LinkageAttr";
+ funcOp->emitError() << "Contains " << linkageAttrName
+ << " attribute not of type LLVM::LinkageAttr";
return nullptr;
}
linkage = attr.getLinkage();
diff --git a/mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir b/mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir
index 0aa185fe25b91..8148e33daede9 100644
--- a/mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir
+++ b/mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir
@@ -40,6 +40,10 @@ func.func @pass_through(%arg0: () -> ()) -> (() -> ()) {
// CHECK-LABEL: llvm.func extern_weak @llvmlinkage(i32)
func.func private @llvmlinkage(i32) attributes { "llvm.linkage" = #llvm.linkage<extern_weak> }
+// CHECK-LABEL: llvm.func @llvmreadnone(i32)
+// CHECK-SAME: llvm.readnone
+func.func private @llvmreadnone(i32) attributes { llvm.readnone }
+
// CHECK-LABEL: llvm.func @body(i32)
func.func private @body(i32)
diff --git a/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-callers.mlir b/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-callers.mlir
index 956924595dc31..8b9360a099001 100644
--- a/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-callers.mlir
+++ b/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-callers.mlir
@@ -68,3 +68,12 @@ func.func @multiple_arg_attr_multiple_res_attr(%arg0: memref<f32> {test.argZero
%2 = arith.constant 2 : i32
return %0, %1, %2 : f32, memref<i32>, i32
}
+
+// CHECK: llvm.func @drop_linkage_attr() -> (!llvm.struct{{.*}} {test.returnOne})
+// CHECK-LABEL: llvm.func @_mlir_ciface_drop_linkage_attr
+// CHECK-NOT: llvm.linkage
+// CHECK: %{{.*}}: !llvm.ptr{{.*}} {test.returnOne}
+func.func @drop_linkage_attr() -> (memref<f32> {test.returnOne}) attributes { llvm.linkage = #llvm.linkage<external> } {
+ %0 = memref.alloc() : memref<f32>
+ return %0 : memref<f32>
+}
diff --git a/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-functions.mlir b/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-functions.mlir
index 1f6a0f5227a55..9d73072aab9d5 100644
--- a/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-functions.mlir
+++ b/mlir/test/Conversion/FuncToLLVM/emit-c-wrappers-for-external-functions.mlir
@@ -39,3 +39,9 @@ func.func private @one_arg_attr_one_res_attr_with_value_return(%arg0: memref<f32
// CHECK-LABEL: llvm.func @_mlir_ciface_multiple_arg_attr_multiple_res_attr
// CHECK: (!llvm.ptr<{{.*}}> {llvm.struct_attrs = [{}, {test.returnOne = 1 : i64}, {test.returnTwo = 2 : i64}]}, !llvm.ptr<{{.*}}> {test.argZero = 0 : i64}, f32, i32 {test.argTwo = 2 : i64}
func.func private @multiple_arg_attr_multiple_res_attr(%arg0: memref<f32> {test.argZero = 0}, %arg1: f32, %arg2: i32 {test.argTwo = 2}) -> (f32, memref<i32> {test.returnOne = 1}, i32 {test.returnTwo = 2})
+
+// CHECK: llvm.func weak @drop_linkage_attr() -> (!llvm.struct{{.*}} {test.returnOne})
+// CHECK-LABEL: llvm.func @_mlir_ciface_drop_linkage_attr
+// CHECK-NOT: llvm.linkage
+// CHECK: !llvm.ptr{{.*}} {test.returnOne}
+func.func private @drop_linkage_attr() -> (memref<f32> {test.returnOne}) attributes { llvm.linkage = #llvm.linkage<weak> }
More information about the Mlir-commits
mailing list