Skip to content

Commit a0d26ee

Browse files
committed
[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow SSE41 phminposuw intrinsic to be used in constexp
1 parent 69b2230 commit a0d26ee

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

clang/include/clang/Basic/BuiltinsX86.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ let Features = "sse4.1", Attributes = [NoThrow, Const, RequiredVectorWidth<128>]
327327
def ptestz128 : X86Builtin<"int(_Vector<2, long long int>, _Vector<2, long long int>)">;
328328
def ptestc128 : X86Builtin<"int(_Vector<2, long long int>, _Vector<2, long long int>)">;
329329
def ptestnzc128 : X86Builtin<"int(_Vector<2, long long int>, _Vector<2, long long int>)">;
330-
def mpsadbw128 : X86Builtin<"_Vector<16, char>(_Vector<16, char>, _Vector<16, char>, _Constant char)">;
330+
def mpsadbw128 : X86Builtin<"_Vector<16, char>(_Vector<16, char>, "
331+
"_Vector<16, char>, _Constant char)">;
331332
}
332333

333334
let Features = "sse4.1", Attributes = [NoThrow, Const, Constexpr, RequiredVectorWidth<128>] in {

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,46 @@ static bool interp__builtin_x86_insert_subvector(InterpState &S, CodePtr OpPC,
28602860
return true;
28612861
}
28622862

2863+
static bool interp__builtin_ia32_phminposuw(InterpState &S, CodePtr OpPC,
2864+
const CallExpr *Call) {
2865+
assert(Call->getNumArgs() == 1);
2866+
2867+
const Pointer &Source = S.Stk.pop<Pointer>();
2868+
const Pointer &Dest = S.Stk.peek<Pointer>();
2869+
2870+
unsigned SourceLen = Source.getNumElems();
2871+
QualType ElemQT = getElemType(Source);
2872+
OptPrimType ElemPT = S.getContext().classify(ElemQT);
2873+
unsigned LaneBitWidth = S.getASTContext().getTypeSize(ElemQT);
2874+
2875+
INT_TYPE_SWITCH_NO_BOOL(*ElemPT, {
2876+
APInt MinIndex(LaneBitWidth, 0);
2877+
APInt MinVal = Source.elem<T>(0).toAPSInt();
2878+
2879+
for (unsigned I = 0; I != SourceLen; ++I) {
2880+
APInt Val = Source.elem<T>(I).toAPSInt();
2881+
if (MinVal.ugt(Val)) {
2882+
MinVal = Val;
2883+
MinIndex = I;
2884+
}
2885+
}
2886+
2887+
bool DestUnsigned = Call->getCallReturnType(S.getASTContext())
2888+
->castAs<VectorType>()
2889+
->getElementType()
2890+
->isUnsignedIntegerOrEnumerationType();
2891+
2892+
Dest.elem<T>(0) = static_cast<T>(APSInt(MinVal, DestUnsigned));
2893+
Dest.elem<T>(1) = static_cast<T>(APSInt(MinIndex, DestUnsigned));
2894+
for (unsigned I = 2; I != SourceLen; ++I) {
2895+
Dest.elem<T>(I) =
2896+
static_cast<T>(APSInt(APInt(LaneBitWidth, 0), DestUnsigned));
2897+
}
2898+
});
2899+
Dest.initializeAllElements();
2900+
return true;
2901+
}
2902+
28632903
static bool interp__builtin_ia32_pternlog(InterpState &S, CodePtr OpPC,
28642904
const CallExpr *Call, bool MaskZ) {
28652905
assert(Call->getNumArgs() == 5);
@@ -3809,6 +3849,10 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
38093849
S, OpPC, Call,
38103850
[](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
38113851

3852+
case X86::BI__builtin_ia32_phminposuw128: {
3853+
return interp__builtin_ia32_phminposuw(S, OpPC, Call);
3854+
}
3855+
38123856
case X86::BI__builtin_ia32_pternlogd128_mask:
38133857
case X86::BI__builtin_ia32_pternlogd256_mask:
38143858
case X86::BI__builtin_ia32_pternlogd512_mask:

clang/lib/AST/ExprConstant.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12216,6 +12216,40 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
1221612216
return Success(R, E);
1221712217
}
1221812218

12219+
case X86::BI__builtin_ia32_phminposuw128: {
12220+
APValue Source;
12221+
if (!Evaluate(Source, Info, E->getArg(0)))
12222+
return false;
12223+
unsigned SourceLen = Source.getVectorLength();
12224+
const VectorType *VT = E->getArg(0)->getType()->castAs<VectorType>();
12225+
QualType ElemQT = VT->getElementType();
12226+
unsigned LaneBitWidth = Info.Ctx.getTypeSize(ElemQT);
12227+
12228+
APInt MinIndex(LaneBitWidth, 0);
12229+
APInt MinVal = Source.getVectorElt(0).getInt();
12230+
for (unsigned I = 0; I != SourceLen; ++I) {
12231+
APInt Val = Source.getVectorElt(I).getInt();
12232+
if (MinVal.ugt(Val)) {
12233+
MinVal = Val;
12234+
MinIndex = I;
12235+
}
12236+
}
12237+
12238+
bool ResultUnsigned = E->getCallReturnType(Info.Ctx)
12239+
->castAs<VectorType>()
12240+
->getElementType()
12241+
->isUnsignedIntegerOrEnumerationType();
12242+
12243+
SmallVector<APValue, 8> Result;
12244+
Result.reserve(SourceLen);
12245+
Result.emplace_back(APSInt(MinVal, ResultUnsigned));
12246+
Result.emplace_back(APSInt(MinIndex, ResultUnsigned));
12247+
for (unsigned I = 0; I != SourceLen - 2; ++I) {
12248+
Result.emplace_back(APSInt(APInt(LaneBitWidth, 0), ResultUnsigned));
12249+
}
12250+
return Success(APValue(Result.data(), Result.size()), E);
12251+
}
12252+
1221912253
case X86::BI__builtin_ia32_pternlogd128_mask:
1222012254
case X86::BI__builtin_ia32_pternlogd256_mask:
1222112255
case X86::BI__builtin_ia32_pternlogd512_mask:

clang/lib/Headers/smmintrin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,8 @@ _mm_packus_epi32(__m128i __V1, __m128i __V2) {
15241524
/// \returns A 128-bit value where bits [15:0] contain the minimum value found
15251525
/// in parameter \a __V, bits [18:16] contain the index of the minimum value
15261526
/// and the remaining bits are set to 0.
1527-
static __inline__ __m128i __DEFAULT_FN_ATTRS_CONSTEXPR _mm_minpos_epu16(__m128i __V) {
1527+
static __inline__ __m128i __DEFAULT_FN_ATTRS_CONSTEXPR
1528+
_mm_minpos_epu16(__m128i __V) {
15281529
return (__m128i)__builtin_ia32_phminposuw128((__v8hi)__V);
15291530
}
15301531

clang/test/CodeGen/X86/sse41-builtins.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ __m128i test_mm_minpos_epu16(__m128i x) {
376376
// CHECK: call <8 x i16> @llvm.x86.sse41.phminposuw(<8 x i16> %{{.*}})
377377
return _mm_minpos_epu16(x);
378378
}
379+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){0,0,0,0, 0,0,0,0}), 0,0,0,0, 0,0,0,0));
380+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){1,0,0,0, 0,0,0,0}), 0,1,0,0, 0,0,0,0));
381+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){65535,65535,65535,65535,65535,65535,65535,65535}), 65535,0,0,0, 0,0,0,0));
382+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){9,8,7,6,5,4,3,2}), 2,7,0,0, 0,0,0,0));
383+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){5,5,5,5,5,5,5,5}), 5,0,0,0, 0,0,0,0));
384+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){5,7,9,4,10,4,11,12}), 4,3,0,0, 0,0,0,0));
385+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){6,0,0,0,0,0,0,0}), 0,1,0,0, 0,0,0,0));
386+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){1000,2000,3000,4000,5000,6000,7000,1}), 1,7,0,0, 0,0,0,0));
387+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){1234,5678,42,9999,65535,0,4242,42}), 0,5,0,0, 0,0,0,0));
388+
TEST_CONSTEXPR(match_v8hu(_mm_minpos_epu16((__m128i)(__v8hu){400,500,12,600,12,700,800,900}), 12,2,0,0, 0,0,0,0));
379389

380390
__m128i test_mm_mpsadbw_epu8(__m128i x, __m128i y) {
381391
// CHECK-LABEL: test_mm_mpsadbw_epu8

0 commit comments

Comments
 (0)