Skip to content

[mlir][llvm] Add LLVM_DependentLibrariesAttr #133385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1327,4 +1327,22 @@ def ModuleFlagAttr
let assemblyFormat = "`<` $behavior `,` $key `,` $value `>`";
}

//===----------------------------------------------------------------------===//
// LLVM_DependentLibrariesAttr
//===----------------------------------------------------------------------===//
def LLVM_DependentLibrariesAttr
: LLVM_Attr<"DependentLibraries", "dependent_libraries"> {
let summary = "LLVM dependent libraries attribute";
let description = [{
Represents the list of dependent libraries for the current module.
This attribute is used to specify the libraries that the module depends
on, and it can be used for linking purposes.

See the following links for more details:
https://llvm.org/docs/LangRef.html#dependent-libs-named-metadata
}];
let parameters = (ins OptionalArrayRefParameter<"StringAttr">:$libs);
let assemblyFormat = "`<` $libs `>`";
}

#endif // LLVMIR_ATTRDEFS
5 changes: 5 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def LLVM_Dialect : Dialect {
return "llvm.emit_c_interface";
}

/// Name of the dependent libraries attribute.
static StringRef getDependentLibrariesAttrName() {
return "llvm.dependent_libraries";
}

/// Returns `true` if the given type is compatible with the LLVM dialect.
static bool isCompatibleType(Type);

Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/ModuleImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ class ModuleImport {
/// attribute.
LogicalResult convertCommandlineMetadata();

/// Converts !llvm.dependent-libraries metadata to llvm.dependent_libraries
/// LLVM ModuleOp attribute.
LogicalResult convertDependentLibrariesMetadata();

/// Converts all LLVM metadata nodes that translate to attributes such as
/// alias analysis or access group metadata, and builds a map from the
/// metadata nodes to the converted attributes.
Expand Down
3 changes: 3 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ class ModuleTranslation {
/// Process the llvm.commandline LLVM Metadata, if it exists.
LogicalResult createCommandlineMetadata();

/// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
LogicalResult createDependentLibrariesMetadata();

/// Translates dialect attributes attached to the given operation.
LogicalResult
convertDialectAttributes(Operation *op,
Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/Target/LLVMIR/ModuleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,24 @@ LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
return success();
}

LogicalResult ModuleImport::convertDependentLibrariesMetadata() {
for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
if (named.getName() != "llvm.dependent-libraries")
continue;
SmallVector<StringRef> libraries;
for (const llvm::MDNode *node : named.operands()) {
if (node->getNumOperands() == 1)
if (auto *mdString =
llvm::dyn_cast<llvm::MDString>(node->getOperand(0)))
libraries.push_back(mdString->getString());
}
if (!libraries.empty())
mlirModule->setAttr(LLVM::LLVMDialect::getDependentLibrariesAttrName(),
builder.getStrArrayAttr(libraries));
}
return success();
}

LogicalResult ModuleImport::convertIdentMetadata() {
for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
// llvm.ident should have a single operand. That operand is itself an
Expand Down Expand Up @@ -625,6 +643,8 @@ LogicalResult ModuleImport::convertMetadata() {
}
if (failed(convertLinkerOptionsMetadata()))
return failure();
if (failed(convertDependentLibrariesMetadata()))
return failure();
if (failed(convertModuleFlagsMetadata()))
return failure();
if (failed(convertIdentMetadata()))
Expand Down
18 changes: 18 additions & 0 deletions mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,22 @@ LogicalResult ModuleTranslation::createCommandlineMetadata() {
return success();
}

LogicalResult ModuleTranslation::createDependentLibrariesMetadata() {
if (auto dependentLibrariesAttr = mlirModule->getDiscardableAttr(
LLVM::LLVMDialect::getDependentLibrariesAttrName())) {
auto *nmd =
llvmModule->getOrInsertNamedMetadata("llvm.dependent-libraries");
llvm::LLVMContext &ctx = llvmModule->getContext();
for (auto lib : cast<ArrayAttr>(dependentLibrariesAttr)) {
auto *md = llvm::MDNode::get(
ctx,
llvm::MDString::get(ctx, mlir::cast<StringAttr>(lib).getValue()));
nmd->addOperand(md);
}
}
return success();
}

void ModuleTranslation::setLoopMetadata(Operation *op,
llvm::Instruction *inst) {
LoopAnnotationAttr attr =
Expand Down Expand Up @@ -2201,6 +2217,8 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
return nullptr;
if (failed(translator.createCommandlineMetadata()))
return nullptr;
if (failed(translator.createDependentLibrariesMetadata()))
return nullptr;

// Convert other top-level operations if possible.
for (Operation &o : getModuleBody(module).getOperations()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; RUN: mlir-translate -import-llvm %s | FileCheck %s

; CHECK: llvm.dependent_libraries = ["foo", "bar"]
!llvm.dependent-libraries = !{!0, !1}
!0 = !{!"foo"}
!1 = !{!"bar"}
8 changes: 8 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,14 @@ module {

// -----

module attributes {llvm.dependent_libraries = ["foo", "bar"]} {}

// CHECK: !llvm.dependent-libraries = !{![[#LIBFOO:]], ![[#LIBBAR:]]}
// CHECK: ![[#LIBFOO]] = !{!"foo"}
// CHECK: ![[#LIBBAR]] = !{!"bar"}

// -----

llvm.mlir.global external constant @const() {addr_space = 0 : i32, dso_local} : i32 {
%0 = llvm.mlir.addressof @const : !llvm.ptr
%1 = llvm.ptrtoint %0 : !llvm.ptr to i64
Expand Down