PR #2213: Use __builtin_prefetch for PrefetchToLocalCacheForWrite on x86-64 Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/2213 On x86-64 targets without PRFCHW, `PrefetchToLocalCacheForWrite()` emits a hard-coded `prefetchw` on the assumption that CPUs lacking the feature treat the opcode as a no-op. 64-bit NetBurst CPUs (Pentium 4 / Xeon, family 15) raise #UD on it instead, so any caller crashes with SIGILL. Firefox hit this as a startup crash on Linux: every report came from family 15 model 4 (Prescott/Nocona) CPUs. Let the compiler pick the instruction: `__builtin_prefetch(addr, 1, 3)` emits `prefetchw` when the target guarantees PRFCHW (e.g. `-mprfchw` or `-march=broadwell`) and `prefetcht0` otherwise. On the non-GNU SSE path, when `_MM_HINT_ET0` is unavailable, the fallback is now `_MM_HINT_T0`. For MSVC this means a read prefetch where there used to be no prefetch at all. Merge e194fbde5ea1f24324a1d5018a74cfd165167677 into 7a1c39bd5a1f17dd61f6f6b7fb52c8cd8643139b Merging this change closes #2213 PiperOrigin-RevId: 1013999131
diff --git a/absl/base/prefetch.h b/absl/base/prefetch.h index 482cde3..96ee28d 100644 --- a/absl/base/prefetch.h +++ b/absl/base/prefetch.h
@@ -117,7 +117,9 @@ // This function is similar to `PrefetchToLocalCache()` except that it // prefetches cachelines with an 'intent to modify' This typically includes // invalidating cache entries for this address in all other cache tiers, and an -// exclusive access intent. +// exclusive access intent. On targets where the compiler cannot assume a +// write-prefetch instruction (e.g. x86-64 without PRFCHW), this is equivalent +// to `PrefetchToLocalCache()`. // // Incorrect or gratuitous use of this function can degrade performance. As this // function can invalidate cached cachelines on other caches and computer cores, @@ -154,15 +156,7 @@ ABSL_ATTRIBUTE_ALWAYS_INLINE inline void PrefetchToLocalCacheForWrite( const void* addr) { - // [x86] gcc/clang don't generate PREFETCHW for __builtin_prefetch(.., 1) - // unless -march=broadwell or newer; this is not generally the default, so we - // manually emit prefetchw. PREFETCHW is recognized as a no-op on older Intel - // processors and has been present on AMD processors since the K6-2. -#if defined(__x86_64__) && !defined(__PRFCHW__) - asm("prefetchw %0" : : "m"(*reinterpret_cast<const char*>(addr))); -#else __builtin_prefetch(addr, 1, 3); -#endif } #elif defined(ABSL_INTERNAL_HAVE_SSE) @@ -183,12 +177,8 @@ const void* addr) { #if defined(_MM_HINT_ET0) _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_ET0); -#elif !defined(_MSC_VER) && defined(__x86_64__) - // _MM_HINT_ET0 is not universally supported. As we commented further - // up, PREFETCHW is recognized as a no-op on older Intel processors - // and has been present on AMD processors since the K6-2. We have this - // disabled for MSVC compilers as this miscompiles on older MSVC compilers. - asm("prefetchw %0" : : "m"(*reinterpret_cast<const char*>(addr))); +#else + _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_T0); #endif }