Skip to content
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
38b90ec
todo list
kimyounhoex1 Sep 3, 2025
eb258ba
todo list
kimyounhoex1 Sep 3, 2025
f93dc8f
feat(exprconst): branch statement handling
kimyounhoex1 Sep 3, 2025
eb95364
feat(exprconst): implement shift in compile time
kimyounhoex1 Sep 6, 2025
f00eec1
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 8, 2025
015c774
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
kimyounhoex1 Sep 8, 2025
bed3603
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
kimyounhoex1 Sep 8, 2025
afe7818
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
kimyounhoex1 Sep 8, 2025
6b0dc7b
Title: [clang] VectorExprEvaluator::VisitCallExpr - add constant fold…
kimyounhoex1 Sep 3, 2025
2c6d360
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 11, 2025
9435420
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 11, 2025
e7356b2
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 11, 2025
ee6874b
[clang][test] VectorExprEvaluator::VisitCallExpr - test constant fold…
kimyounhoex1 Sep 11, 2025
814495e
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 11, 2025
7273801
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 12, 2025
2eed8b4
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
RKSimon Sep 16, 2025
2b02883
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
kimyounhoex1 Sep 17, 2025
a587fe8
Merge branch 'user/kimyounhex1/constexpr-slldq-srldq' of https://gith…
kimyounhoex1 Sep 17, 2025
3f4f873
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 18, 2025
7462ae3
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 20, 2025
6164028
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 20, 2025
fd1470e
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 23, 2025
4517c68
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 23, 2025
bc3739a
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 25, 2025
05efd71
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 29, 2025
7811cb8
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
kimyounhoex1 Sep 29, 2025
6a07417
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 29, 2025
bb0f797
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 30, 2025
ce61b54
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 30, 2025
e86f26c
[clang] VectorExprEvaluator::VisitCallExpr - add constant folding for…
kimyounhoex1 Sep 30, 2025
568b6fa
Merge branch 'main' into user/kimyounhex1/constexpr-slldq-srldq
RKSimon Oct 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clang/include/clang/Basic/BuiltinsX86.td
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ let Header = "emmintrin.h", Attributes = [NoThrow, RequireDeclaration] in {
def _mm_pause : X86LibBuiltin<"void()">;
}

let Features = "sse2", Attributes = [NoThrow, Const, RequiredVectorWidth<128>] in {
let Features = "sse2",
Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<128>] in {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can only add the constexpr attribute to the byteshift intrinsics - better to move the byteshift declarations into the constexor below (psrldi128 et al.)

def psraw128 : X86Builtin<"_Vector<8, short>(_Vector<8, short>, _Vector<8, short>)">;
def psrad128 : X86Builtin<"_Vector<4, int>(_Vector<4, int>, _Vector<4, int>)">;
def psrlw128 : X86Builtin<"_Vector<8, short>(_Vector<8, short>, _Vector<8, short>)">;
Expand Down Expand Up @@ -568,7 +569,8 @@ let Features = "avx", Attributes = [NoThrow, Const, RequiredVectorWidth<256>] in
def vec_set_v8si : X86Builtin<"_Vector<8, int>(_Vector<8, int>, int, _Constant int)">;
}

let Features = "avx2", Attributes = [NoThrow, Const, RequiredVectorWidth<256>] in {
let Features = "avx2",
Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<256>] in {
Copy link
Collaborator

Choose a reason for hiding this comment

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

same - undo this and move the byteshift declarations into the block with psrlqi256 etc.

def mpsadbw256 : X86Builtin<"_Vector<32, char>(_Vector<32, char>, _Vector<32, char>, _Constant char)">;
def packsswb256 : X86Builtin<"_Vector<32, char>(_Vector<16, short>, _Vector<16, short>)">;
def packssdw256 : X86Builtin<"_Vector<16, short>(_Vector<8, int>, _Vector<8, int>)">;
Expand Down Expand Up @@ -2047,7 +2049,8 @@ let Features = "avx512vl", Attributes = [NoThrow, Const, Constexpr, RequiredVect
def psravq256 : X86Builtin<"_Vector<4, long long int>(_Vector<4, long long int>, _Vector<4, long long int>)">;
}

let Features = "avx512bw", Attributes = [NoThrow, Const, RequiredVectorWidth<512>] in {
let Features = "avx512bw",
Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<512>] in {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move the byteshfts down to the block containing pmulhw512?

def psraw512
: X86Builtin<"_Vector<32, short>(_Vector<32, short>, _Vector<8, short>)">;
def psrlw512
Expand Down
43 changes: 43 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12127,6 +12127,49 @@ 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:
case X86::BI__builtin_ia32_pslldqi256_byteshift:
case X86::BI__builtin_ia32_psrldqi256_byteshift:
case X86::BI__builtin_ia32_pslldqi512_byteshift:
case X86::BI__builtin_ia32_psrldqi512_byteshift: {
APSInt Amt;
if (!EvaluateInteger(E->getArg(1), Amt, Info))
return false;
unsigned ShiftVal = (unsigned)Amt.getZExtValue() & 0xff;

APValue Vec;
if (!Evaluate(Vec, Info, E->getArg(0)) || !Vec.isVector())
return false;

unsigned NumElts = Vec.getVectorLength();
const unsigned LaneBytes = 16;
assert(NumElts % LaneBytes == 0);

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 ||
E->getBuiltinCallee() == X86::BI__builtin_ia32_pslldqi512_byteshift);

if (ShiftVal >= LaneBytes)
return ZeroInitialization(E);

for (unsigned LaneBase = 0; LaneBase < NumElts; LaneBase += LaneBytes) {
for (unsigned I = 0; I < LaneBytes; ++I) {
int src = IsLeft ? (I + ShiftVal) : (int)I - (int)ShiftVal;
Copy link
Collaborator

Choose a reason for hiding this comment

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

(style) int Src =


if (src >= 0 && (unsigned)src < LaneBytes)
Result[LaneBase + I] = Vec.getVectorElt(LaneBase + (unsigned)src);
else
Result[LaneBase + I] = APValue(0);
}
}

return Success(APValue(Result.data(), Result.size()), E);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions clang/test/CodeGen/X86/avx2-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ __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))
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}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))

__m128i test_mm_sllv_epi32(__m128i a, __m128i b) {
// CHECK-LABEL: test_mm_sllv_epi32
Expand Down Expand Up @@ -1325,6 +1327,8 @@ __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))
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}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))

__m128i test_mm_srlv_epi32(__m128i a, __m128i b) {
// CHECK-LABEL: test_mm_srlv_epi32
Expand Down
16 changes: 16 additions & 0 deletions clang/test/CodeGen/X86/avx512bw-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -2607,3 +2607,19 @@ void test_mm512_mask_cvtusepi16_storeu_epi8 (void * __P, __mmask32 __M, __m512i
// CHECK: @llvm.x86.avx512.mask.pmovus.wb.mem.512
_mm512_mask_cvtusepi16_storeu_epi8 ( __P, __M, __A);
}

__m512i test_mm512_bslli_epi16(__m512i a) {
// CHECK-LABEL: @test_bslli
// CHECK: shufflevector <64 x i8> %{{.*}}, <64 x i8> zeroinitializer, <64 x i32> <i32 64, i32 65, i32 66, i32 67, i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 68, i32 69, i32 70, i32 71, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 72, i32 73, i32 74, i32 75, 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 76, i32 77, i32 78, i32 79, i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59>
return _mm512_bslli_epi128(a, 4);
}
TEST_CONSTEXPR(match_v64qi(_mm512_bslli_epi128((__m512i)(__v64qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}, 4), 0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12, 0,0,0,0,17,18,19,20,21,22,23,24,25,26,27,28, 0,0,0,0,33,34,35,36,37,38,39,40,41,42,43,44, 0,0,0,0,49,50,51,52,53,54,55,56,57,58,59,60));
TEST_CONSTEXPR(match_v64qi(_mm512_bslli_epi128((__m512i)(__v64qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48, 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));

__m512i test_mm512_bsrli_epi16(__m512i a) {
// CHECK-LABEL: @test_bsrli
// CHECK: shufflevector <64 x i8> %{{.*}}, <64 x i8> zeroinitializer, <64 x i32> <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 16, i32 17, i32 18, 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 64, i32 64, i32 64, i32 64, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, 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, i32 61, i32 62, i32 63, i32 64, i32 64, i32 64, i32 64>
return _mm512_bsrli_epi128(a, 4);
}
TEST_CONSTEXPR(match_v64qi(_mm512_bsrli_epi128((__m512i)(__v64qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48, 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}, 4), 5,6,7,8,9,10,11,12,13,14,15,16,0,0,0,0, 21,22,23,24,25,26,27,28,29,30,31,32,0,0,0,0, 37,38,39,40,41,42,43,44,45,46,47,48,0,0,0,0, 53,54,55,56,57,58,59,60,61,62,63,64,0,0,0,0));
TEST_CONSTEXPR(match_v64qi(_mm512_bsrli_epi128((__m512i)(__v64qi){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48, 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
5 changes: 5 additions & 0 deletions clang/test/CodeGen/X86/sse2-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,13 +1553,18 @@ __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))
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}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))

__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))
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}, 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
Copy link
Collaborator

@RKSimon RKSimon Oct 1, 2025

Choose a reason for hiding this comment

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

ALL of these tests are still failing as you're missing brackets around the vector initialisations (because _mm_srli_si128 is a macro it expands early).

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}), 16), 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))

Update all your other TEST_CONSTEXPR accordingly


void test_mm_store_pd(double* A, __m128d B) {
// CHECK-LABEL: test_mm_store_pd
// CHECK: store <2 x double> %{{.*}}, ptr %{{.*}}, align 16
Expand Down