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

Conversation

el-ev
Copy link
Member

@el-ev el-ev commented Mar 28, 2025

@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Iris (el-ev)

Changes

https://llvm.org/docs/LangRef.html#dependent-libs-named-metadata


Full diff: https://github.com/llvm/llvm-project/pull/133385.diff

7 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td (+15)
  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td (+5)
  • (modified) mlir/include/mlir/Target/LLVMIR/ModuleImport.h (+4)
  • (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+20)
  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+12)
  • (added) mlir/test/Target/LLVMIR/Import/metadata-dependent-libraries.ll (+6)
  • (modified) mlir/test/Target/LLVMIR/llvmir.mlir (+8)
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

@el-ev
Copy link
Member Author

el-ev commented Mar 28, 2025

@bcardosolopes Please take a look at this.

@joker-eph
Copy link
Collaborator

Seems like you're missing tests right now?

@el-ev
Copy link
Member Author

el-ev commented Mar 28, 2025

Seems like you're missing tests right now?

There are two tests. If you feel that's not enough, more can be added.

@joker-eph
Copy link
Collaborator

That's fine, I somehow was seeing the diff of the latest commit alone, so it didn't show the tests, my bad!

Copy link
Contributor

@gysit gysit left a 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.

Copy link

github-actions bot commented Mar 29, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@el-ev
Copy link
Member Author

el-ev commented Apr 3, 2025

ping

@gysit gysit merged commit 92923e5 into llvm:main Apr 4, 2025
11 checks passed
@gysit
Copy link
Contributor

gysit commented Apr 4, 2025

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-ci
Copy link
Collaborator

llvm-ci commented Apr 4, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve-vla running on linaro-g3-04 while building mlir at step 7 "ninja check 1".

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
Step 7 (ninja check 1) failure: stage 1 checked (failure)
...
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/ld64.lld
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/wasm-ld
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/discovery.py:276: warning: input '/home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/runtimes/runtimes-bins/compiler-rt/test/interception/Unit' contained no tests
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/discovery.py:276: warning: input '/home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/runtimes/runtimes-bins/compiler-rt/test/sanitizer_common/Unit' contained no tests
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/ld.lld
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/lld-link
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/ld64.lld
llvm-lit: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/llvm/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/tcwg-buildbot/worker/clang-aarch64-sve-vla/stage1/bin/wasm-ld
-- Testing: 97643 tests, 64 workers --
UNRESOLVED: Flang :: Driver/slp-vectorize.ll (1 of 97643)
******************** TEST 'Flang :: Driver/slp-vectorize.ll' FAILED ********************
Test has no 'RUN:' line
********************
PASS: lld :: COFF/arm64ec-import-range-ext.test (2 of 97643)
PASS: Clang :: OpenMP/target_simd_codegen_registration.cpp (3 of 97643)
PASS: UBSan-MemorySanitizer-aarch64 :: TestCases/ImplicitConversion/signed-integer-truncation-ignorelist.c (4 of 97643)
PASS: LLVM :: CodeGen/AMDGPU/llvm.amdgcn.permlane.ll (5 of 97643)
PASS: UBSan-AddressSanitizer-aarch64 :: TestCases/ImplicitConversion/unsigned-integer-truncation-ignorelist.c (6 of 97643)
PASS: UBSan-AddressSanitizer-aarch64 :: TestCases/ImplicitConversion/signed-integer-truncation-ignorelist.c (7 of 97643)
PASS: LLVM :: tools/llvm-reduce/reduce-distinct-metadata.ll (8 of 97643)
PASS: AddressSanitizer-Unit :: ./Asan-aarch64-inline-Noinst-Test/12/16 (9 of 97643)
PASS: Clang :: Driver/clang_f_opts.c (10 of 97643)
PASS: ScudoStandalone-Unit-GwpAsanTorture :: ./ScudoUnitTest-aarch64-Test/53/71 (11 of 97643)
PASS: UBSan-ThreadSanitizer-aarch64 :: TestCases/ImplicitConversion/unsigned-integer-truncation-ignorelist.c (12 of 97643)
PASS: Clang :: OpenMP/target_teams_distribute_simd_codegen_registration.cpp (13 of 97643)
PASS: UBSan-ThreadSanitizer-aarch64 :: TestCases/ImplicitConversion/signed-integer-truncation-ignorelist.c (14 of 97643)
PASS: MemorySanitizer-AARCH64 :: release_origin.c (15 of 97643)
PASS: Clang :: OpenMP/target_parallel_for_simd_codegen_registration.cpp (16 of 97643)
PASS: libFuzzer-aarch64-default-Linux :: merge-sigusr.test (17 of 97643)
PASS: ScudoStandalone-Unit :: ./ScudoUnitTest-aarch64-Test/53/71 (18 of 97643)
PASS: Clang :: OpenMP/target_parallel_for_codegen_registration.cpp (19 of 97643)
PASS: Clang :: CodeGen/X86/rot-intrinsics.c (20 of 97643)
PASS: Clang :: OpenMP/target_teams_distribute_codegen_registration.cpp (21 of 97643)
PASS: libFuzzer-aarch64-default-Linux :: large.test (22 of 97643)
PASS: Clang :: Analysis/runtime-regression.c (23 of 97643)
PASS: Clang :: CodeGen/X86/sse2-builtins.c (24 of 97643)
PASS: Clang :: Driver/linux-ld.c (25 of 97643)
PASS: Clang :: CodeGen/X86/avx-builtins.c (26 of 97643)
PASS: lld :: MachO/lc-linker-option.ll (27 of 97643)
PASS: SanitizerCommon-hwasan-aarch64-Linux :: Linux/signal_segv_handler.cpp (28 of 97643)
PASS: libFuzzer-aarch64-default-Linux :: msan.test (29 of 97643)
PASS: Clang :: OpenMP/target_teams_distribute_parallel_for_simd_codegen_registration.cpp (30 of 97643)
PASS: SanitizerCommon-lsan-aarch64-Linux :: Linux/signal_segv_handler.cpp (31 of 97643)
PASS: ThreadSanitizer-aarch64 :: deadlock_detector_stress_test.cpp (32 of 97643)
PASS: SanitizerCommon-asan-aarch64-Linux :: Linux/signal_segv_handler.cpp (33 of 97643)
PASS: Clang :: Preprocessor/predefined-arch-macros.c (34 of 97643)
PASS: LLVM :: CodeGen/AMDGPU/sched-group-barrier-pipeline-solver.mir (35 of 97643)
PASS: SanitizerCommon-tsan-aarch64-Linux :: Linux/signal_segv_handler.cpp (36 of 97643)
PASS: SanitizerCommon-msan-aarch64-Linux :: Linux/signal_segv_handler.cpp (37 of 97643)

@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 4, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve2-vla-2stage running on linaro-g4-02 while building mlir at step 12 "ninja check 2".

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
Step 12 (ninja check 2) failure: stage 2 checked (failure)
...
PASS: Flang :: Driver/macro-def-undef.F90 (24776 of 96598)
PASS: Flang :: Driver/msvc-dependent-lib-flags.f90 (24777 of 96598)
PASS: Flang :: Driver/print-effective-triple.f90 (24778 of 96598)
PASS: Flang :: Driver/fixed-line-length.f90 (24779 of 96598)
PASS: Flang :: Driver/print-resource-dir.F90 (24780 of 96598)
PASS: Flang :: Driver/phases.f90 (24781 of 96598)
PASS: Flang :: Driver/parse-error.ll (24782 of 96598)
PASS: Flang :: Driver/predefined-macros-compiler-version.F90 (24783 of 96598)
PASS: Flang :: Driver/override-triple.ll (24784 of 96598)
UNRESOLVED: Flang :: Driver/slp-vectorize.ll (24785 of 96598)
******************** TEST 'Flang :: Driver/slp-vectorize.ll' FAILED ********************
Test has no 'RUN:' line
********************
PASS: Flang :: Driver/parse-fir-error.ll (24786 of 96598)
PASS: Flang :: Driver/print-pipeline-passes.f90 (24787 of 96598)
PASS: Flang :: Driver/mlir-pass-pipeline.f90 (24788 of 96598)
PASS: Flang :: Driver/print-target-triple.f90 (24789 of 96598)
PASS: Flang :: Driver/pthread.f90 (24790 of 96598)
PASS: Flang :: Driver/std2018-wrong.f90 (24791 of 96598)
PASS: Flang :: Driver/scanning-error.f95 (24792 of 96598)
PASS: Flang :: Driver/missing-arg.f90 (24793 of 96598)
PASS: Flang :: Driver/mlink-builtin-bc.f90 (24794 of 96598)
PASS: Flang :: Driver/pp-fixed-form.f90 (24795 of 96598)
PASS: Flang :: Driver/lto-bc.f90 (24796 of 96598)
PASS: Flang :: Driver/parse-ir-error.f95 (24797 of 96598)
PASS: Flang :: Driver/supported-suffices/f03-suffix.f03 (24798 of 96598)
PASS: Flang :: Driver/q-unused-arguments.f90 (24799 of 96598)
PASS: Flang :: Driver/supported-suffices/f08-suffix.f08 (24800 of 96598)
PASS: Flang :: Driver/pass-plugin-not-found.f90 (24801 of 96598)
PASS: Flang :: Driver/multiple-input-files.f90 (24802 of 96598)
PASS: Flang :: Driver/tco-code-gen-llvm.fir (24803 of 96598)
PASS: Flang :: Driver/input-from-stdin/input-from-stdin.f90 (24804 of 96598)
PASS: Clangd Unit Tests :: ./ClangdTests/73/81 (24805 of 96598)
PASS: Flang :: Driver/target-gpu-features.f90 (24806 of 96598)
PASS: Flang :: Driver/mllvm.f90 (24807 of 96598)
PASS: Flang :: Driver/fsave-optimization-record.f90 (24808 of 96598)
PASS: Flang :: Driver/target.f90 (24809 of 96598)
PASS: Flang :: Driver/no-duplicate-main.f90 (24810 of 96598)
PASS: Flang :: Driver/lto-flags.f90 (24811 of 96598)
PASS: Flang :: Driver/unsupported-vscale-max-min.f90 (24812 of 96598)
PASS: Flang :: Driver/unparse-with-modules.f90 (24813 of 96598)
PASS: Flang :: Driver/optimization-remark-invalid.f90 (24814 of 96598)
PASS: Flang :: Driver/prescanner-diag.f90 (24815 of 96598)
PASS: Flang :: Driver/target-machine-error.f90 (24816 of 96598)
PASS: Flang :: Driver/save-temps.f90 (24817 of 96598)
PASS: Flang :: Driver/falias-analysis.f90 (24818 of 96598)
PASS: Flang :: Driver/unparse-use-analyzed.f95 (24819 of 96598)
PASS: Flang :: Driver/optimization-remark-backend.f90 (24820 of 96598)
PASS: Flang :: Driver/save-temps-use-module.f90 (24821 of 96598)

Copy link
Contributor

@gysit gysit left a 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...

@el-ev
Copy link
Member Author

el-ev commented Apr 4, 2025

@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?

@el-ev el-ev deleted the mlir_dependent_lib branch April 4, 2025 07:56
@gysit
Copy link
Contributor

gysit commented Apr 4, 2025

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.

@gysit
Copy link
Contributor

gysit commented Apr 4, 2025

@el-ev can you have a look at the build bot errors above?

https://lab.llvm.org/buildbot/#/builders/17/builds/7017
https://lab.llvm.org/buildbot/#/builders/199/builds/2572

Are they caused by this PR? If yes we should probably revert the PR and try to address the issue.

@el-ev
Copy link
Member Author

el-ev commented Apr 4, 2025

@el-ev can you have a look at the build bot errors above?

lab.llvm.org/buildbot#/builders/17/builds/7017 lab.llvm.org/buildbot#/builders/199/builds/2572

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

@gysit
Copy link
Contributor

gysit commented Apr 4, 2025

Perfect, thanks for having a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants