Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1680,14 +1680,16 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
if (BaseT.getQualifiers().hasUnaligned())
Align = Target->getCharWidth();
if (const auto *VD = dyn_cast<VarDecl>(D)) {
if (VD->hasGlobalStorage() && !ForAlignof) {
uint64_t TypeSize = getTypeSize(T.getTypePtr());
Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize));
}
}
}

// Ensure miminum alignment for global variables.
if (const auto *VD = dyn_cast<VarDecl>(D))
if (VD->hasGlobalStorage() && !ForAlignof) {
uint64_t TypeSize =
!BaseT->isIncompleteType() ? getTypeSize(T.getTypePtr()) : 0;
Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize));
}

// Fields can be subject to extra alignment constraints, like if
// the field is packed, the struct is packed, or the struct has a
// a max-field-alignment constraint (#pragma pack). So calculate
Expand Down
32 changes: 32 additions & 0 deletions clang/test/Driver/systemz-alignment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %clang --target=s390x-linux -S -emit-llvm -o - %s | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a Driver test rather than a CodeGen test using clang_cc1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some other align tests there, like arm-alignment.c, so I thought this should work..?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this when reviewing.

We generally only use Driver tests when we're trying to test some aspect of the driver. For arm-alignment.c, it isn't really trying to test the code generation itself; it's trying to test the effect of alignment-related flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad. There is a SystemZ test for this already, so I moved these tests into it instead. I created a new PR: #73230.

//
// Test that a global variable with an incomplete type gets the minimum
// alignment of 2 per the ABI if no alignment was specified by user.
//
// CHECK: @VarNoAl {{.*}} align 2
// CHECK-NEXT: @VarExplAl1 {{.*}} align 1
// CHECK-NEXT: @VarExplAl4 {{.*}} align 4

// No alignemnt specified by user.
struct incomplete_ty_noal;
extern struct incomplete_ty_noal VarNoAl;
struct incomplete_ty_noal *fun0 (void)
{
return &VarNoAl;
}

// User-specified alignment of 1.
struct incomplete_ty_al1;
extern struct incomplete_ty_al1 __attribute__((aligned(1))) VarExplAl1;
struct incomplete_ty_al1 *fun1 (void)
{
return &VarExplAl1;
}

// User-specified alignment of 4.
struct incomplete_ty_al4;
extern struct incomplete_ty_al4 __attribute__((aligned(4))) VarExplAl4;
struct incomplete_ty_al4 *fun2 (void)
{
return &VarExplAl4;
}