Skip to content

Commit

Permalink
Fix and simplify __has_builtin conditions (#73905)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Aug 14, 2022
1 parent 7d986fa commit c2531ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
19 changes: 6 additions & 13 deletions src/coreclr/gc/env/gcenv.base.h
Expand Up @@ -45,6 +45,10 @@
#define SSIZE_T_MAX ((ptrdiff_t)(SIZE_T_MAX / 2))
#endif

#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

#ifndef _INC_WINDOWS
// -----------------------------------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -186,26 +190,15 @@ typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)(void* lpThreadParameter);
#endif
#else // _MSC_VER

#ifdef __llvm__
#define HAS_IA32_PAUSE __has_builtin(__builtin_ia32_pause)
#define HAS_IA32_MFENCE __has_builtin(__builtin_ia32_mfence)
#else
#define HAS_IA32_PAUSE 0
#define HAS_IA32_MFENCE 0
#endif

// Only clang defines __has_builtin, so we first test for a GCC define
// before using __has_builtin.

#if defined(__i386__) || defined(__x86_64__)

#if (__GNUC__ > 4 && __GNUC_MINOR > 7) || HAS_IA32_PAUSE
#if __has_builtin(__builtin_ia32_pause)
// clang added this intrinsic in 3.8
// gcc added this intrinsic by 4.7.1
#define YieldProcessor __builtin_ia32_pause
#endif // __has_builtin(__builtin_ia32_pause)

#if defined(__GNUC__) || HAS_IA32_MFENCE
#if __has_builtin(__builtin_ia32_mfence)
// clang has had this intrinsic since at least 3.0
// gcc has had this intrinsic since forever
#define MemoryBarrier __builtin_ia32_mfence
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Expand Up @@ -584,12 +584,6 @@ void GCToOSInterface::FlushProcessWriteBuffers()
// otherwise raises a SIGTRAP.
void GCToOSInterface::DebugBreak()
{
// __has_builtin is only defined by clang. GCC doesn't have a debug
// trap intrinsic anyway.
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif // __has_builtin

#if __has_builtin(__builtin_debugtrap)
__builtin_debugtrap();
#else
Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
Expand Up @@ -1039,10 +1039,6 @@ REDHAWK_PALEXPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alert
return WaitForSingleObjectEx(pHandles[0], timeout, alertable);
}

#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

#if !__has_builtin(_mm_pause)
extern "C" void _mm_pause()
// Defined for implementing PalYieldProcessor in PalRedhawk.h
Expand Down
18 changes: 7 additions & 11 deletions src/coreclr/pal/inc/pal.h
Expand Up @@ -4285,15 +4285,11 @@ inline WCHAR *PAL_wcsstr(WCHAR* S, const WCHAR* P)
}
#endif

#if defined(__llvm__)
#define HAS_ROTL __has_builtin(_rotl)
#define HAS_ROTR __has_builtin(_rotr)
#else
#define HAS_ROTL 0
#define HAS_ROTR 0
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

#if !HAS_ROTL
#if !__has_builtin(_rotl)
/*++
Function:
_rotl
Expand All @@ -4311,14 +4307,14 @@ unsigned int __cdecl _rotl(unsigned int value, int shift)
retval = (value << shift) | (value >> (sizeof(int) * CHAR_BIT - shift));
return retval;
}
#endif // !HAS_ROTL
#endif // !__has_builtin(_rotl)

// On 64 bit unix, make the long an int.
#ifdef HOST_64BIT
#define _lrotl _rotl
#endif // HOST_64BIT
#endif

#if !HAS_ROTR
#if !__has_builtin(_rotr)

/*++
Function:
Expand All @@ -4338,7 +4334,7 @@ unsigned int __cdecl _rotr(unsigned int value, int shift)
return retval;
}

#endif // !HAS_ROTR
#endif // !__has_builtin(_rotr)

PALIMPORT int __cdecl abs(int);
// clang complains if this is declared with __int64
Expand Down

0 comments on commit c2531ed

Please sign in to comment.