[llvm] [NFC] Don't recompute type name (PR #119631)
Ian Wood via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 11 15:11:56 PST 2024
https://github.com/IanWood1 created https://github.com/llvm/llvm-project/pull/119631
This change uses a local static variable to cache the `StringRef`.
I found that `RelWithDebInfo` builds of MLIR were spending a relatively large amount of time in `StringRef::find` and I tracked it down to `getTypeName` which utilizes `StringRef` methods that are defined in a separate translation unit. This is especially impactful on perf because `getTypeName` is supposed to be used for debug logging. See an example here: https://github.com/llvm/llvm-project/blob/4b825c7417f72ee88ee3e4316d0c01ed463f1241/mlir/include/mlir/IR/Types.h#L294-L300
>From b9a8ec2d59ba9b6a32e455de2a54901b4d1e058f Mon Sep 17 00:00:00 2001
From: Ian Wood <ianwood2024 at u.northwestern.edu>
Date: Thu, 12 Dec 2024 02:48:10 -0800
Subject: [PATCH] [NFC] cache type name in a static variable
---
llvm/include/llvm/Support/TypeName.h | 30 +++++++++++++++++-----------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/llvm/include/llvm/Support/TypeName.h b/llvm/include/llvm/Support/TypeName.h
index 9547e76a7fa79b..61ba09c2163047 100644
--- a/llvm/include/llvm/Support/TypeName.h
+++ b/llvm/include/llvm/Support/TypeName.h
@@ -13,18 +13,8 @@
namespace llvm {
-/// We provide a function which tries to compute the (demangled) name of a type
-/// statically.
-///
-/// This routine may fail on some platforms or for particularly unusual types.
-/// Do not use it for anything other than logging and debugging aids. It isn't
-/// portable or dependendable in any real sense.
-///
-/// The returned StringRef will point into a static storage duration string.
-/// However, it may not be null terminated and may be some strangely aligned
-/// inner substring of a larger string.
-template <typename DesiredTypeName>
-inline StringRef getTypeName() {
+namespace detail {
+template <typename DesiredTypeName> inline StringRef getTypeNameImpl() {
#if defined(__clang__) || defined(__GNUC__)
StringRef Name = __PRETTY_FUNCTION__;
@@ -58,6 +48,22 @@ inline StringRef getTypeName() {
return "UNKNOWN_TYPE";
#endif
}
+} // namespace detail
+
+/// We provide a function which tries to compute the (demangled) name of a type
+/// statically.
+///
+/// This routine may fail on some platforms or for particularly unusual types.
+/// Do not use it for anything other than logging and debugging aids. It isn't
+/// portable or dependendable in any real sense.
+///
+/// The returned StringRef will point into a static storage duration string.
+/// However, it may not be null terminated and may be some strangely aligned
+/// inner substring of a larger string.
+template <typename DesiredTypeName> inline StringRef getTypeName() {
+ static StringRef Name = detail::getTypeNameImpl<DesiredTypeName>();
+ return Name;
+}
} // namespace llvm
More information about the llvm-commits
mailing list