Skip to content

Commit c7fb0ee

Browse files
authored
[clang][x86] Add constexpr support for ADC/SBB + ADX intrinsics (#110668)
ADC and ADX use the same internal intrinsics - for testing I've taken the same approach as the generic builtin overflow tests, putting the intrinsics in a constexpr test wrapper and comparing the carry/result value pair. I've added the addcarry/subborrow intrinsics to the clang language extension list - I'm not sure if we want to add all ISA intrinsics to the list (although we can if people think it useful?), but I felt we should at least include the baseline x86 intrinsics.
1 parent 8282c58 commit c7fb0ee

File tree

9 files changed

+180
-6
lines changed

9 files changed

+180
-6
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5776,6 +5776,8 @@ The following builtin intrinsics can be used in constant expressions:
57765776
57775777
The following x86-specific intrinsics can be used in constant expressions:
57785778
5779+
* ``_addcarry_u32``
5780+
* ``_addcarry_u64``
57795781
* ``_bit_scan_forward``
57805782
* ``_bit_scan_reverse``
57815783
* ``__bsfd``
@@ -5816,6 +5818,8 @@ The following x86-specific intrinsics can be used in constant expressions:
58165818
* ``_rotwr``
58175819
* ``_lrotl``
58185820
* ``_lrotr``
5821+
* ``_subborrow_u32``
5822+
* ``_subborrow_u64``
58195823
58205824
Debugging the Compiler
58215825
======================

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ X86 Support
536536
* Supported MINMAX intrinsics of ``*_(mask(z)))_minmax(ne)_p[s|d|h|bh]`` and
537537
``*_(mask(z)))_minmax_s[s|d|h]``.
538538

539+
- All intrinsics in adcintrin.h can now be used in constant expressions.
540+
541+
- All intrinsics in adxintrin.h can now be used in constant expressions.
542+
539543
- All intrinsics in lzcntintrin.h can now be used in constant expressions.
540544

541545
- All intrinsics in bmiintrin.h can now be used in constant expressions.

clang/include/clang/Basic/BuiltinsX86.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ TARGET_BUILTIN(__builtin_ia32_wbinvd, "v", "n", "")
543543
TARGET_BUILTIN(__builtin_ia32_wbnoinvd, "v", "n", "wbnoinvd")
544544

545545
// ADX
546-
TARGET_BUILTIN(__builtin_ia32_addcarryx_u32, "UcUcUiUiUi*", "n", "")
547-
TARGET_BUILTIN(__builtin_ia32_subborrow_u32, "UcUcUiUiUi*", "n", "")
546+
TARGET_BUILTIN(__builtin_ia32_addcarryx_u32, "UcUcUiUiUi*", "nE", "")
547+
TARGET_BUILTIN(__builtin_ia32_subborrow_u32, "UcUcUiUiUi*", "nE", "")
548548

549549
// RDSEED
550550
TARGET_BUILTIN(__builtin_ia32_rdseed16_step, "UiUs*", "n", "rdseed")

clang/include/clang/Basic/BuiltinsX86_64.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ TARGET_BUILTIN(__builtin_ia32_incsspq, "vUOi", "n", "shstk")
6666
TARGET_BUILTIN(__builtin_ia32_rdsspq, "UOiUOi", "n", "shstk")
6767
TARGET_BUILTIN(__builtin_ia32_wrssq, "vUOiv*", "n", "shstk")
6868
TARGET_BUILTIN(__builtin_ia32_wrussq, "vUOiv*", "n", "shstk")
69-
TARGET_BUILTIN(__builtin_ia32_addcarryx_u64, "UcUcUOiUOiUOi*", "n", "")
70-
TARGET_BUILTIN(__builtin_ia32_subborrow_u64, "UcUcUOiUOiUOi*", "n", "")
69+
TARGET_BUILTIN(__builtin_ia32_addcarryx_u64, "UcUcUOiUOiUOi*", "nE", "")
70+
TARGET_BUILTIN(__builtin_ia32_subborrow_u64, "UcUcUOiUOiUOi*", "nE", "")
7171
TARGET_BUILTIN(__builtin_ia32_rdrand64_step, "UiUOi*", "n", "rdrnd")
7272
TARGET_BUILTIN(__builtin_ia32_rdseed64_step, "UiUOi*", "n", "rdseed")
7373
TARGET_BUILTIN(__builtin_ia32_lzcnt_u64, "UOiUOi", "ncE", "lzcnt")

clang/lib/AST/ExprConstant.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13464,6 +13464,38 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1346413464
return Success(DidOverflow, E);
1346513465
}
1346613466

13467+
case clang::X86::BI__builtin_ia32_addcarryx_u32:
13468+
case clang::X86::BI__builtin_ia32_addcarryx_u64:
13469+
case clang::X86::BI__builtin_ia32_subborrow_u32:
13470+
case clang::X86::BI__builtin_ia32_subborrow_u64: {
13471+
LValue ResultLValue;
13472+
APSInt CarryIn, LHS, RHS;
13473+
QualType ResultType = E->getArg(3)->getType()->getPointeeType();
13474+
if (!EvaluateInteger(E->getArg(0), CarryIn, Info) ||
13475+
!EvaluateInteger(E->getArg(1), LHS, Info) ||
13476+
!EvaluateInteger(E->getArg(2), RHS, Info) ||
13477+
!EvaluatePointer(E->getArg(3), ResultLValue, Info))
13478+
return false;
13479+
13480+
bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
13481+
BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
13482+
13483+
unsigned BitWidth = LHS.getBitWidth();
13484+
unsigned CarryInBit = CarryIn.ugt(0) ? 1 : 0;
13485+
APInt ExResult =
13486+
IsAdd
13487+
? (LHS.zext(BitWidth + 1) + (RHS.zext(BitWidth + 1) + CarryInBit))
13488+
: (LHS.zext(BitWidth + 1) - (RHS.zext(BitWidth + 1) + CarryInBit));
13489+
13490+
APInt Result = ExResult.extractBits(BitWidth, 0);
13491+
uint64_t CarryOut = ExResult.extractBitsAsZExtValue(1, BitWidth);
13492+
13493+
APValue APV{APSInt(Result, /*isUnsigned=*/true)};
13494+
if (!handleAssignment(Info, E, ResultLValue, ResultType, APV))
13495+
return false;
13496+
return Success(CarryOut, E);
13497+
}
13498+
1346713499
case clang::X86::BI__builtin_ia32_bextr_u32:
1346813500
case clang::X86::BI__builtin_ia32_bextr_u64:
1346913501
case clang::X86::BI__builtin_ia32_bextri_u32:

clang/lib/Headers/adcintrin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
#endif
1616

1717
/* Define the default attributes for the functions in this file. */
18+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
19+
#define __DEFAULT_FN_ATTRS \
20+
__attribute__((__always_inline__, __nodebug__)) constexpr
21+
#else
1822
#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
23+
#endif
1924

2025
/* Use C++ inline semantics in C++, GNU inline for C mode. */
2126
#if defined(__cplusplus)

clang/lib/Headers/adxintrin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
#define __ADXINTRIN_H
1616

1717
/* Define the default attributes for the functions in this file. */
18+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
19+
#define __DEFAULT_FN_ATTRS \
20+
__attribute__((__always_inline__, __nodebug__, __target__("adx"))) constexpr
21+
#else
1822
#define __DEFAULT_FN_ATTRS \
1923
__attribute__((__always_inline__, __nodebug__, __target__("adx")))
24+
#endif
2025

2126
/* Use C++ inline semantics in C++, GNU inline for C mode. */
2227
#if defined(__cplusplus)

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -x c++ -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
23

34
#include <x86intrin.h>
45

@@ -43,3 +44,81 @@ unsigned char test_subborrow_u64(unsigned char __cf, unsigned long long __x,
4344
// CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[SBB]], 0
4445
return _subborrow_u64(__cf, __x, __y, __p);
4546
}
47+
48+
// Test constexpr handling.
49+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
50+
51+
template<typename X>
52+
struct Result {
53+
unsigned char A;
54+
X B;
55+
constexpr bool operator==(const Result<X> &Other) {
56+
return A == Other.A && B == Other.B;
57+
}
58+
};
59+
60+
constexpr Result<unsigned int>
61+
const_test_addcarry_u32(unsigned char __cf, unsigned int __x, unsigned int __y)
62+
{
63+
unsigned int __r{};
64+
return { _addcarry_u32(__cf, __x, __y, &__r), __r };
65+
}
66+
67+
void constexpr adcu32() {
68+
static_assert(const_test_addcarry_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000});
69+
static_assert(const_test_addcarry_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFF});
70+
static_assert(const_test_addcarry_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{1, 0x00000000});
71+
static_assert(const_test_addcarry_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFE});
72+
static_assert(const_test_addcarry_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF});
73+
}
74+
75+
constexpr Result<unsigned int>
76+
const_test_subborrow_u32(unsigned char __cf, unsigned int __x, unsigned int __y)
77+
{
78+
unsigned int __r{};
79+
return { _subborrow_u32(__cf, __x, __y, &__r), __r };
80+
}
81+
82+
void constexpr sbbu32() {
83+
static_assert(const_test_subborrow_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000});
84+
static_assert(const_test_subborrow_u32(0, 0x00000000, 0x00000001) == Result<unsigned int>{1, 0xFFFFFFFF});
85+
static_assert(const_test_subborrow_u32(1, 0x00000000, 0x00000001) == Result<unsigned int>{1, 0xFFFFFFFE});
86+
static_assert(const_test_subborrow_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFD});
87+
static_assert(const_test_subborrow_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{0, 0xFFFFFFFC});
88+
static_assert(const_test_subborrow_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{0, 0x00000000});
89+
static_assert(const_test_subborrow_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF});
90+
}
91+
92+
constexpr Result<unsigned long long>
93+
const_test_addcarry_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y)
94+
{
95+
unsigned long long __r{};
96+
return { _addcarry_u64(__cf, __x, __y, &__r), __r };
97+
}
98+
99+
void constexpr adcu64() {
100+
static_assert(const_test_addcarry_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL});
101+
static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFFULL});
102+
static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0x0000000000000000ULL});
103+
static_assert(const_test_addcarry_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL});
104+
static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL});
105+
}
106+
107+
constexpr Result<unsigned long long>
108+
const_test_subborrow_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y)
109+
{
110+
unsigned long long __r{};
111+
return { _subborrow_u64(__cf, __x, __y, &__r), __r };
112+
}
113+
114+
void constexpr sbbu64() {
115+
static_assert(const_test_subborrow_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL});
116+
static_assert(const_test_subborrow_u64(0, 0x0000000000000000ULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL});
117+
static_assert(const_test_subborrow_u64(1, 0x0000000000000000ULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL});
118+
static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFDULL});
119+
static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFCULL});
120+
static_assert(const_test_subborrow_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{0, 0x0000000000000000ULL});
121+
static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL});
122+
}
123+
124+
#endif

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -x c -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s
23

34
#include <immintrin.h>
45

@@ -22,3 +23,47 @@ unsigned char test_addcarryx_u64(unsigned char __cf, unsigned long long __x,
2223
// CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[ADC]], 0
2324
return _addcarryx_u64(__cf, __x, __y, __p);
2425
}
26+
27+
// Test constexpr handling.
28+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
29+
30+
template<typename X>
31+
struct Result {
32+
unsigned char A;
33+
X B;
34+
constexpr bool operator==(const Result<X> &Other) {
35+
return A == Other.A && B == Other.B;
36+
}
37+
};
38+
39+
constexpr Result<unsigned int>
40+
const_test_addcarryx_u32(unsigned char __cf, unsigned int __x, unsigned int __y)
41+
{
42+
unsigned int __r{};
43+
return { _addcarryx_u32(__cf, __x, __y, &__r), __r };
44+
}
45+
46+
void constexpr addxu32() {
47+
static_assert(const_test_addcarryx_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000});
48+
static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFF});
49+
static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{1, 0x00000000});
50+
static_assert(const_test_addcarryx_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFE});
51+
static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF});
52+
}
53+
54+
constexpr Result<unsigned long long>
55+
const_test_addcarryx_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y)
56+
{
57+
unsigned long long __r{};
58+
return { _addcarryx_u64(__cf, __x, __y, &__r), __r };
59+
}
60+
61+
void constexpr addxu64() {
62+
static_assert(const_test_addcarryx_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL});
63+
static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFFULL});
64+
static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0x0000000000000000ULL});
65+
static_assert(const_test_addcarryx_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL});
66+
static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL});
67+
}
68+
69+
#endif

0 commit comments

Comments
 (0)