stevewan created this revision. stevewan requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
The "aligned" attribute can 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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D107394 Files: clang/lib/AST/ASTContext.cpp clang/test/Layout/aix-alignof-align-and-pack-attr.cpp clang/test/Layout/aix-alignof-align-attr.cpp Index: clang/test/Layout/aix-alignof-align-attr.cpp =================================================================== --- /dev/null +++ clang/test/Layout/aix-alignof-align-attr.cpp @@ -0,0 +1,11 @@ +// 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 + +struct __attribute__((__aligned__(2))) C { + double x; +} c; + +// CHECK: @c = global %struct.C zeroinitializer, align 8 Index: clang/test/Layout/aix-alignof-align-and-pack-attr.cpp =================================================================== --- /dev/null +++ clang/test/Layout/aix-alignof-align-and-pack-attr.cpp @@ -0,0 +1,11 @@ +// 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 + +struct __attribute__((__aligned__(2), packed)) C { + double x; +} c; + +// CHECK: @c = global %struct.C zeroinitializer, align 2 Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -2478,11 +2478,19 @@ return ABIAlign; if (const auto *RT = T->getAs<RecordType>()) { - if (TI.AlignIsRequired || RT->getDecl()->isInvalidDecl()) + const RecordDecl *RD = RT->getDecl(); + + if (RD->isInvalidDecl()) + return ABIAlign; + + // 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->hasAttr<PackedAttr>())) 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;
Index: clang/test/Layout/aix-alignof-align-attr.cpp =================================================================== --- /dev/null +++ clang/test/Layout/aix-alignof-align-attr.cpp @@ -0,0 +1,11 @@ +// 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 + +struct __attribute__((__aligned__(2))) C { + double x; +} c; + +// CHECK: @c = global %struct.C zeroinitializer, align 8 Index: clang/test/Layout/aix-alignof-align-and-pack-attr.cpp =================================================================== --- /dev/null +++ clang/test/Layout/aix-alignof-align-and-pack-attr.cpp @@ -0,0 +1,11 @@ +// 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 + +struct __attribute__((__aligned__(2), packed)) C { + double x; +} c; + +// CHECK: @c = global %struct.C zeroinitializer, align 2 Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -2478,11 +2478,19 @@ return ABIAlign; if (const auto *RT = T->getAs<RecordType>()) { - if (TI.AlignIsRequired || RT->getDecl()->isInvalidDecl()) + const RecordDecl *RD = RT->getDecl(); + + if (RD->isInvalidDecl()) + return ABIAlign; + + // 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->hasAttr<PackedAttr>())) 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits