-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[MLIR][LLVM] Import LLVM target triple into MLIR LLVM Dialect #125084
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 @llvm/pr-subscribers-mlir Author: Guojin (ghehg) ChangesIt would be essential and useful info to have it in MLIR when we are doing optimizations at MLIR level using LLVM IR as input. Thought about making it part of Full diff: https://github.com/llvm/llvm-project/pull/125084.diff 3 Files Affected:
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 84aecbd4373e05..80ae4d679624c2 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -71,6 +71,10 @@ class ModuleImport {
/// specification.
LogicalResult convertDataLayout();
+ /// Converts target triple of the LLVM module to an MLIR target triple
+ /// specification.
+ void convertTargetTriple();
+
/// Stores the mapping between an LLVM value and its MLIR counterpart.
void mapValue(llvm::Value *llvm, Value mlir) { mapValue(llvm) = mlir; }
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index e23ffdedd9a60c..5ebde22cccbdf3 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -662,6 +662,11 @@ LogicalResult ModuleImport::convertDataLayout() {
return success();
}
+void ModuleImport::convertTargetTriple() {
+ mlirModule->setAttr(LLVM::LLVMDialect::getTargetTripleAttrName(),
+ builder.getStringAttr(llvmModule->getTargetTriple()));
+}
+
LogicalResult ModuleImport::convertFunctions() {
for (llvm::Function &func : llvmModule->functions())
if (failed(processFunction(&func)))
@@ -2451,6 +2456,6 @@ mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
return {};
if (failed(moduleImport.convertFunctions()))
return {};
-
+ moduleImport.convertTargetTriple();
return module;
}
diff --git a/mlir/test/Target/LLVMIR/Import/target-triple.ll b/mlir/test/Target/LLVMIR/Import/target-triple.ll
new file mode 100644
index 00000000000000..a04b41a7aeb551
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/target-triple.ll
@@ -0,0 +1,5 @@
+; RUN: mlir-translate -import-llvm %s | FileCheck %s
+; CHECK: module attributes {
+; CHECK-SAME: llvm.target_triple = "aarch64-none-linux-android21"
+target triple = "aarch64-none-linux-android21"
+
|
|
@rolfmorel @ftynse Interesting to know how this could tie up with DLTI. |
|
If I had a vote, there should be a lossless But that's not this PR, I don't think. I'll also toss out the radical position that a conversion from, say, |
gysit
left a comment
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, LGTM.
I think it is ok to have a separate function for the target triple import similar to the data layout import.
With regards to the data layout vs target triple discussion, I am in favor of keeping them separate to mirror LLVM IR. Usually, LLVM dialect tries to follow LLVM IR as closely as possible. However, I agree that there is room for improving the representation of the target triple in LLVM dialect (currently it is a string attribute).
There are such functions +- in the ModuleImport / ModuleTranslation. In particular, the import from LLVM IR uses the |
|
... that's what I get for misreading the title of the PR Yeah, importing the target triple is a good idea but I don't think it affects DLTI much at all - at least at this point |
|
@gysit Re not modeling all fields ... what're we missing? I suspect vector alignments and ... probably some of the address space metadata? (My long term vision here is that, when going to LLVM, you'd have a, say, |
Yes vector and I believe aggregate and function pointer alignment as well. Plus some exotic name mangling flag etc.
The main goal would be that the default data layout is chosen based on the target? That sounds like a good option to me. PS I will land this commit later today should there be no more comments. |
Sorry, was just tagging folks on the DLTI design discussion. I think DLTI could be generating those attributes, too, not just when it's imported from LLVM. My thinking is to have a coherent whole-program target description story, from front-end (be it pytorch or clang), MLIR stack (via DLTI) and LLVM (via triples and DL). |
It would be essential and useful info to have it in MLIR when we are doing optimizations at MLIR level using LLVM IR as input. Thought about making it part of
ModuleImport::convertMetadata(), but decided not as this logic is its own simple thing. However, open to options like that.