-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[mlir][llvm] adds an attribute for the module level assembly #151318
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
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-llvm Author: None (gitoleg) ChangesThis 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:
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
|
a02be32
to
dd8b762
Compare
Looking at the language ref and some code examples in LLVM it looks like there can be many of these module level assembly blocks? Wouldn't it make more sense to implement them as a module level operation similar to GlobalOp / AliasOp etc. I mean an operation that needs to be nested directly within the moduleOp. Or do I miss something and there is just one inline assembly string per module? Then an attribute makes more sense. |
@gysit So my initial intention was to let user to collect all the strings as necessary, but looks like you're right - there should be at least an array of string attributes. Speaking about a special operation - it definitely may be an option. But from my understanding operation should represent something that may change while attributes stands for something constant. But surely it's up to you - what do you prefer? An |
Ok, if this is more like an attribute that is passed through the compiler then an ArrayAttribute maybe an ok solution. |
97dc0a2
to
bc7cf6c
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add the corresponding import implementation?
f8f9c9e
to
5d8479f
Compare
90ef278
to
9a0ec93
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvements!
I have some nit comments otherwise this looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo last nit.
Should we land this for you? If yes can you update the PR description to the commit message you would like to have.
Co-authored-by: Tobias Gysi <[email protected]>
I don't know ) I think that given a PR is rather small, the short description is enough. Feel free to change it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for addressing the comments.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/13496 Here is the relevant piece of the build log for the reference
|
Adds a support for the module level assembly in the LLVM IR dialect.