[clang] 6f91cf7 - [SystemZ][z/OS] Ignore leading zero width bitfield alignment on z/OS target
Fanbo Meng via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 26 07:10:41 PDT 2021
Author: Fanbo Meng
Date: 2021-03-26T10:10:33-04:00
New Revision: 6f91cf75d7f55c7beadeebeac7c1010a2e7c6553
URL: https://github.com/llvm/llvm-project/commit/6f91cf75d7f55c7beadeebeac7c1010a2e7c6553
DIFF: https://github.com/llvm/llvm-project/commit/6f91cf75d7f55c7beadeebeac7c1010a2e7c6553.diff
LOG: [SystemZ][z/OS] Ignore leading zero width bitfield alignment on z/OS target
Zero length bitfield alignment is not respected if they are leading members on z/OS target.
Reviewed By: abhina.sreeskantharajan
Differential Revision: https://reviews.llvm.org/D98890
Added:
Modified:
clang/include/clang/Basic/TargetInfo.h
clang/lib/AST/RecordLayoutBuilder.cpp
clang/lib/Basic/TargetInfo.cpp
clang/lib/Basic/Targets/OSTargets.h
clang/test/CodeGen/SystemZ/zos-alignment.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 9791cb6bbee7..01ca9ca0d2a8 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -155,6 +155,10 @@ struct TransferrableTargetInfo {
/// 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 @@ class TargetInfo : public virtual TransferrableTargetInfo,
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 {
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index 95d69fa5b11a..eb9bfc20342f 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -1627,12 +1627,17 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
// 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;
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index bc3c607dd74e..468c8a24498a 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -104,6 +104,7 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
UseSignedCharForObjCBool = true;
UseBitFieldTypeAlignment = true;
UseZeroLengthBitfieldAlignment = false;
+ UseLeadingZeroLengthBitfield = true;
UseExplicitBitFieldAlignment = true;
ZeroLengthBitfieldBoundary = 0;
HalfFormat = &llvm::APFloat::IEEEhalf();
diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index 4de1b8d2db4f..568f759bfa0d 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -796,6 +796,7 @@ class LLVM_LIBRARY_VISIBILITY ZOSTargetInfo : public OSTargetInfo<Target> {
this->WCharType = TargetInfo::UnsignedInt;
this->UseBitFieldTypeAlignment = false;
this->UseZeroLengthBitfieldAlignment = true;
+ this->UseLeadingZeroLengthBitfield = false;
this->ZeroLengthBitfieldBoundary = 32;
this->MinGlobalAlign = 0;
this->DefaultAlignForAttributeAligned = 128;
diff --git a/clang/test/CodeGen/SystemZ/zos-alignment.c b/clang/test/CodeGen/SystemZ/zos-alignment.c
index 4b572fcac5a9..703fd1a46c3b 100644
--- a/clang/test/CodeGen/SystemZ/zos-alignment.c
+++ b/clang/test/CodeGen/SystemZ/zos-alignment.c
@@ -83,6 +83,35 @@ struct s6 {
// 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;
More information about the cfe-commits
mailing list