[Mlir-commits] [mlir] 21d43da - [MLIR] Primitive linkage lowering of FuncOp

William S. Moses llvmlistbot at llvm.org
Fri Sep 3 17:41:55 PDT 2021


Author: William S. Moses
Date: 2021-09-03T20:41:39-04:00
New Revision: 21d43daf8f4ae2e701329d149e379d057e83d401

URL: https://github.com/llvm/llvm-project/commit/21d43daf8f4ae2e701329d149e379d057e83d401
DIFF: https://github.com/llvm/llvm-project/commit/21d43daf8f4ae2e701329d149e379d057e83d401.diff

LOG: [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

Added: 
    

Modified: 
    mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
    mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
index ada4e1228764a..5da2551cd43c1 100644
--- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
+++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
@@ -255,11 +255,23 @@ struct FuncOpConversionBase : public ConvertOpToLLVMPattern<FuncOp> {
           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<mlir::LLVM::LinkageAttr>()
+                    .getLinkage();
+    }
     auto newFuncOp = rewriter.create<LLVM::LLVMFuncOp>(
-        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 334b21c3ef5bc..fa60fcd1bd83c 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<extern_weak> }
+
 // CHECK-LABEL: llvm.func @body(i32)
 func private @body(i32)
 


        


More information about the Mlir-commits mailing list