llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR][LLVM] Preserve global metadata when importing LLVM IR globals

Open Men-cotton opened this issue 3 months ago • 13 comments

Summary

mlir-translate -import-llvm silently drops the !associated and !absolute_symbol metadata attached to LLVM IR globals. Neither attachment is represented on the imported llvm.mlir.global operations, so an LLVM IR to LLVM dialect to LLVM IR round trip loses them.

Version

commit 2eca7a29cf795683ce6aaaf74fa6549273438c57

Reproducer

Save the following as input.ll:

@a = global i32 0, !associated !0
@b = global i32 0
!0 = !{ptr @b}

; // -----

@a = global i32 0, !absolute_symbol !0
!0 = !{i64 0, i64 42}

Command

mlir-translate -import-llvm -split-input-file input.ll

Actual behavior

The command succeeds, but the imported globals have no representation of either metadata attachment. The relevant excerpts from the two output modules are:

// First split.
llvm.mlir.global external @a(0 : i32) {addr_space = 0 : i32} : i32
llvm.mlir.global external @b(0 : i32) {addr_space = 0 : i32} : i32

// Second split.
llvm.mlir.global external @a(0 : i32) {addr_space = 0 : i32} : i32

Translating the imported modules back to LLVM IR therefore produces globals without !associated or !absolute_symbol.

Expected behavior

The import should preserve the !associated and !absolute_symbol metadata attached to the globals so that translating back to LLVM IR can emit equivalent global metadata.

Additional context

This is distinct from #203017, which preserves references to LLVM globals used as metadata operands. The issue here is that metadata attachments on the globals themselves are not imported.

Men-cotton avatar Aug 11 '26 13:08 Men-cotton

@llvm/issue-subscribers-mlir

Author: Akimasa Watanuki (Men-cotton)

## Summary

mlir-translate -import-llvm silently drops the !associated and !absolute_symbol metadata attached to LLVM IR globals. Neither attachment is represented on the imported llvm.mlir.global operations, so an LLVM IR to LLVM dialect to LLVM IR round trip loses them.

Version

commit 2eca7a29cf795683ce6aaaf74fa6549273438c57

Reproducer

Save the following as input.ll:

@<!-- -->a = global i32 0, !associated !0
@<!-- -->b = global i32 0
!0 = !{ptr @<!-- -->b}

; // -----

@<!-- -->a = global i32 0, !absolute_symbol !0
!0 = !{i64 0, i64 42}

Command

mlir-translate -import-llvm -split-input-file input.ll

Actual behavior

The command succeeds, but the imported globals have no representation of either metadata attachment. The relevant excerpts from the two output modules are:

// First split.
llvm.mlir.global external @<!-- -->a(0 : i32) {addr_space = 0 : i32} : i32
llvm.mlir.global external @<!-- -->b(0 : i32) {addr_space = 0 : i32} : i32

// Second split.
llvm.mlir.global external @<!-- -->a(0 : i32) {addr_space = 0 : i32} : i32

Translating the imported modules back to LLVM IR therefore produces globals without !associated or !absolute_symbol.

Expected behavior

The import should preserve the !associated and !absolute_symbol metadata attached to the globals so that translating back to LLVM IR can emit equivalent global metadata.

Additional context

This is distinct from #203017, which preserves references to LLVM globals used as metadata operands. The issue here is that metadata attachments on the globals themselves are not imported.

@llvm/issue-subscribers-mlir-llvm

Author: Akimasa Watanuki (Men-cotton)

## Summary

mlir-translate -import-llvm silently drops the !associated and !absolute_symbol metadata attached to LLVM IR globals. Neither attachment is represented on the imported llvm.mlir.global operations, so an LLVM IR to LLVM dialect to LLVM IR round trip loses them.

Version

commit 2eca7a29cf795683ce6aaaf74fa6549273438c57

Reproducer

Save the following as input.ll:

@<!-- -->a = global i32 0, !associated !0
@<!-- -->b = global i32 0
!0 = !{ptr @<!-- -->b}

; // -----

@<!-- -->a = global i32 0, !absolute_symbol !0
!0 = !{i64 0, i64 42}

Command

mlir-translate -import-llvm -split-input-file input.ll

Actual behavior

The command succeeds, but the imported globals have no representation of either metadata attachment. The relevant excerpts from the two output modules are:

// First split.
llvm.mlir.global external @<!-- -->a(0 : i32) {addr_space = 0 : i32} : i32
llvm.mlir.global external @<!-- -->b(0 : i32) {addr_space = 0 : i32} : i32

// Second split.
llvm.mlir.global external @<!-- -->a(0 : i32) {addr_space = 0 : i32} : i32

Translating the imported modules back to LLVM IR therefore produces globals without !associated or !absolute_symbol.

Expected behavior

The import should preserve the !associated and !absolute_symbol metadata attached to the globals so that translating back to LLVM IR can emit equivalent global metadata.

Additional context

This is distinct from #203017, which preserves references to LLVM globals used as metadata operands. The issue here is that metadata attachments on the globals themselves are not imported.

Hey @Men-cotton @gysit! I reproduced this on current main.

These global metadata attachments are pretty niche, so I can see how they slipped through, and also why they matter if you’re hitting them. Adjacent to #203017, but for metadata on the global rather than globals used as metadata ops. It'll probably the same style of fix as other first-class llvm.mlir.global fields (might even reuse some if it fits obv).

@nicolasvasilache @MaheshRavishankar @Moxinilian Happy to take this on, would love to get it assigned to me @ktanishqk

ktanishqk avatar Aug 12 '26 05:08 ktanishqk

Side note: longer term, it might be worth thinking more deliberately about how we handle less-common global metadata on the LLVM IR ↔ LLVM dialect boundary. First-class attrs for known kinds vs some way to avoid silent drops for the rest.

ktanishqk avatar Aug 12 '26 05:08 ktanishqk

@ktanishqk having some explicit fields for these two kinds sounds good to me. Maybe synchronize with @Men-cotton before starting so that there is no redundant work.

Side note: longer term, it might be worth thinking more deliberately about how we handle less-common global metadata on the LLVM IR ↔ LLVM dialect boundary. First-class attrs for known kinds vs some way to avoid silent drops for the rest.

Yes, we partially have that if you look at @Men-cotton 's stacked PRs. It has the disadvantage that we have multiple ways of representing the same metadata. The alternative of only having some generic low-level representation seems even less attractive though.

gysit avatar Aug 12 '26 06:08 gysit

@men-cotton, would love to take this up if you're not playing with it already.

@gysit yeah those PRs make sense. I think the multiple representations tradeoff is defensible, but longer-term I'm wondering if we should explore a more systematic approach: a generic metadata fallback mechanism (kinda like an optional dictionary attribute on llvm.mlir.global) that might capture unhandled metadata kinds. This way:

  • Nothing silently drops (forward compat.)
  • First-class attrs still exist for the important/common cases (better val and roundtrip semantics)
  • We avoid gradually adding attrs for every LLVM metadata kind

Not proposing we do it now. The immediate fix (first-class attrs for !associated / !absolute_symbol) is the right move. But it might be worth thinking about as @Men-cotton's metadata work stabilizes. Would give us a clearer architecture for the IR ↔ dialect boundary going forward.

ktanishqk avatar Aug 12 '26 23:08 ktanishqk

@ktanishqk yes I think such a generic fallback mechanism can make sense given how much annotations llvm has.

gysit avatar Aug 13 '26 06:08 gysit

Hi Akimasa,

I’m following up on llvm/llvm-project#215549 (!associated / !absolute_symbol on globals during LLVM IR import).

Tobias suggested syncing with you first so we don’t overlap. Are you already working on this, or okay if I take it?

Warm Regards Tanishq Khurana @ktanishqk

Hi @ktanishqk,

I'm not working on this issue, so please feel free to take it!There might be some merge conflicts down the line. Let us know if you run into any issues.

Men-cotton avatar Aug 14 '26 06:08 Men-cotton

Thanks so much! I will keep an eye out for those. Do you know who'd be the right people to tag apart from @nicolasvasilache @MaheshRavishankar @Moxinilian @gysit to get this assigned?

ktanishqk avatar Aug 14 '26 17:08 ktanishqk

I think @ftynse may know this better based on the description. This seems to be related to more about preservation from LLVM dialect -> LLVM.

MaheshRavishankar avatar Aug 14 '26 17:08 MaheshRavishankar

I'm not sure what you mean by "getting assigned", but to clarify: if you claim you're going to work on it here, go for it! You can open a PR and tag relevant contributors for review.

Moxinilian avatar Aug 15 '26 14:08 Moxinilian

Hey all, this is done!

First-class associated and absolute_symbol attrs on llvm.mlir.global, with import/export and verifier coverage.

PR is here: https://github.com/llvm/llvm-project/pull/218839

@gysit @ftynse @Men-cotton would love a look when you have a chance. @Men-cotton, I tried to stay out of the way of your metadata patches. LMK if this collides with anything still in flight.

ktanishqk avatar Aug 26 '26 05:08 ktanishqk

@Men-cotton would love to know how you feel about this since you're the OP on this issue: https://github.com/llvm/llvm-project/pull/218839#issuecomment-5433352500

fyi the mlir verifier only does the structural checks (self-associated by name, even-length integer range pairs). we don’t match llvm’s full !associated / !absolute_symbol rules: missing symbols, alias-to-self after stripping, empty/overlapping ranges, intptr width, etc. those only fail later on export via llvm::verifyModule. should we tighten the mlir verifier to catch that, or leave it for a follow-up?

ktanishqk avatar Aug 27 '26 01:08 ktanishqk