Author: Evgenii Kudriashov
Date: 2026-07-23T14:33:57+02:00
New Revision: cb383a37440d27238f8a01eee05228910d65d63e

URL: 
https://github.com/llvm/llvm-project/commit/cb383a37440d27238f8a01eee05228910d65d63e
DIFF: 
https://github.com/llvm/llvm-project/commit/cb383a37440d27238f8a01eee05228910d65d63e.diff

LOG: [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).

Added: 
    

Modified: 
    clang/include/clang/Basic/ABIVersions.def
    clang/lib/Basic/Targets/X86.cpp
    clang/lib/Basic/Targets/X86.h
    clang/test/CodeGen/align-x68_64.c
    clang/test/CodeGenCXX/ms-constexpr-static-data-member.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/ABIVersions.def 
b/clang/include/clang/Basic/ABIVersions.def
index 15fb8ddf6d774..b55e8dfa2bbc1 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)
 
 /// Attempt to be ABI-compatible with code generated by Clang 23.0.x.

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 f276f00df942d..9ed7035ccc230 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -956,6 +956,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


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

Reply via email to