-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: Iris (el-ev) Changeshttps://llvm.org/docs/LangRef.html#dependent-libs-named-metadata Full diff: https://github.com/llvm/llvm-project/pull/133385.diff 7 Files Affected:
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 41c30b81770bc..2beb15d1f074c 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -1327,4 +1327,19 @@ 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.
+ }];
+ let parameters = (ins OptionalArrayRefParameter<"StringAttr">:$libs);
+ let assemblyFormat = "`<` $libs `>`";
+}
+
#endif // LLVMIR_ATTRDEFS
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
index 46fae44f7b0fa..7f8b3aa833a37 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
@@ -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);
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 3b164927d41fd..3dc848c413905 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -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.
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index c0711f7dded71..4d7d537c7001c 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -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
@@ -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()))
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 1e2f2c0468045..9bc3ed1860795 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -2157,6 +2157,18 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
llvmModule->setTargetTriple(
llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue()));
+ if (auto dependentLibrariesAttr = m->getDiscardableAttr(
+ LLVM::LLVMDialect::getDependentLibrariesAttrName())) {
+ auto *NMD =
+ llvmModule->getOrInsertNamedMetadata("llvm.dependent-libraries");
+ for (auto lib : cast<ArrayAttr>(dependentLibrariesAttr)) {
+ auto *MD = llvm::MDNode::get(
+ llvmContext,
+ llvm::MDString::get(llvmContext,
+ mlir::cast<StringAttr>(lib).getValue()));
+ NMD->addOperand(MD);
+ }
+ }
return llvmModule;
}
diff --git a/mlir/test/Target/LLVMIR/Import/metadata-dependent-libraries.ll b/mlir/test/Target/LLVMIR/Import/metadata-dependent-libraries.ll
new file mode 100644
index 0000000000000..4a6d438046a36
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/metadata-dependent-libraries.ll
@@ -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"}
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 0238c95835a0f..21d50531c5083 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -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
|
@bcardosolopes Please take a look at this. |
Seems like you're missing tests right now? |
There are two tests. If you feel that's not enough, more can be added. |
That's fine, I somehow was seeing the diff of the latest commit alone, so it didn't show the tests, my bad! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
LGTM modulo some nit comments.
Co-authored-by: Tobias Gysi <[email protected]>
Co-authored-by: Tobias Gysi <[email protected]>
Co-authored-by: Tobias Gysi <[email protected]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
ping |
I hope ping means it is up to me to land the pr :)? Feel free to directly write that you need someone to land your pr on future revisions. That increases chances that the reviewers understand they are supposed to land the PR. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/17/builds/7017 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/199/builds/2572 Here is the relevant piece of the build log for the reference
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleted this since it was in the wrong thread...
@gysit Thanks for reviewing and merging this! I'm quite new to the LLVM contribution process and wasn't sure of the exact procedure to request a merge. Thank you again for the suggestions. As for the latter comment, I'm not sure this applies to this PR. I noticed that #134335 is related ti block addresses, could you please check about that? |
Yeah sorry I posted this in the wrong thread. |
@el-ev can you have a look at the build bot errors above? https://lab.llvm.org/buildbot/#/builders/17/builds/7017 Are they caused by this PR? If yes we should probably revert the PR and try to address the issue. |
The failed test is about flang, irrelavant here. #133128 |
Perfect, thanks for having a look! |
https://llvm.org/docs/LangRef.html#dependent-libs-named-metadata