[clang] [Win/X86] Make _m_prefetch[w] builtins to avoid winnt.h conflicts (PR #115099)

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 8 11:55:14 PST 2024


rnk wrote:

We can adjust the rules around language linkage if we like, but the main reason we implement builtins this way is to support the MSVC intrinsic model, which is to declare extern "C" functions and mark them with `#pragma intrinsic(NAME)` like so:
```
extern "C" void _m_prefetchw(volatile const void *src);
#pragma intrinsic(_m_prefetchw)
// now it works like a builtin
```

winnt.h provides macros that do stuff like this without including our intrinsic headers, so anything they mention this way has to get implemented as a compiler built-in, otherwise users experience surprising linker errors like "_m[m]_prefetch symbol not defined".

The Clang project policy is to be MSVC-compatible enough to compile the system headers. Reimplementing the entire Intel intrinsic API as builtins is out of scope. Any non-system, user code using this mechanism to call Intel vector intrinsics should be updated to include immintrin.h instead.

As a side benefit, this is also good for compile time, since the immintrin.h header is a giant umbrella header that's very bad for compile time. See also [intrin0.h](https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/intrin0.h), which the MSVC STL uses as a compile time optimization.

https://github.com/llvm/llvm-project/pull/115099


More information about the cfe-commits mailing list