Skip to content

Commit a3948ba

Browse files
committed
Fixing invalid prefetch pointers
This fix was originally authored by @yairgott. Also enabling prefetch instructions in ASAN/UBSAN tests because they would not fail anymore with prefetch enabled with this fix.
1 parent 449334d commit a3948ba

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ if(HNSWLIB_EXAMPLES)
197197
if(ENABLE_MSAN)
198198
add_cxx_flags(-fsanitize=memory)
199199
endif()
200-
if(ENABLE_ASAN OR ENABLE_UBSAN)
201-
add_cxx_flags(-DHNSWLIB_USE_PREFETCH=0)
202-
endif()
203200

204201
add_cxx_flags(-Wall -Wextra -Wpedantic -Werror)
205202

hnswlib/hnswalg.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,10 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
283283
// if (candidate_id == 0) continue;
284284
#ifdef USE_SSE
285285
#if HNSWLIB_USE_PREFETCH
286-
_mm_prefetch((char *) (visited_array + *(datal + j + 1)), _MM_HINT_T0);
287-
_mm_prefetch(getDataByInternalId(*(datal + j + 1)), _MM_HINT_T0);
286+
if (j + 1 < size) {
287+
_mm_prefetch((char *) (visited_array + *(datal + j + 1)), _MM_HINT_T0);
288+
_mm_prefetch(getDataByInternalId(*(datal + j + 1)), _MM_HINT_T0);
289+
}
288290
#endif
289291
#endif
290292
if (visited_array[candidate_id] == visited_array_tag) continue;
@@ -393,9 +395,11 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
393395
// if (candidate_id == 0) continue;
394396
#ifdef USE_SSE
395397
#if HNSWLIB_USE_PREFETCH
396-
_mm_prefetch((char *) (visited_array + *(data + j + 1)), _MM_HINT_T0);
397-
_mm_prefetch(data_level0_memory_ + (*(data + j + 1)) * size_data_per_element_ + offsetData_,
398-
_MM_HINT_T0); ////////////
398+
if (j + 1 < size) {
399+
_mm_prefetch((char *) (visited_array + *(data + j + 1)), _MM_HINT_T0);
400+
_mm_prefetch(data_level0_memory_ + (*(data + j + 1)) * size_data_per_element_ + offsetData_,
401+
_MM_HINT_T0);
402+
}
399403
#endif
400404
#endif
401405
if (!(visited_array[candidate_id] == visited_array_tag)) {
@@ -1126,7 +1130,9 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
11261130
for (int i = 0; i < size; i++) {
11271131
#ifdef USE_SSE
11281132
#if HNSWLIB_USE_PREFETCH
1129-
_mm_prefetch(getDataByInternalId(*(datal + i + 1)), _MM_HINT_T0);
1133+
if (i + 1 < size) {
1134+
_mm_prefetch(getDataByInternalId(*(datal + i + 1)), _MM_HINT_T0);
1135+
}
11301136
#endif
11311137
#endif
11321138
tableint cand = datal[i];

0 commit comments

Comments
 (0)