https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/157403
>From 38b90ecc3dc30668860cc80a6c8f075536764b5e Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Thu, 4 Sep 2025 00:55:24 +0900 Subject: [PATCH 01/12] todo list --- clang/lib/AST/ExprConstant.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index b4f1e76187e25..97138134fb075 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11267,6 +11267,10 @@ static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) { return Result; } +// i should emplement SLLDQ, SRLDQ shift (intrinsics) in constant expression +// handling inside this function +// avx2intrin.h -> _mm256_slli_si256 +// emmintrin.h -> _mm_slli_si128 bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { const VectorType *VTy = E->getType()->castAs<VectorType>(); unsigned NElts = VTy->getNumElements(); >From eb258bacfb493ee6e60e833458588752da3a27e4 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Thu, 4 Sep 2025 00:59:28 +0900 Subject: [PATCH 02/12] todo list --- clang/lib/AST/ExprConstant.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 97138134fb075..cfaef65511e9b 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11267,10 +11267,6 @@ static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) { return Result; } -// i should emplement SLLDQ, SRLDQ shift (intrinsics) in constant expression -// handling inside this function -// avx2intrin.h -> _mm256_slli_si256 -// emmintrin.h -> _mm_slli_si128 bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { const VectorType *VTy = E->getType()->castAs<VectorType>(); unsigned NElts = VTy->getNumElements(); @@ -11559,7 +11555,10 @@ static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO, << SourceTy << DestTy; return false; } - +// i should emplement SLLDQ, SRLDQ shift (intrinsics) in constant expression +// handling inside this function +// avx2intrin.h -> _mm256_slli_si256 +// emmintrin.h -> _mm_slli_si128 bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { if (!IsConstantEvaluatedBuiltinCall(E)) return ExprEvaluatorBaseTy::VisitCallExpr(E); >From f93dc8fa5b5baaf37fc04c3e379ab0c56b94d412 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Thu, 4 Sep 2025 01:33:33 +0900 Subject: [PATCH 03/12] feat(exprconst): branch statement handling --- clang/lib/AST/ExprConstant.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index cfaef65511e9b..b55fa4d0975d7 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12042,6 +12042,13 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { } return Success(APValue(ResultElements.data(), ResultElements.size()), E); } + case clang::X86__builtin_ia32_pslldqi128: + case clang::X86__builtin_ia32_psrldqi128: + case clang::X86__builtin_ia32_pslldqi256: + case clang::X86__builtin_ia32_psrldqi256: { + + } + } } >From eb95364b26db0d11e45914ff70c65c8beb1e1c09 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Sat, 6 Sep 2025 19:18:23 +0900 Subject: [PATCH 04/12] feat(exprconst): implement shift in compile time --- clang/lib/AST/ExprConstant.cpp | 66 +++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index b55fa4d0975d7..1ecc36705595a 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12042,13 +12042,71 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { } return Success(APValue(ResultElements.data(), ResultElements.size()), E); } - case clang::X86__builtin_ia32_pslldqi128: - case clang::X86__builtin_ia32_psrldqi128: - case clang::X86__builtin_ia32_pslldqi256: - case clang::X86__builtin_ia32_psrldqi256: { + case X86::BI__builtin_ia32_pslldqi128_byteshift: + case X86::BI__builtin_ia32_psrldqi128_byteshift: { + unsigned BuiltinID = E->getBuiltinCallee(); + APSInt Amt; + if(!EvaluateInteger(E->getArg(1),Amt, Info)) + break; + unsigned Shift = (unsigned)Amt.getZExtValue(); + + APValue Vec; + if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) + break; + + SmallVector<APValue, 16> ResultElements; + ResultElements.reserve(16); + + bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi128_byteshift); + + for (unsigned i = 0; i < 16; i++) { + int SrcIdx = -1; + if (isLeft) + SrcIdx = i + Shift; + else if (i >= Shift) + SrcIdx = i - Shift; + + if (SrcIdx >= 0 && (unsigned)SrcIdx < 16) + ResultElements.push_back(Vec.getVectorElt(SrcIdx)); + else + ResultElements.push_back(APValue(0)); + } + return Success(APValue(ResultElements.data(), ResultElements.size()), E); } + + case X86::BI__builtin_ia32_pslldqi256_byteshift: + case X86::BI__builtin_ia32_psrldqi256_byteshift: { + unsigned BuiltinID = E->getBuiltinCallee(); + + APSInt Amt; + if(!EvaluateInteger(E->getArg(1),Amt, Info)) + break; + unsigned Shift = (unsigned)Amt.getZExtValue(); + + APValue Vec; + if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) + break; + + SmallVector<APValue, 32> ResultElements; + ResultElements.reserve(32); + + bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi256_byteshift); + for (unsigned i = 0; i < 32; i++) { + int SrcIdx = -1; + if (isLeft) + SrcIdx = i + Shift; + else if (i >= Shift) + SrcIdx = i - Shift; + + if (SrcIdx >= 0 && (unsigned)SrcIdx < 32) + ResultElements.push_back(Vec.getVectorElt(SrcIdx)); + else + ResultElements.push_back(APValue(0)); + } + return Success(APValue(ResultElements.data(), ResultElements.size()), E); + } } } >From f00eec1a98a6da4372d03e1c2723241aa9ebd727 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Mon, 8 Sep 2025 17:08:17 +0900 Subject: [PATCH 05/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 1ecc36705595a..2b06705a4870c 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11555,10 +11555,7 @@ static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO, << SourceTy << DestTy; return false; } -// i should emplement SLLDQ, SRLDQ shift (intrinsics) in constant expression -// handling inside this function -// avx2intrin.h -> _mm256_slli_si256 -// emmintrin.h -> _mm_slli_si128 + bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { if (!IsConstantEvaluatedBuiltinCall(E)) return ExprEvaluatorBaseTy::VisitCallExpr(E); @@ -12045,16 +12042,16 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { case X86::BI__builtin_ia32_pslldqi128_byteshift: case X86::BI__builtin_ia32_psrldqi128_byteshift: { unsigned BuiltinID = E->getBuiltinCallee(); - + APSInt Amt; - if(!EvaluateInteger(E->getArg(1),Amt, Info)) + if (!EvaluateInteger(E->getArg(1), Amt, Info)) break; unsigned Shift = (unsigned)Amt.getZExtValue(); - + APValue Vec; if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) break; - + SmallVector<APValue, 16> ResultElements; ResultElements.reserve(16); @@ -12074,20 +12071,20 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { } return Success(APValue(ResultElements.data(), ResultElements.size()), E); } - + case X86::BI__builtin_ia32_pslldqi256_byteshift: case X86::BI__builtin_ia32_psrldqi256_byteshift: { unsigned BuiltinID = E->getBuiltinCallee(); - + APSInt Amt; - if(!EvaluateInteger(E->getArg(1),Amt, Info)) + if (!EvaluateInteger(E->getArg(1), Amt, Info)) break; unsigned Shift = (unsigned)Amt.getZExtValue(); - + APValue Vec; if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) break; - + SmallVector<APValue, 32> ResultElements; ResultElements.reserve(32); >From 6b0dc7bec982fe2acd0dc3a7f0030344190fbd10 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Thu, 4 Sep 2025 01:33:33 +0900 Subject: [PATCH 06/12] Title: [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 psllDqi/psrlDqi intrinsics feat(exprconst): branch statement handling --- clang/lib/AST/ExprConstant.cpp | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index dc78e29378b26..a350816857917 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12095,7 +12095,69 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { break; } } + case X86::BI__builtin_ia32_pslldqi128_byteshift: + case X86::BI__builtin_ia32_psrldqi128_byteshift: { + unsigned BuiltinID = E->getBuiltinCallee(); + + APSInt Amt; + if (!EvaluateInteger(E->getArg(1), Amt, Info)) + break; + unsigned Shift = (unsigned)Amt.getZExtValue(); + + APValue Vec; + if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) + break; + + SmallVector<APValue, 16> ResultElements; + ResultElements.reserve(16); + + bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi128_byteshift); + + for (unsigned i = 0; i < 16; i++) { + int SrcIdx = -1; + if (isLeft) + SrcIdx = i + Shift; + else if (i >= Shift) + SrcIdx = i - Shift; + + if (SrcIdx >= 0 && (unsigned)SrcIdx < 16) + ResultElements.push_back(Vec.getVectorElt(SrcIdx)); + else + ResultElements.push_back(APValue(0)); + } + return Success(APValue(ResultElements.data(), ResultElements.size()), E); + } + + case X86::BI__builtin_ia32_pslldqi256_byteshift: + case X86::BI__builtin_ia32_psrldqi256_byteshift: { + unsigned BuiltinID = E->getBuiltinCallee(); + + APSInt Amt; + if (!EvaluateInteger(E->getArg(1), Amt, Info)) + break; + unsigned Shift = (unsigned)Amt.getZExtValue(); + + APValue Vec; + if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) + break; + SmallVector<APValue, 32> ResultElements; + ResultElements.reserve(32); + + bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi256_byteshift); + + for (unsigned i = 0; i < 32; i++) { + int SrcIdx = -1; + if (isLeft) + SrcIdx = i + Shift; + else if (i >= Shift) + SrcIdx = i - Shift; + + if (SrcIdx >= 0 && (unsigned)SrcIdx < 32) + ResultElements.push_back(Vec.getVectorElt(SrcIdx)); + else + ResultElements.push_back(APValue(0)); + } return Success(APValue(ResultElements.data(), ResultElements.size()), E); } } >From 2c6d3607c9be26d20851374f7accd7ffeb5e9afc Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Thu, 11 Sep 2025 22:38:37 +0900 Subject: [PATCH 07/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 86 ++++++++++++++-------------------- 1 file changed, 36 insertions(+), 50 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index a350816857917..1dc3d869fa5c8 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11592,7 +11592,10 @@ static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO, << SourceTy << DestTy; return false; } - +// i should emplement SLLDQ, SRLDQ shift (intrinsics) in constant expression +// handling inside this function +// avx2intrin.h -> _mm256_slli_si256 +// emmintrin.h -> _mm_slli_si128 bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { if (!IsConstantEvaluatedBuiltinCall(E)) return ExprEvaluatorBaseTy::VisitCallExpr(E); @@ -12096,69 +12099,52 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { } } case X86::BI__builtin_ia32_pslldqi128_byteshift: - case X86::BI__builtin_ia32_psrldqi128_byteshift: { - unsigned BuiltinID = E->getBuiltinCallee(); - - APSInt Amt; - if (!EvaluateInteger(E->getArg(1), Amt, Info)) - break; - unsigned Shift = (unsigned)Amt.getZExtValue(); - - APValue Vec; - if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) - break; - - SmallVector<APValue, 16> ResultElements; - ResultElements.reserve(16); - - bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi128_byteshift); - - for (unsigned i = 0; i < 16; i++) { - int SrcIdx = -1; - if (isLeft) - SrcIdx = i + Shift; - else if (i >= Shift) - SrcIdx = i - Shift; - - if (SrcIdx >= 0 && (unsigned)SrcIdx < 16) - ResultElements.push_back(Vec.getVectorElt(SrcIdx)); - else - ResultElements.push_back(APValue(0)); - } - return Success(APValue(ResultElements.data(), ResultElements.size()), E); - } - + case X86::BI__builtin_ia32_psrldqi128_byteshift: case X86::BI__builtin_ia32_pslldqi256_byteshift: case X86::BI__builtin_ia32_psrldqi256_byteshift: { - unsigned BuiltinID = E->getBuiltinCallee(); - APSInt Amt; if (!EvaluateInteger(E->getArg(1), Amt, Info)) - break; - unsigned Shift = (unsigned)Amt.getZExtValue(); + return false; + unsigned ShiftVal = (unsigned)Amt.getZExtValue() & 0xff; APValue Vec; if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) break; - SmallVector<APValue, 32> ResultElements; - ResultElements.reserve(32); + unsigned NumElts = Vec.getVectorLength(); + const unsigned LaneBytes = 16; + SmallVector<APValue, 64> Result; + Result.resize(NumElts, APValue(0)); - bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi256_byteshift); + bool IsLeft = (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || + E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); - for (unsigned i = 0; i < 32; i++) { - int SrcIdx = -1; - if (isLeft) - SrcIdx = i + Shift; - else if (i >= Shift) - SrcIdx = i - Shift; + SmallVector<APValue, 64> Result; + Result.resize(NumElts, APValue(0)); - if (SrcIdx >= 0 && (unsigned)SrcIdx < 32) - ResultElements.push_back(Vec.getVectorElt(SrcIdx)); + SmallVector<int, 64> Indices; + Indices.reserve(NumElts); + + unsigned LaneSize = LaneBytes; + for (unsigned laneBase = 0; laneBase < NumElts; laneBase += LaneSize) { + for (unsigned i = 0; i < LaneSize; ++i) { + int src = IsLeft ? (i + ShiftVal) : (int)i - (int)ShiftVal; + + if (src >= 0 && (unsigned)src < LaneSize) + Indices.push_back(laneBase + src); + else + Indices.push_back(-1); + } + } + + for (unsigned i = 0; i < NumElts; i++) { + int src = Indices[i]; + if (src >= 0) + Result[i] = Vec.getVectorElt((unsigned)src); else - ResultElements.push_back(APValue(0)); + Result[i] = APValue(0); } - return Success(APValue(ResultElements.data(), ResultElements.size()), E); + return Success(APValue(Result.data(), Result.size()), E); } } } >From 94354209d2f396b340fbed4ab19ba4e9ea950b65 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Fri, 12 Sep 2025 00:45:32 +0900 Subject: [PATCH 08/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 1dc3d869fa5c8..e04ba2193828b 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12116,8 +12116,9 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { SmallVector<APValue, 64> Result; Result.resize(NumElts, APValue(0)); - bool IsLeft = (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || - E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); + bool IsLeft = + (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || + E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); SmallVector<APValue, 64> Result; Result.resize(NumElts, APValue(0)); @@ -12136,7 +12137,7 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { Indices.push_back(-1); } } - + for (unsigned i = 0; i < NumElts; i++) { int src = Indices[i]; if (src >= 0) >From e7356b294143e2cd32b9fbd5d237809119fe4443 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Fri, 12 Sep 2025 00:48:37 +0900 Subject: [PATCH 09/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 96 ++++------------------------------ 1 file changed, 11 insertions(+), 85 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index e04ba2193828b..463760243e783 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12004,70 +12004,6 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { return Success(APValue(ResultElements.data(), ResultElements.size()), E); } - case X86::BI__builtin_ia32_pslldqi128_byteshift: - case X86::BI__builtin_ia32_psrldqi128_byteshift: { - unsigned BuiltinID = E->getBuiltinCallee(); - - APSInt Amt; - if (!EvaluateInteger(E->getArg(1), Amt, Info)) - break; - unsigned Shift = (unsigned)Amt.getZExtValue(); - - APValue Vec; - if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) - break; - - SmallVector<APValue, 16> ResultElements; - ResultElements.reserve(16); - - bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi128_byteshift); - - for (unsigned i = 0; i < 16; i++) { - int SrcIdx = -1; - if (isLeft) - SrcIdx = i + Shift; - else if (i >= Shift) - SrcIdx = i - Shift; - - if (SrcIdx >= 0 && (unsigned)SrcIdx < 16) - ResultElements.push_back(Vec.getVectorElt(SrcIdx)); - else - ResultElements.push_back(APValue(0)); - } - return Success(APValue(ResultElements.data(), ResultElements.size()), E); - } - - case X86::BI__builtin_ia32_pslldqi256_byteshift: - case X86::BI__builtin_ia32_psrldqi256_byteshift: { - unsigned BuiltinID = E->getBuiltinCallee(); - - APSInt Amt; - if (!EvaluateInteger(E->getArg(1), Amt, Info)) - break; - unsigned Shift = (unsigned)Amt.getZExtValue(); - - APValue Vec; - if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) - break; - - SmallVector<APValue, 32> ResultElements; - ResultElements.reserve(32); - - bool isLeft = (BuiltinID == X86::BI__builtin_ia32_pslldqi256_byteshift); - - for (unsigned i = 0; i < 32; i++) { - int SrcIdx = -1; - if (isLeft) - SrcIdx = i + Shift; - else if (i >= Shift) - SrcIdx = i - Shift; - - if (SrcIdx >= 0 && (unsigned)SrcIdx < 32) - ResultElements.push_back(Vec.getVectorElt(SrcIdx)); - else - ResultElements.push_back(APValue(0)); - } - case Builtin::BI__builtin_elementwise_fshl: case Builtin::BI__builtin_elementwise_fshr: { APValue SourceHi, SourceLo, SourceShift; @@ -12113,38 +12049,28 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { unsigned NumElts = Vec.getVectorLength(); const unsigned LaneBytes = 16; - SmallVector<APValue, 64> Result; - Result.resize(NumElts, APValue(0)); - - bool IsLeft = - (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || - E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); + assert(NumElts % LaneBytes == 0); SmallVector<APValue, 64> Result; Result.resize(NumElts, APValue(0)); - SmallVector<int, 64> Indices; - Indices.reserve(NumElts); + bool IsLeft = (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || + E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); + + if (ShiftVal >= LaneBytes) + return Success(APValue(Result.data(), Result.size()), E); - unsigned LaneSize = LaneBytes; - for (unsigned laneBase = 0; laneBase < NumElts; laneBase += LaneSize) { - for (unsigned i = 0; i < LaneSize; ++i) { + for (unsigned laneBase = 0; laneBase < NumElts; laneBase += LaneBytes) { + for (unsigned i = 0; i < LaneBytes; ++i) { int src = IsLeft ? (i + ShiftVal) : (int)i - (int)ShiftVal; - if (src >= 0 && (unsigned)src < LaneSize) - Indices.push_back(laneBase + src); + if (src >= 0 && (unsigned)src < LaneBytes) + Result[laneBase + i] = Vec.getVectorElt(laneBase + (unsigned)src); else - Indices.push_back(-1); + Result[laneBase + i] = APValue(0); } } - for (unsigned i = 0; i < NumElts; i++) { - int src = Indices[i]; - if (src >= 0) - Result[i] = Vec.getVectorElt((unsigned)src); - else - Result[i] = APValue(0); - } return Success(APValue(Result.data(), Result.size()), E); } } >From ee6874b681ddef500d0219524769eaedac5ab33f Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Fri, 12 Sep 2025 00:51:53 +0900 Subject: [PATCH 10/12] [clang][test] VectorExprEvaluator::VisitCallExpr - test constant folding for X86 pslldqi/psrldqi infrinsics --- clang/test/CodeGen/X86/avx2-builtins.c | 2 ++ clang/test/CodeGen/X86/sse2-builtins.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/clang/test/CodeGen/X86/avx2-builtins.c b/clang/test/CodeGen/X86/avx2-builtins.c index 724a5f693f9fe..e9ab142cd278f 100644 --- a/clang/test/CodeGen/X86/avx2-builtins.c +++ b/clang/test/CodeGen/X86/avx2-builtins.c @@ -1168,6 +1168,7 @@ __m256i test_mm256_slli_si256(__m256i a) { // CHECK: shufflevector <32 x i8> zeroinitializer, <32 x i8> %{{.*}}, <32 x i32> <i32 13, i32 14, i32 15, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 29, i32 30, i32 31, i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60> return _mm256_slli_si256(a, 3); } +TEST_CONSTEXPR(match_v32qi(_mm256_slli_si256((__m256i)(__v32qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116}, 5),0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,0,0,0,0,0,101,102,103,104,105,106,107,108,109,110,111)) __m128i test_mm_sllv_epi32(__m128i a, __m128i b) { // CHECK-LABEL: test_mm_sllv_epi32 @@ -1311,6 +1312,7 @@ __m256i test_mm256_srli_si256(__m256i a) { // CHECK: shufflevector <32 x i8> %{{.*}}, <32 x i8> zeroinitializer, <32 x i32> <i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 32, i32 33, i32 34, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 48, i32 49, i32 50> return _mm256_srli_si256(a, 3); } +TEST_CONSTEXPR(match_v32qi(_mm256_srli_si256((__m256i)(__v32qi){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116}, 5), 6,7,8,9,10,11,12,13,14,15,16,0,0,0,0,0, 106,107,108,109,110,111,112,113,114,115,116,0,0,0,0,0)) __m128i test_mm_srlv_epi32(__m128i a, __m128i b) { // CHECK-LABEL: test_mm_srlv_epi32 diff --git a/clang/test/CodeGen/X86/sse2-builtins.c b/clang/test/CodeGen/X86/sse2-builtins.c index f5de5069c0046..502ce9423264c 100644 --- a/clang/test/CodeGen/X86/sse2-builtins.c +++ b/clang/test/CodeGen/X86/sse2-builtins.c @@ -1551,12 +1551,14 @@ __m128i test_mm_srli_si128(__m128i A) { // CHECK: shufflevector <16 x i8> %{{.*}}, <16 x i8> zeroinitializer, <16 x i32> <i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20> return _mm_srli_si128(A, 5); } +TEST_CONSTEXPR(match_v16qi(_mm_slli_si128((__m128i)(__v16qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, 3),0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13)) __m128i test_mm_srli_si128_2(__m128i A) { // CHECK-LABEL: test_mm_srli_si128_2 // ret <2 x i64> zeroinitializer return _mm_srli_si128(A, 17); } +TEST_CONSTEXPR(match_v16qi(_mm_srli_si128((__m128i)(__v16qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, 3),4,5,6,7,8,9,10,11,12,13,14,15,16,0,0,0)) void test_mm_store_pd(double* A, __m128d B) { // CHECK-LABEL: test_mm_store_pd >From 814495e0c94b696ebb58639d4a325ea2402fe8bc Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Fri, 12 Sep 2025 01:52:24 +0900 Subject: [PATCH 11/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 463760243e783..085d1edd8fc21 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12054,8 +12054,9 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { SmallVector<APValue, 64> Result; Result.resize(NumElts, APValue(0)); - bool IsLeft = (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || - E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); + bool IsLeft = + (E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi128_byteshift || + E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi256_byteshift); if (ShiftVal >= LaneBytes) return Success(APValue(Result.data(), Result.size()), E); >From 727380152f264161f7a86efb2c75073e9cfa0e19 Mon Sep 17 00:00:00 2001 From: kimyounhoex1 <dordy...@gmail.com> Date: Fri, 12 Sep 2025 15:14:56 +0900 Subject: [PATCH 12/12] [clang] VectorExprEvaluator::VisitCallExpr - add constant folding for X86 pslldqi/psrldqi infrinsics --- clang/lib/AST/ExprConstant.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 085d1edd8fc21..bc7a1d212a195 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12034,6 +12034,9 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { break; } } + + return Success(APValue(ResultElements.data(), ResultElements.size()), E); + } case X86::BI__builtin_ia32_pslldqi128_byteshift: case X86::BI__builtin_ia32_psrldqi128_byteshift: case X86::BI__builtin_ia32_pslldqi256_byteshift: @@ -12045,7 +12048,7 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { APValue Vec; if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector()) - break; + return false; unsigned NumElts = Vec.getVectorLength(); const unsigned LaneBytes = 16; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits