-
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
Draft
Mohxen
wants to merge
1
commit into
llvm:main
Choose a base branch
from
Mohxen:feature-psadbw-constexpr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+74
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1901,6 +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); | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Misc utilities | ||
| //===----------------------------------------------------------------------===// | ||
|
|
@@ -12078,6 +12080,54 @@ static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result, | |
| return true; | ||
| } | ||
|
|
||
| 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))) | ||
| return false; | ||
|
|
||
| if (!A.isVector() || !B.isVector()) | ||
| return false; | ||
|
|
||
| unsigned Len = A.getVectorLength(); | ||
| if (Len != 16) // psadbw128 uses 16 bytes (2 × 8) | ||
| return false; | ||
|
|
||
| // 2) Compute SAD over two 8-byte blocks | ||
| uint64_t Sum0 = 0; | ||
| uint64_t Sum1 = 0; | ||
|
|
||
| // bytes 0..7 | ||
| for (unsigned i = 0; i < 8; ++i) { | ||
| uint64_t a = A.getVectorElt(i).getInt().getZExtValue(); | ||
| uint64_t b = B.getVectorElt(i).getInt().getZExtValue(); | ||
| Sum0 += (a > b ? a - b : b - a); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use APIntOps::abdu ? |
||
|
|
||
| // bytes 8..15 | ||
| for (unsigned i = 8; i < 16; ++i) { | ||
| uint64_t a = A.getVectorElt(i).getInt().getZExtValue(); | ||
| uint64_t b = B.getVectorElt(i).getInt().getZExtValue(); | ||
| Sum1 += (a > b ? a - b : b - a); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // 3) Build result vector of two 64-bit elements | ||
| SmallVector<APValue, 2> Elts; | ||
| QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType(); | ||
| bool Unsigned = ElemTy->isUnsignedIntegerType(); | ||
| unsigned BW = Info.Ctx.getIntWidth(ElemTy); // usually 64 | ||
|
|
||
| Elts.emplace_back(APValue(APSInt(APInt(BW, Sum0), Unsigned))); | ||
| Elts.emplace_back(APValue(APSInt(APInt(BW, Sum1), Unsigned))); | ||
|
|
||
| // APValue(const APValue *E, unsigned N) – copies the elements | ||
| Result = APValue(Elts.data(), Elts.size()); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| static bool evalShuffleGeneric( | ||
| EvalInfo &Info, const CallExpr *Call, APValue &Out, | ||
| llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)> | ||
|
|
@@ -12342,6 +12392,9 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { | |
| case clang::X86::BI__builtin_ia32_pavgw512: | ||
| return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU); | ||
|
|
||
| case X86::BI__builtin_ia32_psadbw128: | ||
| return EvaluatePSADBW128(E, Info, Result); | ||
|
|
||
| case clang::X86::BI__builtin_ia32_pmulhrsw128: | ||
| case clang::X86::BI__builtin_ia32_pmulhrsw256: | ||
| case clang::X86::BI__builtin_ia32_pmulhrsw512: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O0 \ | ||
| // RUN: -fexperimental-new-constant-interpreter -verify %s | ||
|
|
||
| // No headers allowed. Use Clang's vector types directly. | ||
|
|
||
| typedef char v16qi __attribute__((vector_size(16))); | ||
| typedef long long v2di __attribute__((vector_size(16))); | ||
|
|
||
| constexpr v2di test_psadbw128() { | ||
| v16qi a = {10,20,30,40,50,60,70,80, | ||
| 1,2,3,4,5,6,7,8}; | ||
|
|
||
| v16qi b = {5,15,25,45,55,55,75,85, | ||
| 10,0,3,9,1,10,2,8}; | ||
|
|
||
| // Call the builtin directly | ||
| return __builtin_ia32_psadbw128(a, b); | ||
| } | ||
|
|
||
| static_assert(test_psadbw128()[0] == 40, "block0 mismatch"); | ||
| static_assert(test_psadbw128()[1] == 29, "block1 mismatch"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.