-
Notifications
You must be signed in to change notification settings - Fork 15.3k
First draft: [Clang] Add constant evaluation support for x86 psadbw/p… #169253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…sadbw128 intrinsic
|
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 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. |
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
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); | ||
| } |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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); | ||
| } |
There was a problem hiding this comment.
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)));
}
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) ClangClang.AST/ByteCode/x86-psadbw-psadbw128.cppIf 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 |
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:
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