Skip to content

Commit 2b958b9

Browse files
[SelectionDAG] Salvage debuginfo when combining load and sext instrs. (#169779)
SelectionDAG uses the DAGCombiner to fold a load followed by a sext to a load and sext instruction. For example, in x86 we will see that ``` %1 = load i32, ptr @GloBaRR #dbg_value(i32 %1, !43, !DIExpression(), !52) %2 = sext i32 %1 to i64, !dbg !53 ``` is converted to: ``` %0:gr64_nosp = MOVSX64rm32 $rip, 1, $noreg, @GloBaRR, $noreg, debug-instr-number 1, debug-location !51 DBG_VALUE $noreg, $noreg, !"Idx", !DIExpression(), debug-location !52 ``` The `DBG_VALUE` needs to be transferred correctly to the new combined instruction, and it needs to be appended with a `DIExpression` which contains a `DW_OP_LLVM_fragment`, describing that the lower bits of the virtual register contain the value. This patch fixes the above described problem.
1 parent e603fac commit 2b958b9

File tree

3 files changed

+170
-2
lines changed

3 files changed

+170
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "llvm/IR/Attributes.h"
5252
#include "llvm/IR/Constant.h"
5353
#include "llvm/IR/DataLayout.h"
54+
#include "llvm/IR/DebugInfoMetadata.h"
5455
#include "llvm/IR/DerivedTypes.h"
5556
#include "llvm/IR/Function.h"
5657
#include "llvm/IR/Metadata.h"
@@ -78,6 +79,7 @@
7879
#include <variant>
7980

8081
#include "MatchContext.h"
82+
#include "SDNodeDbgValue.h"
8183

8284
using namespace llvm;
8385
using namespace llvm::SDPatternMatch;
@@ -14465,17 +14467,52 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1446514467
LN0->getBasePtr(), N0.getValueType(),
1446614468
LN0->getMemOperand());
1446714469
Combiner.ExtendSetCCUses(SetCCs, N0, ExtLoad, ExtOpc);
14470+
unsigned Opcode = N->getOpcode();
14471+
bool IsSigned = Opcode == ISD::SIGN_EXTEND;
1446814472
// If the load value is used only by N, replace it via CombineTo N.
14469-
bool NoReplaceTrunc = SDValue(LN0, 0).hasOneUse();
14470-
Combiner.CombineTo(N, ExtLoad);
14473+
SDValue OldLoadVal(LN0, 0);
14474+
SDValue OldExtValue(N, 0);
14475+
bool NoReplaceTrunc = OldLoadVal.hasOneUse();
14476+
14477+
// Because we are replacing a load and a s|z ext with a load-s|z ext
14478+
// instruction, the dbg_value attached to the load will be of a smaller bit
14479+
// width, and we have to add a DW_OP_LLVM_convert expression to get the
14480+
// correct size.
14481+
auto SalvageToOldLoadSize = [&](SDValue From, SDValue To, bool IsSigned) {
14482+
for (SDDbgValue *Dbg : DAG.GetDbgValues(From.getNode())) {
14483+
unsigned VarBitsFrom = From->getValueSizeInBits(0);
14484+
unsigned VarBitsTo = To->getValueSizeInBits(0);
14485+
14486+
// Build a convert expression for the s|z extend.
14487+
const DIExpression *OldE = Dbg->getExpression();
14488+
auto *NewE =
14489+
DIExpression::appendExt(OldE, VarBitsFrom, VarBitsTo, IsSigned);
14490+
14491+
// Create a new SDDbgValue that points at the widened node with the
14492+
// fragment.
14493+
Dbg->setIsInvalidated();
14494+
Dbg->setIsEmitted();
14495+
SDDbgValue *NewDV = DAG.getDbgValue(
14496+
Dbg->getVariable(), NewE, To.getNode(), To.getResNo(),
14497+
Dbg->isIndirect(), Dbg->getDebugLoc(), Dbg->getOrder());
14498+
DAG.AddDbgValue(NewDV, /*isParametet*/ false);
14499+
}
14500+
};
14501+
1447114502
if (NoReplaceTrunc) {
14503+
if (LN0->getHasDebugValue())
14504+
SalvageToOldLoadSize(OldLoadVal, ExtLoad, IsSigned);
14505+
14506+
if (N->getHasDebugValue())
14507+
DAG.transferDbgValues(OldExtValue, ExtLoad);
1447214508
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
1447314509
Combiner.recursivelyDeleteUnusedNodes(LN0);
1447414510
} else {
1447514511
SDValue Trunc =
1447614512
DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
1447714513
Combiner.CombineTo(LN0, Trunc, ExtLoad.getValue(1));
1447814514
}
14515+
Combiner.CombineTo(N, ExtLoad);
1447914516
return SDValue(N, 0); // Return N so it doesn't get rechecked!
1448014517
}
1448114518

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; This test checks that after SelectionDAG runs, it preserves the debug info that is lost due to the DAGCombiner combining a load and a sext instruction, where the #dbg_value is pointing to the result of the load.
2+
; However, in this case, the load has multiple uses.
3+
4+
; RUN: llc %s -mtriple=x86_64-unkown-linux -start-before=x86-isel -stop-after=x86-isel -o - | FileCheck %s --check-prefix=MIR
5+
; RUN: llc -O2 %s -start-before=x86-isel -mtriple=x86_64-unkown-linux --filetype=obj -o %t.o
6+
; RUN: llvm-dwarfdump %t.o --name Idx | FileCheck %s --check-prefix=DUMP
7+
; RUN: llvm-dwarfdump %t.o --name Idx2 | FileCheck %s --check-prefix=DUMP2
8+
9+
; MIR: ![[IDX:[0-9]+]] = !DILocalVariable(name: "Idx"
10+
; MIR: ![[IDX2:[0-9]+]] = !DILocalVariable(name: "Idx2"
11+
; MIR: name: _Z8useValuei
12+
; MIR: name: main
13+
; MIR: debugValueSubstitutions
14+
; MIR-NEXT: - { srcinst: [[INSTR_NUM2:[0-9]+]], srcop: 0, dstinst: [[INSTR_NUM:[0-9]+]], dstop: 0, subreg: 6 }
15+
; MIR-LABEL: bb.0 (%ir-block.0)
16+
; MIR: %{{[0-9a-f]+}}{{.*}} = MOVSX64rm32 ${{.*}}, 1, $noreg, @GlobArr, $noreg, debug-instr-number [[INSTR_NUM]]
17+
; MIR-NEXT: {{.*}} = COPY %0.sub_32bit
18+
; MIR-NEXT DBG_INSTR_REF ![[IDX]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref([[INSTR_NUM2]], 0)
19+
; MIR-NEXT DBG_INSTR_REF ![[IDX2]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref([[INSTR_NUM]], 0)
20+
21+
; DUMP: DW_AT_location (indexed ({{[0-9a-f]+}}x{{[0-9a-f]+}}) loclist = 0x{{[0-9a-f]+}}:
22+
; DUMP-NEXT: [0x{{[0-9a-f]+}}, 0x{{[0-9a-f]+}}): DW_OP_reg3 RBX)
23+
24+
; DUMP2: DW_AT_location (indexed ({{[0-9a-f]+}}x{{[0-9a-f]+}}) loclist = 0x{{[0-9a-f]+}}:
25+
; DUMP2-NEXT: [0x{{[0-9a-f]+}}, 0x{{[0-9a-f]+}}): DW_OP_reg3 RBX)
26+
27+
28+
29+
@GlobArr = dso_local local_unnamed_addr global [5 x i32] [i32 1, i32 1, i32 2, i32 3, i32 5], align 16, !dbg !0
30+
@__const.main.Data = private unnamed_addr constant [7 x i32] [i32 10, i32 20, i32 30, i32 40, i32 50, i32 60, i32 70], align 16
31+
define dso_local void @_Z8useValuei(i32 noundef %0) local_unnamed_addr #0 !dbg !22 {
32+
ret void, !dbg !28
33+
}
34+
define dso_local noundef i32 @main() local_unnamed_addr #1 !dbg !29 {
35+
%1 = load i32, ptr @GlobArr
36+
#dbg_value(i32 %1, !43, !DIExpression(), !52)
37+
%2 = sext i32 %1 to i64
38+
#dbg_value(i64 %2, !57, !DIExpression(), !52)
39+
tail call void @_Z8useValuei(i32 noundef %1), !dbg !56
40+
%3 = getelementptr inbounds i32, ptr @__const.main.Data, i64 %2
41+
%4 = load i32, ptr %3
42+
tail call void @_Z8useValuei(i32 noundef %4), !dbg !56
43+
ret i32 0
44+
}
45+
!llvm.dbg.cu = !{!2}
46+
!llvm.module.flags = !{!10, !11, !16}
47+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
48+
!1 = distinct !DIGlobalVariable(type: !6, isDefinition: true)
49+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, emissionKind: FullDebug, nameTableKind: None)
50+
!3 = !DIFile(filename: "/tmp/test.cpp", directory: "/Users/srastogi/Development/llvm-project/build_ninja", checksumkind: CSK_MD5, checksum: "0fe735937e606b4db3e3b2e9253eff90")
51+
!6 = !DICompositeType(tag: DW_TAG_array_type, elements: !8)
52+
!7 = !DIBasicType()
53+
!8 = !{}
54+
!10 = !{i32 7, !"Dwarf Version", i32 5}
55+
!11 = !{i32 2, !"Debug Info Version", i32 3}
56+
!16 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
57+
!22 = distinct !DISubprogram(type: !23, unit: !2, keyInstructions: true)
58+
!23 = !DISubroutineType(types: !24)
59+
!24 = !{}
60+
!28 = !DILocation(scope: !22, atomRank: 1)
61+
!29 = distinct !DISubprogram(type: !30, unit: !2, keyInstructions: true)
62+
!30 = !DISubroutineType(types: !31)
63+
!31 = !{}
64+
!38 = distinct !DILexicalBlock(scope: !29, line: 5, column: 3)
65+
!43 = !DILocalVariable(name: "Idx", scope: !44, type: !7)
66+
!44 = distinct !DILexicalBlock(scope: !38, line: 5, column: 3)
67+
!46 = distinct !DILexicalBlock(scope: !44, line: 5, column: 27)
68+
!52 = !DILocation(scope: !44)
69+
!56 = !DILocation(scope: !46)
70+
!57 = !DILocalVariable(name: "Idx2", scope: !44, type: !7)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; This test checks that after SelectionDAG runs, it preserves the debug info that is lost due to the DAGCombiner combining a load and a sext instruction, where the #dbg_value is pointing to the result of the load.
2+
; RUN: llc %s -mtriple=x86_64-unkown-linux -start-before=x86-isel -stop-after=x86-isel -o - | FileCheck %s --check-prefix=MIR
3+
; RUN: llc -O2 %s -start-before=x86-isel -mtriple=x86_64-unkown-linux --filetype=obj -o %t.o
4+
; RUN: llvm-dwarfdump %t.o --name Idx | FileCheck %s --check-prefix=DUMP
5+
; RUN: llvm-dwarfdump %t.o --name Idx2 | FileCheck %s --check-prefix=DUMP2
6+
7+
; MIR: ![[IDX:[0-9]+]] = !DILocalVariable(name: "Idx"
8+
; MIR: ![[IDX2:[0-9]+]] = !DILocalVariable(name: "Idx2"
9+
; MIR-LABEL: bb.0
10+
; MIR: %{{[0-9a-f]+}}{{.*}} = MOVSX64rm32 ${{.*}}, 1, $noreg, @GlobArr, $noreg, debug-instr-number [[INSTR_NUM:[0-9]+]]
11+
; MIR-NEXT: DBG_INSTR_REF ![[IDX]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, DW_OP_stack_value), dbg-instr-ref([[INSTR_NUM]], 0)
12+
; MIR-NEXT: DBG_INSTR_REF ![[IDX2]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref([[INSTR_NUM]], 0)
13+
14+
; DUMP: DW_AT_location (indexed ({{[0-9a-f]+}}x{{[0-9a-f]+}}) loclist = 0x{{[0-9a-f]+}}:
15+
; DUMP-NEXT: [0x{{[0-9a-f]+}}, 0x{{[0-9a-f]+}}): DW_OP_breg0 RAX+0, DW_OP_convert (0x{{[0-9a-f]+}}) "DW_ATE_signed_32", DW_OP_convert (0x{{[0-9a-f]+}}) "DW_ATE_signed_64", DW_OP_stack_value)
16+
17+
; DUMP2: DW_AT_location (indexed ({{[0-9a-f]+}}x{{[0-9a-f]+}}) loclist = 0x{{[0-9a-f]+}}:
18+
; DUMP2-NEXT: [0x{{[0-9a-f]+}}, 0x{{[0-9a-f]+}}): DW_OP_reg0 RAX)
19+
20+
21+
@GlobArr = dso_local local_unnamed_addr global [5 x i32] [i32 1, i32 1, i32 2, i32 3, i32 5], align 16, !dbg !0
22+
@__const.main.Data = private unnamed_addr constant [7 x i32] [i32 10, i32 20, i32 30, i32 40, i32 50, i32 60, i32 70], align 16
23+
define dso_local void @_Z8useValuei(i32 noundef %0) local_unnamed_addr #0 !dbg !22 {
24+
ret void, !dbg !28
25+
}
26+
define dso_local noundef i32 @main() local_unnamed_addr #1 !dbg !29 {
27+
%1 = load i32, ptr @GlobArr
28+
#dbg_value(i32 %1, !43, !DIExpression(), !52)
29+
%2 = sext i32 %1 to i64
30+
#dbg_value(i64 %2, !57, !DIExpression(), !52)
31+
%3 = getelementptr inbounds i32, ptr @__const.main.Data, i64 %2
32+
%4 = load i32, ptr %3
33+
tail call void @_Z8useValuei(i32 noundef %4), !dbg !56
34+
ret i32 0
35+
}
36+
!llvm.dbg.cu = !{!2}
37+
!llvm.module.flags = !{!10, !11, !16}
38+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
39+
!1 = distinct !DIGlobalVariable(type: !6, isDefinition: true)
40+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, emissionKind: FullDebug, nameTableKind: None)
41+
!3 = !DIFile(filename: "/tmp/test.cpp", directory: "/Users/srastogi/Development/llvm-project/build_ninja", checksumkind: CSK_MD5, checksum: "0fe735937e606b4db3e3b2e9253eff90")
42+
!6 = !DICompositeType(tag: DW_TAG_array_type, elements: !8)
43+
!7 = !DIBasicType()
44+
!8 = !{}
45+
!10 = !{i32 7, !"Dwarf Version", i32 5}
46+
!11 = !{i32 2, !"Debug Info Version", i32 3}
47+
!16 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
48+
!22 = distinct !DISubprogram(type: !23, unit: !2, keyInstructions: true)
49+
!23 = !DISubroutineType(types: !24)
50+
!24 = !{}
51+
!28 = !DILocation(scope: !22, atomRank: 1)
52+
!29 = distinct !DISubprogram(type: !30, unit: !2, keyInstructions: true)
53+
!30 = !DISubroutineType(types: !31)
54+
!31 = !{}
55+
!38 = distinct !DILexicalBlock(scope: !29, line: 5, column: 3)
56+
!43 = !DILocalVariable(name: "Idx", scope: !44, type: !7)
57+
!44 = distinct !DILexicalBlock(scope: !38, line: 5, column: 3)
58+
!46 = distinct !DILexicalBlock(scope: !44, line: 5, column: 27)
59+
!52 = !DILocation(scope: !44)
60+
!56 = !DILocation(scope: !46)
61+
!57 = !DILocalVariable(name: "Idx2", scope: !44, type: !7)

0 commit comments

Comments
 (0)