From: Valentin Clement Date: Tue, 18 Oct 2022 17:52:20 +0000 (+0200) Subject: [flang] Add getTypeDescriptorBindingTableName function X-Git-Tag: upstream/17.0.6~30202 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=87b2d1d04ed6d2e46e961a9f73bb8d607d8692da;p=platform%2Fupstream%2Fllvm.git [flang] Add getTypeDescriptorBindingTableName function Type descriptor and its binding table are defined as fir.global in FIR. Their names are derived from the derived-type name. This patch adds a new function `getTypeDescriptorBindingTableName` in the NameUniquer and refactor the `GetTypeDescriptorName` function to reuse the same code. This will be used in the fir.dispatch code generation. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D136167 --- diff --git a/flang/include/flang/Optimizer/Support/InternalNames.h b/flang/include/flang/Optimizer/Support/InternalNames.h index ab6a477..6defb3d 100644 --- a/flang/include/flang/Optimizer/Support/InternalNames.h +++ b/flang/include/flang/Optimizer/Support/InternalNames.h @@ -14,6 +14,9 @@ #include "llvm/ADT/StringRef.h" #include +static constexpr llvm::StringRef typeDescriptorSeparator = ".dt."; +static constexpr llvm::StringRef bindingTableSeparator = ".v."; + namespace fir { /// Internal name mangling of identifiers @@ -142,6 +145,12 @@ struct NameUniquer { /// not a valid mangled derived type name. static std::string getTypeDescriptorName(llvm::StringRef mangledTypeName); + /// Given a mangled derived type name, get the name of the related binding + /// table object. Returns an empty string if \p mangledTypeName is not a valid + /// mangled derived type name. + static std::string + getTypeDescriptorBindingTableName(llvm::StringRef mangledTypeName); + private: static std::string intAsString(std::int64_t i); static std::string doKind(std::int64_t kind); diff --git a/flang/lib/Optimizer/Support/InternalNames.cpp b/flang/lib/Optimizer/Support/InternalNames.cpp index b1cedb0..d908660 100644 --- a/flang/lib/Optimizer/Support/InternalNames.cpp +++ b/flang/lib/Optimizer/Support/InternalNames.cpp @@ -335,12 +335,12 @@ mangleTypeDescriptorKinds(llvm::ArrayRef kinds) { return result; } -std::string -fir::NameUniquer::getTypeDescriptorName(llvm::StringRef mangledTypeName) { - auto result = deconstruct(mangledTypeName); - if (result.first != NameKind::DERIVED_TYPE) +static std::string getDerivedTypeObjectName(llvm::StringRef mangledTypeName, + const llvm::StringRef separator) { + auto result = fir::NameUniquer::deconstruct(mangledTypeName); + if (result.first != fir::NameUniquer::NameKind::DERIVED_TYPE) return ""; - std::string varName = ".dt." + result.second.name + + std::string varName = separator.str() + result.second.name + mangleTypeDescriptorKinds(result.second.kinds); llvm::SmallVector modules; for (const std::string &mod : result.second.modules) @@ -348,5 +348,15 @@ fir::NameUniquer::getTypeDescriptorName(llvm::StringRef mangledTypeName) { llvm::Optional host; if (result.second.host) host = *result.second.host; - return doVariable(modules, host, varName); + return fir::NameUniquer::doVariable(modules, host, varName); +} + +std::string +fir::NameUniquer::getTypeDescriptorName(llvm::StringRef mangledTypeName) { + return getDerivedTypeObjectName(mangledTypeName, typeDescriptorSeparator); +} + +std::string fir::NameUniquer::getTypeDescriptorBindingTableName( + llvm::StringRef mangledTypeName) { + return getDerivedTypeObjectName(mangledTypeName, bindingTableSeparator); } diff --git a/flang/unittests/Optimizer/InternalNamesTest.cpp b/flang/unittests/Optimizer/InternalNamesTest.cpp index 3453c5e..fca8ac1 100644 --- a/flang/unittests/Optimizer/InternalNamesTest.cpp +++ b/flang/unittests/Optimizer/InternalNamesTest.cpp @@ -255,4 +255,21 @@ TEST(InternalNamesTest, isExternalFacingUniquedName) { ASSERT_TRUE(NameUniquer::isExternalFacingUniquedName(result)); } +TEST(InternalNamesTest, getTypeDescriptorName) { + std::string derivedTypeName = "_QMdispatch1Tp1"; + std::string expectedBindingTableName = "_QMdispatch1E.dt.p1"; + ASSERT_EQ(expectedBindingTableName, + fir::NameUniquer::getTypeDescriptorName(derivedTypeName)); + ASSERT_EQ("", fir::NameUniquer::getTypeDescriptorName("_QMdispatch1Pp1")); +} + +TEST(InternalNamesTest, getTypeDescriptorBindingTableName) { + std::string derivedTypeName = "_QMdispatch1Tp1"; + std::string expectedBindingTableName = "_QMdispatch1E.v.p1"; + ASSERT_EQ(expectedBindingTableName, + fir::NameUniquer::getTypeDescriptorBindingTableName(derivedTypeName)); + ASSERT_EQ("", + fir::NameUniquer::getTypeDescriptorBindingTableName("_QMdispatch1Pp1")); +} + // main() from gtest_main