MaggieYi created this revision.
MaggieYi added reviewers: echristo, jmorse, wolfgangp, probinson, dblaikie, 
aprantl.
Herald added a project: All.
MaggieYi requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The detailed description of the issue could be found in 
https://bugs.llvm.org/show_bug.cgi?id=51277.

In the original code, the 'getDeclAlignIfRequired' function is used.
The 'getDeclAlignIfRequired' function will return the max alignment
of all aligned attributes if the type has aligned attributes. The
function doesn’t consider the type at all.

The 'getTypeAlignIfRequired' function uses the type’s alignment value,
which also used by the 'alignof' function. I think we should use the
function of 'getTypeAlignIfRequired'.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124006

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


Index: clang/test/CodeGen/debug-info-struct-align.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/debug-info-struct-align.c
@@ -0,0 +1,21 @@
+//  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)));
+struct MyType mt;
+
+struct MyType1 {
+  int m;
+} __attribute__((packed, aligned(1)));
+struct MyType1 mt1;
+
+struct MyType2 {
+  __attribute__((packed))int m;
+} __attribute__((aligned(1)));
+struct MyType2 mt2;
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3553,7 +3553,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);
 


Index: clang/test/CodeGen/debug-info-struct-align.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/debug-info-struct-align.c
@@ -0,0 +1,21 @@
+//  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)));
+struct MyType mt;
+
+struct MyType1 {
+  int m;
+} __attribute__((packed, aligned(1)));
+struct MyType1 mt1;
+
+struct MyType2 {
+  __attribute__((packed))int m;
+} __attribute__((aligned(1)));
+struct MyType2 mt2;
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3553,7 +3553,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);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to