[Mlir-commits] [mlir] [mlir][llvm] adds an attribute for the module level assembly (PR #151318)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Jul 30 05:17:19 PDT 2025
https://github.com/gitoleg created https://github.com/llvm/llvm-project/pull/151318
This small PR adds a support for the module level assembly in the LLVM IR dialect.
As an alternative way I can introduce a separate attribute in the LLVM dialect for file scope assembly, but given it's a module-level entity and is nothing but a string - I thing it's ok to implement it in the same fashion like, for example, triple attribute.
Also, one extremely small test added.
Please let me know, if I need to implement it somehow different.
>From a02be327759dd580b31bb6f0b155c71f9574e0b4 Mon Sep 17 00:00:00 2001
From: gitoleg <forown at yandex.ru>
Date: Wed, 30 Jul 2025 14:57:37 +0300
Subject: [PATCH] [mlir][llvm] adds an attribute for the module level assembly
---
mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td | 3 +++
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 4 ++++
mlir/test/Target/LLVMIR/module-asm.mlir | 5 +++++
3 files changed, 12 insertions(+)
create mode 100644 mlir/test/Target/LLVMIR/module-asm.mlir
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
index c4c011f30b3bc..e6e3ad48c407f 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
@@ -71,6 +71,9 @@ def LLVM_Dialect : Dialect {
return "llvm.emit_c_interface";
}
+ /// Name of the module level assembly attribute.
+ static StringRef getModuleLevelAsmAttrName() { return "llvm.module_asm"; }
+
/// Returns `true` if the given type is compatible with the LLVM dialect.
static bool isCompatibleType(Type);
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 1722d74c08b62..46ec4d1abdc81 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1531,6 +1531,10 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
+ if (auto asmAttr =
+ m->getDiscardableAttr(LLVM::LLVMDialect::getModuleLevelAsmAttrName()))
+ llvmModule->setModuleInlineAsm(cast<StringAttr>(asmAttr).getValue());
+
return llvmModule;
}
diff --git a/mlir/test/Target/LLVMIR/module-asm.mlir b/mlir/test/Target/LLVMIR/module-asm.mlir
new file mode 100644
index 0000000000000..fa8158ac2677a
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/module-asm.mlir
@@ -0,0 +1,5 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+module attributes {llvm.module_asm = "foo"} {}
+
+// CHECK: module asm "foo"
\ No newline at end of file
More information about the Mlir-commits
mailing list