From 3593ae4312f6156c9ca50d46cdb55a8dfad782d0 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Fri, 24 Sep 2021 14:05:23 +0200 Subject: [PATCH] [flang][fir] Add support to mangle/deconstruct namelist group name Add support to create unique name for namelist group and be able to deconstruct them. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D110331 Co-authored-by: Jean Perier Co-authored-by: Eric Schweitz --- .../include/flang/Optimizer/Support/InternalNames.h | 8 +++++++- flang/lib/Lower/Mangler.cpp | 6 ++++++ flang/lib/Optimizer/Support/InternalNames.cpp | 13 +++++++++++++ flang/unittests/Optimizer/InternalNamesTest.cpp | 11 +++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/flang/include/flang/Optimizer/Support/InternalNames.h b/flang/include/flang/Optimizer/Support/InternalNames.h index fa98cc2a8e49..36e3ed093ca4 100644 --- a/flang/include/flang/Optimizer/Support/InternalNames.h +++ b/flang/include/flang/Optimizer/Support/InternalNames.h @@ -41,7 +41,8 @@ struct NameUniquer { INTRINSIC_TYPE_DESC, PROCEDURE, TYPE_DESC, - VARIABLE + VARIABLE, + NAMELIST_GROUP }; /// Components of an unparsed unique name @@ -112,6 +113,11 @@ struct NameUniquer { llvm::Optional host, llvm::StringRef name); + /// Unique a namelist group name + static std::string doNamelistGroup(llvm::ArrayRef modules, + llvm::Optional host, + llvm::StringRef name); + /// Entry point for the PROGRAM (called by the runtime) /// Can be overridden with the `--main-entry-name=` option. static llvm::StringRef doProgramEntry(); diff --git a/flang/lib/Lower/Mangler.cpp b/flang/lib/Lower/Mangler.cpp index 07d9e63e0423..f74afc5b53dc 100644 --- a/flang/lib/Lower/Mangler.cpp +++ b/flang/lib/Lower/Mangler.cpp @@ -114,6 +114,12 @@ Fortran::lower::mangle::mangleName(const Fortran::semantics::Symbol &symbol, symbolName); return fir::NameUniquer::doVariable(modNames, optHost, symbolName); }, + [&](const Fortran::semantics::NamelistDetails &) { + auto modNames = moduleNames(ultimateSymbol); + auto optHost = hostName(ultimateSymbol); + return fir::NameUniquer::doNamelistGroup(modNames, optHost, + symbolName); + }, [&](const Fortran::semantics::CommonBlockDetails &) { return fir::NameUniquer::doCommonBlock(symbolName); }, diff --git a/flang/lib/Optimizer/Support/InternalNames.cpp b/flang/lib/Optimizer/Support/InternalNames.cpp index a7493d7494fe..bd281638e443 100644 --- a/flang/lib/Optimizer/Support/InternalNames.cpp +++ b/flang/lib/Optimizer/Support/InternalNames.cpp @@ -205,6 +205,15 @@ fir::NameUniquer::doVariable(llvm::ArrayRef modules, return result.append(toLower(name)); } +std::string +fir::NameUniquer::doNamelistGroup(llvm::ArrayRef modules, + llvm::Optional host, + llvm::StringRef name) { + std::string result = prefix(); + result.append(doModulesHost(modules, host)).append("G"); + return result.append(toLower(name)); +} + llvm::StringRef fir::NameUniquer::doProgramEntry() { if (mainEntryName.size()) return mainEntryName; @@ -279,6 +288,10 @@ fir::NameUniquer::deconstruct(llvm::StringRef uniq) { else kinds.push_back(readInt(uniq, i, i + 1, end)); break; + case 'G': + nk = NameKind::NAMELIST_GROUP; + name = readName(uniq, i, i + 1, end); + break; default: assert(false && "unknown uniquing code"); diff --git a/flang/unittests/Optimizer/InternalNamesTest.cpp b/flang/unittests/Optimizer/InternalNamesTest.cpp index 831d7997e3f8..1a837660ec63 100644 --- a/flang/unittests/Optimizer/InternalNamesTest.cpp +++ b/flang/unittests/Optimizer/InternalNamesTest.cpp @@ -162,6 +162,12 @@ TEST(InternalNamesTest, doProgramEntry) { ASSERT_EQ(actual.str(), expectedMangledName); } +TEST(InternalNamesTest, doNamelistGroup) { + llvm::StringRef actual = NameUniquer::doNamelistGroup({"mod1"}, {}, {"nlg"}); + std::string expectedMangledName = "_QMmod1Gnlg"; + ASSERT_EQ(actual, expectedMangledName); +} + TEST(InternalNamesTest, deconstructTest) { std::pair actual = NameUniquer::deconstruct("_QBhello"); auto expectedNameKind = NameUniquer::NameKind::COMMON; @@ -208,6 +214,11 @@ TEST(InternalNamesTest, complexdeconstructTest) { expectedNameKind = NameKind::DISPATCH_TABLE; expectedComponents = {{}, {}, "t", {}}; validateDeconstructedName(actual, expectedNameKind, expectedComponents); + + actual = NameUniquer::deconstruct("_QFmstartGmpitop"); + expectedNameKind = NameKind::NAMELIST_GROUP; + expectedComponents = {{}, {"mstart"}, "mpitop", {}}; + validateDeconstructedName(actual, expectedNameKind, expectedComponents); } // main() from gtest_main