[Mlir-commits] [flang] [mlir] [flang][mlir] Add llvm.ident metadata when compiling with flang (PR #102506)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Aug 8 10:26:05 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-llvm
@llvm/pr-subscribers-mlir
Author: Tarun Prabhu (tarunprabhu)
<details>
<summary>Changes</summary>
This brings the behavior of flang in line with clang which also adds this metadata unconditionally.
Some time ago, there was a question on flang's slack channel asking if there was a way to determine if some binary was compiled with flang. With this patch, the compiler version string will be present in an object file and can be retrieved using, for instance, the `string` command on *nix.
---
Full diff: https://github.com/llvm/llvm-project/pull/102506.diff
10 Files Affected:
- (modified) flang/include/flang/Optimizer/Dialect/Support/FIRContext.h (+6)
- (modified) flang/lib/Lower/Bridge.cpp (+2)
- (modified) flang/lib/Optimizer/Dialect/Support/FIRContext.cpp (+16)
- (added) flang/test/Lower/ident.f90 (+5)
- (modified) mlir/include/mlir/Target/LLVMIR/ModuleImport.h (+3)
- (modified) mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h (+3)
- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+16)
- (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+15)
- (added) mlir/test/Target/LLVMIR/Import/ident.ll (+6)
- (added) mlir/test/Target/LLVMIR/ident.mlir (+6)
``````````diff
diff --git a/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h b/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h
index bd31aa0782493c..e45011c8e02a33 100644
--- a/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h
+++ b/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h
@@ -71,6 +71,12 @@ void setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features);
/// Get the target features from the Module.
mlir::LLVM::TargetFeaturesAttr getTargetFeatures(mlir::ModuleOp mod);
+/// Set the compiler identifier for the module.
+void setIdent(mlir::ModuleOp mod, llvm::StringRef ident);
+
+/// Get the compiler identifier from the Module.
+llvm::StringRef getIdent(mlir::ModuleOp mod);
+
/// Helper for determining the target from the host, etc. Tools may use this
/// function to provide a consistent interpretation of the `--target=<string>`
/// command-line option.
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 3d5b2e2a2fe028..ccbb481f472d81 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "flang/Lower/Bridge.h"
+#include "flang/Common/Version.h"
#include "flang/Lower/Allocatable.h"
#include "flang/Lower/CallInterface.h"
#include "flang/Lower/Coarray.h"
@@ -6125,6 +6126,7 @@ Fortran::lower::LoweringBridge::LoweringBridge(
fir::setTargetFeatures(*module.get(), targetMachine.getTargetFeatureString());
fir::support::setMLIRDataLayout(*module.get(),
targetMachine.createDataLayout());
+ fir::setIdent(*module.get(), Fortran::common::getFlangFullVersion());
}
void Fortran::lower::genCleanUpInRegionIfAny(
diff --git a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
index 1aa631cb391269..98d31c596636d7 100644
--- a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
+++ b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
@@ -114,6 +114,22 @@ mlir::LLVM::TargetFeaturesAttr fir::getTargetFeatures(mlir::ModuleOp mod) {
return {};
}
+static constexpr const char *identName = "llvm.ident";
+
+void fir::setIdent(mlir::ModuleOp mod, llvm::StringRef ident) {
+ if (ident.empty())
+ return;
+
+ auto *ctx = mod.getContext();
+ mod->setAttr(identName, mlir::StringAttr::get(ctx, ident));
+}
+
+llvm::StringRef fir::getIdent(mlir::ModuleOp mod) {
+ if (auto attr = mod->getAttrOfType<mlir::StringAttr>(identName))
+ return attr;
+ return {};
+}
+
std::string fir::determineTargetTriple(llvm::StringRef triple) {
// Treat "" or "default" as stand-ins for the default machine.
if (triple.empty() || triple == "default")
diff --git a/flang/test/Lower/ident.f90 b/flang/test/Lower/ident.f90
new file mode 100644
index 00000000000000..c5201edbcf83f7
--- /dev/null
+++ b/flang/test/Lower/ident.f90
@@ -0,0 +1,5 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
+
+! CHECK: module attributes {
+! CHECK-SAME: llvm.ident = "flang version {{.+}}"
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 5364e2ad473254..11f62c283d6a96 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -196,6 +196,9 @@ class ModuleImport {
/// LLVM dialect operation.
LogicalResult convertLinkerOptionsMetadata();
+ /// Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
+ LogicalResult convertIdentMetadata();
+
/// Converts all LLVM metadata nodes that translate to attributes such as
/// alias analysis or access group metadata, and builds a map from the
/// metadata nodes to the converted attributes.
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
index 85fdfed3bdbeb2..3c85338bc642f6 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -329,6 +329,9 @@ class ModuleTranslation {
/// metadata nodes for them.
LogicalResult createTBAAMetadata();
+ /// Process the ident LLVM Metadata, if it exists.
+ LogicalResult createIdentMetadata();
+
/// Translates dialect attributes attached to the given operation.
LogicalResult
convertDialectAttributes(Operation *op,
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index f283980ff1e006..9ed3b56817aa2f 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -518,6 +518,20 @@ LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
return success();
}
+LogicalResult ModuleImport::convertIdentMetadata() {
+ for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
+ // llvm.ident should have a single operand. That operand is itself an
+ // MDNode with a single string operand.
+ if (named.getName() == "llvm.ident")
+ if (named.getNumOperands() == 1)
+ if (auto *md = dyn_cast<llvm::MDNode>(named.getOperand(0)))
+ if (auto *mdStr = dyn_cast<llvm::MDString>(md->getOperand(0)))
+ mlirModule->setAttr("llvm.ident",
+ builder.getStringAttr(mdStr->getString()));
+ }
+ return success();
+}
+
LogicalResult ModuleImport::convertMetadata() {
OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToEnd(mlirModule.getBody());
@@ -546,6 +560,8 @@ LogicalResult ModuleImport::convertMetadata() {
}
if (failed(convertLinkerOptionsMetadata()))
return failure();
+ if (failed(convertIdentMetadata()))
+ return failure();
return success();
}
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index b468228ea78b78..9089cba71f97bd 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1814,6 +1814,19 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
return success();
}
+LogicalResult ModuleTranslation::createIdentMetadata() {
+ if (auto attr = mlirModule->getAttrOfType<mlir::StringAttr>("llvm.ident")) {
+ llvm::StringRef ident = attr;
+ llvm::LLVMContext &ctx = llvmModule->getContext();
+ llvm::NamedMDNode *namedMd =
+ llvmModule->getOrInsertNamedMetadata("llvm.ident");
+ llvm::MDNode *md = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, ident));
+ namedMd->addOperand(md);
+ }
+
+ return success();
+}
+
void ModuleTranslation::setLoopMetadata(Operation *op,
llvm::Instruction *inst) {
LoopAnnotationAttr attr =
@@ -1965,6 +1978,8 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
return nullptr;
if (failed(translator.createTBAAMetadata()))
return nullptr;
+ if (failed(translator.createIdentMetadata()))
+ return nullptr;
// Convert other top-level operations if possible.
for (Operation &o : getModuleBody(module).getOperations()) {
diff --git a/mlir/test/Target/LLVMIR/Import/ident.ll b/mlir/test/Target/LLVMIR/Import/ident.ll
new file mode 100644
index 00000000000000..d6582611fcbe29
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/ident.ll
@@ -0,0 +1,6 @@
+; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
+
+; CHECK: module attributes {
+; CHECK-SAME: "flang version 61.7.4"
+!llvm.ident = !{!0}
+!0 = !{!"flang version 61.7.4"}
diff --git a/mlir/test/Target/LLVMIR/ident.mlir b/mlir/test/Target/LLVMIR/ident.mlir
new file mode 100644
index 00000000000000..a15cced3506fae
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/ident.mlir
@@ -0,0 +1,6 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK: !llvm.ident = !{![[ID:[0-9]+]]}
+// CHECK: ![[ID]] = !{!"flang version 61.7.4"}
+module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", llvm.ident = "flang version 61.7.4", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
+}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/102506
More information about the Mlir-commits
mailing list