From 21d43daf8f4ae2e701329d149e379d057e83d401 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Sun, 22 Aug 2021 16:44:17 -0400 Subject: [PATCH] [MLIR] Primitive linkage lowering of FuncOp FuncOp always lowers to an LLVM external linkage presently. This makes it impossible to define functions in mlir which are local to the current module. Until MLIR FuncOps have a more formal linkage specification, this commit allows funcop's to have an optionally specified llvm.linkage attribute, whose value will be used as the linkage of the llvm funcop when lowered. Differential Revision: https://reviews.llvm.org/D108524 Support LLVM linkage --- mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp | 14 +++++++++++++- mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp index ada4e12..5da2551 100644 --- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp +++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp @@ -255,11 +255,23 @@ protected: rewriter.getNamedAttr(function_like_impl::getArgDictAttrName(), rewriter.getArrayAttr(newArgAttrs))); } + for (auto pair : llvm::enumerate(attributes)) { + if (pair.value().first == "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")) { + linkage = funcOp->getAttr("llvm.linkage") + .cast() + .getLinkage(); + } auto newFuncOp = rewriter.create( - funcOp.getLoc(), funcOp.getName(), llvmType, LLVM::Linkage::External, + funcOp.getLoc(), funcOp.getName(), llvmType, linkage, /*dsoLocal*/ false, attributes); rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(), newFuncOp.end()); diff --git a/mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir b/mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir index 334b21c..fa60fcd 100644 --- a/mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir +++ b/mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir @@ -37,6 +37,9 @@ func @pass_through(%arg0: () -> ()) -> (() -> ()) { return %bbarg : () -> () } +// CHECK-LABEL: llvm.func extern_weak @llvmlinkage(i32) +func private @llvmlinkage(i32) attributes { "llvm.linkage" = #llvm.linkage } + // CHECK-LABEL: llvm.func @body(i32) func private @body(i32) -- 2.7.4