Skip to content

Commit 90aac06

Browse files
tarunprabhuTarun Prabhu
andauthored
[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. Co-authored-by: Tarun Prabhu <[email protected]>
1 parent 03e6675 commit 90aac06

File tree

11 files changed

+83
-0
lines changed

11 files changed

+83
-0
lines changed

flang/include/flang/Optimizer/Dialect/Support/FIRContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ void setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features);
7171
/// Get the target features from the Module.
7272
mlir::LLVM::TargetFeaturesAttr getTargetFeatures(mlir::ModuleOp mod);
7373

74+
/// Set the compiler identifier for the module.
75+
void setIdent(mlir::ModuleOp mod, llvm::StringRef ident);
76+
77+
/// Get the compiler identifier from the Module.
78+
llvm::StringRef getIdent(mlir::ModuleOp mod);
79+
7480
/// Helper for determining the target from the host, etc. Tools may use this
7581
/// function to provide a consistent interpretation of the `--target=<string>`
7682
/// command-line option.

flang/lib/Lower/Bridge.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "flang/Lower/Bridge.h"
14+
#include "flang/Common/Version.h"
1415
#include "flang/Lower/Allocatable.h"
1516
#include "flang/Lower/CallInterface.h"
1617
#include "flang/Lower/Coarray.h"
@@ -6125,6 +6126,7 @@ Fortran::lower::LoweringBridge::LoweringBridge(
61256126
fir::setTargetFeatures(*module.get(), targetMachine.getTargetFeatureString());
61266127
fir::support::setMLIRDataLayout(*module.get(),
61276128
targetMachine.createDataLayout());
6129+
fir::setIdent(*module.get(), Fortran::common::getFlangFullVersion());
61286130
}
61296131

61306132
void Fortran::lower::genCleanUpInRegionIfAny(

flang/lib/Optimizer/Dialect/Support/FIRContext.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ mlir::LLVM::TargetFeaturesAttr fir::getTargetFeatures(mlir::ModuleOp mod) {
114114
return {};
115115
}
116116

117+
void fir::setIdent(mlir::ModuleOp mod, llvm::StringRef ident) {
118+
if (ident.empty())
119+
return;
120+
121+
mlir::MLIRContext *ctx = mod.getContext();
122+
mod->setAttr(mlir::LLVM::LLVMDialect::getIdentAttrName(),
123+
mlir::StringAttr::get(ctx, ident));
124+
}
125+
126+
llvm::StringRef fir::getIdent(mlir::ModuleOp mod) {
127+
if (auto attr = mod->getAttrOfType<mlir::StringAttr>(
128+
mlir::LLVM::LLVMDialect::getIdentAttrName()))
129+
return attr;
130+
return {};
131+
}
132+
117133
std::string fir::determineTargetTriple(llvm::StringRef triple) {
118134
// Treat "" or "default" as stand-ins for the default machine.
119135
if (triple.empty() || triple == "default")

flang/test/Lower/ident.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
3+
4+
! CHECK: module attributes {
5+
! CHECK-SAME: llvm.ident = "flang version {{.+}}"

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def LLVM_Dialect : Dialect {
3232
static StringRef getNoAliasScopesAttrName() { return "noalias_scopes"; }
3333
static StringRef getAliasScopesAttrName() { return "alias_scopes"; }
3434
static StringRef getAccessGroupsAttrName() { return "access_groups"; }
35+
static StringRef getIdentAttrName() { return "llvm.ident"; }
3536

3637
/// Names of llvm parameter attributes.
3738
static StringRef getAlignAttrName() { return "llvm.align"; }

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ class ModuleImport {
196196
/// LLVM dialect operation.
197197
LogicalResult convertLinkerOptionsMetadata();
198198

199+
/// Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
200+
LogicalResult convertIdentMetadata();
201+
199202
/// Converts all LLVM metadata nodes that translate to attributes such as
200203
/// alias analysis or access group metadata, and builds a map from the
201204
/// metadata nodes to the converted attributes.

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ class ModuleTranslation {
329329
/// metadata nodes for them.
330330
LogicalResult createTBAAMetadata();
331331

332+
/// Process the ident LLVM Metadata, if it exists.
333+
LogicalResult createIdentMetadata();
334+
332335
/// Translates dialect attributes attached to the given operation.
333336
LogicalResult
334337
convertDialectAttributes(Operation *op,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,23 @@ LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
518518
return success();
519519
}
520520

521+
LogicalResult ModuleImport::convertIdentMetadata() {
522+
for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
523+
// llvm.ident should have a single operand. That operand is itself an
524+
// MDNode with a single string operand.
525+
if (named.getName() != LLVMDialect::getIdentAttrName())
526+
continue;
527+
528+
if (named.getNumOperands() == 1)
529+
if (auto *md = dyn_cast<llvm::MDNode>(named.getOperand(0)))
530+
if (md->getNumOperands() == 1)
531+
if (auto *mdStr = dyn_cast<llvm::MDString>(md->getOperand(0)))
532+
mlirModule->setAttr(LLVMDialect::getIdentAttrName(),
533+
builder.getStringAttr(mdStr->getString()));
534+
}
535+
return success();
536+
}
537+
521538
LogicalResult ModuleImport::convertMetadata() {
522539
OpBuilder::InsertionGuard guard(builder);
523540
builder.setInsertionPointToEnd(mlirModule.getBody());
@@ -546,6 +563,8 @@ LogicalResult ModuleImport::convertMetadata() {
546563
}
547564
if (failed(convertLinkerOptionsMetadata()))
548565
return failure();
566+
if (failed(convertIdentMetadata()))
567+
return failure();
549568
return success();
550569
}
551570

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,20 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
18141814
return success();
18151815
}
18161816

1817+
LogicalResult ModuleTranslation::createIdentMetadata() {
1818+
if (auto attr = mlirModule->getAttrOfType<StringAttr>(
1819+
LLVMDialect::getIdentAttrName())) {
1820+
StringRef ident = attr;
1821+
llvm::LLVMContext &ctx = llvmModule->getContext();
1822+
llvm::NamedMDNode *namedMd =
1823+
llvmModule->getOrInsertNamedMetadata(LLVMDialect::getIdentAttrName());
1824+
llvm::MDNode *md = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, ident));
1825+
namedMd->addOperand(md);
1826+
}
1827+
1828+
return success();
1829+
}
1830+
18171831
void ModuleTranslation::setLoopMetadata(Operation *op,
18181832
llvm::Instruction *inst) {
18191833
LoopAnnotationAttr attr =
@@ -1965,6 +1979,8 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
19651979
return nullptr;
19661980
if (failed(translator.createTBAAMetadata()))
19671981
return nullptr;
1982+
if (failed(translator.createIdentMetadata()))
1983+
return nullptr;
19681984

19691985
// Convert other top-level operations if possible.
19701986
for (Operation &o : getModuleBody(module).getOperations()) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
2+
3+
; CHECK: module attributes {
4+
; CHECK-SAME: llvm.ident = "flang version 61.7.4"
5+
!llvm.ident = !{!0}
6+
!0 = !{!"flang version 61.7.4"}

mlir/test/Target/LLVMIR/ident.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
// CHECK: !llvm.ident = !{![[ID:[0-9]+]]}
4+
// CHECK: ![[ID]] = !{!"flang version 61.7.4"}
5+
module attributes {llvm.ident = "flang version 61.7.4"} {
6+
}

0 commit comments

Comments
 (0)