llvm.org GIT mirror llvm / a959524
[Debugify] Move debug value intrinsics closer to their operand defs Before this patch, debugify would insert debug value intrinsics before the terminating instruction in a block. This had the advantage of being simple, but was a bit too simple/unrealistic. This patch teaches debugify to insert debug values immediately after their operand defs. This enables better testing of the compiler. For example, with this patch, `opt -debugify-each` is able to identify a vectorizer DI-invariance bug fixed in llvm.org/PR32761. In this bug, the vectorizer produced different output with/without debug info present. Reverting Davide's bugfix locally, I see: $ ~/scripts/opt-check-dbg-invar.sh ./bin/opt \ .../SLPVectorizer/AArch64/spillcost-di.ll -slp-vectorizer Comparing: -slp-vectorizer .../SLPVectorizer/AArch64/spillcost-di.ll Baseline: /var/folders/j8/t4w0bp8j6x1g6fpghkcb4sjm0000gp/T/tmp.iYYeL1kf With DI : /var/folders/j8/t4w0bp8j6x1g6fpghkcb4sjm0000gp/T/tmp.sQtQSeet 9,11c9,11 < %5 = getelementptr inbounds %0, %0* %2, i64 %0, i32 1 < %6 = bitcast i64* %4 to <2 x i64>* < %7 = load <2 x i64>, <2 x i64>* %6, align 8, !tbaa !0 --- > %5 = load i64, i64* %4, align 8, !tbaa !0 > %6 = getelementptr inbounds %0, %0* %2, i64 %0, i32 1 > %7 = load i64, i64* %6, align 8, !tbaa !5 12a13 > store i64 %5, i64* %8, align 8, !tbaa !0 14,15c15 < %10 = bitcast i64* %8 to <2 x i64>* < store <2 x i64> %7, <2 x i64>* %10, align 8, !tbaa !0 --- > store i64 %7, i64* %9, align 8, !tbaa !5 :: Found a test case ^ Running this over the *.ll files in tree, I found four additional examples which compile differently with/without DI present. I plan on filing bugs for these. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334118 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 1 year, 3 months ago
4 changed file(s) with 32 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
66 declare void @test_f()
77
88 define i32* @test_salvage() {
9 ; Check that all four original local variables have their values preserved.
910 ; CHECK-LABEL: @test_salvage()
1011 ; CHECK-NEXT: malloc
12 ; CHECK-NEXT: @llvm.dbg.value(metadata i8* %p, metadata ![[p:.*]], metadata !DIExpression())
1113 ; CHECK-NEXT: bitcast
14 ; CHECK-NEXT: @llvm.dbg.value(metadata i32* %P, metadata ![[P:.*]], metadata !DIExpression())
15 ; CHECK-NEXT: @llvm.dbg.value(metadata i32* %P, metadata ![[DEAD:.*]], metadata !DIExpression(DW_OP_deref))
16 ; CHECK-NEXT: call void @llvm.dbg.value(metadata i32* %P, metadata ![[DEAD2:.*]], metadata !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 1, DW_OP_stack_value))
1217 ; CHECK-NEXT: call void @test_f()
1318 ; CHECK-NEXT: store i32 0, i32* %P
1419
15 ; Check that all four original local variables have their values preserved.
16 ; CHECK-NEXT: @llvm.dbg.value(metadata i8* %p, metadata ![[p:.*]], metadata !DIExpression())
17 ; CHECK-NEXT: @llvm.dbg.value(metadata i32* %P, metadata ![[P:.*]], metadata !DIExpression())
18 ; CHECK-NEXT: @llvm.dbg.value(metadata i32* %P, metadata ![[DEAD:.*]], metadata !DIExpression(DW_OP_deref))
19 ; CHECK-NEXT: call void @llvm.dbg.value(metadata i32* %P, metadata ![[DEAD2:.*]], metadata !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 1, DW_OP_stack_value))
2020 %p = tail call i8* @malloc(i32 4)
2121 %P = bitcast i8* %p to i32*
2222 %DEAD = load i32, i32* %P
140140 ; CHECK: br label %if.end3
141141 ; CHECK: if.end3:
142142 ; CHECK: %[[PREPHI:.*]] = phi i64 [ %sub.ptr.sub, %if.else ], [ %[[SUB]], %if.then2 ], [ %sub.ptr.sub, %entry ]
143 ; CHECK: %[[DIV:.*]] = ashr exact i64 %[[PREPHI]], 2
144143 ; CHECK: call void @llvm.dbg.value(metadata i32* %p.0, metadata [[var_p0:![0-9]+]], metadata !DIExpression())
145144 ; CHECK: call void @llvm.dbg.value(metadata i64 %sub.ptr.rhs.cast5.pre-phi, metadata [[var_sub_ptr:![0-9]+]], metadata !DIExpression())
145 ; CHECK: %[[DIV:.*]] = ashr exact i64 %[[PREPHI]], 2
146146 ; CHECK: ret i64 %[[DIV]]
147147
148148 declare void @bar(...) local_unnamed_addr #1
1717 ; Same as above
1818 define i32 @test2(i32* %addr.i) {
1919 ; CHECK-LABEL: @test2
20 ; CHECK-NEXT: call void @llvm.dbg.value(metadata i32* %addr.i, metadata [[var_a:![0-9]+]], metadata !DIExpression(DW_OP_deref))
2021 ; CHECK-NEXT: fence
21 ; CHECK-NEXT: call void @llvm.dbg.value(metadata i32* %addr.i, metadata [[var_a:![0-9]+]], metadata !DIExpression(DW_OP_deref))
22 ; CHECK-NOT: load
2223 ; CHECK-NEXT: call void @llvm.dbg.value(metadata i32* %addr.i, metadata [[var_a2:![0-9]+]], metadata !DIExpression(DW_OP_deref))
23 ; CHECK-NOT: load
2424 ; CHECK: ret
2525 %a = load i32, i32* %addr.i, align 4
2626 fence release
4343 return F.isDeclaration() || !F.hasExactDefinition();
4444 }
4545
46 /// Find a suitable insertion point for debug values intrinsics.
46 /// Find the basic block's terminating instruction.
4747 ///
48 /// These must be inserted before the terminator. Special care is needed to
49 /// handle musttail and deopt calls, as these behave like (but are in fact not)
50 /// terminators.
51 Instruction *findDebugValueInsertionPoint(BasicBlock &BB) {
48 /// Special care is needed to handle musttail and deopt calls, as these behave
49 /// like (but are in fact not) terminators.
50 Instruction *findTerminatingInstruction(BasicBlock &BB) {
5251 if (auto *I = BB.getTerminatingMustTailCall())
5352 return I;
5453 if (auto *I = BB.getTerminatingDeoptimizeCall())
108107 if (BB.isEHPad())
109108 continue;
110109
111 Instruction *LastInst = findDebugValueInsertionPoint(BB);
110 // Find the terminating instruction, after which no debug values are
111 // attached.
112 Instruction *LastInst = findTerminatingInstruction(BB);
113 assert(LastInst && "Expected basic block with a terminator");
114
115 // Maintain an insertion point which can't be invalidated when updates
116 // are made.
117 BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
118 assert(InsertPt != BB.end() && "Expected to find an insertion point");
119 Instruction *InsertBefore = &*InsertPt;
112120
113121 // Attach debug values.
114 for (auto It = BB.begin(), End = LastInst->getIterator(); It != End;
115 ++It) {
116 Instruction &I = *It;
117
122 for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
118123 // Skip void-valued instructions.
119 if (I.getType()->isVoidTy())
124 if (I->getType()->isVoidTy())
120125 continue;
121126
122 // Skip any just-inserted intrinsics.
123 if (isa(&I))
124 break;
127 // Phis and EH pads must be grouped at the beginning of the block.
128 // Only advance the insertion point when we finish visiting these.
129 if (!isa(I) && !I->isEHPad())
130 InsertBefore = I->getNextNode();
125131
126132 std::string Name = utostr(NextVar++);
127 const DILocation *Loc = I.getDebugLoc().get();
133 const DILocation *Loc = I->getDebugLoc().get();
128134 auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
129 getCachedDIType(I.getType()),
135 getCachedDIType(I->getType()),
130136 /*AlwaysPreserve=*/true);
131 DIB.insertDbgValueIntrinsic(&I, LocalVar, DIB.createExpression(), Loc,
132 LastInst);
137 DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc,
138 InsertBefore);
133139 }
134140 }
135141 DIB.finalizeSubprogram(SP);