scipy: BUG: Wrong use of `__builtin_prefetch()`

Describe your issue.

This code is wrong: https://github.com/scipy/scipy/blob/6a918e90245f0c456cb2ddd73e9e8d34e44c4259/scipy/spatial/ckdtree/src/query.cxx#L295

The 3rd argument of __builtin_prefetch() should be either 0, 1, 2 or 3 not m. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fprefetch

When this code was rewritten in https://github.com/scipy/scipy/commit/6975656966bd526570af5616096487e10921f163 by @rainwoodman to use NPY_PREFETCH(), the previous code used m to fill a cache line. However, __builtin_prefetch() always reads a full cache line and the third argument is the expected level of locality. Values larger than 3 are invalid but they are possibly interpreted as 3. But the real bug is that the locality of the prefetch function depends on m which is the dimension of the points!

This line: https://github.com/scipy/scipy/blob/6a918e90245f0c456cb2ddd73e9e8d34e44c4259/scipy/spatial/ckdtree/src/ckdtree_decl.h#L12C1-L12C63

should simply be:

#define CKDTREE_PREFETCH(x)  NPY_PREFETCH(x,0, 3)

Reproducing Code Example

NA

Error message

NA

SciPy/NumPy/Python version and system information

NA

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 25 (21 by maintainers)

Most upvoted comments

Cool! Out of curiosity, how did you find this? Static analyzer or just reading the source?

Just reading the source. I wanted to create a stand-alone version of the code that I could use in a project of mine.