Skip to content

Commit 92923e5

Browse files
el-evgysit
andauthored
[mlir][llvm] Add LLVM_DependentLibrariesAttr (#133385)
https://llvm.org/docs/LangRef.html#dependent-libs-named-metadata --------- Co-authored-by: Tobias Gysi <[email protected]>
1 parent 92c93f5 commit 92923e5

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,4 +1327,23 @@ def ModuleFlagAttr
13271327
let assemblyFormat = "`<` $behavior `,` $key `,` $value `>`";
13281328
}
13291329

1330+
//===----------------------------------------------------------------------===//
1331+
// LLVM_DependentLibrariesAttr
1332+
//===----------------------------------------------------------------------===//
1333+
1334+
def LLVM_DependentLibrariesAttr
1335+
: LLVM_Attr<"DependentLibraries", "dependent_libraries"> {
1336+
let summary = "LLVM dependent libraries attribute";
1337+
let description = [{
1338+
Represents the list of dependent libraries for the current module.
1339+
This attribute is used to specify the libraries that the module depends
1340+
on, and it can be used for linking purposes.
1341+
1342+
See the following links for more details:
1343+
https://llvm.org/docs/LangRef.html#dependent-libs-named-metadata
1344+
}];
1345+
let parameters = (ins OptionalArrayRefParameter<"StringAttr">:$libs);
1346+
let assemblyFormat = "`<` $libs `>`";
1347+
}
1348+
13301349
#endif // LLVMIR_ATTRDEFS

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ def LLVM_Dialect : Dialect {
8383
return "llvm.emit_c_interface";
8484
}
8585

86+
/// Name of the dependent libraries attribute.
87+
static StringRef getDependentLibrariesAttrName() {
88+
return "llvm.dependent_libraries";
89+
}
90+
8691
/// Returns `true` if the given type is compatible with the LLVM dialect.
8792
static bool isCompatibleType(Type);
8893

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ class ModuleImport {
229229
/// attribute.
230230
LogicalResult convertCommandlineMetadata();
231231

232+
/// Converts !llvm.dependent-libraries metadata to llvm.dependent_libraries
233+
/// LLVM ModuleOp attribute.
234+
LogicalResult convertDependentLibrariesMetadata();
235+
232236
/// Converts all LLVM metadata nodes that translate to attributes such as
233237
/// alias analysis or access group metadata, and builds a map from the
234238
/// metadata nodes to the converted attributes.

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ class ModuleTranslation {
363363
/// Process the llvm.commandline LLVM Metadata, if it exists.
364364
LogicalResult createCommandlineMetadata();
365365

366+
/// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
367+
LogicalResult createDependentLibrariesMetadata();
368+
366369
/// Translates dialect attributes attached to the given operation.
367370
LogicalResult
368371
convertDialectAttributes(Operation *op,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,23 @@ LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
563563
return success();
564564
}
565565

566+
LogicalResult ModuleImport::convertDependentLibrariesMetadata() {
567+
for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
568+
if (named.getName() != "llvm.dependent-libraries")
569+
continue;
570+
SmallVector<StringRef> libraries;
571+
for (const llvm::MDNode *node : named.operands()) {
572+
if (node->getNumOperands() == 1)
573+
if (auto *mdString = dyn_cast<llvm::MDString>(node->getOperand(0)))
574+
libraries.push_back(mdString->getString());
575+
}
576+
if (!libraries.empty())
577+
mlirModule->setAttr(LLVM::LLVMDialect::getDependentLibrariesAttrName(),
578+
builder.getStrArrayAttr(libraries));
579+
}
580+
return success();
581+
}
582+
566583
LogicalResult ModuleImport::convertIdentMetadata() {
567584
for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
568585
// llvm.ident should have a single operand. That operand is itself an
@@ -625,6 +642,8 @@ LogicalResult ModuleImport::convertMetadata() {
625642
}
626643
if (failed(convertLinkerOptionsMetadata()))
627644
return failure();
645+
if (failed(convertDependentLibrariesMetadata()))
646+
return failure();
628647
if (failed(convertModuleFlagsMetadata()))
629648
return failure();
630649
if (failed(convertIdentMetadata()))

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,22 @@ LogicalResult ModuleTranslation::createCommandlineMetadata() {
20362036
return success();
20372037
}
20382038

2039+
LogicalResult ModuleTranslation::createDependentLibrariesMetadata() {
2040+
if (auto dependentLibrariesAttr = mlirModule->getDiscardableAttr(
2041+
LLVM::LLVMDialect::getDependentLibrariesAttrName())) {
2042+
auto *nmd =
2043+
llvmModule->getOrInsertNamedMetadata("llvm.dependent-libraries");
2044+
llvm::LLVMContext &ctx = llvmModule->getContext();
2045+
for (auto libAttr :
2046+
cast<ArrayAttr>(dependentLibrariesAttr).getAsRange<StringAttr>()) {
2047+
auto *md =
2048+
llvm::MDNode::get(ctx, llvm::MDString::get(ctx, libAttr.getValue()));
2049+
nmd->addOperand(md);
2050+
}
2051+
}
2052+
return success();
2053+
}
2054+
20392055
void ModuleTranslation::setLoopMetadata(Operation *op,
20402056
llvm::Instruction *inst) {
20412057
LoopAnnotationAttr attr =
@@ -2201,6 +2217,8 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
22012217
return nullptr;
22022218
if (failed(translator.createCommandlineMetadata()))
22032219
return nullptr;
2220+
if (failed(translator.createDependentLibrariesMetadata()))
2221+
return nullptr;
22042222

22052223
// Convert other top-level operations if possible.
22062224
for (Operation &o : getModuleBody(module).getOperations()) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: mlir-translate -import-llvm %s | FileCheck %s
2+
3+
; CHECK: llvm.dependent_libraries = ["foo", "bar"]
4+
!llvm.dependent-libraries = !{!0, !1}
5+
!0 = !{!"foo"}
6+
!1 = !{!"bar"}

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,14 @@ module {
28252825

28262826
// -----
28272827

2828+
module attributes {llvm.dependent_libraries = ["foo", "bar"]} {}
2829+
2830+
// CHECK: !llvm.dependent-libraries = !{![[#LIBFOO:]], ![[#LIBBAR:]]}
2831+
// CHECK: ![[#LIBFOO]] = !{!"foo"}
2832+
// CHECK: ![[#LIBBAR]] = !{!"bar"}
2833+
2834+
// -----
2835+
28282836
llvm.mlir.global external constant @const() {addr_space = 0 : i32, dso_local} : i32 {
28292837
%0 = llvm.mlir.addressof @const : !llvm.ptr
28302838
%1 = llvm.ptrtoint %0 : !llvm.ptr to i64

0 commit comments

Comments
 (0)