llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Simon Pilgrim (RKSimon) <details> <summary>Changes</summary> ADC and ADX use the same internal intrinsics - for testing I've taken a same approach as the generic builtin overfloaw 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. --- Full diff: https://github.com/llvm/llvm-project/pull/110668.diff 9 Files Affected: - (modified) clang/docs/LanguageExtensions.rst (+4) - (modified) clang/docs/ReleaseNotes.rst (+4) - (modified) clang/include/clang/Basic/BuiltinsX86.def (+2-2) - (modified) clang/include/clang/Basic/BuiltinsX86_64.def (+2-2) - (modified) clang/lib/AST/ExprConstant.cpp (+48) - (modified) clang/lib/Headers/adcintrin.h (+5) - (modified) clang/lib/Headers/adxintrin.h (+5) - (modified) clang/test/CodeGen/X86/adc-builtins.c (+80-1) - (modified) clang/test/CodeGen/X86/adx-builtins.c (+46-1) ``````````diff diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index ea4b4bcec55e77..88fef0c6c04003 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -5759,6 +5759,8 @@ The following builtin intrinsics can be used in constant expressions: The following x86-specific intrinsics can be used in constant expressions: +* ``_addcarry_u32`` +* ``_addcarry_u64`` * ``_bit_scan_forward`` * ``_bit_scan_reverse`` * ``__bsfd`` @@ -5799,6 +5801,8 @@ The following x86-specific intrinsics can be used in constant expressions: * ``_rotwr`` * ``_lrotl`` * ``_lrotr`` +* ``_subborrow_u32`` +* ``_subborrow_u64`` Debugging the Compiler ====================== diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index a55a3b8687e46e..b36eec120a52b4 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -518,6 +518,10 @@ X86 Support * Supported MINMAX intrinsics of ``*_(mask(z)))_minmax(ne)_p[s|d|h|bh]`` and ``*_(mask(z)))_minmax_s[s|d|h]``. +- All intrinsics in adcintrin.h can now be used in constant expressions. + +- All intrinsics in adxintrin.h can now be used in constant expressions. + - All intrinsics in lzcntintrin.h can now be used in constant expressions. - All intrinsics in bmiintrin.h can now be used in constant expressions. diff --git a/clang/include/clang/Basic/BuiltinsX86.def b/clang/include/clang/Basic/BuiltinsX86.def index 2a987abcf9a350..4c6b22cca421ca 100644 --- a/clang/include/clang/Basic/BuiltinsX86.def +++ b/clang/include/clang/Basic/BuiltinsX86.def @@ -543,8 +543,8 @@ TARGET_BUILTIN(__builtin_ia32_wbinvd, "v", "n", "") TARGET_BUILTIN(__builtin_ia32_wbnoinvd, "v", "n", "wbnoinvd") // ADX -TARGET_BUILTIN(__builtin_ia32_addcarryx_u32, "UcUcUiUiUi*", "n", "") -TARGET_BUILTIN(__builtin_ia32_subborrow_u32, "UcUcUiUiUi*", "n", "") +TARGET_BUILTIN(__builtin_ia32_addcarryx_u32, "UcUcUiUiUi*", "nE", "") +TARGET_BUILTIN(__builtin_ia32_subborrow_u32, "UcUcUiUiUi*", "nE", "") // RDSEED TARGET_BUILTIN(__builtin_ia32_rdseed16_step, "UiUs*", "n", "rdseed") diff --git a/clang/include/clang/Basic/BuiltinsX86_64.def b/clang/include/clang/Basic/BuiltinsX86_64.def index d5fdb272d92d10..2c591edb2835cd 100644 --- a/clang/include/clang/Basic/BuiltinsX86_64.def +++ b/clang/include/clang/Basic/BuiltinsX86_64.def @@ -66,8 +66,8 @@ TARGET_BUILTIN(__builtin_ia32_incsspq, "vUOi", "n", "shstk") TARGET_BUILTIN(__builtin_ia32_rdsspq, "UOiUOi", "n", "shstk") TARGET_BUILTIN(__builtin_ia32_wrssq, "vUOiv*", "n", "shstk") TARGET_BUILTIN(__builtin_ia32_wrussq, "vUOiv*", "n", "shstk") -TARGET_BUILTIN(__builtin_ia32_addcarryx_u64, "UcUcUOiUOiUOi*", "n", "") -TARGET_BUILTIN(__builtin_ia32_subborrow_u64, "UcUcUOiUOiUOi*", "n", "") +TARGET_BUILTIN(__builtin_ia32_addcarryx_u64, "UcUcUOiUOiUOi*", "nE", "") +TARGET_BUILTIN(__builtin_ia32_subborrow_u64, "UcUcUOiUOiUOi*", "nE", "") TARGET_BUILTIN(__builtin_ia32_rdrand64_step, "UiUOi*", "n", "rdrnd") TARGET_BUILTIN(__builtin_ia32_rdseed64_step, "UiUOi*", "n", "rdseed") TARGET_BUILTIN(__builtin_ia32_lzcnt_u64, "UOiUOi", "ncE", "lzcnt") diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index cd8e9ee1e156fa..8dce6e7eab23da 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -13464,6 +13464,54 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, return Success(DidOverflow, E); } + case clang::X86::BI__builtin_ia32_addcarryx_u32: + case clang::X86::BI__builtin_ia32_addcarryx_u64: { + LValue ResultLValue; + APSInt CarryIn, LHS, RHS; + QualType ResultType = E->getArg(3)->getType()->getPointeeType(); + if (!EvaluateInteger(E->getArg(0), CarryIn, Info) || + !EvaluateInteger(E->getArg(1), LHS, Info) || + !EvaluateInteger(E->getArg(2), RHS, Info) || + !EvaluatePointer(E->getArg(3), ResultLValue, Info)) + return false; + + unsigned BitWidth = LHS.getBitWidth(); + APInt ExResult = LHS.zext(BitWidth + 1) + RHS.zext(BitWidth + 1) + + (CarryIn.ugt(0) ? 1 : 0); + + APInt Result = ExResult.extractBits(BitWidth, 0); + uint64_t CarryOut = ExResult.extractBitsAsZExtValue(1, BitWidth); + + APValue APV{APSInt(Result, /*isUnsigned=*/true)}; + if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) + return false; + return Success(CarryOut, E); + } + + case clang::X86::BI__builtin_ia32_subborrow_u32: + case clang::X86::BI__builtin_ia32_subborrow_u64: { + LValue ResultLValue; + APSInt CarryIn, LHS, RHS; + QualType ResultType = E->getArg(3)->getType()->getPointeeType(); + if (!EvaluateInteger(E->getArg(0), CarryIn, Info) || + !EvaluateInteger(E->getArg(1), LHS, Info) || + !EvaluateInteger(E->getArg(2), RHS, Info) || + !EvaluatePointer(E->getArg(3), ResultLValue, Info)) + return false; + + unsigned BitWidth = LHS.getBitWidth(); + APInt ExResult = LHS.zext(BitWidth + 1) - + (RHS.zext(BitWidth + 1) + (CarryIn.ugt(0) ? 1 : 0)); + + APInt Result = ExResult.extractBits(BitWidth, 0); + uint64_t CarryOut = ExResult.extractBitsAsZExtValue(1, BitWidth); + + APValue APV{APSInt(Result, /*isUnsigned=*/true)}; + if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) + return false; + return Success(CarryOut, E); + } + case clang::X86::BI__builtin_ia32_bextr_u32: case clang::X86::BI__builtin_ia32_bextr_u64: case clang::X86::BI__builtin_ia32_bextri_u32: diff --git a/clang/lib/Headers/adcintrin.h b/clang/lib/Headers/adcintrin.h index 0065a1b543f816..5c68fce9370b24 100644 --- a/clang/lib/Headers/adcintrin.h +++ b/clang/lib/Headers/adcintrin.h @@ -15,7 +15,12 @@ #endif /* Define the default attributes for the functions in this file. */ +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define __DEFAULT_FN_ATTRS \ + __attribute__((__always_inline__, __nodebug__)) constexpr +#else #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__)) +#endif /* Use C++ inline semantics in C++, GNU inline for C mode. */ #if defined(__cplusplus) diff --git a/clang/lib/Headers/adxintrin.h b/clang/lib/Headers/adxintrin.h index bc6a4caf35337e..055e91f8e2b302 100644 --- a/clang/lib/Headers/adxintrin.h +++ b/clang/lib/Headers/adxintrin.h @@ -15,8 +15,13 @@ #define __ADXINTRIN_H /* Define the default attributes for the functions in this file. */ +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define __DEFAULT_FN_ATTRS \ + __attribute__((__always_inline__, __nodebug__, __target__("adx"))) constexpr +#else #define __DEFAULT_FN_ATTRS \ __attribute__((__always_inline__, __nodebug__, __target__("adx"))) +#endif /* Use C++ inline semantics in C++, GNU inline for C mode. */ #if defined(__cplusplus) diff --git a/clang/test/CodeGen/X86/adc-builtins.c b/clang/test/CodeGen/X86/adc-builtins.c index 162a78c474f902..87e3b9a662bce1 100644 --- a/clang/test/CodeGen/X86/adc-builtins.c +++ b/clang/test/CodeGen/X86/adc-builtins.c @@ -1,4 +1,5 @@ -// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -x c++ -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s #include <x86intrin.h> @@ -43,3 +44,81 @@ unsigned char test_subborrow_u64(unsigned char __cf, unsigned long long __x, // CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[SBB]], 0 return _subborrow_u64(__cf, __x, __y, __p); } + +// Test constexpr handling. +#if defined(__cplusplus) && (__cplusplus >= 201103L) + +template<typename X> +struct Result { + unsigned char A; + X B; + constexpr bool operator==(const Result<X> &Other) { + return A == Other.A && B == Other.B; + } +}; + +constexpr Result<unsigned int> +const_test_addcarry_u32(unsigned char __cf, unsigned int __x, unsigned int __y) +{ + unsigned int __r{}; + return { _addcarry_u32(__cf, __x, __y, &__r), __r }; +} + +void constexpr adcu32() { + static_assert(const_test_addcarry_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000}); + static_assert(const_test_addcarry_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFF}); + static_assert(const_test_addcarry_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{1, 0x00000000}); + static_assert(const_test_addcarry_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFE}); + static_assert(const_test_addcarry_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF}); +} + +constexpr Result<unsigned int> +const_test_subborrow_u32(unsigned char __cf, unsigned int __x, unsigned int __y) +{ + unsigned int __r{}; + return { _subborrow_u32(__cf, __x, __y, &__r), __r }; +} + +void constexpr sbbu32() { + static_assert(const_test_subborrow_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000}); + static_assert(const_test_subborrow_u32(0, 0x00000000, 0x00000001) == Result<unsigned int>{1, 0xFFFFFFFF}); + static_assert(const_test_subborrow_u32(1, 0x00000000, 0x00000001) == Result<unsigned int>{1, 0xFFFFFFFE}); + static_assert(const_test_subborrow_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFD}); + static_assert(const_test_subborrow_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{0, 0xFFFFFFFC}); + static_assert(const_test_subborrow_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{0, 0x00000000}); + static_assert(const_test_subborrow_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF}); +} + +constexpr Result<unsigned long long> +const_test_addcarry_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y) +{ + unsigned long long __r{}; + return { _addcarry_u64(__cf, __x, __y, &__r), __r }; +} + +void constexpr adcu64() { + static_assert(const_test_addcarry_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL}); + static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFFULL}); + static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0x0000000000000000ULL}); + static_assert(const_test_addcarry_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL}); + static_assert(const_test_addcarry_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL}); +} + +constexpr Result<unsigned long long> +const_test_subborrow_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y) +{ + unsigned long long __r{}; + return { _subborrow_u64(__cf, __x, __y, &__r), __r }; +} + +void constexpr sbbu64() { + static_assert(const_test_subborrow_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL}); + static_assert(const_test_subborrow_u64(0, 0x0000000000000000ULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL}); + static_assert(const_test_subborrow_u64(1, 0x0000000000000000ULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL}); + static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFDULL}); + static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFCULL}); + static_assert(const_test_subborrow_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{0, 0x0000000000000000ULL}); + static_assert(const_test_subborrow_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL}); +} + +#endif \ No newline at end of file diff --git a/clang/test/CodeGen/X86/adx-builtins.c b/clang/test/CodeGen/X86/adx-builtins.c index 563a50b9bc84eb..53861bc3ceb72d 100644 --- a/clang/test/CodeGen/X86/adx-builtins.c +++ b/clang/test/CodeGen/X86/adx-builtins.c @@ -1,4 +1,5 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -x c -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s #include <immintrin.h> @@ -22,3 +23,47 @@ unsigned char test_addcarryx_u64(unsigned char __cf, unsigned long long __x, // CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[ADC]], 0 return _addcarryx_u64(__cf, __x, __y, __p); } + +// Test constexpr handling. +#if defined(__cplusplus) && (__cplusplus >= 201103L) + +template<typename X> +struct Result { + unsigned char A; + X B; + constexpr bool operator==(const Result<X> &Other) { + return A == Other.A && B == Other.B; + } +}; + +constexpr Result<unsigned int> +const_test_addcarryx_u32(unsigned char __cf, unsigned int __x, unsigned int __y) +{ + unsigned int __r{}; + return { _addcarryx_u32(__cf, __x, __y, &__r), __r }; +} + +void constexpr addxu32() { + static_assert(const_test_addcarryx_u32(0, 0x00000000, 0x00000000) == Result<unsigned int>{0, 0x00000000}); + static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFE, 0x00000000) == Result<unsigned int>{0, 0xFFFFFFFF}); + static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFE, 0x00000001) == Result<unsigned int>{1, 0x00000000}); + static_assert(const_test_addcarryx_u32(0, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFE}); + static_assert(const_test_addcarryx_u32(1, 0xFFFFFFFF, 0xFFFFFFFF) == Result<unsigned int>{1, 0xFFFFFFFF}); +} + +constexpr Result<unsigned long long> +const_test_addcarryx_u64(unsigned char __cf, unsigned long long __x, unsigned long long __y) +{ + unsigned long long __r{}; + return { _addcarryx_u64(__cf, __x, __y, &__r), __r }; +} + +void constexpr addxu64() { + static_assert(const_test_addcarryx_u64(0, 0x0000000000000000ULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0x0000000000000000ULL}); + static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL) == Result<unsigned long long>{0, 0xFFFFFFFFFFFFFFFFULL}); + static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000001ULL) == Result<unsigned long long>{1, 0x0000000000000000ULL}); + static_assert(const_test_addcarryx_u64(0, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFEULL}); + static_assert(const_test_addcarryx_u64(1, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL) == Result<unsigned long long>{1, 0xFFFFFFFFFFFFFFFFULL}); +} + +#endif \ No newline at end of file `````````` </details> https://github.com/llvm/llvm-project/pull/110668 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits