Skip to content

Commit df01243

Browse files
authored
[release/6.0] Define __cpuid{ex} only when there's no builtin one (backport of #73065) (#77507)
* Define __cpuid{ex} only when there's no builtin one (backport of #73065) * Fix and simplify __has_builtin conditions (backport of #73905)
1 parent c56b716 commit df01243

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

eng/common/native/find-native-compiler.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if [ -z "$CLR_CC" ]; then
5555
# Set default versions
5656
if [ -z "$majorVersion" ]; then
5757
# note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero.
58-
if [ "$compiler" = "clang" ]; then versions=( 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 )
58+
if [[ "$compiler" == "clang" ]]; then versions=( 15 14 13 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 )
5959
elif [ "$compiler" = "gcc" ]; then versions=( 9 8 7 6 5 4.9 ); fi
6060

6161
for version in "${versions[@]}"; do

src/coreclr/gc/env/gcenv.base.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#define SSIZE_T_MAX ((ptrdiff_t)(SIZE_T_MAX / 2))
4444
#endif
4545

46+
#ifndef __has_builtin
47+
#define __has_builtin(x) 0
48+
#endif
49+
4650
#ifndef _INC_WINDOWS
4751
// -----------------------------------------------------------------------------------------------------------
4852
//
@@ -191,26 +195,15 @@ typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)(void* lpThreadParameter);
191195
#endif
192196
#else // _MSC_VER
193197

194-
#ifdef __llvm__
195-
#define HAS_IA32_PAUSE __has_builtin(__builtin_ia32_pause)
196-
#define HAS_IA32_MFENCE __has_builtin(__builtin_ia32_mfence)
197-
#else
198-
#define HAS_IA32_PAUSE 0
199-
#define HAS_IA32_MFENCE 0
200-
#endif
201-
202-
// Only clang defines __has_builtin, so we first test for a GCC define
203-
// before using __has_builtin.
204-
205198
#if defined(__i386__) || defined(__x86_64__)
206199

207-
#if (__GNUC__ > 4 && __GNUC_MINOR > 7) || HAS_IA32_PAUSE
200+
#if __has_builtin(__builtin_ia32_pause)
208201
// clang added this intrinsic in 3.8
209202
// gcc added this intrinsic by 4.7.1
210203
#define YieldProcessor __builtin_ia32_pause
211204
#endif // __has_builtin(__builtin_ia32_pause)
212205

213-
#if defined(__GNUC__) || HAS_IA32_MFENCE
206+
#if __has_builtin(__builtin_ia32_mfence)
214207
// clang has had this intrinsic since at least 3.0
215208
// gcc has had this intrinsic since forever
216209
#define MemoryBarrier __builtin_ia32_mfence

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,6 @@ void GCToOSInterface::FlushProcessWriteBuffers()
575575
// otherwise raises a SIGTRAP.
576576
void GCToOSInterface::DebugBreak()
577577
{
578-
// __has_builtin is only defined by clang. GCC doesn't have a debug
579-
// trap intrinsic anyway.
580-
#ifndef __has_builtin
581-
#define __has_builtin(x) 0
582-
#endif // __has_builtin
583-
584578
#if __has_builtin(__builtin_debugtrap)
585579
__builtin_debugtrap();
586580
#else

src/coreclr/pal/inc/pal.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4092,15 +4092,11 @@ inline WCHAR *PAL_wcsstr(WCHAR* S, const WCHAR* P)
40924092
}
40934093
#endif
40944094

4095-
#if defined(__llvm__)
4096-
#define HAS_ROTL __has_builtin(_rotl)
4097-
#define HAS_ROTR __has_builtin(_rotr)
4098-
#else
4099-
#define HAS_ROTL 0
4100-
#define HAS_ROTR 0
4095+
#ifndef __has_builtin
4096+
#define __has_builtin(x) 0
41014097
#endif
41024098

4103-
#if !HAS_ROTL
4099+
#if !__has_builtin(_rotl)
41044100
/*++
41054101
Function:
41064102
_rotl
@@ -4118,14 +4114,14 @@ unsigned int __cdecl _rotl(unsigned int value, int shift)
41184114
retval = (value << shift) | (value >> (sizeof(int) * CHAR_BIT - shift));
41194115
return retval;
41204116
}
4121-
#endif // !HAS_ROTL
4117+
#endif // !__has_builtin(_rotl)
41224118

41234119
// On 64 bit unix, make the long an int.
41244120
#ifdef HOST_64BIT
41254121
#define _lrotl _rotl
4126-
#endif // HOST_64BIT
4122+
#endif
41274123

4128-
#if !HAS_ROTR
4124+
#if !__has_builtin(_rotr)
41294125

41304126
/*++
41314127
Function:
@@ -4145,7 +4141,7 @@ unsigned int __cdecl _rotr(unsigned int value, int shift)
41454141
return retval;
41464142
}
41474143

4148-
#endif // !HAS_ROTR
4144+
#endif // !__has_builtin(_rotr)
41494145

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

src/coreclr/vm/amd64/unixstubs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C"
1010
PORTABILITY_ASSERT("Implement for PAL");
1111
}
1212

13+
#if !__has_builtin(__cpuid)
1314
void __cpuid(int cpuInfo[4], int function_id)
1415
{
1516
// Based on the Clang implementation provided in cpuid.h:
@@ -20,7 +21,9 @@ extern "C"
2021
: "0"(function_id)
2122
);
2223
}
24+
#endif
2325

26+
#if !__has_builtin(__cpuidex)
2427
void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id)
2528
{
2629
// Based on the Clang implementation provided in cpuid.h:
@@ -31,6 +34,7 @@ extern "C"
3134
: "0"(function_id), "2"(subFunction_id)
3235
);
3336
}
37+
#endif
3438

3539
DWORD xmmYmmStateSupport()
3640
{

0 commit comments

Comments
 (0)