Skip to content

Conversation

@0xzre
Copy link

@0xzre 0xzre commented Nov 15, 2025

Fixes #167681

…allow VALIGND/Q element shift intrinsics to be used in constexpr
@github-actions
Copy link

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 llvmbot added clang Clang issues not falling into any other category backend:X86 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Nov 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: Muhammad Abdul (0xzre)

Changes

Fixes #167681


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

4 Files Affected:

  • (modified) clang/include/clang/Basic/BuiltinsX86.td (+5-5)
  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+21)
  • (modified) clang/lib/AST/ExprConstant.cpp (+24)
  • (added) clang/test/AST/ByteCode/x86-valign-builtins.cpp (+53)
diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index 69d18679fd6ec..aad0361ba9a8b 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -1072,24 +1072,24 @@ let Features = "avx512f", Attributes = [NoThrow, RequiredVectorWidth<512>] in {
   def storeaps512_mask : X86Builtin<"void(_Vector<16, float *>, _Vector<16, float>, unsigned short)">;
 }
 
-let Features = "avx512f", Attributes = [NoThrow, Const, RequiredVectorWidth<512>] in {
+let Features = "avx512f", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<512>] in {
   def alignq512 : X86Builtin<"_Vector<8, long long int>(_Vector<8, long long int>, _Vector<8, long long int>, _Constant int)">;
   def alignd512 : X86Builtin<"_Vector<16, int>(_Vector<16, int>, _Vector<16, int>, _Constant int)">;
 }
 
-let Features = "avx512vl", Attributes = [NoThrow, Const, RequiredVectorWidth<128>] in {
+let Features = "avx512vl", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<128>] in {
   def alignd128 : X86Builtin<"_Vector<4, int>(_Vector<4, int>, _Vector<4, int>, _Constant int)">;
 }
 
-let Features = "avx512vl", Attributes = [NoThrow, Const, RequiredVectorWidth<256>] in {
+let Features = "avx512vl", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<256>] in {
   def alignd256 : X86Builtin<"_Vector<8, int>(_Vector<8, int>, _Vector<8, int>, _Constant int)">;
 }
 
-let Features = "avx512vl", Attributes = [NoThrow, Const, RequiredVectorWidth<128>] in {
+let Features = "avx512vl", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<128>] in {
   def alignq128 : X86Builtin<"_Vector<2, long long int>(_Vector<2, long long int>, _Vector<2, long long int>, _Constant int)">;
 }
 
-let Features = "avx512vl", Attributes = [NoThrow, Const, RequiredVectorWidth<256>] in {
+let Features = "avx512vl", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<256>] in {
   def alignq256 : X86Builtin<"_Vector<4, long long int>(_Vector<4, long long int>, _Vector<4, long long int>, _Constant int)">;
 }
 
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index cee3c1b8cf8f3..7c08475065d9d 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -4774,6 +4774,27 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
           return std::pair<unsigned, int>{VecIdx, ElemIdx};
         });
 
+  case X86::BI__builtin_ia32_alignd128:
+  case X86::BI__builtin_ia32_alignd256:
+  case X86::BI__builtin_ia32_alignd512:
+  case X86::BI__builtin_ia32_alignq128:
+  case X86::BI__builtin_ia32_alignq256:
+  case X86::BI__builtin_ia32_alignq512: {
+    const unsigned NumElts =
+        Call->getType()->castAs<VectorType>()->getNumElements();
+    return interp__builtin_ia32_shuffle_generic(
+        S, OpPC, Call, [NumElts](unsigned DstIdx, unsigned Shift) {
+          unsigned Imm = Shift & 0xFF;
+          unsigned EffectiveShift = Imm & (NumElts - 1);
+          unsigned SourcePos = DstIdx + EffectiveShift;
+          unsigned VecIdx = SourcePos < NumElts ? 1u : 0u;
+          unsigned ElemIdx =
+              SourcePos < NumElts ? SourcePos : SourcePos - NumElts;
+          return std::pair<unsigned, int>{VecIdx,
+                                          static_cast<int>(ElemIdx)};
+        });
+  }
+
   default:
     S.FFDiag(S.Current->getLocation(OpPC),
              diag::note_invalid_subexpr_in_const_expr)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 29357eec2eeb6..b3f17da8e1158 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13551,6 +13551,30 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
       return false;
     return Success(R, E);
   }
+  case X86::BI__builtin_ia32_alignd128:
+  case X86::BI__builtin_ia32_alignd256:
+  case X86::BI__builtin_ia32_alignd512:
+  case X86::BI__builtin_ia32_alignq128:
+  case X86::BI__builtin_ia32_alignq256:
+  case X86::BI__builtin_ia32_alignq512: {
+    APValue R;
+    const unsigned NumElts =
+        E->getType()->castAs<VectorType>()->getNumElements();
+    if (!evalShuffleGeneric(
+            Info, E, R, [NumElts](unsigned DstIdx, unsigned Shift) {
+              unsigned Imm = Shift & 0xFF;
+              unsigned EffectiveShift = Imm & (NumElts - 1);
+              unsigned SourcePos = DstIdx + EffectiveShift;
+              unsigned VecIdx = SourcePos < NumElts ? 1 : 0;
+              unsigned ElemIdx =
+                  SourcePos < NumElts ? SourcePos : SourcePos - NumElts;
+
+              return std::pair<unsigned, int>{VecIdx,
+                                              static_cast<int>(ElemIdx)};
+            }))
+      return false;
+    return Success(R, E);
+  }
   case X86::BI__builtin_ia32_permvarsi256:
   case X86::BI__builtin_ia32_permvarsf256:
   case X86::BI__builtin_ia32_permvardf512:
diff --git a/clang/test/AST/ByteCode/x86-valign-builtins.cpp b/clang/test/AST/ByteCode/x86-valign-builtins.cpp
new file mode 100644
index 0000000000000..a54e72fab90ce
--- /dev/null
+++ b/clang/test/AST/ByteCode/x86-valign-builtins.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown -target-feature +avx512f -target-feature +avx512vl -target-feature +avx512dq -verify=expected -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown -target-feature +avx512f -target-feature +avx512vl -target-feature +avx512dq -verify=ref %s
+
+// expected-no-diagnostics
+// ref-no-diagnostics
+
+#define __MM_MALLOC_H
+#include <immintrin.h>
+
+using v4si = int __attribute__((vector_size(16)));
+using v8si = int __attribute__((vector_size(32)));
+using v16si = int __attribute__((vector_size(64)));
+using v4di = long long __attribute__((vector_size(32)));
+
+constexpr v4si test_alignr_epi32_128() {
+  v4si A = {100, 200, 300, 400};
+  v4si B = {10, 20, 30, 40};
+  return (v4si)_mm_alignr_epi32((__m128i)A, (__m128i)B, 1);
+}
+
+constexpr v8si test_alignr_epi32_256() {
+  v8si A = {100, 200, 300, 400, 500, 600, 700, 800};
+  v8si B = {1, 2, 3, 4, 5, 6, 7, 8};
+  return (v8si)_mm256_alignr_epi32((__m256i)A, (__m256i)B, 3);
+}
+
+constexpr v16si test_alignr_epi32_512_wrap() {
+  v16si A = {100, 200, 300, 400, 500, 600, 700, 800,
+             900, 1000, 1100, 1200, 1300, 1400, 1500, 1600};
+  v16si B = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
+  return (v16si)_mm512_alignr_epi32((__m512i)A, (__m512i)B, 19);
+}
+
+constexpr v4di test_alignr_epi64_256() {
+  v4di A = {10, 11, 12, 13};
+  v4di B = {1, 2, 3, 4};
+  return (v4di)_mm256_alignr_epi64((__m256i)A, (__m256i)B, 2);
+}
+
+constexpr v4si R128 = test_alignr_epi32_128();
+static_assert(R128[0] == 20 && R128[1] == 30 && R128[2] == 40 && R128[3] == 100);
+
+constexpr v8si R256 = test_alignr_epi32_256();
+static_assert(R256[0] == 4 && R256[1] == 5 && R256[2] == 6 && R256[3] == 7);
+static_assert(R256[4] == 8 && R256[5] == 100 && R256[6] == 200 && R256[7] == 300);
+
+constexpr v16si R512 = test_alignr_epi32_512_wrap();
+static_assert(R512[0] == 3 && R512[1] == 4 && R512[2] == 5 && R512[3] == 6);
+static_assert(R512[8] == 11 && R512[9] == 12 && R512[10] == 13 && R512[11] == 14);
+static_assert(R512[12] == 15 && R512[13] == 100 && R512[14] == 200 && R512[15] == 300);
+
+constexpr v4di R64 = test_alignr_epi64_256();
+static_assert(R64[0] == 3 && R64[1] == 4 && R64[2] == 10 && R64[3] == 11);

@RKSimon RKSimon self-requested a review November 15, 2025 15:42
@github-actions
Copy link

github-actions bot commented Nov 15, 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 cpp,c -- clang/lib/AST/ByteCode/InterpBuiltin.cpp clang/lib/AST/ExprConstant.cpp clang/test/CodeGen/X86/avx512f-builtins.c clang/test/CodeGen/X86/avx512vl-builtins.c --diff_from_common_commit

⚠️
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/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 1a8a2a357..3605942c3 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -4781,8 +4781,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
   case X86::BI__builtin_ia32_alignq128:
   case X86::BI__builtin_ia32_alignq256:
   case X86::BI__builtin_ia32_alignq512: {
-    unsigned NumElems =
-        Call->getType()->castAs<VectorType>()->getNumElements();
+    unsigned NumElems = Call->getType()->castAs<VectorType>()->getNumElements();
     return interp__builtin_ia32_shuffle_generic(
         S, OpPC, Call, [NumElems](unsigned DstIdx, unsigned Shift) {
           unsigned Imm = Shift & 0xFF;

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.

Tests should be added to the existing avx512*builtins.cpp files next to the corresponding intrinsics test coverage using the TEST_CONSTEXPR helpers - see #164078 for an example

@0xzre
Copy link
Author

0xzre commented Nov 16, 2025

Tests should be added to the existing avx512*builtins.cpp files next to the corresponding intrinsics test coverage using the TEST_CONSTEXPR helpers - see #164078 for an example

Implemented the tests 👍

@RKSimon RKSimon self-requested a review November 17, 2025 12:54
unsigned SourcePos = DstIdx + EffectiveShift;
unsigned VecIdx = SourcePos < NumElems ? 1u : 0u;
unsigned ElemIdx =
SourcePos < NumElems ? SourcePos : SourcePos - NumElems;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
SourcePos < NumElems ? SourcePos : SourcePos - NumElems;
unsigned ElemIdx = SourcePos & (NumElems - 1);

Copy link
Author

Choose a reason for hiding this comment

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

oh nice, fixed

unsigned SourcePos = DstIdx + EffectiveShift;
unsigned VecIdx = SourcePos < NumElts ? 1 : 0;
unsigned ElemIdx =
SourcePos < NumElts ? SourcePos : SourcePos - NumElts;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
SourcePos < NumElts ? SourcePos : SourcePos - NumElts;
unsigned ElemIdx = SourcePos & (NumElems - 1);

@0xzre 0xzre requested a review from RKSimon November 19, 2025 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang][X86] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow VALIGND/Q element shift intrinsics to be used in constexpr

4 participants