diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -907,6 +907,7 @@ UnitAttr:$constant, StrAttr:$sym_name, Linkage:$linkage, + UnitAttr:$dso_local, OptionalAttr:$value, OptionalAttr:$alignment, DefaultValuedAttr, "0">:$addr_space, @@ -1017,6 +1018,7 @@ "StringRef":$name, "Attribute":$value, CArg<"uint64_t", "0">:$alignment, CArg<"unsigned", "0">:$addrSpace, + CArg<"bool", "false">:$dsoLocal, CArg<"ArrayRef", "{}">:$attrs)> ]; @@ -1081,6 +1083,7 @@ }]; let arguments = (ins DefaultValuedAttr:$linkage, + UnitAttr:$dso_local, OptionalAttr:$personality, OptionalAttr:$passthrough); @@ -1091,6 +1094,7 @@ let builders = [ OpBuilder<(ins "StringRef":$name, "Type":$type, CArg<"Linkage", "Linkage::External">:$linkage, + CArg<"bool", "false">:$dsoLocal, CArg<"ArrayRef", "{}">:$attrs, CArg<"ArrayRef", "{}">:$argAttrs)> ]; diff --git a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp --- a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp +++ b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp @@ -70,7 +70,7 @@ attributes.emplace_back(kernelAttributeName, rewriter.getUnitAttr()); auto llvmFuncOp = rewriter.create( gpuFuncOp.getLoc(), gpuFuncOp.getName(), funcType, - LLVM::Linkage::External, attributes); + LLVM::Linkage::External, /*dsoLocal*/ false, attributes); { // Insert operations that correspond to converted workgroup and private diff --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp --- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp +++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp @@ -1266,7 +1266,7 @@ typeConverter.convertFunctionTypeCWrapper(type); auto wrapperFuncOp = rewriter.create( loc, llvm::formatv("_mlir_ciface_{0}", funcOp.getName()).str(), - wrapperFuncType, LLVM::Linkage::External, attributes); + wrapperFuncType, LLVM::Linkage::External, /*dsoLocal*/ false, attributes); OpBuilder::InsertionGuard guard(rewriter); rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock()); @@ -1330,7 +1330,7 @@ // Create the auxiliary function. auto wrapperFunc = builder.create( loc, llvm::formatv("_mlir_ciface_{0}", funcOp.getName()).str(), - wrapperType, LLVM::Linkage::External, attributes); + wrapperType, LLVM::Linkage::External, /*dsoLocal*/ false, attributes); builder.setInsertionPointToStart(newFuncOp.addEntryBlock()); @@ -1441,7 +1441,7 @@ // functions have linkage. auto newFuncOp = rewriter.create( funcOp.getLoc(), funcOp.getName(), llvmType, LLVM::Linkage::External, - attributes); + /*dsoLocal*/ false, attributes); rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(), newFuncOp.end()); if (failed(rewriter.convertRegionTypes(&newFuncOp.getBody(), *typeConverter, diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp --- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp @@ -1267,7 +1267,7 @@ void GlobalOp::build(OpBuilder &builder, OperationState &result, Type type, bool isConstant, Linkage linkage, StringRef name, Attribute value, uint64_t alignment, unsigned addrSpace, - ArrayRef attrs) { + bool dsoLocal, ArrayRef attrs) { result.addAttribute(SymbolTable::getSymbolAttrName(), builder.getStringAttr(name)); result.addAttribute("type", TypeAttr::get(type)); @@ -1275,6 +1275,8 @@ result.addAttribute("constant", builder.getUnitAttr()); if (value) result.addAttribute("value", value); + if (dsoLocal) + result.addAttribute("dso_local", builder.getUnitAttr()); // Only add an alignment attribute if the "alignment" input // is different from 0. The value must also be a power of two, but @@ -1756,7 +1758,7 @@ void LLVMFuncOp::build(OpBuilder &builder, OperationState &result, StringRef name, Type type, LLVM::Linkage linkage, - ArrayRef attrs, + bool dsoLocal, ArrayRef attrs, ArrayRef argAttrs) { result.addRegion(); result.addAttribute(SymbolTable::getSymbolAttrName(), @@ -1765,6 +1767,8 @@ result.addAttribute(getLinkageAttrName(), builder.getI64IntegerAttr(static_cast(linkage))); result.attributes.append(attrs.begin(), attrs.end()); + if (dsoLocal) + result.addAttribute("dso_local", builder.getUnitAttr()); if (argAttrs.empty()) return; diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -425,6 +425,14 @@ linkage == llvm::GlobalVariable::ExternalWeakLinkage; } +/// Sets the runtime preemption specifier of `gv` to dso_local if +/// `dsoLocalRequested` is true, otherwise it is left unchanged. +static void addRuntimePreemptionSpecifier(bool dsoLocalRequested, + llvm::GlobalValue *gv) { + if (dsoLocalRequested) + gv->setDSOLocal(true); +} + /// Create named global variables that correspond to llvm.mlir.global /// definitions. LogicalResult ModuleTranslation::convertGlobals() { @@ -458,6 +466,8 @@ if (op.section().hasValue()) var->setSection(*op.section()); + addRuntimePreemptionSpecifier(op.dso_local(), var); + Optional alignment = op.alignment(); if (alignment.hasValue()) var->setAlignment(llvm::MaybeAlign(alignment.getValue())); @@ -687,6 +697,7 @@ llvm::Function *llvmFunc = cast(llvmFuncCst.getCallee()); llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage())); mapFunction(function.getName(), llvmFunc); + addRuntimePreemptionSpecifier(function.dso_local(), llvmFunc); // Forward the pass-through attributes to LLVM. if (failed(forwardPassthroughAttributes(function.getLoc(), diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir --- a/mlir/test/Target/LLVMIR/llvmir.mlir +++ b/mlir/test/Target/LLVMIR/llvmir.mlir @@ -79,6 +79,13 @@ // CHECK: @unnamed_addr = private unnamed_addr constant i64 42 llvm.mlir.global private unnamed_addr constant @unnamed_addr(42 : i64) : i64 +// +// dso_local attribute. +// + +llvm.mlir.global @has_dso_local(42 : i64) {dso_local} : i64 +// CHECK: @has_dso_local = dso_local global i64 42 + // // Section attribute. // @@ -428,6 +435,15 @@ llvm.return } +// +// dso_local attribute. +// + +// CHECK: define dso_local void @dso_local_func +llvm.func @dso_local_func() attributes {dso_local} { + llvm.return +} + // // MemRef type conversion, allocation and communication with functions. //