Skip to content

Commit 90fd8a5

Browse files
committed
stdlib: define and use SWIFT_{,UN}LIKELY
Rather than directly using the extension `__builtin_expect`, use a macro to permit the removal of the builtin on compilers which do not support this (i.e. cl). This permits us to build the swift compiler with MSVC again.
1 parent 1a949ea commit 90fd8a5

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

stdlib/public/SwiftShims/RefCount.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class RefCounts {
739739
do {
740740
newbits = oldbits;
741741
bool fast = newbits.incrementStrongExtraRefCount(inc);
742-
if (__builtin_expect(!fast, 0)) {
742+
if (SWIFT_UNLIKELY(!fast)) {
743743
if (oldbits.isImmortal())
744744
return;
745745
return incrementSlow(oldbits, inc);
@@ -752,7 +752,7 @@ class RefCounts {
752752
auto oldbits = refCounts.load(SWIFT_MEMORY_ORDER_CONSUME);
753753
auto newbits = oldbits;
754754
bool fast = newbits.incrementStrongExtraRefCount(inc);
755-
if (__builtin_expect(!fast, 0)) {
755+
if (SWIFT_UNLIKELY(!fast)) {
756756
if (oldbits.isImmortal())
757757
return;
758758
return incrementNonAtomicSlow(oldbits, inc);
@@ -770,7 +770,7 @@ class RefCounts {
770770

771771
newbits = oldbits;
772772
bool fast = newbits.incrementStrongExtraRefCount(1);
773-
if (__builtin_expect(!fast, 0)) {
773+
if (SWIFT_UNLIKELY(!fast)) {
774774
if (oldbits.isImmortal())
775775
return true;
776776
return tryIncrementSlow(oldbits);
@@ -787,7 +787,7 @@ class RefCounts {
787787

788788
auto newbits = oldbits;
789789
bool fast = newbits.incrementStrongExtraRefCount(1);
790-
if (__builtin_expect(!fast, 0)) {
790+
if (SWIFT_UNLIKELY(!fast)) {
791791
if (oldbits.isImmortal())
792792
return true;
793793
return tryIncrementNonAtomicSlow(oldbits);
@@ -1000,7 +1000,7 @@ class RefCounts {
10001000
newbits = oldbits;
10011001
bool fast =
10021002
newbits.decrementStrongExtraRefCount(dec);
1003-
if (__builtin_expect(!fast, 0)) {
1003+
if (SWIFT_UNLIKELY(!fast)) {
10041004
if (oldbits.isImmortal()) {
10051005
return false;
10061006
}

stdlib/public/SwiftShims/Visibility.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#define __has_attribute(x) 0
2727
#endif
2828

29+
#if !defined(__has_builtin)
30+
#define __has_builtin(builtin) 0
31+
#endif
32+
2933
#if __has_feature(nullability)
3034
// Provide macros to temporarily suppress warning about the use of
3135
// _Nullable and _Nonnull.
@@ -160,5 +164,13 @@
160164
#define SWIFT_RUNTIME_STDLIB_INTERNAL SWIFT_LIBRARY_VISIBILITY
161165
#endif
162166

167+
#if __has_builtin(__builtin_expect)
168+
#define SWIFT_LIKELY(expression) (__builtin_expect(!!(expression), 1))
169+
#define SWIFT_UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
170+
#else
171+
#define SWIFT_LIKELY(expression) ((expression))
172+
#define SWIFT_UNLIKELY(expression) ((expression))
173+
#endif
174+
163175
// SWIFT_STDLIB_SHIMS_VISIBILITY_H
164176
#endif

0 commit comments

Comments
 (0)