prefetch.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2023 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: prefetch.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines prefetch functions to prefetch memory contents
  20. // into the first level cache (L1) for the current CPU. The prefetch logic
  21. // offered in this header is limited to prefetching first level cachelines
  22. // only, and is aimed at relatively 'simple' prefetching logic.
  23. //
  24. #ifndef Y_ABSL_BASE_PREFETCH_H_
  25. #define Y_ABSL_BASE_PREFETCH_H_
  26. #include "y_absl/base/config.h"
  27. #if defined(Y_ABSL_INTERNAL_HAVE_SSE)
  28. #include <xmmintrin.h>
  29. #endif
  30. #if defined(_MSC_VER) && _MSC_VER >= 1900 && \
  31. (defined(_M_X64) || defined(_M_IX86))
  32. #include <intrin.h>
  33. #pragma intrinsic(_mm_prefetch)
  34. #endif
  35. namespace y_absl {
  36. Y_ABSL_NAMESPACE_BEGIN
  37. // Moves data into the L1 cache before it is read, or "prefetches" it.
  38. //
  39. // The value of `addr` is the address of the memory to prefetch. If
  40. // the target and compiler support it, data prefetch instructions are
  41. // generated. If the prefetch is done some time before the memory is
  42. // read, it may be in the cache by the time the read occurs.
  43. //
  44. // This method prefetches data with the highest degree of temporal locality;
  45. // data is prefetched where possible into all levels of the cache.
  46. //
  47. // Incorrect or gratuitous use of this function can degrade performance.
  48. // Use this function only when representative benchmarks show an improvement.
  49. //
  50. // Example:
  51. //
  52. // // Computes incremental checksum for `data`.
  53. // int ComputeChecksum(int sum, y_absl::string_view data);
  54. //
  55. // // Computes cumulative checksum for all values in `data`
  56. // int ComputeChecksum(y_absl::Span<const TString> data) {
  57. // int sum = 0;
  58. // auto it = data.begin();
  59. // auto pit = data.begin();
  60. // auto end = data.end();
  61. // for (int dist = 8; dist > 0 && pit != data.end(); --dist, ++pit) {
  62. // y_absl::PrefetchToLocalCache(pit->data());
  63. // }
  64. // for (; pit != end; ++pit, ++it) {
  65. // sum = ComputeChecksum(sum, *it);
  66. // y_absl::PrefetchToLocalCache(pit->data());
  67. // }
  68. // for (; it != end; ++it) {
  69. // sum = ComputeChecksum(sum, *it);
  70. // }
  71. // return sum;
  72. // }
  73. //
  74. void PrefetchToLocalCache(const void* addr);
  75. // Moves data into the L1 cache before it is read, or "prefetches" it.
  76. //
  77. // This function is identical to `PrefetchToLocalCache()` except that it has
  78. // non-temporal locality: the fetched data should not be left in any of the
  79. // cache tiers. This is useful for cases where the data is used only once /
  80. // short term, for example, invoking a destructor on an object.
  81. //
  82. // Incorrect or gratuitous use of this function can degrade performance.
  83. // Use this function only when representative benchmarks show an improvement.
  84. //
  85. // Example:
  86. //
  87. // template <typename Iterator>
  88. // void DestroyPointers(Iterator begin, Iterator end) {
  89. // size_t distance = std::min(8U, bars.size());
  90. //
  91. // int dist = 8;
  92. // auto prefetch_it = begin;
  93. // while (prefetch_it != end && --dist;) {
  94. // y_absl::PrefetchToLocalCacheNta(*prefetch_it++);
  95. // }
  96. // while (prefetch_it != end) {
  97. // delete *begin++;
  98. // y_absl::PrefetchToLocalCacheNta(*prefetch_it++);
  99. // }
  100. // while (begin != end) {
  101. // delete *begin++;
  102. // }
  103. // }
  104. //
  105. void PrefetchToLocalCacheNta(const void* addr);
  106. // Moves data into the L1 cache with the intent to modify it.
  107. //
  108. // This function is similar to `PrefetchToLocalCache()` except that it
  109. // prefetches cachelines with an 'intent to modify' This typically includes
  110. // invalidating cache entries for this address in all other cache tiers, and an
  111. // exclusive access intent.
  112. //
  113. // Incorrect or gratuitous use of this function can degrade performance. As this
  114. // function can invalidate cached cachelines on other caches and computer cores,
  115. // incorrect usage of this function can have an even greater negative impact
  116. // than incorrect regular prefetches.
  117. // Use this function only when representative benchmarks show an improvement.
  118. //
  119. // Example:
  120. //
  121. // void* Arena::Allocate(size_t size) {
  122. // void* ptr = AllocateBlock(size);
  123. // y_absl::PrefetchToLocalCacheForWrite(p);
  124. // return ptr;
  125. // }
  126. //
  127. void PrefetchToLocalCacheForWrite(const void* addr);
  128. #if Y_ABSL_HAVE_BUILTIN(__builtin_prefetch) || defined(__GNUC__)
  129. #define Y_ABSL_HAVE_PREFETCH 1
  130. // See __builtin_prefetch:
  131. // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html.
  132. //
  133. inline void PrefetchToLocalCache(const void* addr) {
  134. __builtin_prefetch(addr, 0, 3);
  135. }
  136. inline void PrefetchToLocalCacheNta(const void* addr) {
  137. __builtin_prefetch(addr, 0, 0);
  138. }
  139. inline void PrefetchToLocalCacheForWrite(const void* addr) {
  140. // [x86] gcc/clang don't generate PREFETCHW for __builtin_prefetch(.., 1)
  141. // unless -march=broadwell or newer; this is not generally the default, so we
  142. // manually emit prefetchw. PREFETCHW is recognized as a no-op on older Intel
  143. // processors and has been present on AMD processors since the K6-2.
  144. #if defined(__x86_64__)
  145. asm("prefetchw (%0)" : : "r"(addr));
  146. #else
  147. __builtin_prefetch(addr, 1, 3);
  148. #endif
  149. }
  150. #elif defined(Y_ABSL_INTERNAL_HAVE_SSE)
  151. #define Y_ABSL_HAVE_PREFETCH 1
  152. inline void PrefetchToLocalCache(const void* addr) {
  153. _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_T0);
  154. }
  155. inline void PrefetchToLocalCacheNta(const void* addr) {
  156. _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_NTA);
  157. }
  158. inline void PrefetchToLocalCacheForWrite(const void* addr) {
  159. #if defined(_MM_HINT_ET0)
  160. _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_ET0);
  161. #elif !defined(_MSC_VER) && defined(__x86_64__)
  162. // _MM_HINT_ET0 is not universally supported. As we commented further
  163. // up, PREFETCHW is recognized as a no-op on older Intel processors
  164. // and has been present on AMD processors since the K6-2. We have this
  165. // disabled for MSVC compilers as this miscompiles on older MSVC compilers.
  166. asm("prefetchw (%0)" : : "r"(addr));
  167. #endif
  168. }
  169. #else
  170. inline void PrefetchToLocalCache(const void* addr) {}
  171. inline void PrefetchToLocalCacheNta(const void* addr) {}
  172. inline void PrefetchToLocalCacheForWrite(const void* addr) {}
  173. #endif
  174. Y_ABSL_NAMESPACE_END
  175. } // namespace y_absl
  176. #endif // Y_ABSL_BASE_PREFETCH_H_