[Mlir-commits] [mlir] [MLIR][LLVM] Import LLVM target triple into MLIR LLVM Dialect (PR #125084)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jan 30 08:22:05 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Guojin (ghehg)

<details>
<summary>Changes</summary>

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()](https://github.com/llvm/llvm-project/blob/242aa8c743fe4344844753d8faf59744235319df/mlir/lib/Target/LLVMIR/ModuleImport.cpp#L597)`, but decided not as this logic is its own simple thing. However, open to options like that.

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


3 Files Affected:

- (modified) mlir/include/mlir/Target/LLVMIR/ModuleImport.h (+4) 
- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+6-1) 
- (added) mlir/test/Target/LLVMIR/Import/target-triple.ll (+5) 


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

``````````

</details>


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


More information about the Mlir-commits mailing list