[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
This commit is contained in:
William S. Moses 2021-08-22 16:44:17 -04:00
parent 775ab780fd
commit 21d43daf8f
2 changed files with 16 additions and 1 deletions

View 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());

View 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)