r337047 - Use external layout information to layout bit-fields for MS ABI.
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 13 14:07:43 PDT 2018
Author: rsmith
Date: Fri Jul 13 14:07:42 2018
New Revision: 337047
URL: http://llvm.org/viewvc/llvm-project?rev=337047&view=rev
Log:
Use external layout information to layout bit-fields for MS ABI.
Patch by Aleksandr Urakov!
Differential Revision: https://reviews.llvm.org/D49227
Added:
cfe/trunk/test/CodeGenCXX/Inputs/override-bit-field-layout.layout
cfe/trunk/test/CodeGenCXX/override-bit-field-layout.cpp
Modified:
cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=337047&r1=337046&r2=337047&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Fri Jul 13 14:07:42 2018
@@ -2677,7 +2677,7 @@ void MicrosoftRecordLayoutBuilder::layou
// Check to see if this bitfield fits into an existing allocation. Note:
// MSVC refuses to pack bitfields of formal types with different sizes
// into the same allocation.
- if (!IsUnion && LastFieldIsNonZeroWidthBitfield &&
+ if (!UseExternalLayout && !IsUnion && LastFieldIsNonZeroWidthBitfield &&
CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) {
placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField);
RemainingBitsInField -= Width;
@@ -2689,6 +2689,14 @@ void MicrosoftRecordLayoutBuilder::layou
placeFieldAtOffset(CharUnits::Zero());
Size = std::max(Size, Info.Size);
// TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
+ } else if (UseExternalLayout) {
+ auto FieldBitOffset = External.getExternalFieldOffset(FD);
+ placeFieldAtBitOffset(FieldBitOffset);
+ auto NewSize = Context.toCharUnitsFromBits(
+ llvm::alignTo(FieldBitOffset + Width, Context.getCharWidth()));
+ assert(NewSize >= Size && "bit field offset already allocated");
+ Size = NewSize;
+ Alignment = std::max(Alignment, Info.Alignment);
} else {
// Allocate a new block of memory and place the bitfield in it.
CharUnits FieldOffset = Size.alignTo(Info.Alignment);
Added: cfe/trunk/test/CodeGenCXX/Inputs/override-bit-field-layout.layout
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/Inputs/override-bit-field-layout.layout?rev=337047&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/Inputs/override-bit-field-layout.layout (added)
+++ cfe/trunk/test/CodeGenCXX/Inputs/override-bit-field-layout.layout Fri Jul 13 14:07:42 2018
@@ -0,0 +1,16 @@
+
+*** Dumping AST Record Layout
+Type: struct S1
+
+Layout: <ASTRecordLayout
+ Size:16
+ Alignment:16
+ FieldOffsets: [0, 11]>
+
+*** Dumping AST Record Layout
+Type: struct S2
+
+Layout: <ASTRecordLayout
+ Size:128
+ Alignment:64
+ FieldOffsets: [64]>
Added: cfe/trunk/test/CodeGenCXX/override-bit-field-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/override-bit-field-layout.cpp?rev=337047&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/override-bit-field-layout.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/override-bit-field-layout.cpp Fri Jul 13 14:07:42 2018
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -w -fdump-record-layouts-simple -foverride-record-layout=%S/Inputs/override-bit-field-layout.layout %s | FileCheck %s
+
+// CHECK: Type: struct S1
+// CHECK: FieldOffsets: [0, 11]
+struct S1 {
+ short a : 3;
+ short b : 5;
+};
+
+// CHECK: Type: struct S2
+// CHECK: FieldOffsets: [64]
+struct S2 {
+ virtual ~S2() = default;
+ short a : 3;
+};
+
+void use_structs() {
+ S1 s1s[sizeof(S1)];
+ S2 s2s[sizeof(S2)];
+}
More information about the cfe-commits
mailing list