Fix issues with TypeIndex on MacOS, i.e. hash on the type name where available since this otherwise causes problems when loading different shared libraries with RTLD_LOCAL.

PiperOrigin-RevId: 317395983
Change-Id: I14b3add5fa19725b2150b68813364d16b8320130
This commit is contained in:
A. Unique TensorFlower 2020-06-19 15:55:58 -07:00 committed by TensorFlower Gardener
parent e3809400b2
commit 1823f87735

View File

@ -24,6 +24,10 @@ limitations under the License.
#include "tensorflow/core/platform/types.h"
#if defined(MACOS) || defined(TARGET_OS_MAC)
#include "tensorflow/core/platform/hash.h"
#endif // defined(MACOS) || defined(TARGET_OS_MAC)
namespace tensorflow {
// On some platforms, we would like to avoid using RTTI in order to have smaller
@ -53,10 +57,33 @@ class TypeIndex {
// Returns a TypeIndex object that corresponds to a typename.
template <typename T>
static TypeIndex Make(const char* name) {
static TypeIndex Make() {
static bool hash_bit[1];
#if defined(__GXX_RTTI) || defined(_CPPRTTI)
#if defined(MACOS) || defined(TARGET_OS_MAC)
// Use a hash based on the type name to avoid issues due to RTLD_LOCAL on
// MacOS (b/156979412).
return TypeIndex(Hash64(typeid(T).name()), typeid(T).name());
#else
// Use the real type name if we have RTTI.
return TypeIndex(static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)),
name);
typeid(T).name());
#endif // defined(MACOS) || defined(TARGET_OS_MAC)
#else
#if defined(MACOS) || defined(TARGET_OS_MAC)
// Warn MacOS users that not using RTTI can cause problems (b/156979412).
#warning \
"Compiling with RTTI disabled on MacOS can cause problems when comparing " \
"types across shared libraries."
#endif // defined(MACOS) || defined(TARGET_OS_MAC)
// No type names available.
return TypeIndex(static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)),
"[RTTI disabled]");
#endif // __GXX_RTTI
}
private:
@ -70,12 +97,7 @@ class TypeIndex {
template <typename T>
inline TypeIndex MakeTypeIndex() {
#if defined(__GXX_RTTI) || defined(_CPPRTTI)
// Use the real type name if we have RTTI.
return TypeIndex::Make<T>(typeid(T).name());
#else
return TypeIndex::Make<T>("[RTTI disabled]");
#endif // __GXX_RTTI
return TypeIndex::Make<T>();
}
} // namespace tensorflow