[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:21:28 PST 2025
https://github.com/ghehg created https://github.com/llvm/llvm-project/pull/125084
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.
>From fd56ef0513ad212e3a5dae02c6ae2dc884f5a749 Mon Sep 17 00:00:00 2001
From: Guojin He <he.guojin at gmail.com>
Date: Thu, 30 Jan 2025 11:08:35 -0500
Subject: [PATCH] [MLIR][LLVM] Import LLVM target triple into MLIR
---
mlir/include/mlir/Target/LLVMIR/ModuleImport.h | 4 ++++
mlir/lib/Target/LLVMIR/ModuleImport.cpp | 7 ++++++-
mlir/test/Target/LLVMIR/Import/target-triple.ll | 5 +++++
3 files changed, 15 insertions(+), 1 deletion(-)
create mode 100644 mlir/test/Target/LLVMIR/Import/target-triple.ll
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"
+
More information about the Mlir-commits
mailing list