[clang] c7fb0ee - [clang][x86] Add constexpr support for ADC/SBB + ADX intrinsics (#110668)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 2 07:45:07 PDT 2024


Author: Simon Pilgrim
Date: 2024-10-02T15:45:02+01:00
New Revision: c7fb0eed05e768093fc202e94df1c0d88fd7c2f0

URL: https://github.com/llvm/llvm-project/commit/c7fb0eed05e768093fc202e94df1c0d88fd7c2f0
DIFF: https://github.com/llvm/llvm-project/commit/c7fb0eed05e768093fc202e94df1c0d88fd7c2f0.diff

LOG: [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.

Added: 
    

Modified: 
    clang/docs/LanguageExtensions.rst
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/BuiltinsX86.def
    clang/include/clang/Basic/BuiltinsX86_64.def
    clang/lib/AST/ExprConstant.cpp
    clang/lib/Headers/adcintrin.h
    clang/lib/Headers/adxintrin.h
    clang/test/CodeGen/X86/adc-builtins.c
    clang/test/CodeGen/X86/adx-builtins.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index f0506ac09c0a2d..fe41742a25c642 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5776,6 +5776,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``
@@ -5816,6 +5818,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 1402304d7e00d3..b7d287fdf4cc61 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -536,6 +536,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 48816d3078826c..3a73cea97fcc32 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13464,6 +13464,38 @@ 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:
+  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;
+
+    bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
+                 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
+
+    unsigned BitWidth = LHS.getBitWidth();
+    unsigned CarryInBit = CarryIn.ugt(0) ? 1 : 0;
+    APInt ExResult =
+        IsAdd
+            ? (LHS.zext(BitWidth + 1) + (RHS.zext(BitWidth + 1) + CarryInBit))
+            : (LHS.zext(BitWidth + 1) - (RHS.zext(BitWidth + 1) + CarryInBit));
+
+    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


        


More information about the cfe-commits mailing list