Skip to content

Commit afec08e

Browse files
authored
[clang] Add intrin0.h header to mimic intrin0.h used by MSVC STL for clang-cl (#75711)
Fixes #53520. #### Description #### Provide `intrin0.h` to be the minimal set of intrinsics that the MSVC STL requires. The `intrin0.h` header matches the latest header provided by MSVC 1939 which does include some extra intrinsics that the MSVC STL does not use. Inside `BuiltinHeaders.def` I kept the header description as `intrin.h`. If you want me to change those to `intrin0.h` for the moved intrinsics let me know. This should now allow `immintrin.h` to be used with function targets for runtime cpu detection of simd instruction sets without worrying about the compile-time overhead from MSVC STL including `intrin.h` on clang. I still need to figure out how to best update MSVC STL to detect for the presence of `intrin0.h` from clang and to use this header over `intrin.h`. #### Testing #### Built clang locally and ran the test suite. I still need to do a pass over the existing unit tests for the ms intrinsics to make sure there aren't any gaps. Wanted to get this PR up for discussion first. Modified latest MSVC STL from github to point to `intrin0.h` for clang. Wrote some test files that included MSVC STL headers that rely on intrinsics such as `atomic`, `bit` and `vector`. Built the unit tests against x86, arm, aarch64, and x64. #### Benchmarks #### The following include times are based on the x64 target with the modified headers in this PR. These timings were done by using `clang-cl.exe -ftime-trace` and taking the wall time for parsing `intrin.h` and `intrin0.h`. `intrin.h` takes ~897ms to parse. `intrin0.h` takes ~1ms to parse. If there is anything required or a different approach is preferred let me know. I would very much like to move this over the finish line so we can use function targets with clang-cl.
1 parent 08701e3 commit afec08e

10 files changed

+408
-413
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ Android Support
454454
Windows Support
455455
^^^^^^^^^^^^^^^
456456

457+
- Clang-cl now supports function targets with intrinsic headers. This allows
458+
for runtime feature detection of intrinsics. Previously under clang-cl
459+
``immintrin.h`` and similar intrinsic headers would only include the intrinsics
460+
if building with that feature enabled at compile time, e.g. ``avxintrin.h``
461+
would only be included if AVX was enabled at compile time. This was done to work
462+
around include times from MSVC STL including ``intrin.h`` under clang-cl.
463+
Clang-cl now provides ``intrin0.h`` for MSVC STL and therefore all intrinsic
464+
features without requiring enablement at compile time.
465+
Fixes: (`#53520 <https://github.com/llvm/llvm-project/issues/53520>`_)
466+
467+
- Improved compile times with MSVC STL. MSVC provides ``intrin0.h`` which is a
468+
header that only includes intrinsics that are used by MSVC STL to avoid the
469+
use of ``intrin.h``. MSVC STL when compiled under clang uses ``intrin.h``
470+
instead. Clang-cl now provides ``intrin0.h`` for the same compiler throughput
471+
purposes as MSVC. Clang-cl also provides ``yvals_core.h`` to redefine
472+
``_STL_INTRIN_HEADER`` to expand to ``intrin0.h`` instead of ``intrin.h``.
473+
This also means that if all intrinsic features are enabled at compile time
474+
including STL headers will no longer slow down compile times since ``intrin.h``
475+
is not included from MSVC STL.
476+
457477
LoongArch Support
458478
^^^^^^^^^^^^^^^^^
459479

clang/lib/Headers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,10 @@ set(x86_files
254254
)
255255

256256
set(windows_only_files
257+
intrin0.h
257258
intrin.h
258259
vadefs.h
260+
yvals_core.h
259261
)
260262

261263
set(utility_files

clang/lib/Headers/bmiintrin.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ _mm_tzcnt_64(unsigned long long __X)
161161

162162
#undef __RELAXED_FN_ATTRS
163163

164-
#if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
165-
defined(__BMI__)
164+
#if !defined(__SCE__) || __has_feature(modules) || defined(__BMI__)
166165

167166
/* Define the default attributes for the functions in this file. */
168167
#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
@@ -610,7 +609,6 @@ __blsr_u64(unsigned long long __X)
610609

611610
#undef __DEFAULT_FN_ATTRS
612611

613-
#endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
614-
|| defined(__BMI__) */
612+
#endif /* !defined(__SCE__) || __has_feature(modules) || defined(__BMI__) */
615613

616614
#endif /* __BMIINTRIN_H */

0 commit comments

Comments
 (0)