Skip to content

Commit

Permalink
[clang] Add intrin0.h header to mimic intrin0.h used by MSVC STL …
Browse files Browse the repository at this point in the history
…for clang-cl (llvm#75711)

Fixes llvm#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.
  • Loading branch information
MaxEW707 authored and chencha3 committed Mar 22, 2024
1 parent 9f9b933 commit d650183
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 413 deletions.
20 changes: 20 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,26 @@ Android Support
Windows Support
^^^^^^^^^^^^^^^

- Clang-cl now supports function targets with intrinsic headers. This allows
for runtime feature detection of intrinsics. Previously under clang-cl
``immintrin.h`` and similar intrinsic headers would only include the intrinsics
if building with that feature enabled at compile time, e.g. ``avxintrin.h``
would only be included if AVX was enabled at compile time. This was done to work
around include times from MSVC STL including ``intrin.h`` under clang-cl.
Clang-cl now provides ``intrin0.h`` for MSVC STL and therefore all intrinsic
features without requiring enablement at compile time.
Fixes: (`#53520 <https://github.com/llvm/llvm-project/issues/53520>`_)

- Improved compile times with MSVC STL. MSVC provides ``intrin0.h`` which is a
header that only includes intrinsics that are used by MSVC STL to avoid the
use of ``intrin.h``. MSVC STL when compiled under clang uses ``intrin.h``
instead. Clang-cl now provides ``intrin0.h`` for the same compiler throughput
purposes as MSVC. Clang-cl also provides ``yvals_core.h`` to redefine
``_STL_INTRIN_HEADER`` to expand to ``intrin0.h`` instead of ``intrin.h``.
This also means that if all intrinsic features are enabled at compile time
including STL headers will no longer slow down compile times since ``intrin.h``
is not included from MSVC STL.

LoongArch Support
^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ set(x86_files
)

set(windows_only_files
intrin0.h
intrin.h
vadefs.h
yvals_core.h
)

set(utility_files
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/Headers/bmiintrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ _mm_tzcnt_64(unsigned long long __X)

#undef __RELAXED_FN_ATTRS

#if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
defined(__BMI__)
#if !defined(__SCE__) || __has_feature(modules) || defined(__BMI__)

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

#undef __DEFAULT_FN_ATTRS

#endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
|| defined(__BMI__) */
#endif /* !defined(__SCE__) || __has_feature(modules) || defined(__BMI__) */

#endif /* __BMIINTRIN_H */
Loading

0 comments on commit d650183

Please sign in to comment.