[clang] [Clang][AArch64] Define x86_64 macros for ARM64EC targets (PR #65420)

Billy Laws via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 5 15:48:35 PDT 2023


https://github.com/bylaws created https://github.com/llvm/llvm-project/pull/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

>From 6e4c14898322ab3caeceb1e0859ea58470a77c6a Mon Sep 17 00:00:00 2001
From: Billy Laws <blaws05 at gmail.com>
Date: Tue, 5 Sep 2023 21:56:48 +0100
Subject: [PATCH] [Clang][AArch64] Define x86_64 macros for ARM64EC targets

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 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
---
 clang/lib/Basic/Targets/AArch64.cpp           | 21 +++++++++++--
 .../test/Preprocessor/predefined-win-macros.c | 30 +++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index 6c43c8b592622d0..bdf5c4d98ebcea4 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -336,7 +336,18 @@ void AArch64TargetInfo::getTargetDefinesARMV94A(const LangOptions &Opts,
 void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
                                          MacroBuilder &Builder) const {
   // Target identification.
-  Builder.defineMacro("__aarch64__");
+  if (getTriple().isWindowsArm64EC()) {
+    // Define the same set of macros as would be defined on x86_64 to ensure that
+    // ARM64EC datatype layouts match those of x86_64 compiled code
+    Builder.defineMacro("__amd64__");
+    Builder.defineMacro("__amd64");
+    Builder.defineMacro("__x86_64");
+    Builder.defineMacro("__x86_64__");
+    Builder.defineMacro("__arm64ec__");
+  } else {
+    Builder.defineMacro("__aarch64__");
+  }
+
   // Inline assembly supports AArch64 flag outputs.
   Builder.defineMacro("__GCC_ASM_FLAG_OUTPUTS__");
 
@@ -1466,7 +1477,13 @@ MicrosoftARM64TargetInfo::MicrosoftARM64TargetInfo(const llvm::Triple &Triple,
 void MicrosoftARM64TargetInfo::getTargetDefines(const LangOptions &Opts,
                                                 MacroBuilder &Builder) const {
   WindowsARM64TargetInfo::getTargetDefines(Opts, Builder);
-  Builder.defineMacro("_M_ARM64", "1");
+  if (getTriple().isWindowsArm64EC()) {
+    Builder.defineMacro("_M_X64", "100");
+    Builder.defineMacro("_M_AMD64", "100");
+    Builder.defineMacro("_M_ARM64EC", "1");
+  } else {
+    Builder.defineMacro("_M_ARM64", "1");
+  }
 }
 
 TargetInfo::CallingConvKind
diff --git a/clang/test/Preprocessor/predefined-win-macros.c b/clang/test/Preprocessor/predefined-win-macros.c
index 1ac249550a48927..0623996855ccac1 100644
--- a/clang/test/Preprocessor/predefined-win-macros.c
+++ b/clang/test/Preprocessor/predefined-win-macros.c
@@ -93,6 +93,19 @@
 // CHECK-ARM64-WIN: #define _WIN32 1
 // CHECK-ARM64-WIN: #define _WIN64 1
 
+// RUN: %clang_cc1 -triple arm64ec-windows %s -E -dM -o - \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-ARM64EC-WIN
+
+// CHECK-ARM64EC-WIN-NOT: #define WIN32 1
+// CHECK-ARM64EC-WIN-NOT: #define WIN64 1
+// CHECK-ARM64EC-WIN-NOT: #define WINNT 1
+// CHECK-ARM64EC-WIN-NOT: #define _M_ARM64 1
+// CHECK-ARM64EC-WIN: #define _M_AMD64 100
+// CHECK-ARM64EC-WIN: #define _M_ARM64EC 1
+// CHECK-ARM64EC-WIN: #define _M_X64 100
+// CHECK-ARM64EC-WIN: #define _WIN32 1
+// CHECK-ARM64EC-WIN: #define _WIN64 1
+
 // RUN: %clang_cc1 -triple i686-windows-gnu %s -E -dM -o - \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-X86-MINGW
 
@@ -131,3 +144,20 @@
 // CHECK-ARM64-MINGW: #define _WIN64 1
 // CHECK-ARM64-MINGW: #define __GCC_ASM_FLAG_OUTPUTS__ 1
 // CHECK-ARM64-MINGW: #define __aarch64__ 1
+
+// RUN: %clang_cc1 -triple arm64ec-windows-gnu %s -E -dM -o - \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-ARM64EC-MINGW
+
+// CHECK-ARM64EC-MINGW-NOT: #define _M_ARM64EC 1
+// CHECK-ARM64EC-MINGW: #define WIN32 1
+// CHECK-ARM64EC-MINGW: #define WIN64 1
+// CHECK-ARM64EC-MINGW: #define WINNT 1
+// CHECK-ARM64EC-MINGW: #define _WIN32 1
+// CHECK-ARM64EC-MINGW: #define _WIN64 1
+// CHECK-ARM64EC-MINGW: #define __GCC_ASM_FLAG_OUTPUTS__ 1
+// CHECK-ARM64EC-MINGW-NOT: #define __aarch64__ 1
+// CHECK-ARM64EC-MINGW: #define __amd64 1
+// CHECK-ARM64EC-MINGW: #define __amd64__ 1
+// CHECK-ARM64EC-MINGW: #define __arm64ec__ 1
+// CHECK-ARM64EC-MINGW: #define __x86_64 1
+// CHECK-ARM64EC-MINGW: #define __x86_64__ 1
\ No newline at end of file



More information about the cfe-commits mailing list