Skip to content

Conversation

@vedantjk
Copy link

@vedantjk vedantjk commented Sep 2, 2025

Enable constexpr usage of vector bitcast intrinsics by changing from __DEFAULT_FN_ATTRS* to
__DEFAULT_FN_ATTRS*_CONSTEXPR attributes.

Updated intrinsics:

  • AVX-512 bitcast intrinsics: _mm512_castpd_ps, _mm512_castps_pd, _mm512_cast*_si512,
    _mm512_castsi512_*
  • AVX-512 truncation casts: _mm512_cast*512_*128/256
  • AVX-512 FP16 bitcast intrinsics: all _mm*_castph_* and _mm*_cast*_ph functions
  • AVX-512 FP16 truncation casts: _mm*_castph*_ph*

Note: Extension casts (e.g., _mm512_castpd128_pd512) remain non-constexpr as they use
__builtin_nondeterministic_value, which cannot be used in constexpr contexts.

Fixes #156348

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Sep 2, 2025
@github-actions
Copy link

github-actions bot commented Sep 2, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 2, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: None (vedantjk)

Changes

Enable constexpr usage of vector bitcast intrinsics by changing from __DEFAULT_FN_ATTRS* to
__DEFAULT_FN_ATTRS*_CONSTEXPR attributes.

Updated intrinsics:

  • AVX-512 bitcast intrinsics: _mm512_castpd_ps, _mm512_castps_pd, _mm512_cast*_si512,
    _mm512_castsi512_*
  • AVX-512 truncation casts: _mm512_cast*512_*128/256
  • AVX-512 FP16 bitcast intrinsics: all _mm*_castph_* and _mm*_cast*_ph functions
  • AVX-512 FP16 truncation casts: _mm*_castph*_ph*

Note: Extension casts (e.g., _mm512_castpd128_pd512) remain non-constexpr as they use
__builtin_nondeterministic_value, which cannot be used in constexpr contexts.


Full diff: https://github.com/llvm/llvm-project/pull/156381.diff

2 Files Affected:

  • (modified) clang/lib/Headers/avx512fintrin.h (+10-10)
  • (modified) clang/lib/Headers/avx512fp16intrin.h (+21-21)
diff --git a/clang/lib/Headers/avx512fintrin.h b/clang/lib/Headers/avx512fintrin.h
index 0006e334022b6..317891dfb8c08 100644
--- a/clang/lib/Headers/avx512fintrin.h
+++ b/clang/lib/Headers/avx512fintrin.h
@@ -408,19 +408,19 @@ _mm512_castps256_ps512(__m256 __a)
                                  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
 }
 
-static __inline __m128d __DEFAULT_FN_ATTRS512
+static __inline __m128d __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castpd512_pd128(__m512d __a)
 {
   return __builtin_shufflevector(__a, __a, 0, 1);
 }
 
-static __inline __m256d __DEFAULT_FN_ATTRS512
+static __inline __m256d __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castpd512_pd256 (__m512d __A)
 {
   return __builtin_shufflevector(__A, __A, 0, 1, 2, 3);
 }
 
-static __inline __m128 __DEFAULT_FN_ATTRS512
+static __inline __m128 __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castps512_ps128(__m512 __a)
 {
   return __builtin_shufflevector(__a, __a, 0, 1, 2, 3);
@@ -431,13 +431,13 @@ _mm512_castps512_ps256(__m512 __A) {
   return __builtin_shufflevector(__A, __A, 0, 1, 2, 3, 4, 5, 6, 7);
 }
 
-static __inline __m512 __DEFAULT_FN_ATTRS512
+static __inline __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castpd_ps (__m512d __A)
 {
   return (__m512) (__A);
 }
 
-static __inline __m512i __DEFAULT_FN_ATTRS512
+static __inline __m512i __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castpd_si512 (__m512d __A)
 {
   return (__m512i) (__A);
@@ -452,13 +452,13 @@ _mm512_castpd128_pd512 (__m128d __A)
       __B, 0, 1, 2, 3, 4, 5, 6, 7);
 }
 
-static __inline __m512d __DEFAULT_FN_ATTRS512
+static __inline __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castps_pd (__m512 __A)
 {
   return (__m512d) (__A);
 }
 
-static __inline __m512i __DEFAULT_FN_ATTRS512
+static __inline __m512i __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castps_si512 (__m512 __A)
 {
   return (__m512i) (__A);
@@ -488,19 +488,19 @@ _mm512_castsi256_si512 (__m256i __A)
    return  __builtin_shufflevector( __A, __builtin_nondeterministic_value(__A), 0, 1, 2, 3, 4, 5, 6, 7);
 }
 
-static __inline __m512 __DEFAULT_FN_ATTRS512
+static __inline __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castsi512_ps (__m512i __A)
 {
   return (__m512) (__A);
 }
 
-static __inline __m512d __DEFAULT_FN_ATTRS512
+static __inline __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castsi512_pd (__m512i __A)
 {
   return (__m512d) (__A);
 }
 
-static __inline __m128i __DEFAULT_FN_ATTRS512
+static __inline __m128i __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castsi512_si128 (__m512i __A)
 {
   return (__m128i)__builtin_shufflevector(__A, __A , 0, 1);
diff --git a/clang/lib/Headers/avx512fp16intrin.h b/clang/lib/Headers/avx512fp16intrin.h
index 6989b86a7b68c..b8145613d06fe 100644
--- a/clang/lib/Headers/avx512fp16intrin.h
+++ b/clang/lib/Headers/avx512fp16intrin.h
@@ -113,93 +113,93 @@ _mm512_set1_pch(_Float16 _Complex __h) {
   return (__m512h)_mm512_set1_ps(__builtin_bit_cast(float, __h));
 }
 
-static __inline__ __m128 __DEFAULT_FN_ATTRS128 _mm_castph_ps(__m128h __a) {
+static __inline__ __m128 __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_ps(__m128h __a) {
   return (__m128)__a;
 }
 
-static __inline__ __m256 __DEFAULT_FN_ATTRS256 _mm256_castph_ps(__m256h __a) {
+static __inline__ __m256 __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castph_ps(__m256h __a) {
   return (__m256)__a;
 }
 
-static __inline__ __m512 __DEFAULT_FN_ATTRS512 _mm512_castph_ps(__m512h __a) {
+static __inline__ __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castph_ps(__m512h __a) {
   return (__m512)__a;
 }
 
-static __inline__ __m128d __DEFAULT_FN_ATTRS128 _mm_castph_pd(__m128h __a) {
+static __inline__ __m128d __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_pd(__m128h __a) {
   return (__m128d)__a;
 }
 
-static __inline__ __m256d __DEFAULT_FN_ATTRS256 _mm256_castph_pd(__m256h __a) {
+static __inline__ __m256d __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castph_pd(__m256h __a) {
   return (__m256d)__a;
 }
 
-static __inline__ __m512d __DEFAULT_FN_ATTRS512 _mm512_castph_pd(__m512h __a) {
+static __inline__ __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castph_pd(__m512h __a) {
   return (__m512d)__a;
 }
 
-static __inline__ __m128i __DEFAULT_FN_ATTRS128 _mm_castph_si128(__m128h __a) {
+static __inline__ __m128i __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_si128(__m128h __a) {
   return (__m128i)__a;
 }
 
-static __inline__ __m256i __DEFAULT_FN_ATTRS256
+static __inline__ __m256i __DEFAULT_FN_ATTRS256_CONSTEXPR
 _mm256_castph_si256(__m256h __a) {
   return (__m256i)__a;
 }
 
-static __inline__ __m512i __DEFAULT_FN_ATTRS512
+static __inline__ __m512i __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castph_si512(__m512h __a) {
   return (__m512i)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128 _mm_castps_ph(__m128 __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castps_ph(__m128 __a) {
   return (__m128h)__a;
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS256 _mm256_castps_ph(__m256 __a) {
+static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castps_ph(__m256 __a) {
   return (__m256h)__a;
 }
 
-static __inline__ __m512h __DEFAULT_FN_ATTRS512 _mm512_castps_ph(__m512 __a) {
+static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castps_ph(__m512 __a) {
   return (__m512h)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128 _mm_castpd_ph(__m128d __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castpd_ph(__m128d __a) {
   return (__m128h)__a;
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS256 _mm256_castpd_ph(__m256d __a) {
+static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castpd_ph(__m256d __a) {
   return (__m256h)__a;
 }
 
-static __inline__ __m512h __DEFAULT_FN_ATTRS512 _mm512_castpd_ph(__m512d __a) {
+static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castpd_ph(__m512d __a) {
   return (__m512h)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128 _mm_castsi128_ph(__m128i __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castsi128_ph(__m128i __a) {
   return (__m128h)__a;
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS256
+static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR
 _mm256_castsi256_ph(__m256i __a) {
   return (__m256h)__a;
 }
 
-static __inline__ __m512h __DEFAULT_FN_ATTRS512
+static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castsi512_ph(__m512i __a) {
   return (__m512h)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS256
+static __inline__ __m128h __DEFAULT_FN_ATTRS256_CONSTEXPR
 _mm256_castph256_ph128(__m256h __a) {
   return __builtin_shufflevector(__a, __a, 0, 1, 2, 3, 4, 5, 6, 7);
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS512
+static __inline__ __m128h __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castph512_ph128(__m512h __a) {
   return __builtin_shufflevector(__a, __a, 0, 1, 2, 3, 4, 5, 6, 7);
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS512
+static __inline__ __m256h __DEFAULT_FN_ATTRS512_CONSTEXPR
 _mm512_castph512_ph256(__m512h __a) {
   return __builtin_shufflevector(__a, __a, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
                                  12, 13, 14, 15);

Copy link
Member

@ckoparkar ckoparkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

static __inline__ __m128 __DEFAULT_FN_ATTRS128 _mm_castph_ps(__m128h __a) {
static __inline__ __m128 __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_ps(__m128h __a) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please run clang-format on this patch? Some lines here are longer than 80 chars so the code formatter check will likely fail.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do and raise a new revision.

@RKSimon RKSimon self-requested a review September 2, 2025 07:09
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - the next step is to add suitable test coverage to the relevant builtins file like we have in the other x86 constexpr patches e.g. #152435

@github-actions
Copy link

github-actions bot commented Sep 2, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h -- clang/lib/Headers/avx512fintrin.h clang/lib/Headers/avx512fp16intrin.h

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/Headers/avx512fintrin.h b/clang/lib/Headers/avx512fintrin.h
index 317891dfb..3b65ef4a3 100644
--- a/clang/lib/Headers/avx512fintrin.h
+++ b/clang/lib/Headers/avx512fintrin.h
@@ -409,20 +409,17 @@ _mm512_castps256_ps512(__m256 __a)
 }
 
 static __inline __m128d __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castpd512_pd128(__m512d __a)
-{
+_mm512_castpd512_pd128(__m512d __a) {
   return __builtin_shufflevector(__a, __a, 0, 1);
 }
 
 static __inline __m256d __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castpd512_pd256 (__m512d __A)
-{
+_mm512_castpd512_pd256(__m512d __A) {
   return __builtin_shufflevector(__A, __A, 0, 1, 2, 3);
 }
 
 static __inline __m128 __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castps512_ps128(__m512 __a)
-{
+_mm512_castps512_ps128(__m512 __a) {
   return __builtin_shufflevector(__a, __a, 0, 1, 2, 3);
 }
 
@@ -432,14 +429,12 @@ _mm512_castps512_ps256(__m512 __A) {
 }
 
 static __inline __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castpd_ps (__m512d __A)
-{
+_mm512_castpd_ps(__m512d __A) {
   return (__m512) (__A);
 }
 
 static __inline __m512i __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castpd_si512 (__m512d __A)
-{
+_mm512_castpd_si512(__m512d __A) {
   return (__m512i) (__A);
 }
 
@@ -453,14 +448,12 @@ _mm512_castpd128_pd512 (__m128d __A)
 }
 
 static __inline __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castps_pd (__m512 __A)
-{
+_mm512_castps_pd(__m512 __A) {
   return (__m512d) (__A);
 }
 
 static __inline __m512i __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castps_si512 (__m512 __A)
-{
+_mm512_castps_si512(__m512 __A) {
   return (__m512i) (__A);
 }
 
@@ -489,20 +482,17 @@ _mm512_castsi256_si512 (__m256i __A)
 }
 
 static __inline __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castsi512_ps (__m512i __A)
-{
+_mm512_castsi512_ps(__m512i __A) {
   return (__m512) (__A);
 }
 
 static __inline __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castsi512_pd (__m512i __A)
-{
+_mm512_castsi512_pd(__m512i __A) {
   return (__m512d) (__A);
 }
 
 static __inline __m128i __DEFAULT_FN_ATTRS512_CONSTEXPR
-_mm512_castsi512_si128 (__m512i __A)
-{
+_mm512_castsi512_si128(__m512i __A) {
   return (__m128i)__builtin_shufflevector(__A, __A , 0, 1);
 }
 
diff --git a/clang/lib/Headers/avx512fp16intrin.h b/clang/lib/Headers/avx512fp16intrin.h
index b8145613d..cb053768b 100644
--- a/clang/lib/Headers/avx512fp16intrin.h
+++ b/clang/lib/Headers/avx512fp16intrin.h
@@ -113,31 +113,38 @@ _mm512_set1_pch(_Float16 _Complex __h) {
   return (__m512h)_mm512_set1_ps(__builtin_bit_cast(float, __h));
 }
 
-static __inline__ __m128 __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_ps(__m128h __a) {
+static __inline__ __m128 __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castph_ps(__m128h __a) {
   return (__m128)__a;
 }
 
-static __inline__ __m256 __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castph_ps(__m256h __a) {
+static __inline__ __m256 __DEFAULT_FN_ATTRS256_CONSTEXPR
+_mm256_castph_ps(__m256h __a) {
   return (__m256)__a;
 }
 
-static __inline__ __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castph_ps(__m512h __a) {
+static __inline__ __m512 __DEFAULT_FN_ATTRS512_CONSTEXPR
+_mm512_castph_ps(__m512h __a) {
   return (__m512)__a;
 }
 
-static __inline__ __m128d __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_pd(__m128h __a) {
+static __inline__ __m128d __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castph_pd(__m128h __a) {
   return (__m128d)__a;
 }
 
-static __inline__ __m256d __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castph_pd(__m256h __a) {
+static __inline__ __m256d __DEFAULT_FN_ATTRS256_CONSTEXPR
+_mm256_castph_pd(__m256h __a) {
   return (__m256d)__a;
 }
 
-static __inline__ __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castph_pd(__m512h __a) {
+static __inline__ __m512d __DEFAULT_FN_ATTRS512_CONSTEXPR
+_mm512_castph_pd(__m512h __a) {
   return (__m512d)__a;
 }
 
-static __inline__ __m128i __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castph_si128(__m128h __a) {
+static __inline__ __m128i __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castph_si128(__m128h __a) {
   return (__m128i)__a;
 }
 
@@ -151,31 +158,38 @@ _mm512_castph_si512(__m512h __a) {
   return (__m512i)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castps_ph(__m128 __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castps_ph(__m128 __a) {
   return (__m128h)__a;
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castps_ph(__m256 __a) {
+static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR
+_mm256_castps_ph(__m256 __a) {
   return (__m256h)__a;
 }
 
-static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castps_ph(__m512 __a) {
+static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR
+_mm512_castps_ph(__m512 __a) {
   return (__m512h)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castpd_ph(__m128d __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castpd_ph(__m128d __a) {
   return (__m128h)__a;
 }
 
-static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR _mm256_castpd_ph(__m256d __a) {
+static __inline__ __m256h __DEFAULT_FN_ATTRS256_CONSTEXPR
+_mm256_castpd_ph(__m256d __a) {
   return (__m256h)__a;
 }
 
-static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR _mm512_castpd_ph(__m512d __a) {
+static __inline__ __m512h __DEFAULT_FN_ATTRS512_CONSTEXPR
+_mm512_castpd_ph(__m512d __a) {
   return (__m512h)__a;
 }
 
-static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR _mm_castsi128_ph(__m128i __a) {
+static __inline__ __m128h __DEFAULT_FN_ATTRS128_CONSTEXPR
+_mm_castsi128_ph(__m128i __a) {
   return (__m128h)__a;
 }
 

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test coverage?

@RKSimon RKSimon closed this Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Headers][X86] Allow vector bitcast intrinsics to be used in constexpr

4 participants