https://github.com/llvmbot created 
https://github.com/llvm/llvm-project/pull/212122

Backport cb383a37440d27238f8a01eee05228910d65d63e

Requested by: @zwuis

>From 5f919ec557c48a533cc755c407d3409ad465a9b9 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <[email protected]>
Date: Thu, 23 Jul 2026 14:33:57 +0200
Subject: [PATCH] [Clang][X86] Introduce Clang ABI Gate for MSVC alignment
 (#210305)

On x86_64-windows-msvc after 8ecec455183f, clang applies the MSVC
size-based global-alignment scheme (Microsoft64BitMinGlobalAlign) and
does not apply the Sys V "large array" alignment increase. Users may
want to preserve the earlier ABI for compatibility with objects produced
by older clang releases.

Gate this behavior on the Clang ABI compatibility level. When
`-fclang-abi-compat=22` (or lower) is in effect,
MicrosoftX86_64TargetInfo restores LargeArrayMinWidth/LargeArrayAlign to
128 and getMinGlobalAlign skips the Microsoft64BitMinGlobalAlign step,
matching the older alignment choices.

Assisted by Claude (Anthropic).

(cherry picked from commit cb383a37440d27238f8a01eee05228910d65d63e)
---
 clang/include/clang/Basic/ABIVersions.def     |  9 +++++++--
 clang/lib/Basic/Targets/X86.cpp               | 19 +++++++++++++++++++
 clang/lib/Basic/Targets/X86.h                 |  8 ++++++++
 clang/test/CodeGen/align-x68_64.c             |  2 ++
 .../ms-constexpr-static-data-member.cpp       |  8 ++++++++
 5 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/ABIVersions.def 
b/clang/include/clang/Basic/ABIVersions.def
index a9f72481ee0ea..967a409ca22bf 100644
--- a/clang/include/clang/Basic/ABIVersions.def
+++ b/clang/include/clang/Basic/ABIVersions.def
@@ -136,8 +136,13 @@ ABI_VER_MAJOR(20)
 ABI_VER_MAJOR(21)
 
 /// Attempt to be ABI-compatible with code generated by Clang 22.0.x.
-/// This causes clang to mangle lambdas in default member initializers of
-/// local classes as nested-name entities instead of local-name entities.
+/// This causes clang to:
+///   - Mangle lambdas in default member initializers of local classes as
+///     nested-name entities instead of local-name entities.
+///   - On x86_64 Windows, apply the System V psABI "large array" alignment
+///     rule instead of matching MSVC.
+///   - On x86_64 Windows, omit MSVC's size-based minimum alignment of global
+///     variables.
 ABI_VER_MAJOR(22)
 
 /// Conform to the underlying platform's C and C++ ABIs as closely as we can.
diff --git a/clang/lib/Basic/Targets/X86.cpp b/clang/lib/Basic/Targets/X86.cpp
index 7d8a74d62be74..8ab39b750dc99 100644
--- a/clang/lib/Basic/Targets/X86.cpp
+++ b/clang/lib/Basic/Targets/X86.cpp
@@ -1882,5 +1882,24 @@ MicrosoftX86_64TargetInfo::getMinGlobalAlign(uint64_t 
TypeSize,
   unsigned Align =
       WindowsX86_64TargetInfo::getMinGlobalAlign(TypeSize, HasNonWeakDef);
 
+  // Skip the MSVC size-based global-alignment increase under
+  // -fclang-abi-compat<=22.
+  if (!UseMSVCCompatGlobalAlign)
+    return Align;
+
   return std::max(Align, Microsoft64BitMinGlobalAlign(TypeSize));
 }
+
+void MicrosoftX86_64TargetInfo::adjust(DiagnosticsEngine &Diags,
+                                       LangOptions &Opts,
+                                       const TargetInfo *Aux) {
+  WindowsX86_64TargetInfo::adjust(Diags, Opts, Aux);
+  // Under -fclang-abi-compat<=22, restore the prior x86_64-windows-msvc
+  // behavior: apply the Sys V "large array" alignment increase and skip the
+  // MSVC size-based global-alignment increase.
+  if (Opts.isCompatibleWith(LangOptions::ClangABI::Ver22)) {
+    UseMSVCCompatGlobalAlign = false;
+    LargeArrayMinWidth = 128;
+    LargeArrayAlign = 128;
+  }
+}
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index e305d9017d897..27f33c9d03672 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -982,6 +982,14 @@ class LLVM_LIBRARY_VISIBILITY MicrosoftX86_64TargetInfo
 
   unsigned getMinGlobalAlign(uint64_t TypeSize,
                              bool HasNonWeakDef) const override;
+
+  void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
+              const TargetInfo *Aux) override;
+
+private:
+  // Whether to apply the MSVC size-based global-alignment scheme. Disabled by
+  // -fclang-abi-compat<=22 to restore prior x86_64-windows-msvc behavior.
+  bool UseMSVCCompatGlobalAlign = true;
 };
 
 // x86-64 MinGW target
diff --git a/clang/test/CodeGen/align-x68_64.c 
b/clang/test/CodeGen/align-x68_64.c
index 91f5fac199136..af20ffa3a4d1e 100644
--- a/clang/test/CodeGen/align-x68_64.c
+++ b/clang/test/CodeGen/align-x68_64.c
@@ -1,6 +1,8 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | 
FileCheck %s
 // RUN: %clang_cc1 -triple x86_64-windows-gnu       -emit-llvm %s -o - | 
FileCheck %s
 // RUN: %clang_cc1 -triple x86_64-windows-msvc      -emit-llvm %s -o - | 
FileCheck --check-prefix=MSVC %s
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fclang-abi-compat=22 \
+// RUN:   -emit-llvm %s -o - | FileCheck %s
 // PR5599
 
 char arr[16];
diff --git a/clang/test/CodeGenCXX/ms-constexpr-static-data-member.cpp 
b/clang/test/CodeGenCXX/ms-constexpr-static-data-member.cpp
index 4b191fd472c20..623eb312ccf3c 100644
--- a/clang/test/CodeGenCXX/ms-constexpr-static-data-member.cpp
+++ b/clang/test/CodeGenCXX/ms-constexpr-static-data-member.cpp
@@ -1,4 +1,6 @@
 // RUN: %clang_cc1 -emit-llvm -triple=x86_64-windows-msvc %s -o - | FileCheck 
%s
+// RUN: %clang_cc1 -emit-llvm -triple=x86_64-windows-msvc \
+// RUN:   -fclang-abi-compat=22 %s -o - | FileCheck %s --check-prefix=COMPAT
 
 struct Foo { int x, y; };
 
@@ -24,3 +26,9 @@ void usethem() {
 // CHECK-DAG: @"?sdm_char_ptr@S@@2QEBDEB" = linkonce_odr dso_local constant 
ptr @"??_C@_04JIHMPGLA@asdf?$AA@", comdat, align 8
 
 // CHECK-DAG: @"?sdm_udt@S@@2UFoo@@B" = linkonce_odr dso_local constant 
%struct.Foo { i32 1, i32 2 }, comdat, align 8
+
+// COMPAT-DAG: @"?sdm_char_array@S@@2QBDB" = linkonce_odr dso_local constant 
[5 x i8] c"asdf\00", comdat, align 1
+
+// COMPAT-DAG: @"?sdm_char_ptr@S@@2QEBDEB" = linkonce_odr dso_local constant 
ptr @"??_C@_04JIHMPGLA@asdf?$AA@", comdat, align 8
+
+// COMPAT-DAG: @"?sdm_udt@S@@2UFoo@@B" = linkonce_odr dso_local constant 
%struct.Foo { i32 1, i32 2 }, comdat, align 4

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to