[clang] c6863a4 - [X86] Enable constexpr on POPCNT intrinsics (PR31446)

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 21 02:20:51 PDT 2020


Author: Simon Pilgrim
Date: 2020-08-21T10:20:37+01:00
New Revision: c6863a4ab8b96ba4b878cb2da7e101bc6bcfaeef

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

LOG: [X86] Enable constexpr on POPCNT intrinsics (PR31446)

Followup to D86229, this enables constexpr on the alternative (which fallback to generic code) POPCNT intrinsics defined in ia32intrin.h

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Headers/ia32intrin.h
    clang/test/CodeGen/popcnt-builtins.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f97e95a55c0e..0c864d8b4d14 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -156,8 +156,9 @@ CUDA Support in Clang
 X86 Support in Clang
 --------------------
 
-- The x86 intrinsics ``_mm_popcnt_u32`` and ``_mm_popcnt_u64`` may now be used
-  within constexpr expressions.
+- The x86 intrinsics ``_mm_popcnt_u32``, ``_mm_popcnt_u64``, ``_popcnt32``,
+  ``_popcnt64``, ``__popcntd`` and ``__popcntq``  may now be used within
+  constexpr expressions.
 
 Internal API Changes
 --------------------

diff  --git a/clang/lib/Headers/ia32intrin.h b/clang/lib/Headers/ia32intrin.h
index b4812517b2ea..bed75e18c4f6 100644
--- a/clang/lib/Headers/ia32intrin.h
+++ b/clang/lib/Headers/ia32intrin.h
@@ -18,6 +18,12 @@
 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
 #define __DEFAULT_FN_ATTRS_SSE42 __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
 
+#if defined(__cplusplus) && (__cplusplus >= 201103L)
+#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS constexpr
+#else
+#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS
+#endif
+
 /** Find the first set bit starting from the lsb. Result is undefined if
  *  input is 0.
  *
@@ -142,7 +148,7 @@ __bswapq(long long __A) {
  *  \returns A 32-bit integer containing the number of bits with value 1 in the
  *     source operand.
  */
-static __inline__ int __DEFAULT_FN_ATTRS
+static __inline__ int __DEFAULT_FN_ATTRS_CONSTEXPR
 __popcntd(unsigned int __A)
 {
   return __builtin_popcount(__A);
@@ -163,7 +169,7 @@ __popcntd(unsigned int __A)
  *  \returns A 64-bit integer containing the number of bits with value 1 in the
  *     source operand.
  */
-static __inline__ long long __DEFAULT_FN_ATTRS
+static __inline__ long long __DEFAULT_FN_ATTRS_CONSTEXPR
 __popcntq(unsigned long long __A)
 {
   return __builtin_popcountll(__A);
@@ -435,5 +441,6 @@ __rorq(unsigned long long __X, int __C) {
 
 #undef __DEFAULT_FN_ATTRS
 #undef __DEFAULT_FN_ATTRS_SSE42
+#undef __DEFAULT_FN_ATTRS_CONSTEXPR
 
 #endif /* __IA32INTRIN_H */

diff  --git a/clang/test/CodeGen/popcnt-builtins.c b/clang/test/CodeGen/popcnt-builtins.c
index 40b786275e93..e59ffaa031a6 100644
--- a/clang/test/CodeGen/popcnt-builtins.c
+++ b/clang/test/CodeGen/popcnt-builtins.c
@@ -46,10 +46,24 @@ long long test__popcntq(unsigned long long __X) {
 #if defined(__POPCNT__)
 char ctpop32_0[_mm_popcnt_u32(0x00000000) == 0 ? 1 : -1];
 char ctpop32_1[_mm_popcnt_u32(0x000000F0) == 4 ? 1 : -1];
+#endif
+
+char popcnt32_0[_popcnt32(0x00000000) == 0 ? 1 : -1];
+char popcnt32_1[_popcnt32(0x100000F0) == 5 ? 1 : -1];
+
+char popcntd_0[__popcntd(0x00000000) == 0 ? 1 : -1];
+char popcntd_1[__popcntd(0x00F000F0) == 8 ? 1 : -1];
 
 #ifdef __x86_64__
+#if defined(__POPCNT__)
 char ctpop64_0[_mm_popcnt_u64(0x0000000000000000ULL) == 0 ? 1 : -1];
 char ctpop64_1[_mm_popcnt_u64(0xF000000000000001ULL) == 5 ? 1 : -1];
 #endif
+
+char popcnt64_0[_popcnt64(0x0000000000000000ULL) == 0 ? 1 : -1];
+char popcnt64_1[_popcnt64(0xF00000F000000001ULL) == 9 ? 1 : -1];
+
+char popcntq_0[__popcntq(0x0000000000000000ULL) == 0 ? 1 : -1];
+char popcntq_1[__popcntq(0xF000010000300001ULL) == 8 ? 1 : -1];
 #endif
 #endif


        


More information about the cfe-commits mailing list