Skip to content

Commit a3caf7f

Browse files
committed
[DebugInfo] Improve dbg preservation in LSR.
Use SCEV to salvage additional @llvm.dbg.value that have turned into referencing undef after transformation (and traditional salvageDebugInfo). Before transformation compute SCEV for each @llvm.dbg.value in the loop body and store it (along side its current DIExpression). After transformation update those @llvm.dbg.value now referencing undef by comparing its stored SCEV to the SCEV of the current loop-header PHI-nodes. Allow match with offset by inserting compensation code in the DIExpression. Fixes : PR38815 Differential Revision: https://reviews.llvm.org/D87494
1 parent cf4aa68 commit a3caf7f

File tree

4 files changed

+141
-10
lines changed

4 files changed

+141
-10
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,15 @@ class ScalarEvolution {
11171117
const SCEV *S, const Loop *L,
11181118
SmallPtrSetImpl<const SCEVPredicate *> &Preds);
11191119

1120+
/// Compute \p LHS - \p RHS and returns the result as an APInt if it is a
1121+
/// constant, and None if it isn't.
1122+
///
1123+
/// This is intended to be a cheaper version of getMinusSCEV. We can be
1124+
/// frugal here since we just bail out of actually constructing and
1125+
/// canonicalizing an expression in the cases where the result isn't going
1126+
/// to be a constant.
1127+
Optional<APInt> computeConstantDifference(const SCEV *LHS, const SCEV *RHS);
1128+
11201129
private:
11211130
/// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
11221131
/// Value is deleted.
@@ -1799,15 +1808,6 @@ class ScalarEvolution {
17991808
bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
18001809
SCEV::NoWrapFlags &Flags);
18011810

1802-
/// Compute \p LHS - \p RHS and returns the result as an APInt if it is a
1803-
/// constant, and None if it isn't.
1804-
///
1805-
/// This is intended to be a cheaper version of getMinusSCEV. We can be
1806-
/// frugal here since we just bail out of actually constructing and
1807-
/// canonicalizing an expression in the cases where the result isn't going
1808-
/// to be a constant.
1809-
Optional<APInt> computeConstantDifference(const SCEV *LHS, const SCEV *RHS);
1810-
18111811
/// Drop memoized information computed for S.
18121812
void forgetMemoizedResults(const SCEV *S);
18131813

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "llvm/ADT/Hashing.h"
6060
#include "llvm/ADT/PointerIntPair.h"
6161
#include "llvm/ADT/STLExtras.h"
62+
#include "llvm/ADT/SetOperations.h"
6263
#include "llvm/ADT/SetVector.h"
6364
#include "llvm/ADT/SmallBitVector.h"
6465
#include "llvm/ADT/SmallPtrSet.h"
@@ -80,6 +81,7 @@
8081
#include "llvm/IR/BasicBlock.h"
8182
#include "llvm/IR/Constant.h"
8283
#include "llvm/IR/Constants.h"
84+
#include "llvm/IR/DebugInfoMetadata.h"
8385
#include "llvm/IR/DerivedTypes.h"
8486
#include "llvm/IR/Dominators.h"
8587
#include "llvm/IR/GlobalValue.h"
@@ -5776,6 +5778,27 @@ static bool ReduceLoopStrength(Loop *L, IVUsers &IU, ScalarEvolution &SE,
57765778
if (MSSA)
57775779
MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
57785780

5781+
// Debug preservation - record all llvm.dbg.value from the loop as well as
5782+
// the SCEV of their variable location. Since salvageDebugInfo may change the
5783+
// DIExpression we need to store the original here as well (i.e. it needs to
5784+
// be in sync with the SCEV).
5785+
SmallVector<
5786+
std::tuple<DbgValueInst *, const Type *, const SCEV *, DIExpression *>,
5787+
32>
5788+
DbgValues;
5789+
for (auto &B : L->getBlocks()) {
5790+
for (auto &I : *B) {
5791+
if (DbgValueInst *D = dyn_cast<DbgValueInst>(&I)) {
5792+
auto V = D->getVariableLocation();
5793+
if (!SE.isSCEVable(V->getType()))
5794+
continue;
5795+
auto DS = SE.getSCEV(V);
5796+
DbgValues.push_back(
5797+
std::make_tuple(D, V->getType(), DS, D->getExpression()));
5798+
}
5799+
}
5800+
}
5801+
57795802
// Run the main LSR transformation.
57805803
Changed |=
57815804
LSRInstance(L, IU, SE, DT, LI, TTI, AC, TLI, MSSAU.get()).getChanged();
@@ -5797,6 +5820,40 @@ static bool ReduceLoopStrength(Loop *L, IVUsers &IU, ScalarEvolution &SE,
57975820
DeleteDeadPHIs(L->getHeader(), &TLI, MSSAU.get());
57985821
}
57995822
}
5823+
// Debug preservation - go through all recorded llvm.dbg.value and for those
5824+
// that now have an undef variable location use the recorded SCEV to try and
5825+
// update it. Compare with SCEV of Phi-nodes of loop header to find a
5826+
// suitable update candidate. SCEV match with constant offset is allowed and
5827+
// will be compensated for in the DIExpression.
5828+
if (Changed) {
5829+
for (auto &D : DbgValues) {
5830+
auto DbgValue = std::get<DbgValueInst *>(D);
5831+
auto DbgValueType = std::get<const Type *>(D);
5832+
auto DbgValueSCEV = std::get<const SCEV *>(D);
5833+
auto DbgDIExpr = std::get<DIExpression *>(D);
5834+
if (!isa<UndefValue>(DbgValue->getVariableLocation()))
5835+
continue;
5836+
for (PHINode &Phi : L->getHeader()->phis()) {
5837+
if (DbgValueType != Phi.getType())
5838+
continue;
5839+
if (!SE.isSCEVable(Phi.getType()))
5840+
continue;
5841+
auto PhiSCEV = SE.getSCEV(&Phi);
5842+
if (Optional<APInt> Offset =
5843+
SE.computeConstantDifference(DbgValueSCEV, PhiSCEV)) {
5844+
auto &Ctx = DbgValue->getContext();
5845+
DbgValue->setOperand(
5846+
0, MetadataAsValue::get(Ctx, ValueAsMetadata::get(&Phi)));
5847+
if (Offset.getValue().getSExtValue()) {
5848+
SmallVector<uint64_t, 8> Ops;
5849+
DIExpression::appendOffset(Ops, Offset.getValue().getSExtValue());
5850+
DbgDIExpr = DIExpression::prependOpcodes(DbgDIExpr, Ops, true);
5851+
}
5852+
DbgValue->setOperand(2, MetadataAsValue::get(Ctx, DbgDIExpr));
5853+
}
5854+
}
5855+
}
5856+
}
58005857
return Changed;
58015858
}
58025859

llvm/test/DebugInfo/COFF/fpo-shrink-wrap.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
; ASM: popl %ebx
3434
; ASM: [[EPILOGUE]]: # %return
3535
; ASM: retl $8
36-
; ASM: Ltmp10:
36+
; ASM: Ltmp11:
3737
; ASM: .cv_fpo_endproc
3838

3939
; Note how RvaStart advances 7 bytes to skip the shrink-wrapped portion.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
; RUN: opt < %s -loop-reduce -S | FileCheck %s
2+
3+
; Test that LSR preserves debug-info for induction variables.
4+
5+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
6+
7+
define dso_local void @foo(i8* nocapture %p) local_unnamed_addr !dbg !7 {
8+
; CHECK-LABEL: @foo(
9+
entry:
10+
call void @llvm.dbg.value(metadata i8* %p, metadata !13, metadata !DIExpression()), !dbg !16
11+
call void @llvm.dbg.value(metadata i8 0, metadata !14, metadata !DIExpression()), !dbg !17
12+
br label %for.body, !dbg !18
13+
14+
for.cond.cleanup: ; preds = %for.body
15+
ret void, !dbg !19
16+
17+
for.body: ; preds = %entry, %for.body
18+
; CHECK-LABEL: for.body:
19+
%i.06 = phi i8 [ 0, %entry ], [ %inc, %for.body ]
20+
%p.addr.05 = phi i8* [ %p, %entry ], [ %add.ptr, %for.body ]
21+
call void @llvm.dbg.value(metadata i8 %i.06, metadata !14, metadata !DIExpression()), !dbg !17
22+
call void @llvm.dbg.value(metadata i8* %p.addr.05, metadata !13, metadata !DIExpression()), !dbg !16
23+
; CHECK-NOT: call void @llvm.dbg.value(metadata i8* undef
24+
; CHECK: call void @llvm.dbg.value(metadata i8* %lsr.iv, metadata ![[MID_p:[0-9]+]], metadata !DIExpression(DW_OP_constu, 3, DW_OP_minus, DW_OP_stack_value)), !dbg !16
25+
%add.ptr = getelementptr inbounds i8, i8* %p.addr.05, i64 3, !dbg !20
26+
call void @llvm.dbg.value(metadata i8* %add.ptr, metadata !13, metadata !DIExpression()), !dbg !16
27+
; CHECK-NOT: call void @llvm.dbg.value(metadata i8* undef
28+
; CHECK: call void @llvm.dbg.value(metadata i8* %lsr.iv, metadata ![[MID_p]], metadata !DIExpression()), !dbg !16
29+
store i8 %i.06, i8* %add.ptr, align 1, !dbg !23, !tbaa !24
30+
%inc = add nuw nsw i8 %i.06, 1, !dbg !27
31+
call void @llvm.dbg.value(metadata i8 %inc, metadata !14, metadata !DIExpression()), !dbg !17
32+
%exitcond.not = icmp eq i8 %inc, 32, !dbg !28
33+
br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !dbg !18, !llvm.loop !29
34+
}
35+
36+
declare void @llvm.dbg.value(metadata, metadata, metadata)
37+
38+
!llvm.dbg.cu = !{!0}
39+
!llvm.module.flags = !{!3, !4, !5}
40+
!llvm.ident = !{!6}
41+
42+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
43+
!1 = !DIFile(filename: "lsrdbg.c", directory: "/")
44+
!2 = !{}
45+
!3 = !{i32 7, !"Dwarf Version", i32 4}
46+
!4 = !{i32 2, !"Debug Info Version", i32 3}
47+
!5 = !{i32 1, !"wchar_size", i32 4}
48+
!6 = !{!"clang version 12.0.0"}
49+
!7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
50+
!8 = !DISubroutineType(types: !9)
51+
!9 = !{null, !10}
52+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
53+
!11 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char)
54+
!12 = !{!13, !14}
55+
!13 = !DILocalVariable(name: "p", arg: 1, scope: !7, file: !1, line: 2, type: !10)
56+
; CHECK: ![[MID_p]] = !DILocalVariable(name: "p", arg: 1, scope: !7, file: !1, line: 2, type: !10)
57+
!14 = !DILocalVariable(name: "i", scope: !15, file: !1, line: 4, type: !11)
58+
!15 = distinct !DILexicalBlock(scope: !7, file: !1, line: 4, column: 3)
59+
!16 = !DILocation(line: 0, scope: !7)
60+
!17 = !DILocation(line: 0, scope: !15)
61+
!18 = !DILocation(line: 4, column: 3, scope: !15)
62+
!19 = !DILocation(line: 8, column: 1, scope: !7)
63+
!20 = !DILocation(line: 5, column: 7, scope: !21)
64+
!21 = distinct !DILexicalBlock(scope: !22, file: !1, line: 4, column: 42)
65+
!22 = distinct !DILexicalBlock(scope: !15, file: !1, line: 4, column: 3)
66+
!23 = !DILocation(line: 6, column: 8, scope: !21)
67+
!24 = !{!25, !25, i64 0}
68+
!25 = !{!"omnipotent char", !26, i64 0}
69+
!26 = !{!"Simple C/C++ TBAA"}
70+
!27 = !DILocation(line: 4, column: 38, scope: !22)
71+
!28 = !DILocation(line: 4, column: 31, scope: !22)
72+
!29 = distinct !{!29, !18, !30, !31}
73+
!30 = !DILocation(line: 7, column: 3, scope: !15)
74+
!31 = !{!"llvm.loop.unroll.disable"}

0 commit comments

Comments
 (0)