[MLIR] Primitive linkage lowering of FuncOp
authorWilliam S. Moses <gh@wsmoses.com>
Sun, 22 Aug 2021 20:44:17 +0000 (16:44 -0400)
committerWilliam S. Moses <gh@wsmoses.com>
Sat, 4 Sep 2021 00:41:39 +0000 (20:41 -0400)
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
mlir/test/Conversion/StandardToLLVM/convert-funcs.mlir

index ada4e12..5da2551 100644 (file)
@@ -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<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());
index 334b21c..fa60fcd 100644 (file)
@@ -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)