Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -161,6 +161,10 @@ /// If non-zero, specifies a fixed alignment value for bitfields that follow /// zero length bitfield, regardless of the zero length bitfield type. unsigned ZeroLengthBitfieldBoundary; + + /// If non-zero, specifies a maximum alignment to truncate alignment + /// specified in the aligned attribute of a static variable to this value. + unsigned MaxAlignedAttribute; }; /// OpenCL type kinds. @@ -774,6 +778,12 @@ return ZeroLengthBitfieldBoundary; } + /// Get the maximum alignment in bits for a static variable with + /// aligned attribute. + unsigned getMaxAlignedAttribute() const { + return MaxAlignedAttribute; + } + /// Check whether explicit bitfield alignment attributes should be // honored, as in "__attribute__((aligned(2))) int b : 1;". bool useExplicitBitFieldAlignment() const { Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -1783,6 +1783,13 @@ } } + // Some targets have hard limitation on the maximum requestable alignment in + // aligned attribute for static variables. + const unsigned MaxAlignedAttr = getTargetInfo().getMaxAlignedAttribute(); + const auto *VD = dyn_cast(D); + if (MaxAlignedAttr && VD && VD->getStorageClass() == SC_Static) + Align = std::min(Align, MaxAlignedAttr); + return toCharUnitsFromBits(Align); } Index: clang/lib/Basic/TargetInfo.cpp =================================================================== --- clang/lib/Basic/TargetInfo.cpp +++ clang/lib/Basic/TargetInfo.cpp @@ -106,6 +106,7 @@ UseZeroLengthBitfieldAlignment = false; UseExplicitBitFieldAlignment = true; ZeroLengthBitfieldBoundary = 0; + MaxAlignedAttribute = 0; HalfFormat = &llvm::APFloat::IEEEhalf(); FloatFormat = &llvm::APFloat::IEEEsingle(); DoubleFormat = &llvm::APFloat::IEEEdouble(); Index: clang/lib/Basic/Targets/OSTargets.h =================================================================== --- clang/lib/Basic/Targets/OSTargets.h +++ clang/lib/Basic/Targets/OSTargets.h @@ -788,6 +788,7 @@ ZOSTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) : OSTargetInfo(Triple, Opts) { this->WCharType = TargetInfo::UnsignedInt; + this->MaxAlignedAttribute = 128; this->UseBitFieldTypeAlignment = false; this->UseZeroLengthBitfieldAlignment = true; this->ZeroLengthBitfieldBoundary = 32; Index: clang/test/CodeGen/SystemZ/zos-alignment.c =================================================================== --- clang/test/CodeGen/SystemZ/zos-alignment.c +++ clang/test/CodeGen/SystemZ/zos-alignment.c @@ -1,4 +1,16 @@ -// RUN: %clang_cc1 -emit-llvm-only -triple s390x-none-zos -fdump-record-layouts %s | FileCheck %s +// RUN: %clang_cc1 -emit-llvm-only -triple s390x-none-zos -fdump-record-layouts %s | FileCheck %s --check-prefix=CHECK +// RUN: %clang_cc1 -emit-llvm -triple s390x-none-zos %s -o - | FileCheck %s --check-prefix=DECL + +static int __attribute__((aligned(32))) v0; +int __attribute__((aligned(32))) v1; +typedef int __attribute__((aligned(32))) int32; +static int32 v2; +int32 v3; +int f0() { return v0 + v1 + v2 + v3; } +// DECL: @v0 {{.*}} align 16 +// DECL-NEXT: @v1 {{.*}} align 32 +// DECL-NEXT: @v2 {{.*}} align 16 +// DECL-NEXT: @v3 {{.*}} align 32 struct s0 { short a:3;