[Mlir-commits] [mlir] [MLIR][LLVM] Fix the import of LLVM IR metadata (PR #170631)

Tobias Gysi llvmlistbot at llvm.org
Thu Dec 4 01:18:14 PST 2025


https://github.com/gysit created https://github.com/llvm/llvm-project/pull/170631

Change `getSupportedMetadata` to return `SmallVector<unsigned>` instead of `ArrayRef<unsigned>` and make the list non-static. This ensures metadata identifiers are correctly obtained per LLVM context, preventing incorrect import when multiple contexts are used (for metadata like vector hints or work group sizes which have non-static IDs).

>From 245c0de5e29dab823a971e5ad208d4a2df81a5ff Mon Sep 17 00:00:00 2001
From: Tobias Gysi <tobias.gysi at nextsilicon.com>
Date: Thu, 4 Dec 2025 09:20:43 +0100
Subject: [PATCH] [MLIR][LLVM] Fix the import of LLVM IR metadata

Change `getSupportedMetadata` to return `SmallVector<unsigned>` instead
of `ArrayRef<unsigned>` and make the list non-static. This ensures
metadata identifiers are correctly obtained per LLVM context, preventing
incorrect import when multiple contexts are used (for metadata like
vector hints or work group sizes which have non-static IDs).
---
 .../mlir/Target/LLVMIR/LLVMImportInterface.h  | 12 ++++++++----
 .../LLVMIR/LLVMIRToLLVMTranslation.cpp        | 19 ++++++++++---------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h b/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h
index 6a42627e17e60..0e50fac7e85dd 100644
--- a/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h
+++ b/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h
@@ -84,10 +84,14 @@ class LLVMImportDialectInterface
 
   /// Hook for derived dialect interfaces to publish the supported metadata
   /// kinds. As every metadata kind has a unique integer identifier, the
-  /// function returns the list of supported metadata identifiers. `ctx` can be
-  /// used to obtain IDs of metadata kinds that do not have a fixed static one.
-  virtual ArrayRef<unsigned>
-  getSupportedMetadata(llvm::LLVMContext &ctx) const {
+  /// function returns the list of supported metadata identifiers. The
+  /// `llvmContext` parameter is used to obtain identifiers for metadata kinds
+  /// that do not have a fixed static identifier. Since different LLVM contexts
+  /// can assign different identifiers to these non-static metadata kinds, the
+  /// function must recompute the list of supported metadata identifiers on each
+  /// call.
+  virtual SmallVector<unsigned>
+  getSupportedMetadata(llvm::LLVMContext &llvmContext) const {
     return {};
   }
 };
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index 81c9da1d98c40..2d4a18cc4b145 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -80,8 +80,9 @@ static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
 
 /// Returns the list of LLVM IR metadata kinds that are convertible to MLIR LLVM
 /// dialect attributes.
-static ArrayRef<unsigned> getSupportedMetadataImpl(llvm::LLVMContext &context) {
-  static const SmallVector<unsigned> convertibleMetadata = {
+static SmallVector<unsigned>
+getSupportedMetadataImpl(llvm::LLVMContext &llvmContext) {
+  SmallVector<unsigned> convertibleMetadata = {
       llvm::LLVMContext::MD_prof,
       llvm::LLVMContext::MD_tbaa,
       llvm::LLVMContext::MD_access_group,
@@ -91,10 +92,10 @@ static ArrayRef<unsigned> getSupportedMetadataImpl(llvm::LLVMContext &context) {
       llvm::LLVMContext::MD_dereferenceable,
       llvm::LLVMContext::MD_dereferenceable_or_null,
       llvm::LLVMContext::MD_mmra,
-      context.getMDKindID(vecTypeHintMDName),
-      context.getMDKindID(workGroupSizeHintMDName),
-      context.getMDKindID(reqdWorkGroupSizeMDName),
-      context.getMDKindID(intelReqdSubGroupSizeMDName)};
+      llvmContext.getMDKindID(vecTypeHintMDName),
+      llvmContext.getMDKindID(workGroupSizeHintMDName),
+      llvmContext.getMDKindID(reqdWorkGroupSizeMDName),
+      llvmContext.getMDKindID(intelReqdSubGroupSizeMDName)};
   return convertibleMetadata;
 }
 
@@ -505,9 +506,9 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
 
   /// Returns the list of LLVM IR metadata kinds that are convertible to MLIR
   /// LLVM dialect attributes.
-  ArrayRef<unsigned>
-  getSupportedMetadata(llvm::LLVMContext &context) const final {
-    return getSupportedMetadataImpl(context);
+  SmallVector<unsigned>
+  getSupportedMetadata(llvm::LLVMContext &llvmContext) const final {
+    return getSupportedMetadataImpl(llvmContext);
   }
 };
 } // namespace



More information about the Mlir-commits mailing list