Skip to content

Conversation

@Mohxen
Copy link

@Mohxen Mohxen commented Nov 23, 2025

This is the first draft of adding constant evaluation support for the
x86 PSADBW and PSADBW128 intrinsics in Clang’s constexpr interpreter.

The patch currently:

  • Adds initial handling logic in ExprConstant.cpp
  • Includes an early version of the test under
    clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp

This PR is still under development. Feedback on structure, integration
location, and testing is welcome before polishing the final version.

Partial fix: #157522

@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.

@github-actions
Copy link

⚠️ 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 -- clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp clang/lib/AST/ExprConstant.cpp --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/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3a41b262d..e862ba7b7 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1901,7 +1901,8 @@ static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
 static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
                                EvalInfo &Info);
 
-static bool EvaluatePSADBW128(const CallExpr *E, EvalInfo &Info, APValue &Result);
+static bool EvaluatePSADBW128(const CallExpr *E, EvalInfo &Info,
+                              APValue &Result);
 
 //===----------------------------------------------------------------------===//
 // Misc utilities
@@ -12080,11 +12081,11 @@ static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
   return true;
 }
 
-static bool EvaluatePSADBW128(const CallExpr *E, EvalInfo &Info, APValue &Result) {
+static bool EvaluatePSADBW128(const CallExpr *E, EvalInfo &Info,
+                              APValue &Result) {
   // 1) Evaluate the arguments into APValues
   APValue A, B;
-  if (!Evaluate(A, Info, E->getArg(0)) ||
-      !Evaluate(B, Info, E->getArg(1)))
+  if (!Evaluate(A, Info, E->getArg(0)) || !Evaluate(B, Info, E->getArg(1)))
     return false;
 
   if (!A.isVector() || !B.isVector())
@@ -12127,7 +12128,6 @@ static bool EvaluatePSADBW128(const CallExpr *E, EvalInfo &Info, APValue &Result
   return true;
 }
 
-
 static bool evalShuffleGeneric(
     EvalInfo &Info, const CallExpr *Call, APValue &Out,
     llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
@@ -12411,7 +12411,7 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
     return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
 
   case X86::BI__builtin_ia32_psadbw128:
-  return EvaluatePSADBW128(E, Info, Result);
+    return EvaluatePSADBW128(E, Info, Result);
 
   case clang::X86::BI__builtin_ia32_pmulhrsw128:
   case clang::X86::BI__builtin_ia32_pmulhrsw256:

uint64_t a = A.getVectorElt(i).getInt().getZExtValue();
uint64_t b = B.getVectorElt(i).getInt().getZExtValue();
Sum0 += (a > b ? a - b : b - a);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use APIntOps::abdu ?

!Evaluate(B, Info, E->getArg(1)))
return false;

if (!A.isVector() || !B.isVector())
Copy link
Collaborator

Choose a reason for hiding this comment

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

The ia32_psadbw builtins have fixed types - don't bother with so much isVector/getVectorLength checks - assert((getVectorLength() % 16) == 0) should be enough.

uint64_t a = A.getVectorElt(i).getInt().getZExtValue();
uint64_t b = B.getVectorElt(i).getInt().getZExtValue();
Sum1 += (a > b ? a - b : b - a);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert to a format that will handle 256/512 variants now - otherwise you'll just end up rewriting all of this later on.

SmallVector<APValue, 8> Elts;
for (unsigned Lane = 0; Lane != Len; Lane += 8) {
  for (unsigend I = 0; I != 8; ++I) {
    APInt A = A.getVectorElt(Lane + I).getInt();
    APInt B = B.getVectorElt(Lane + I).getInt();
  }
etc.
  Elts.emplace_back(APValue(APSInt(APInt(64, Sum), Unsigned)));
}

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 84729 tests passed
  • 1108 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

Clang

Clang.AST/ByteCode/x86-psadbw-psadbw128.cpp
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -std=c++20 -triple x86_64-unknown-linux-gnu -O0    -fexperimental-new-constant-interpreter -verify /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -std=c++20 -triple x86_64-unknown-linux-gnu -O0 -fexperimental-new-constant-interpreter -verify /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp
# .---command stderr------------
# | error: no expected directives found: consider use of 'expected-no-diagnostics'
# | error: 'expected-error' diagnostics seen but not expected: 
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 20: static assertion expression is not an integral constant expression
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 21: static assertion expression is not an integral constant expression
# | error: 'expected-note' diagnostics seen but not expected: 
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 17: subexpression not valid in a constant expression
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 20: in call to 'test_psadbw128()'
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 17: subexpression not valid in a constant expression
# |   File /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/AST/ByteCode/x86-psadbw-psadbw128.cpp Line 21: in call to 'test_psadbw128()'
# | 7 errors generated.
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - add MMX/SSE/AVX/AVX512 VPSADBW / VMPSADBW / VDBPSADBW intrinsics to be used in constexpr

2 participants