Skip to content

Commit 773e6c3

Browse files
authored
[clang][bytecode] Support remaining add_sat like X86 builtins (llvm#155358)
1 parent 745415d commit 773e6c3

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

clang/lib/AST/ByteCode/Integral.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ template <unsigned Bits, bool Signed> class Integral final {
318318
template <typename T> static bool CheckMulUB(T A, T B, T &R) {
319319
if constexpr (std::is_signed_v<T>) {
320320
return llvm::MulOverflow<T>(A, B, R);
321+
} else if constexpr (sizeof(T) < sizeof(int)) {
322+
// Silly integer promotion rules will convert both A and B to int,
323+
// even it T is unsigned. Prevent that by manually casting to uint first.
324+
R = static_cast<T>(static_cast<unsigned>(A) * static_cast<unsigned>(B));
325+
return false;
321326
} else {
322327
R = A * B;
323328
return false;

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,9 +2514,9 @@ static bool interp__builtin_is_within_lifetime(InterpState &S, CodePtr OpPC,
25142514
return true;
25152515
}
25162516

2517-
static bool interp__builtin_elementwise_sat(InterpState &S, CodePtr OpPC,
2518-
const CallExpr *Call,
2519-
unsigned BuiltinID) {
2517+
static bool interp__builtin_elementwise_int_binop(InterpState &S, CodePtr OpPC,
2518+
const CallExpr *Call,
2519+
unsigned BuiltinID) {
25202520
assert(Call->getNumArgs() == 2);
25212521

25222522
// Single integer case.
@@ -2553,6 +2553,8 @@ static bool interp__builtin_elementwise_sat(InterpState &S, CodePtr OpPC,
25532553
const Pointer &LHS = S.Stk.pop<Pointer>();
25542554
const Pointer &Dst = S.Stk.peek<Pointer>();
25552555
PrimType ElemT = *S.getContext().classify(VT->getElementType());
2556+
bool DestUnsigned =
2557+
VT->getElementType()->isUnsignedIntegerOrEnumerationType();
25562558
unsigned NumElems = VT->getNumElements();
25572559
for (unsigned I = 0; I != NumElems; ++I) {
25582560
APSInt Elem1;
@@ -2586,6 +2588,34 @@ static bool interp__builtin_elementwise_sat(InterpState &S, CodePtr OpPC,
25862588
Result = APSInt(llvm::APIntOps::mulhs(Elem1, Elem2),
25872589
/*isUnsigned=*/false);
25882590
break;
2591+
case clang::X86::BI__builtin_ia32_psllv2di:
2592+
case clang::X86::BI__builtin_ia32_psllv4di:
2593+
case clang::X86::BI__builtin_ia32_psllv4si:
2594+
case clang::X86::BI__builtin_ia32_psllv8si:
2595+
if (Elem2.uge(Elem2.getBitWidth())) {
2596+
Result = APSInt(APInt::getZero(Elem2.getBitWidth()), DestUnsigned);
2597+
break;
2598+
}
2599+
Result = APSInt(Elem1.shl(Elem2.getZExtValue()), DestUnsigned);
2600+
break;
2601+
case clang::X86::BI__builtin_ia32_psrav4si:
2602+
case clang::X86::BI__builtin_ia32_psrav8si:
2603+
if (Elem2.uge(Elem2.getBitWidth())) {
2604+
Result = APSInt(Elem1.ashr(Elem2.getBitWidth() - 1), DestUnsigned);
2605+
break;
2606+
}
2607+
Result = APSInt(Elem1.ashr(Elem2.getZExtValue()), DestUnsigned);
2608+
break;
2609+
case clang::X86::BI__builtin_ia32_psrlv2di:
2610+
case clang::X86::BI__builtin_ia32_psrlv4di:
2611+
case clang::X86::BI__builtin_ia32_psrlv4si:
2612+
case clang::X86::BI__builtin_ia32_psrlv8si:
2613+
if (Elem2.uge(Elem2.getBitWidth())) {
2614+
Result = APSInt(APInt::getZero(Elem2.getBitWidth()), DestUnsigned);
2615+
break;
2616+
}
2617+
Result = APSInt(Elem1.lshr(Elem2.getZExtValue()), DestUnsigned);
2618+
break;
25892619
default:
25902620
llvm_unreachable("Wrong builtin ID");
25912621
}
@@ -3232,7 +3262,17 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
32323262
case clang::X86::BI__builtin_ia32_pmulhw128:
32333263
case clang::X86::BI__builtin_ia32_pmulhw256:
32343264
case clang::X86::BI__builtin_ia32_pmulhw512:
3235-
return interp__builtin_elementwise_sat(S, OpPC, Call, BuiltinID);
3265+
case clang::X86::BI__builtin_ia32_psllv2di:
3266+
case clang::X86::BI__builtin_ia32_psllv4di:
3267+
case clang::X86::BI__builtin_ia32_psllv4si:
3268+
case clang::X86::BI__builtin_ia32_psllv8si:
3269+
case clang::X86::BI__builtin_ia32_psrav4si:
3270+
case clang::X86::BI__builtin_ia32_psrav8si:
3271+
case clang::X86::BI__builtin_ia32_psrlv2di:
3272+
case clang::X86::BI__builtin_ia32_psrlv4di:
3273+
case clang::X86::BI__builtin_ia32_psrlv4si:
3274+
case clang::X86::BI__builtin_ia32_psrlv8si:
3275+
return interp__builtin_elementwise_int_binop(S, OpPC, Call, BuiltinID);
32363276

32373277
case Builtin::BI__builtin_elementwise_max:
32383278
case Builtin::BI__builtin_elementwise_min:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefixes=CHECK,X86
88
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefixes=CHECK,X86
99

10+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +avx2 -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X64
11+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +avx2 -fno-signed-char -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X64
12+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X86
13+
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -fno-signed-char -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X86
14+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +avx2 -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X64
15+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +avx2 -fno-signed-char -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X64
16+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X86
17+
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=i386-apple-darwin -target-feature +avx2 -fno-signed-char -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,X86
1018

1119
#include <immintrin.h>
1220
#include "builtin_test_helpers.h"

0 commit comments

Comments
 (0)