diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -185,9 +185,12 @@ else addGlobalName(GV->getName(), *VariableDIE, DeclContext); - if (uint32_t AlignInBytes = GV->getAlignInBytes()) - addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, - AlignInBytes); + // DW_AT_alignment was introduced in DWARF 5, in strict DWARF mode, only + // generate this attribute when the DWARF version is not smaller than 5. + if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) + if (uint32_t AlignInBytes = GV->getAlignInBytes()) + addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, + AlignInBytes); if (MDTuple *TP = GV->getTemplateParams()) addTemplateParams(*VariableDIE, DINodeArray(TP)); @@ -1481,7 +1484,10 @@ if (!Name.empty()) addString(VariableDie, dwarf::DW_AT_name, Name); const auto *DIVar = Var.getVariable(); - if (DIVar) + // DW_AT_alignment was introduced in DWARF 5, in strict DWARF mode, only + // generate this attribute when the DWARF version is not smaller than 5. + if (DIVar && + (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5)) if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, AlignInBytes); diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -992,9 +992,12 @@ RLang); // Add align info if available. - if (uint32_t AlignInBytes = CTy->getAlignInBytes()) - addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, - AlignInBytes); + // DW_AT_alignment was introduced in DWARF 5, in strict DWARF mode, only + // generate this attribute when the DWARF version is not smaller than 5. + if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) + if (uint32_t AlignInBytes = CTy->getAlignInBytes()) + addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, + AlignInBytes); } } @@ -1613,7 +1616,10 @@ } else { // This is not a bitfield. OffsetInBytes = DT->getOffsetInBits() / 8; - if (AlignInBytes) + // DW_AT_alignment was introduced in DWARF 5, in strict DWARF mode, only + // generate this attribute when the DWARF version is not smaller than 5. + if (AlignInBytes && + (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5)) addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, AlignInBytes); } @@ -1694,9 +1700,12 @@ if (const ConstantFP *CFP = dyn_cast_or_null(DT->getConstant())) addConstantFPValue(StaticMemberDIE, CFP); - if (uint32_t AlignInBytes = DT->getAlignInBytes()) - addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, - AlignInBytes); + // DW_AT_alignment was introduced in DWARF 5, in strict DWARF mode, only + // generate this attribute when the DWARF version is not smaller than 5. + if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) + if (uint32_t AlignInBytes = DT->getAlignInBytes()) + addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, + AlignInBytes); return &StaticMemberDIE; } diff --git a/llvm/test/DebugInfo/PowerPC/alignment.ll b/llvm/test/DebugInfo/PowerPC/alignment.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/PowerPC/alignment.ll @@ -0,0 +1,37 @@ +; RUN: llc -filetype=obj -mtriple=powerpc64le-unknown-linux-gnu < %s | \ +; RUN: llvm-dwarfdump -debug-info - | FileCheck %s +; RUN: llc -filetype=obj -mtriple=powerpc64le-unknown-linux-gnu \ +; RUN: -strict-dwarf=true < %s | llvm-dwarfdump -debug-info - | \ +; RUN: FileCheck %s --check-prefix=STRICT + +; CHECK: DW_AT_alignment +; STRICT-NOT: DW_AT_alignment + +@_ZL3var = internal global i32 0, align 16, !dbg !0 + +define dso_local signext i32 @_Z3foov() #0 !dbg !11 { + %1 = load i32, i32* @_ZL3var, align 16, !dbg !15 + ret i32 %1, !dbg !16 +} + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!7, !8, !9} +!llvm.ident = !{!10} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "var", linkageName: "_ZL3var", scope: !2, file: !3, line: 1, type: !6, isLocal: true, isDefinition: true, align: 128) +!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 13.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "align.cpp", directory: "./") +!4 = !{} +!5 = !{!0} +!6 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!7 = !{i32 7, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{i32 1, !"wchar_size", i32 4} +!10 = !{!"clang version 13.0.0"} +!11 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !3, file: !3, line: 3, type: !12, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4) +!12 = !DISubroutineType(types: !13) +!13 = !{!14} +!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!15 = !DILocation(line: 5, column: 18, scope: !11) +!16 = !DILocation(line: 5, column: 11, scope: !11)