[clang] 6358c74 - [Clang][AST] Do not lower the alignment of `_Atomic(T)` below that of `T` (#239264)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 19:50:19 PST 2026
Author: Akash Manna
Date: 2026-11-24T03:50:12Z
New Revision: 6358c7498434b1673cbdd2b154cbf87bef0c81f4
URL: https://github.com/llvm/llvm-project/commit/6358c7498434b1673cbdd2b154cbf87bef0c81f4
DIFF: https://github.com/llvm/llvm-project/commit/6358c7498434b1673cbdd2b154cbf87bef0c81f4.diff
LOG: [Clang][AST] Do not lower the alignment of `_Atomic(T)` below that of `T` (#239264)
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. Whenever it
is not raised, the value type's alignment requirement is kept as well,
so record layout places such a member like a plain `T`, including under
`#pragma pack` in the Microsoft layout. This matches GCC behavior.
AI was involved!
Added:
clang/test/CodeGen/atomic-overaligned-value.c
Modified:
clang/docs/ReleaseNotes.md
clang/lib/AST/ASTContext.cpp
clang/test/Sema/atomic-type.c
clang/test/SemaCXX/atomic-type.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 2c472c64e0fa6..785455d8d58c6 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -153,6 +153,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
diff erences in how structures containing matrix types are
classified for ABI purposes. The previous exclusion of matrix types appears
@@ -922,6 +929,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 7d5fd98b34894..3c46873c41359 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2649,8 +2649,13 @@ 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);
+ // 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;
}
}
break;
diff --git a/clang/test/CodeGen/atomic-overaligned-value.c b/clang/test/CodeGen/atomic-overaligned-value.c
new file mode 100644
index 0000000000000..520550fec4e44
--- /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 3f171e4000d93..63f6595e1d430 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.
@@ -20,3 +21,51 @@ _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)), "");
+
+// _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), "");
diff --git a/clang/test/SemaCXX/atomic-type.cpp b/clang/test/SemaCXX/atomic-type.cpp
index 3200a59bc86a2..13078570edf85 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
More information about the cfe-commits
mailing list