[clang] [Clang][AST] Do not lower the alignment of `_Atomic(T)` below that of `T` (PR #239264)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 15 20:11:37 PST 2026
https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/239264
>From 5a4f684a8a6cf5743e21d674af276a8e0c5027f3 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 5 Nov 2026 11:07:04 +0530
Subject: [PATCH 1/3] [clang][AST] Do not lower the alignment of `_Atomic(T)`
below that of `T`
Fixes #111295
`getTypeInfoImpl` rounds the size of `_Atomic(T)` up to a power of two and then sets the alignment equal to that size. When `T` is more aligned than it is wide, e.g. `typedef __attribute__((aligned(8))) int`, this lowers the alignment, `_Atomic(T)` ends up less aligned than `T`, and `AtomicInfo` asserts on every access.
The alignment is now only raised to the size, never lowered, and the value type's alignment requirement is kept so record layout places such a member like a plain `T`. This matches GCC.
Assisted-by: LLM tools.
---
clang/docs/ReleaseNotes.md | 7 ++
clang/lib/AST/ASTContext.cpp | 8 +-
clang/test/CodeGen/atomic-overaligned-value.c | 73 +++++++++++++++++++
clang/test/Sema/atomic-type.c | 38 ++++++++++
clang/test/SemaCXX/atomic-type.cpp | 10 +++
5 files changed, 134 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGen/atomic-overaligned-value.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..60c16cf6b8e8ed 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -157,6 +157,11 @@ features cannot lower the translation-unit ABI level;
accesses to these fields. `__attribute__((packed))` on the field or the
enclosing record still reduces the alignment. (#GH208256)
+- `_Atomic(T)` is now never less aligned than `T`, matching GCC. Previously an
+ over-aligned `T`, such as an `int` typedef declared with
+ `__attribute__((aligned(8)))`, lost its alignment under `_Atomic`, which
+ affects the layout of structures with such members. (#GH111295)
+
### AST Dumping Potentially Breaking Changes
### Clang Frontend Potentially Breaking Changes
@@ -779,6 +784,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed incorrect handling of C++ import preprocessing token when a digraph character after import. (#GH190693)
- Fixed a crash when emitting RTTI for a `dllexport` class, or the fundamental type descriptors for `__cxxabiv1::__fundamental_type_info`, under `-fvisibility=hidden`. (#GH207963)
- Fixed an assertion failure when passing a wide string literal to `__builtin_nan`. (#GH212108)
+- Fixed an assertion failure when accessing an `_Atomic` object whose value type is more aligned than it is
+ wide, such as an `int` typedef declared with `__attribute__((aligned(8)))`. (#GH111295)
- Fixed an assertion failure when converting between an x87 `long double` vector and another vector type of the same size. (#GH173254), (#GH63548)
- Fixed a constraint comparison bug in partial ordering. (#GH182671)
- Fixed a rejected-valid case that used an explicit object parameter in an out-of-line definition of a nested class member. (#GH136472)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 01d71388cbc0e3..ba960f2e4fee2a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2640,8 +2640,12 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
// Round the size up to a power of 2.
Width = llvm::bit_ceil(Width);
- // Set the alignment equal to the size.
- Align = static_cast<unsigned>(Width);
+ // Set the alignment equal to the size, unless the value type is
+ // already more aligned than that.
+ if (Align <= Width)
+ Align = static_cast<unsigned>(Width);
+ else
+ AlignRequirement = Info.AlignRequirement;
}
}
break;
diff --git a/clang/test/CodeGen/atomic-overaligned-value.c b/clang/test/CodeGen/atomic-overaligned-value.c
new file mode 100644
index 00000000000000..520550fec4e440
--- /dev/null
+++ b/clang/test/CodeGen/atomic-overaligned-value.c
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
+//
+// GH111295: _Atomic(T) is never less aligned than T.
+
+typedef __attribute__((aligned(8))) int alignedint;
+typedef __attribute__((aligned(16))) char alignedchar;
+enum __attribute__((aligned(8))) alignedenum { AE0 };
+struct S3 { char c[3]; };
+typedef struct S3 __attribute__((aligned(16))) alignedS3;
+
+// CHECK: @a = {{(dso_local )?}}global i32 0, align 8
+_Atomic(alignedint) a;
+// CHECK: @c = {{(dso_local )?}}global i8 0, align 16
+_Atomic(alignedchar) c;
+// CHECK: @e = {{(dso_local )?}}global i32 0, align 8
+_Atomic(enum alignedenum) e;
+// CHECK: @s = {{(dso_local )?}}global { %struct.S3, [1 x i8] } zeroinitializer, align 16
+_Atomic(alignedS3) s;
+// CHECK: @plain = {{(dso_local )?}}global i32 0, align 4
+_Atomic(int) plain;
+
+// CHECK-LABEL: define {{.*}} i32 @cmp(
+// CHECK: %r0.addr = alloca i32, align 8
+// CHECK: load atomic i32, ptr @a seq_cst, align 8
+// CHECK: icmp eq i32
+int cmp(alignedint r0) { return r0 == a; }
+
+// CHECK-LABEL: define {{.*}} void @store(
+// CHECK: store atomic i32 {{.*}}, ptr @a seq_cst, align 8
+void store(alignedint v) { a = v; }
+
+// CHECK-LABEL: define {{.*}} void @init(
+// CHECK: %l = alloca i32, align 8
+// CHECK: store i32 {{.*}}, ptr %l, align 8
+void init(alignedint v) { _Atomic(alignedint) l = v; }
+
+// CHECK-LABEL: define {{.*}} void @c11init(
+// CHECK: store i32 {{.*}}, ptr @a, align 8
+void c11init(alignedint v) { __c11_atomic_init(&a, v); }
+
+// CHECK-LABEL: define {{.*}} i32 @c11load(
+// CHECK: load atomic i32, ptr @a seq_cst, align 8
+alignedint c11load(void) { return __c11_atomic_load(&a, __ATOMIC_SEQ_CST); }
+
+// CHECK-LABEL: define {{.*}} void @c11store(
+// CHECK: store atomic i32 {{.*}}, ptr @a seq_cst, align 8
+void c11store(alignedint v) { __c11_atomic_store(&a, v, __ATOMIC_SEQ_CST); }
+
+// CHECK-LABEL: define {{.*}} i32 @c11xchg(
+// CHECK: atomicrmw xchg ptr @a, i32 {{.*}} seq_cst, align 8
+alignedint c11xchg(alignedint v) {
+ return __c11_atomic_exchange(&a, v, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}} i32 @load_ptr(
+// CHECK: load atomic i32, ptr %{{.*}} seq_cst, align 8
+alignedint load_ptr(_Atomic(alignedint) *p) { return *p; }
+
+// CHECK-LABEL: define {{.*}} i8 @load_char(
+// CHECK: load atomic i8, ptr @c seq_cst, align 16
+alignedchar load_char(void) { return c; }
+
+// CHECK-LABEL: define {{.*}} i32 @load_enum(
+// CHECK: load atomic i32, ptr @e seq_cst, align 8
+enum alignedenum load_enum(void) { return e; }
+
+// CHECK-LABEL: define {{.*}}@load_struct(
+// CHECK: load atomic i32, ptr @s seq_cst, align 16
+alignedS3 load_struct(void) { return s; }
+
+// CHECK-LABEL: define {{.*}} i32 @load_plain(
+// CHECK: load atomic i32, ptr @plain seq_cst, align 4
+int load_plain(void) { return plain; }
diff --git a/clang/test/Sema/atomic-type.c b/clang/test/Sema/atomic-type.c
index 3f171e4000d935..6212bc9907b188 100644
--- a/clang/test/Sema/atomic-type.c
+++ b/clang/test/Sema/atomic-type.c
@@ -20,3 +20,41 @@ _Atomic(struct ErrorS) error2; // expected-error {{_Atomic cannot be applied to
_Atomic(int[10]) error3; // expected-error {{_Atomic cannot be applied to array type}}
_Atomic(const int) error4; // expected-error {{_Atomic cannot be applied to qualified type}}
_Atomic(_Atomic(int)) error5; // expected-error {{_Atomic cannot be applied to atomic type}}
+
+// GH111295: _Atomic(T) is never less aligned than T.
+typedef __attribute__((aligned(8))) int alignedint;
+_Static_assert(sizeof(alignedint) == sizeof(int), "");
+_Static_assert(_Alignof(alignedint) == 8, "");
+_Static_assert(sizeof(_Atomic(alignedint)) == sizeof(int), "");
+_Static_assert(_Alignof(_Atomic(alignedint)) == 8, "");
+
+typedef __attribute__((aligned(16))) char alignedchar;
+_Static_assert(sizeof(_Atomic(alignedchar)) == 1, "");
+_Static_assert(_Alignof(_Atomic(alignedchar)) == 16, "");
+
+typedef __attribute__((aligned)) int defaultalignedint;
+_Static_assert(_Alignof(_Atomic(defaultalignedint)) == _Alignof(defaultalignedint), "");
+
+enum __attribute__((aligned(8))) alignedenum { AE };
+_Static_assert(sizeof(_Atomic(enum alignedenum)) == sizeof(enum alignedenum), "");
+_Static_assert(_Alignof(_Atomic(enum alignedenum)) == 8, "");
+
+struct S3 { char c[3]; };
+typedef struct S3 __attribute__((aligned(16))) alignedS3;
+_Static_assert(sizeof(alignedS3) == 3, "");
+_Static_assert(sizeof(_Atomic(alignedS3)) == sizeof(_Atomic(struct S3)), "");
+_Static_assert(_Alignof(_Atomic(alignedS3)) == 16, "");
+
+struct Wrap { char c; _Atomic(alignedint) a; };
+_Static_assert(__builtin_offsetof(struct Wrap, a) == 8, "");
+_Static_assert(sizeof(struct Wrap) == 16, "");
+_Static_assert(_Alignof(struct Wrap) == 8, "");
+
+// Other atomic types are still rounded up to a power of two.
+_Static_assert(sizeof(_Atomic(char)) == 1 && _Alignof(_Atomic(char)) == 1, "");
+_Static_assert(sizeof(_Atomic(int)) == sizeof(int) && _Alignof(_Atomic(int)) == sizeof(int), "");
+_Static_assert(sizeof(_Atomic(long long)) == 8 && _Alignof(_Atomic(long long)) == 8, "");
+_Static_assert(sizeof(_Atomic(struct S3)) == 4 && _Alignof(_Atomic(struct S3)) == 4, "");
+typedef __attribute__((aligned(1))) int underalignedint;
+_Static_assert(_Alignof(underalignedint) == 1, "");
+_Static_assert(_Alignof(_Atomic(underalignedint)) == _Alignof(_Atomic(int)), "");
diff --git a/clang/test/SemaCXX/atomic-type.cpp b/clang/test/SemaCXX/atomic-type.cpp
index 3200a59bc86a2d..13078570edf856 100644
--- a/clang/test/SemaCXX/atomic-type.cpp
+++ b/clang/test/SemaCXX/atomic-type.cpp
@@ -111,3 +111,13 @@ namespace non_trivially_copyable {
_Atomic S s; // expected-error {{_Atomic cannot be applied to type 'S' which is not trivially copyable}} \
// expected-warning {{'_Atomic' is a C11 extension}}
}
+
+#if __cplusplus >= 201103L
+namespace GH111295 {
+ typedef __attribute__((aligned(8))) int alignedint;
+ static_assert(alignof(alignedint) == 8, "");
+ static_assert(sizeof(_Atomic(alignedint)) == sizeof(int), ""); // expected-warning {{'_Atomic' is a C11 extension}}
+ static_assert(alignof(_Atomic(alignedint)) == 8, ""); // expected-warning {{'_Atomic' is a C11 extension}}
+ static_assert(alignof(_Atomic(int)) == sizeof(int), ""); // expected-warning {{'_Atomic' is a C11 extension}}
+}
+#endif
>From 9a9159ecf2dece23b2a6d3ff233cd92cbb68f46b Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 6 Nov 2026 08:48:39 +0530
Subject: [PATCH 2/3] Keep the alignment requirement when _Atomic does not
raise the alignment
Also note why a value type can be more aligned than it is wide, and test the #pragma pack layout under the Microsoft record layout.
---
clang/docs/ReleaseNotes.md | 4 +++-
clang/lib/AST/ASTContext.cpp | 7 ++++---
clang/test/Sema/atomic-type.c | 11 +++++++++++
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 60c16cf6b8e8ed..a82603dbb78fb4 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -160,7 +160,9 @@ features cannot lower the translation-unit ABI level;
- `_Atomic(T)` is now never less aligned than `T`, matching GCC. Previously an
over-aligned `T`, such as an `int` typedef declared with
`__attribute__((aligned(8)))`, lost its alignment under `_Atomic`, which
- affects the layout of structures with such members. (#GH111295)
+ affects the layout of structures with such members. In the Microsoft record
+ layout, an `_Atomic` member of such a typedef now also resists `#pragma pack`
+ like the typedef itself. (#GH111295)
### AST Dumping Potentially Breaking Changes
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index ba960f2e4fee2a..295fc5a85ae201 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2640,9 +2640,10 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
// Round the size up to a power of 2.
Width = llvm::bit_ceil(Width);
- // Set the alignment equal to the size, unless the value type is
- // already more aligned than that.
- if (Align <= Width)
+ // Raise the alignment to the size. If that would not raise it, keep the
+ // value type's alignment and alignment requirement; it can exceed the
+ // width only through __attribute__((aligned)) on a typedef or enum.
+ if (Align < Width)
Align = static_cast<unsigned>(Width);
else
AlignRequirement = Info.AlignRequirement;
diff --git a/clang/test/Sema/atomic-type.c b/clang/test/Sema/atomic-type.c
index 6212bc9907b188..63f6595e1d4308 100644
--- a/clang/test/Sema/atomic-type.c
+++ b/clang/test/Sema/atomic-type.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 %s -verify -fsyntax-only
+// RUN: %clang_cc1 %s -verify -fsyntax-only -triple x86_64-pc-windows-msvc
// Basic parsing/Sema tests for _Atomic
// No operations are actually supported on objects of this type yet.
@@ -58,3 +59,13 @@ _Static_assert(sizeof(_Atomic(struct S3)) == 4 && _Alignof(_Atomic(struct S3)) =
typedef __attribute__((aligned(1))) int underalignedint;
_Static_assert(_Alignof(underalignedint) == 1, "");
_Static_assert(_Alignof(_Atomic(underalignedint)) == _Alignof(_Atomic(int)), "");
+
+// _Atomic(T) keeps T's alignment requirement and resists #pragma pack like T.
+typedef __attribute__((aligned(4))) int alignedint4;
+#pragma pack(1)
+struct PackedPlain { char c; alignedint4 b; alignedint a; };
+struct PackedAtomic { char c; _Atomic(alignedint4) b; _Atomic(alignedint) a; };
+#pragma pack()
+_Static_assert(__builtin_offsetof(struct PackedAtomic, b) == __builtin_offsetof(struct PackedPlain, b), "");
+_Static_assert(__builtin_offsetof(struct PackedAtomic, a) == __builtin_offsetof(struct PackedPlain, a), "");
+_Static_assert(sizeof(struct PackedAtomic) == sizeof(struct PackedPlain), "");
>From 6aa88d593bad18165fa250cc80e92edcebdf3af2 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 6 Nov 2026 08:53:53 +0530
Subject: [PATCH 3/3] Reposition the release notes entry to avoid the
conflicts.
---
clang/docs/ReleaseNotes.md | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a82603dbb78fb4..cbf7418c6cdb61 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -143,6 +143,13 @@ features cannot lower the translation-unit ABI level;
longer classified according to a single member. `-fclang-abi-compat=23`
restores the previous behavior. (#GH227130)
+- `_Atomic(T)` is now never less aligned than `T`, matching GCC. Previously an
+ over-aligned `T`, such as an `int` typedef declared with
+ `__attribute__((aligned(8)))`, lost its alignment under `_Atomic`, which
+ affects the layout of structures with such members. In the Microsoft record
+ layout, an `_Atomic` member of such a typedef now also resists `#pragma pack`
+ like the typedef itself. (#GH111295)
+
- Clang now considers matrix types in its isHomogeneousAggregate() handling,
which can lead to differences in how structures containing matrix types are
classified for ABI purposes. The previous exclusion of matrix types appears
@@ -157,13 +164,6 @@ features cannot lower the translation-unit ABI level;
accesses to these fields. `__attribute__((packed))` on the field or the
enclosing record still reduces the alignment. (#GH208256)
-- `_Atomic(T)` is now never less aligned than `T`, matching GCC. Previously an
- over-aligned `T`, such as an `int` typedef declared with
- `__attribute__((aligned(8)))`, lost its alignment under `_Atomic`, which
- affects the layout of structures with such members. In the Microsoft record
- layout, an `_Atomic` member of such a typedef now also resists `#pragma pack`
- like the typedef itself. (#GH111295)
-
### AST Dumping Potentially Breaking Changes
### Clang Frontend Potentially Breaking Changes
More information about the cfe-commits
mailing list