diff --git a/tensorflow/core/framework/type_index.h b/tensorflow/core/framework/type_index.h index fd27d8bcb35d4d..fcf68677a1209f 100644 --- a/tensorflow/core/framework/type_index.h +++ b/tensorflow/core/framework/type_index.h @@ -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 - 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(reinterpret_cast(hash_bit)), + 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(reinterpret_cast(hash_bit)), - name); + "[RTTI disabled]"); +#endif // __GXX_RTTI } private: @@ -70,12 +97,7 @@ class TypeIndex { template inline TypeIndex MakeTypeIndex() { -#if defined(__GXX_RTTI) || defined(_CPPRTTI) - // Use the real type name if we have RTTI. - return TypeIndex::Make(typeid(T).name()); -#else - return TypeIndex::Make("[RTTI disabled]"); -#endif // __GXX_RTTI + return TypeIndex::Make(); } } // namespace tensorflow