[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:55 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-llvm

Author: None (gitoleg)

<details>
<summary>Changes</summary>

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.


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


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td (+3) 
- (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+4) 
- (added) mlir/test/Target/LLVMIR/module-asm.mlir (+5) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/151318


More information about the Mlir-commits mailing list