[clang] b09ba42 - Bug 51277: [DWARF] DW_AT_alignment incorrect when

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


Author: Ying Yi
Date: 2022-04-22T12:15:00+01:00
New Revision: b09ba42620768c0092b20cf526a30b14752a5dc9

URL: https://github.com/llvm/llvm-project/commit/b09ba42620768c0092b20cf526a30b14752a5dc9
DIFF: https://github.com/llvm/llvm-project/commit/b09ba42620768c0092b20cf526a30b14752a5dc9.diff

LOG: Bug 51277: [DWARF] DW_AT_alignment incorrect when
attribute((__aligned__)) is present but ignored`

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'.

Reviewed By: dblaikie, jmorse, wolfgangp

Differential Revision: https://reviews.llvm.org/D124006

Added: 
    clang/test/CodeGenCXX/debug-info-struct-align.cpp

Modified: 
    clang/lib/CodeGen/CGDebugInfo.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 846b9db76dd4a..a6ea2c0f31abf 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3561,7 +3561,11 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
     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);
 

diff  --git a/clang/test/CodeGenCXX/debug-info-struct-align.cpp b/clang/test/CodeGenCXX/debug-info-struct-align.cpp
new file mode 100644
index 0000000000000..6d75c71476ba1
--- /dev/null
+++ b/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");


        


More information about the cfe-commits mailing list