Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -155,6 +155,10 @@ /// zero-length bitfield. unsigned UseZeroLengthBitfieldAlignment : 1; + /// Whether zero length bitfield alignment is respected if they are the + /// leading members. + unsigned UseLeadingZeroLengthBitfield : 1; + /// Whether explicit bit field alignment attributes are honored. unsigned UseExplicitBitFieldAlignment : 1; @@ -768,6 +772,12 @@ return UseZeroLengthBitfieldAlignment; } + /// Check whether zero length bitfield alignment is respected if they are + /// leading members. + bool useLeadingZeroLengthBitfield() const { + return UseLeadingZeroLengthBitfield; + } + /// Get the fixed alignment value in bits for a member that follows /// a zero length bitfield. unsigned getZeroLengthBitfieldBoundary() const { Index: clang/lib/AST/RecordLayoutBuilder.cpp =================================================================== --- clang/lib/AST/RecordLayoutBuilder.cpp +++ clang/lib/AST/RecordLayoutBuilder.cpp @@ -1627,12 +1627,17 @@ // Some such targets do honor it on zero-width bitfields. if (FieldSize == 0 && Context.getTargetInfo().useZeroLengthBitfieldAlignment()) { - // The alignment to round up to is the max of the field's natural - // alignment and a target-specific fixed value (sometimes zero). - unsigned ZeroLengthBitfieldBoundary = - Context.getTargetInfo().getZeroLengthBitfieldBoundary(); - FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary); - + // Some targets don't honor leading zero-width bitfield. + if (!IsUnion && FieldOffset == 0 && + !Context.getTargetInfo().useLeadingZeroLengthBitfield()) + FieldAlign = 1; + else { + // The alignment to round up to is the max of the field's natural + // alignment and a target-specific fixed value (sometimes zero). + unsigned ZeroLengthBitfieldBoundary = + Context.getTargetInfo().getZeroLengthBitfieldBoundary(); + FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary); + } // If that doesn't apply, just ignore the field alignment. } else { FieldAlign = 1; Index: clang/lib/Basic/TargetInfo.cpp =================================================================== --- clang/lib/Basic/TargetInfo.cpp +++ clang/lib/Basic/TargetInfo.cpp @@ -104,6 +104,7 @@ UseSignedCharForObjCBool = true; UseBitFieldTypeAlignment = true; UseZeroLengthBitfieldAlignment = false; + UseLeadingZeroLengthBitfield = true; UseExplicitBitFieldAlignment = true; ZeroLengthBitfieldBoundary = 0; HalfFormat = &llvm::APFloat::IEEEhalf(); Index: clang/lib/Basic/Targets/OSTargets.h =================================================================== --- clang/lib/Basic/Targets/OSTargets.h +++ clang/lib/Basic/Targets/OSTargets.h @@ -790,6 +790,7 @@ this->WCharType = TargetInfo::UnsignedInt; this->UseBitFieldTypeAlignment = false; this->UseZeroLengthBitfieldAlignment = true; + this->UseLeadingZeroLengthBitfield = false; this->ZeroLengthBitfieldBoundary = 32; this->MinGlobalAlign = 0; this->DefaultAlignForAttributeAligned = 128; Index: clang/test/CodeGen/SystemZ/zos-alignment.c =================================================================== --- clang/test/CodeGen/SystemZ/zos-alignment.c +++ clang/test/CodeGen/SystemZ/zos-alignment.c @@ -83,6 +83,35 @@ // CHECK-NEXT: 8 | char *[] b // CHECK-NEXT: | [sizeof=8, align=8] +struct s7 { + long :0; + short a; +} S7; +// CHECK: 0 | struct s7 +// CHECK-NEXT: 0:- | long +// CHECK-NEXT: 0 | short a +// CHECK-NEXT: | [sizeof=2, align=2] + +#pragma pack(2) +struct s8 { + unsigned long :0; + long long a; +} S8; +#pragma pack() +// CHECK: 0 | struct s8 +// CHECK-NEXT: 0:- | unsigned long +// CHECK-NEXT: 0 | long long a +// CHECK-NEXT: | [sizeof=8, align=2] + +struct s9 { + unsigned int :0; + unsigned short :0; +} S9; +// CHECK: 0 | struct s9 +// CHECK-NEXT: 0:- | unsigned int +// CHECK-NEXT: 0:- | unsigned short +// CHECK-NEXT: | [sizeof=0, align=1] + struct s10 { unsigned int __attribute__((aligned)) a; } S10;