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

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 20 13:39:25 PDT 2020


Author: Simon Pilgrim
Date: 2020-08-20T21:38:04+01:00
New Revision: cff0db08761f310dfebb5b41b307d2c12bda85fc

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

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

This is a first step patch to enable constexpr support and testing to a large number of x86 intrinsics.

All I've done here is provide a DEFAULT_FN_ATTRS_CONSTEXPR variant to our existing DEFAULT_FN_ATTRS tag approach that adds constexpr on c++ builds. The clang cuda headers do something similar.

I've started with POPCNT mainly as its tiny and are wrappers to generic __builtin_* intrinsics which already act as constexpr.

Differential Revision: https://reviews.llvm.org/D86229

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 03eca8a26843..f97e95a55c0e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -153,6 +153,12 @@ 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.
+
 Internal API Changes
 --------------------
 

diff  --git a/clang/lib/Headers/popcntintrin.h b/clang/lib/Headers/popcntintrin.h
index 312901014796..0aa94aecda5b 100644
--- a/clang/lib/Headers/popcntintrin.h
+++ b/clang/lib/Headers/popcntintrin.h
@@ -13,6 +13,12 @@
 /* Define the default attributes for the functions in this file. */
 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("popcnt")))
 
+#if defined(__cplusplus) && (__cplusplus >= 201103L)
+#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS constexpr
+#else
+#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS
+#endif
+
 /// Counts the number of bits in the source operand having a value of 1.
 ///
 /// \headerfile <x86intrin.h>
@@ -23,7 +29,7 @@
 ///    An unsigned 32-bit integer operand.
 /// \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
 _mm_popcnt_u32(unsigned int __A)
 {
   return __builtin_popcount(__A);
@@ -40,7 +46,7 @@ _mm_popcnt_u32(unsigned int __A)
 ///    An unsigned 64-bit integer operand.
 /// \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
 _mm_popcnt_u64(unsigned long long __A)
 {
   return __builtin_popcountll(__A);
@@ -48,5 +54,6 @@ _mm_popcnt_u64(unsigned long long __A)
 #endif /* __x86_64__ */
 
 #undef __DEFAULT_FN_ATTRS
+#undef __DEFAULT_FN_ATTRS_CONSTEXPR
 
 #endif /* __POPCNTINTRIN_H */

diff  --git a/clang/test/CodeGen/popcnt-builtins.c b/clang/test/CodeGen/popcnt-builtins.c
index d9e7c5238513..40b786275e93 100644
--- a/clang/test/CodeGen/popcnt-builtins.c
+++ b/clang/test/CodeGen/popcnt-builtins.c
@@ -1,6 +1,7 @@
-// RUN: %clang_cc1 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +popcnt -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,CHECK-POPCNT
-// RUN: %clang_cc1 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
-
+// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +popcnt -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,CHECK-POPCNT
+// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +popcnt -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,CHECK-POPCNT
+// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
 
 #include <x86intrin.h>
 
@@ -39,3 +40,16 @@ long long test__popcntq(unsigned long long __X) {
   return __popcntq(__X);
 }
 #endif
+
+// Test constexpr handling.
+#if defined(__cplusplus) && (__cplusplus >= 201103L)
+#if defined(__POPCNT__)
+char ctpop32_0[_mm_popcnt_u32(0x00000000) == 0 ? 1 : -1];
+char ctpop32_1[_mm_popcnt_u32(0x000000F0) == 4 ? 1 : -1];
+
+#ifdef __x86_64__
+char ctpop64_0[_mm_popcnt_u64(0x0000000000000000ULL) == 0 ? 1 : -1];
+char ctpop64_1[_mm_popcnt_u64(0xF000000000000001ULL) == 5 ? 1 : -1];
+#endif
+#endif
+#endif


        


More information about the cfe-commits mailing list