[Mlir-commits] [flang] [mlir] [flang][mlir] Add llvm.ident metadata when compiling with flang (PR #102506)

Tarun Prabhu llvmlistbot at llvm.org
Mon Aug 12 07:45:32 PDT 2024


https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/102506

>From 0bcc60501579b203be564fab10bbcdff3eace917 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Thu, 8 Aug 2024 11:18:29 -0600
Subject: [PATCH 1/3] [flang][mlir] Add llvm.ident metadata when compiling with
 flang

This brings the behavior of flang in line with clang which also adds this
metadata unconditionally.
---
 .../flang/Optimizer/Dialect/Support/FIRContext.h |  6 ++++++
 flang/lib/Lower/Bridge.cpp                       |  2 ++
 .../lib/Optimizer/Dialect/Support/FIRContext.cpp | 16 ++++++++++++++++
 flang/test/Lower/ident.f90                       |  5 +++++
 mlir/include/mlir/Target/LLVMIR/ModuleImport.h   |  3 +++
 .../mlir/Target/LLVMIR/ModuleTranslation.h       |  3 +++
 mlir/lib/Target/LLVMIR/ModuleImport.cpp          | 16 ++++++++++++++++
 mlir/lib/Target/LLVMIR/ModuleTranslation.cpp     | 15 +++++++++++++++
 mlir/test/Target/LLVMIR/Import/ident.ll          |  6 ++++++
 mlir/test/Target/LLVMIR/ident.mlir               |  6 ++++++
 10 files changed, 78 insertions(+)
 create mode 100644 flang/test/Lower/ident.f90
 create mode 100644 mlir/test/Target/LLVMIR/Import/ident.ll
 create mode 100644 mlir/test/Target/LLVMIR/ident.mlir

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

>From 6fd182df0de25a2bc0e5768638a27ba7af968119 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Mon, 12 Aug 2024 07:57:02 -0600
Subject: [PATCH 2/3] Address reviewer comments.

---
 flang/lib/Optimizer/Dialect/Support/FIRContext.cpp |  8 ++++----
 mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td    |  1 +
 mlir/lib/Target/LLVMIR/ModuleImport.cpp            | 11 +++++++----
 mlir/lib/Target/LLVMIR/ModuleTranslation.cpp       |  7 ++++---
 mlir/test/Target/LLVMIR/Import/ident.ll            |  2 +-
 mlir/test/Target/LLVMIR/ident.mlir                 |  4 ++--
 6 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
index 98d31c596636d7..9731d233d80217 100644
--- a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
+++ b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
@@ -114,18 +114,18 @@ 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));
+  mod->setAttr(mlir::LLVM::LLVMDialect::getIdentAttrName(),
+               mlir::StringAttr::get(ctx, ident));
 }
 
 llvm::StringRef fir::getIdent(mlir::ModuleOp mod) {
-  if (auto attr = mod->getAttrOfType<mlir::StringAttr>(identName))
+  if (auto attr = mod->getAttrOfType<mlir::StringAttr>(
+          mlir::LLVM::LLVMDialect::getIdentAttrName()))
     return attr;
   return {};
 }
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
index c4c011f30b3bcd..0d86a9bde18d96 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
@@ -27,6 +27,7 @@ def LLVM_Dialect : Dialect {
     static StringRef getNoAliasScopesAttrName() { return "noalias_scopes"; }
     static StringRef getAliasScopesAttrName() { return "alias_scopes"; }
     static StringRef getAccessGroupsAttrName() { return "access_groups"; }
+    static StringRef getIdentAttrName() { return "llvm.ident"; }
 
     /// Names of llvm parameter attributes.
     static StringRef getAlignAttrName() { return "llvm.align"; }
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 9ed3b56817aa2f..bd761650534880 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -522,11 +522,14 @@ 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 (named.getName() != LLVMDialect::getIdentAttrName())
+      continue;
+
+    if (named.getNumOperands() == 1)
+      if (auto *md = dyn_cast<llvm::MDNode>(named.getOperand(0)))
+        if (md->getNumOperands() == 1)
           if (auto *mdStr = dyn_cast<llvm::MDString>(md->getOperand(0)))
-            mlirModule->setAttr("llvm.ident",
+            mlirModule->setAttr(LLVMDialect::getIdentAttrName(),
                                 builder.getStringAttr(mdStr->getString()));
   }
   return success();
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 9089cba71f97bd..930300d26c4479 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1815,11 +1815,12 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
 }
 
 LogicalResult ModuleTranslation::createIdentMetadata() {
-  if (auto attr = mlirModule->getAttrOfType<mlir::StringAttr>("llvm.ident")) {
-    llvm::StringRef ident = attr;
+  if (auto attr = mlirModule->getAttrOfType<StringAttr>(
+          LLVMDialect::getIdentAttrName())) {
+    StringRef ident = attr;
     llvm::LLVMContext &ctx = llvmModule->getContext();
     llvm::NamedMDNode *namedMd =
-        llvmModule->getOrInsertNamedMetadata("llvm.ident");
+        llvmModule->getOrInsertNamedMetadata(LLVMDialect::getIdentAttrName());
     llvm::MDNode *md = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, ident));
     namedMd->addOperand(md);
   }
diff --git a/mlir/test/Target/LLVMIR/Import/ident.ll b/mlir/test/Target/LLVMIR/Import/ident.ll
index d6582611fcbe29..1728fdf249ede7 100644
--- a/mlir/test/Target/LLVMIR/Import/ident.ll
+++ b/mlir/test/Target/LLVMIR/Import/ident.ll
@@ -1,6 +1,6 @@
 ; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
 
 ; CHECK: module attributes {
-; CHECK-SAME: "flang version 61.7.4"
+; CHECK-SAME: llvm.ident = "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
index a15cced3506fae..e2c343df3c7f33 100644
--- a/mlir/test/Target/LLVMIR/ident.mlir
+++ b/mlir/test/Target/LLVMIR/ident.mlir
@@ -2,5 +2,5 @@
 
 // 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
+module attributes {llvm.ident = "flang version 61.7.4"} {
+}

>From b7fd0099e4c7fc77857395086d762c570bca6448 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Mon, 12 Aug 2024 08:45:07 -0600
Subject: [PATCH 3/3] Address reviewer comments.

---
 flang/lib/Optimizer/Dialect/Support/FIRContext.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
index 9731d233d80217..5bd8e2d4336361 100644
--- a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
+++ b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
@@ -118,7 +118,7 @@ void fir::setIdent(mlir::ModuleOp mod, llvm::StringRef ident) {
   if (ident.empty())
     return;
 
-  auto *ctx = mod.getContext();
+  mlir::MLIRContext *ctx = mod.getContext();
   mod->setAttr(mlir::LLVM::LLVMDialect::getIdentAttrName(),
                mlir::StringAttr::get(ctx, ident));
 }



More information about the Mlir-commits mailing list