Skip to content

Commit 1950ebd

Browse files
[ARM64EC] Fix compilation of intrin.h in ARM64EC mode. (#87717)
intrin.h checks for x86_64. But the "x86_64" define is also defined for ARM64EC, and we don't support all the intrinsics in ARM64EC mode. Fix the preprocessor checks to handle this correctly. (If we actually need some of these intrinsics in ARM64EC mode, we can revisit later.) Not exactly sure how I didn't run into this issue before now... I think I've built code that requires these headers, but maybe not since the define fix landed.
1 parent 16b3e43 commit 1950ebd

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

clang/lib/Headers/intrin.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <intrin0.h>
1919

2020
/* First include the standard intrinsics. */
21-
#if defined(__i386__) || defined(__x86_64__)
21+
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
2222
#include <x86intrin.h>
2323
#endif
2424

@@ -166,7 +166,7 @@ unsigned __int32 xbegin(void);
166166
void _xend(void);
167167

168168
/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
169-
#ifdef __x86_64__
169+
#if defined(__x86_64__) && !defined(__arm64ec__)
170170
void __addgsbyte(unsigned long, unsigned char);
171171
void __addgsdword(unsigned long, unsigned long);
172172
void __addgsqword(unsigned long, unsigned __int64);
@@ -236,7 +236,8 @@ __int64 _mul128(__int64, __int64, __int64 *);
236236
/*----------------------------------------------------------------------------*\
237237
|* movs, stos
238238
\*----------------------------------------------------------------------------*/
239-
#if defined(__i386__) || defined(__x86_64__)
239+
240+
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
240241
static __inline__ void __DEFAULT_FN_ATTRS __movsb(unsigned char *__dst,
241242
unsigned char const *__src,
242243
size_t __n) {
@@ -305,7 +306,7 @@ static __inline__ void __DEFAULT_FN_ATTRS __stosw(unsigned short *__dst,
305306
: "memory");
306307
}
307308
#endif
308-
#ifdef __x86_64__
309+
#if defined(__x86_64__) && !defined(__arm64ec__)
309310
static __inline__ void __DEFAULT_FN_ATTRS __movsq(
310311
unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
311312
__asm__ __volatile__("rep movsq"
@@ -324,7 +325,7 @@ static __inline__ void __DEFAULT_FN_ATTRS __stosq(unsigned __int64 *__dst,
324325
/*----------------------------------------------------------------------------*\
325326
|* Misc
326327
\*----------------------------------------------------------------------------*/
327-
#if defined(__i386__) || defined(__x86_64__)
328+
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
328329
static __inline__ void __DEFAULT_FN_ATTRS __halt(void) {
329330
__asm__ volatile("hlt");
330331
}
@@ -339,7 +340,7 @@ static __inline__ void __DEFAULT_FN_ATTRS __nop(void) {
339340
/*----------------------------------------------------------------------------*\
340341
|* MS AArch64 specific
341342
\*----------------------------------------------------------------------------*/
342-
#if defined(__aarch64__)
343+
#if defined(__aarch64__) || defined(__arm64ec__)
343344
unsigned __int64 __getReg(int);
344345
long _InterlockedAdd(long volatile *Addend, long Value);
345346
__int64 _InterlockedAdd64(__int64 volatile *Addend, __int64 Value);
@@ -383,7 +384,7 @@ void __cdecl __prefetch(void *);
383384
/*----------------------------------------------------------------------------*\
384385
|* Privileged intrinsics
385386
\*----------------------------------------------------------------------------*/
386-
#if defined(__i386__) || defined(__x86_64__)
387+
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
387388
static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
388389
__readmsr(unsigned long __register) {
389390
// Loads the contents of a 64-bit model specific register (MSR) specified in
@@ -397,7 +398,6 @@ __readmsr(unsigned long __register) {
397398
__asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
398399
return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
399400
}
400-
#endif
401401

402402
static __inline__ unsigned __LPTRINT_TYPE__ __DEFAULT_FN_ATTRS __readcr3(void) {
403403
unsigned __LPTRINT_TYPE__ __cr3_val;
@@ -413,6 +413,7 @@ static __inline__ void __DEFAULT_FN_ATTRS
413413
__writecr3(unsigned __INTPTR_TYPE__ __cr3_val) {
414414
__asm__ ("mov {%0, %%cr3|cr3, %0}" : : "r"(__cr3_val) : "memory");
415415
}
416+
#endif
416417

417418
#ifdef __cplusplus
418419
}

clang/lib/Headers/intrin0.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef __INTRIN0_H
1616
#define __INTRIN0_H
1717

18-
#ifdef __x86_64__
18+
#if defined(__x86_64__) && !defined(__arm64ec__)
1919
#include <adcintrin.h>
2020
#endif
2121

@@ -27,7 +27,7 @@ unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
2727
unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
2828
void _ReadWriteBarrier(void);
2929

30-
#if defined(__aarch64__)
30+
#if defined(__aarch64__) || defined(__arm64ec__)
3131
unsigned int _CountLeadingZeros(unsigned long);
3232
unsigned int _CountLeadingZeros64(unsigned _int64);
3333
unsigned char _InterlockedCompareExchange128_acq(__int64 volatile *_Destination,
@@ -44,7 +44,7 @@ unsigned char _InterlockedCompareExchange128_rel(__int64 volatile *_Destination,
4444
__int64 *_ComparandResult);
4545
#endif
4646

47-
#ifdef __x86_64__
47+
#ifdef __x86_64__ && !defined(__arm64ec__)
4848
unsigned __int64 _umul128(unsigned __int64, unsigned __int64,
4949
unsigned __int64 *);
5050
unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
@@ -55,7 +55,7 @@ unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
5555
unsigned char _Shift);
5656
#endif
5757

58-
#if defined(__x86_64__) || defined(__i386__)
58+
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
5959
void _mm_pause(void);
6060
#endif
6161

@@ -83,7 +83,7 @@ __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
8383
__int64 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask);
8484
#endif
8585

86-
#if defined(__arm__) || defined(__aarch64__)
86+
#if defined(__arm__) || defined(__aarch64__) || defined(__arm64ec__)
8787
/*----------------------------------------------------------------------------*\
8888
|* Interlocked Exchange Add
8989
\*----------------------------------------------------------------------------*/

clang/test/Headers/ms-intrin.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
// RUN: -ffreestanding -fsyntax-only -Werror \
1919
// RUN: -isystem %S/Inputs/include %s
2020

21+
// RUN: %clang_cc1 -triple aarch64--windows \
22+
// RUN: -fms-compatibility -fms-compatibility-version=17.00 \
23+
// RUN: -ffreestanding -fsyntax-only -Werror \
24+
// RUN: -isystem %S/Inputs/include %s
25+
26+
// RUN: %clang_cc1 -triple arm64ec--windows \
27+
// RUN: -fms-compatibility -fms-compatibility-version=17.00 \
28+
// RUN: -ffreestanding -fsyntax-only -Werror \
29+
// RUN: -isystem %S/Inputs/include %s
30+
2131
// REQUIRES: x86-registered-target
2232

2333
// intrin.h needs size_t, but -ffreestanding prevents us from getting it from
@@ -41,15 +51,15 @@ void f() {
4151
__stosd(0, 0, 0);
4252
__stosw(0, 0, 0);
4353

44-
#ifdef _M_X64
54+
#if defined(_M_X64) && !defined(_M_ARM64EC)
4555
__movsq(0, 0, 0);
4656
__stosq(0, 0, 0);
4757
#endif
4858

4959
int info[4];
5060
__cpuid(info, 0);
5161
__cpuidex(info, 0, 0);
52-
#if defined(_M_X64) || defined(_M_IX86)
62+
#if (defined(_M_X64) && !defined(_M_ARM64EC)) || defined(_M_IX86)
5363
_xgetbv(0);
5464
#endif
5565
__halt();

0 commit comments

Comments
 (0)