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

Simon Pilgrim via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 19 09:58:52 PDT 2020


RKSimon created this revision.
RKSimon added reviewers: craig.topper, echristo, spatel, erichkeane, andreadb.
Herald added a project: clang.
RKSimon requested review of this revision.

This is a RFC 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.

Testing wise - at the moment I've copied the existing popcnt-builtins.c file - popcnt-builtins.cpp will fail to build if the intrinsic can't correctly handle the constexpr modifier - I'm open to suggestions on whether we can avoid duplication and still have test coverage on c and cpp.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86229

Files:
  clang/lib/Headers/popcntintrin.h
  clang/test/CodeGen/popcnt-builtins.cpp


Index: clang/test/CodeGen/popcnt-builtins.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/popcnt-builtins.cpp
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -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 -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+
+
+#include <x86intrin.h>
+
+#ifdef __POPCNT__
+int test_mm_popcnt_u32(unsigned int __X) {
+  //CHECK-POPCNT: call i32 @llvm.ctpop.i32
+  return _mm_popcnt_u32(__X);
+}
+#endif
+
+int test_popcnt32(unsigned int __X) {
+  //CHECK: call i32 @llvm.ctpop.i32
+  return _popcnt32(__X);
+}
+
+int test__popcntd(unsigned int __X) {
+  //CHECK: call i32 @llvm.ctpop.i32
+  return __popcntd(__X);
+}
+
+#ifdef __x86_64__
+#ifdef __POPCNT__
+long long test_mm_popcnt_u64(unsigned long long __X) {
+  //CHECK-POPCNT: call i64 @llvm.ctpop.i64
+  return _mm_popcnt_u64(__X);
+}
+#endif
+
+long long test_popcnt64(unsigned long long __X) {
+  //CHECK: call i64 @llvm.ctpop.i64
+  return _popcnt64(__X);
+}
+
+long long test__popcntq(unsigned long long __X) {
+  //CHECK: call i64 @llvm.ctpop.i64
+  return __popcntq(__X);
+}
+#endif
Index: clang/lib/Headers/popcntintrin.h
===================================================================
--- clang/lib/Headers/popcntintrin.h
+++ 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")))
 
+#ifdef __cplusplus
+#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 @@
 ///    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 @@
 #endif /* __x86_64__ */
 
 #undef __DEFAULT_FN_ATTRS
+#undef __DEFAULT_FN_ATTRS_CONSTEXPR
 
 #endif /* __POPCNTINTRIN_H */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86229.286584.patch
Type: text/x-patch
Size: 2754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200819/1a01521c/attachment.bin>


More information about the cfe-commits mailing list