Skip to content

Commit e92cbfb

Browse files
authored
[libc] Update some __builtin_* usage to be compatible with MSVC. (#157870)
__builtin_trap, __builtin_expect, __builtin_ctz*, __builtin_clz*, __builtin_popcount*.
1 parent a1fc0f9 commit e92cbfb

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,16 @@ countr_zero(T value) {
104104
}
105105
#if __has_builtin(__builtin_ctzs)
106106
ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
107-
#endif
107+
#endif // __has_builtin(__builtin_ctzs)
108+
#if __has_builtin(__builtin_ctz)
108109
ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
110+
#endif // __has_builtin(__builtin_ctz)
111+
#if __has_builtin(__builtin_ctzl)
109112
ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)
113+
#endif // __has_builtin(__builtin_ctzl)
114+
#if __has_builtin(__builtin_ctzll)
110115
ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
116+
#endif // __has_builtin(__builtin_ctzll)
111117
#endif // __has_builtin(__builtin_ctzg)
112118

113119
/// Count number of 0's from the most significant bit to the least
@@ -143,10 +149,16 @@ countl_zero(T value) {
143149
}
144150
#if __has_builtin(__builtin_clzs)
145151
ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
146-
#endif
152+
#endif // __has_builtin(__builtin_clzs)
153+
#if __has_builtin(__builtin_clz)
147154
ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
155+
#endif // __has_builtin(__builtin_clz)
156+
#if __has_builtin(__builtin_clzl)
148157
ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)
158+
#endif // __has_builtin(__builtin_clzl)
159+
#if __has_builtin(__builtin_clzll)
149160
ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)
161+
#endif // __has_builtin(__builtin_clzll)
150162
#endif // __has_builtin(__builtin_clzg)
151163

152164
#undef ADD_SPECIALIZATION
@@ -283,11 +295,17 @@ popcount(T value) {
283295
[[nodiscard]] LIBC_INLINE constexpr int popcount<TYPE>(TYPE value) { \
284296
return BUILTIN(value); \
285297
}
298+
#if __has_builtin(__builtin_popcount)
286299
ADD_SPECIALIZATION(unsigned char, __builtin_popcount)
287300
ADD_SPECIALIZATION(unsigned short, __builtin_popcount)
288301
ADD_SPECIALIZATION(unsigned, __builtin_popcount)
302+
#endif // __builtin_popcount
303+
#if __has_builtin(__builtin_popcountl)
289304
ADD_SPECIALIZATION(unsigned long, __builtin_popcountl)
305+
#endif // __builtin_popcountl
306+
#if __has_builtin(__builtin_popcountll)
290307
ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
308+
#endif // __builtin_popcountll
291309
#endif // __builtin_popcountg
292310
#undef ADD_SPECIALIZATION
293311

libc/src/__support/macros/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ add_header_library(
44
config
55
HDRS
66
config.h
7+
DEPENDS
8+
libc.src.__support.macros.properties.architectures
9+
libc.src.__support.macros.properties.compiler
710
)
811

912
add_header_library(

libc/src/__support/macros/config.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
1414
#define LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
1515

16+
#include "src/__support/macros/properties/architectures.h"
17+
#include "src/__support/macros/properties/compiler.h"
18+
19+
#ifdef LIBC_COMPILER_IS_MSVC
20+
#include <intrin.h>
21+
#endif // LIBC_COMPILER_IS_MSVC
22+
1623
// Workaround for compilers that do not support builtin detection.
1724
// FIXME: This is only required for the GPU portion which should be moved.
1825
#ifndef __has_builtin
@@ -27,6 +34,19 @@
2734
#define LIBC_HAS_FEATURE(f) 0
2835
#endif
2936

37+
#ifdef LIBC_COMPILER_IS_MSVC
38+
39+
// __builtin_trap replacement
40+
#ifdef LIBC_TARGET_ARCH_IS_X86
41+
#define __builtin_trap __ud2
42+
#else // arm64
43+
#define __builtin_trap() __break(1)
44+
#endif
45+
46+
#define __builtin_expect(value, expectation) (value)
47+
48+
#endif // LIBC_COMPILER_IS_MSVC
49+
3050
#ifdef __clang__
3151
// Declare a LIBC_NAMESPACE with hidden visibility. `namespace
3252
// LIBC_NAMESPACE_DECL {` should be used around all declarations and definitions

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ libc_support_library(
117117
libc_support_library(
118118
name = "__support_macros_config",
119119
hdrs = ["src/__support/macros/config.h"],
120+
deps = [
121+
"__support_macros_properties_architectures",
122+
"__support_macros_properties_compiler",
123+
],
120124
)
121125

122126
################################# Include Files ################################

0 commit comments

Comments
 (0)