[PATCH] D124006: [DebugInfo] Use the 'getTypeAlignIfRequired' function to get DW_AT_alignment correct when attribute((__aligned__)) is present.

Ying Yi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 22 04:16:21 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb09ba4262076: Bug 51277: [DWARF] DW_AT_alignment incorrect when (authored by MaggieYi).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124006/new/

https://reviews.llvm.org/D124006

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/test/CodeGenCXX/debug-info-struct-align.cpp


Index: clang/test/CodeGenCXX/debug-info-struct-align.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/debug-info-struct-align.cpp
@@ -0,0 +1,27 @@
+//  Test for debug info related to DW_AT_alignment attribute in the struct type.
+// RUN: %clang_cc1 -dwarf-version=5 -debug-info-kind=standalone -S -emit-llvm %s -o - | FileCheck %s
+
+// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType", {{.*}}, align: 32
+// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType1", {{.*}}, align: 8
+// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType2", {{.*}}, align: 8
+
+struct MyType {
+  int m;
+} __attribute__((aligned(1)));
+MyType mt;
+
+static_assert(alignof(MyType) == 4, "alignof MyType is wrong");
+
+struct MyType1 {
+  int m;
+} __attribute__((packed, aligned(1)));
+MyType1 mt1;
+
+static_assert(alignof(MyType1) == 1, "alignof MyType1 is wrong");
+
+struct MyType2 {
+  __attribute__((packed)) int m;
+} __attribute__((aligned(1)));
+MyType2 mt2;
+
+static_assert(alignof(MyType2) == 1, "alignof MyType2 is wrong");
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3561,7 +3561,11 @@
     return getOrCreateRecordFwdDecl(Ty, RDContext);
 
   uint64_t Size = CGM.getContext().getTypeSize(Ty);
-  auto Align = getDeclAlignIfRequired(D, CGM.getContext());
+  // __attribute__((aligned)) can increase or decrease alignment *except* on a
+  // struct or struct member, where it only increases  alignment unless 'packed'
+  // is also specified. To handle this case, the `getTypeAlignIfRequired` needs
+  // to be used.
+  auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
 
   SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124006.424430.patch
Type: text/x-patch
Size: 1909 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220422/ce007e2f/attachment.bin>


More information about the cfe-commits mailing list