[PATCH] D43734: [RecordLayout] Don't align to non-power-of-2 sizes when using -mms-bitfields

Martin Storsjö via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 24 14:29:23 PST 2018


mstorsjo created this revision.
mstorsjo added reviewers: compnerd, rnk, majnemer.

When targeting GNU/MinGW for i386, the size of the "long double" data type is 12 bytes (while it is 8 bytes in MSVC). When building
with -mms-bitfields to have struct layouts match MSVC, data types are laid out in a struct with alignment according to their size. However, this doesn't make sense for the long double type, since it doesn't match MSVC at all, and aligning to a non-power-of-2 size triggers other asserts later.

This matches what GCC does, aligning a long double to 4 bytes in structs on i386 even when -mms-bitfields is specified.

This fixes asserts when using the max_align_t data type when building for MinGW/i386 with the -mms-bitfields flag.


Repository:
  rC Clang

https://reviews.llvm.org/D43734

Files:
  lib/AST/RecordLayoutBuilder.cpp
  test/CodeGen/mingw-long-double.c


Index: test/CodeGen/mingw-long-double.c
===================================================================
--- test/CodeGen/mingw-long-double.c
+++ test/CodeGen/mingw-long-double.c
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -triple i686-windows-gnu -emit-llvm -o - %s \
 // RUN:    | FileCheck %s --check-prefix=GNU32
+// RUN: %clang_cc1 -triple i686-windows-gnu -emit-llvm -o - %s -mms-bitfields \
+// RUN:    | FileCheck %s --check-prefix=GNU32
 // RUN: %clang_cc1 -triple x86_64-windows-gnu -emit-llvm -o - %s \
 // RUN:    | FileCheck %s --check-prefix=GNU64
 // RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm -o - %s \
Index: lib/AST/RecordLayoutBuilder.cpp
===================================================================
--- lib/AST/RecordLayoutBuilder.cpp
+++ lib/AST/RecordLayoutBuilder.cpp
@@ -1752,7 +1752,8 @@
       QualType T = Context.getBaseElementType(D->getType());
       if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
         CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
-        if (TypeSize > FieldAlign)
+        if (TypeSize > FieldAlign &&
+            llvm::isPowerOf2_64(TypeSize.getQuantity()))
           FieldAlign = TypeSize;
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43734.135805.patch
Type: text/x-patch
Size: 1197 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180224/9a70e338/attachment.bin>


More information about the cfe-commits mailing list