[clang] a919165 - [AIX] "aligned" attribute should not decrease type alignment returned by __alignof__
Steven Wan via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 5 15:19:17 PDT 2021
Author: Steven Wan
Date: 2021-08-05T18:18:58-04:00
New Revision: a91916500d67155ca17bfdf3294541f871f90a45
URL: https://github.com/llvm/llvm-project/commit/a91916500d67155ca17bfdf3294541f871f90a45
DIFF: https://github.com/llvm/llvm-project/commit/a91916500d67155ca17bfdf3294541f871f90a45.diff
LOG: [AIX] "aligned" attribute should not decrease type alignment returned by __alignof__
`__alignof__(x)` always returns `ABIAlign` if the "x" is marked `__attribute__((aligned()))`. However, the "aligned" attribute should only increase the alignment of a struct, or struct member, unless it's used together with the "packed" attribute, or used as a part of a typedef, in which case, the "aligned" attribute can both increase and decrease alignment.
Reviewed By: sfertile
Differential Revision: https://reviews.llvm.org/D107598
Added:
clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
Modified:
clang/lib/AST/ASTContext.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4ff2aa5307bbe..4c9dc42c5b5dd 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2478,11 +2478,16 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
return ABIAlign;
if (const auto *RT = T->getAs<RecordType>()) {
- if (TI.AlignIsRequired || RT->getDecl()->isInvalidDecl())
+ const RecordDecl *RD = RT->getDecl();
+
+ // When used as part of a typedef, or together with a 'packed' attribute,
+ // the 'aligned' attribute can be used to decrease alignment.
+ if ((TI.AlignIsRequired && T->getAs<TypedefType>() != nullptr) ||
+ RD->isInvalidDecl())
return ABIAlign;
unsigned PreferredAlign = static_cast<unsigned>(
- toBits(getASTRecordLayout(RT->getDecl()).PreferredAlignment));
+ toBits(getASTRecordLayout(RD).PreferredAlignment));
assert(PreferredAlign >= ABIAlign &&
"PreferredAlign should be at least as large as ABIAlign.");
return PreferredAlign;
diff --git a/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp b/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
new file mode 100644
index 0000000000000..51f3c5a2adc11
--- /dev/null
+++ b/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
+// RUN: FileCheck %s
+
+// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -S -emit-llvm -x c++ < %s | \
+// RUN: FileCheck %s
+
+namespace test1 {
+struct __attribute__((__aligned__(2))) C {
+ double x;
+} c;
+
+// CHECK: @{{.*}}test1{{.*}}c{{.*}} = global %"struct.test1::C" zeroinitializer, align 8
+} // namespace test1
+
+namespace test2 {
+struct __attribute__((__aligned__(2), packed)) C {
+ double x;
+} c;
+
+// CHECK: @{{.*}}test2{{.*}}c{{.*}} = global %"struct.test2::C" zeroinitializer, align 2
+} // namespace test2
+
+namespace test3 {
+struct __attribute__((__aligned__(16))) C {
+ double x;
+} c;
+
+// CHECK: @{{.*}}test3{{.*}}c{{.*}} = global %"struct.test3::C" zeroinitializer, align 16
+} // namespace test3
More information about the cfe-commits
mailing list