Skip to content

Commit 003a721

Browse files
authored
[NFC] Don't recompute type name (#119631)
This change uses a local static variable to cache the computed `StringRef` containing the type's name. 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
1 parent 3fcc302 commit 003a721

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

llvm/include/llvm/Support/TypeName.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,8 @@
1313

1414
namespace llvm {
1515

16-
/// We provide a function which tries to compute the (demangled) name of a type
17-
/// statically.
18-
///
19-
/// This routine may fail on some platforms or for particularly unusual types.
20-
/// Do not use it for anything other than logging and debugging aids. It isn't
21-
/// portable or dependendable in any real sense.
22-
///
23-
/// The returned StringRef will point into a static storage duration string.
24-
/// However, it may not be null terminated and may be some strangely aligned
25-
/// inner substring of a larger string.
26-
template <typename DesiredTypeName>
27-
inline StringRef getTypeName() {
16+
namespace detail {
17+
template <typename DesiredTypeName> inline StringRef getTypeNameImpl() {
2818
#if defined(__clang__) || defined(__GNUC__)
2919
StringRef Name = __PRETTY_FUNCTION__;
3020

@@ -58,6 +48,22 @@ inline StringRef getTypeName() {
5848
return "UNKNOWN_TYPE";
5949
#endif
6050
}
51+
} // namespace detail
52+
53+
/// We provide a function which tries to compute the (demangled) name of a type
54+
/// statically.
55+
///
56+
/// This routine may fail on some platforms or for particularly unusual types.
57+
/// Do not use it for anything other than logging and debugging aids. It isn't
58+
/// portable or dependendable in any real sense.
59+
///
60+
/// The returned StringRef will point into a static storage duration string.
61+
/// However, it may not be null terminated and may be some strangely aligned
62+
/// inner substring of a larger string.
63+
template <typename DesiredTypeName> inline StringRef getTypeName() {
64+
static StringRef Name = detail::getTypeNameImpl<DesiredTypeName>();
65+
return Name;
66+
}
6167

6268
} // namespace llvm
6369

0 commit comments

Comments
 (0)