[flang-commits] [flang] 87b2d1d - [flang] Add getTypeDescriptorBindingTableName function
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Tue Oct 18 10:56:01 PDT 2022
Author: Valentin Clement
Date: 2022-10-18T19:55:55+02:00
New Revision: 87b2d1d04ed6d2e46e961a9f73bb8d607d8692da
URL: https://github.com/llvm/llvm-project/commit/87b2d1d04ed6d2e46e961a9f73bb8d607d8692da
DIFF: https://github.com/llvm/llvm-project/commit/87b2d1d04ed6d2e46e961a9f73bb8d607d8692da.diff
LOG: [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
Added:
Modified:
flang/include/flang/Optimizer/Support/InternalNames.h
flang/lib/Optimizer/Support/InternalNames.cpp
flang/unittests/Optimizer/InternalNamesTest.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Support/InternalNames.h b/flang/include/flang/Optimizer/Support/InternalNames.h
index ab6a477f6fc00..6defb3d567143 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 <cstdint>
+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 b1cedb068a383..d90866026c43f 100644
--- a/flang/lib/Optimizer/Support/InternalNames.cpp
+++ b/flang/lib/Optimizer/Support/InternalNames.cpp
@@ -335,12 +335,12 @@ mangleTypeDescriptorKinds(llvm::ArrayRef<std::int64_t> 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<llvm::StringRef> modules;
for (const std::string &mod : result.second.modules)
@@ -348,5 +348,15 @@ fir::NameUniquer::getTypeDescriptorName(llvm::StringRef mangledTypeName) {
llvm::Optional<llvm::StringRef> 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 3453c5e4836cc..fca8ac16f919f 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
More information about the flang-commits
mailing list