Skip to content

Commit 97fe519

Browse files
authored
[Clang][AArch64] Define x86_64 macros for ARM64EC targets (#65420)
The ARM64EC ABI requires that struct layouts match between regular x86_64 code and ARM64EC code. Ensure this is always the case by defining the same set of macros as are set when targeting x86_64 but with the addition of `__arm64ec__/_M_ARM64EC` macros that can be used for any ARM64EC specific code. More details can be found here: https://techcommunity.microsoft.com/t5/windows-os-platform-blog/getting-to-know-arm64ec-defines-and-intrinsic-functions/ba-p/2957235
1 parent 9ae41a1 commit 97fe519

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

clang/lib/Basic/Targets/AArch64.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,18 @@ void AArch64TargetInfo::getTargetDefinesARMV94A(const LangOptions &Opts,
336336
void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
337337
MacroBuilder &Builder) const {
338338
// Target identification.
339-
Builder.defineMacro("__aarch64__");
339+
if (getTriple().isWindowsArm64EC()) {
340+
// Define the same set of macros as would be defined on x86_64 to ensure that
341+
// ARM64EC datatype layouts match those of x86_64 compiled code
342+
Builder.defineMacro("__amd64__");
343+
Builder.defineMacro("__amd64");
344+
Builder.defineMacro("__x86_64");
345+
Builder.defineMacro("__x86_64__");
346+
Builder.defineMacro("__arm64ec__");
347+
} else {
348+
Builder.defineMacro("__aarch64__");
349+
}
350+
340351
// Inline assembly supports AArch64 flag outputs.
341352
Builder.defineMacro("__GCC_ASM_FLAG_OUTPUTS__");
342353

@@ -1466,7 +1477,13 @@ MicrosoftARM64TargetInfo::MicrosoftARM64TargetInfo(const llvm::Triple &Triple,
14661477
void MicrosoftARM64TargetInfo::getTargetDefines(const LangOptions &Opts,
14671478
MacroBuilder &Builder) const {
14681479
WindowsARM64TargetInfo::getTargetDefines(Opts, Builder);
1469-
Builder.defineMacro("_M_ARM64", "1");
1480+
if (getTriple().isWindowsArm64EC()) {
1481+
Builder.defineMacro("_M_X64", "100");
1482+
Builder.defineMacro("_M_AMD64", "100");
1483+
Builder.defineMacro("_M_ARM64EC", "1");
1484+
} else {
1485+
Builder.defineMacro("_M_ARM64", "1");
1486+
}
14701487
}
14711488

14721489
TargetInfo::CallingConvKind

clang/test/Preprocessor/predefined-win-macros.c

+30
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@
9393
// CHECK-ARM64-WIN: #define _WIN32 1
9494
// CHECK-ARM64-WIN: #define _WIN64 1
9595

96+
// RUN: %clang_cc1 -triple arm64ec-windows %s -E -dM -o - \
97+
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-ARM64EC-WIN
98+
99+
// CHECK-ARM64EC-WIN-NOT: #define WIN32 1
100+
// CHECK-ARM64EC-WIN-NOT: #define WIN64 1
101+
// CHECK-ARM64EC-WIN-NOT: #define WINNT 1
102+
// CHECK-ARM64EC-WIN-NOT: #define _M_ARM64 1
103+
// CHECK-ARM64EC-WIN: #define _M_AMD64 100
104+
// CHECK-ARM64EC-WIN: #define _M_ARM64EC 1
105+
// CHECK-ARM64EC-WIN: #define _M_X64 100
106+
// CHECK-ARM64EC-WIN: #define _WIN32 1
107+
// CHECK-ARM64EC-WIN: #define _WIN64 1
108+
96109
// RUN: %clang_cc1 -triple i686-windows-gnu %s -E -dM -o - \
97110
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-X86-MINGW
98111

@@ -131,3 +144,20 @@
131144
// CHECK-ARM64-MINGW: #define _WIN64 1
132145
// CHECK-ARM64-MINGW: #define __GCC_ASM_FLAG_OUTPUTS__ 1
133146
// CHECK-ARM64-MINGW: #define __aarch64__ 1
147+
148+
// RUN: %clang_cc1 -triple arm64ec-windows-gnu %s -E -dM -o - \
149+
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-ARM64EC-MINGW
150+
151+
// CHECK-ARM64EC-MINGW-NOT: #define _M_ARM64EC 1
152+
// CHECK-ARM64EC-MINGW: #define WIN32 1
153+
// CHECK-ARM64EC-MINGW: #define WIN64 1
154+
// CHECK-ARM64EC-MINGW: #define WINNT 1
155+
// CHECK-ARM64EC-MINGW: #define _WIN32 1
156+
// CHECK-ARM64EC-MINGW: #define _WIN64 1
157+
// CHECK-ARM64EC-MINGW: #define __GCC_ASM_FLAG_OUTPUTS__ 1
158+
// CHECK-ARM64EC-MINGW-NOT: #define __aarch64__ 1
159+
// CHECK-ARM64EC-MINGW: #define __amd64 1
160+
// CHECK-ARM64EC-MINGW: #define __amd64__ 1
161+
// CHECK-ARM64EC-MINGW: #define __arm64ec__ 1
162+
// CHECK-ARM64EC-MINGW: #define __x86_64 1
163+
// CHECK-ARM64EC-MINGW: #define __x86_64__ 1

0 commit comments

Comments
 (0)