Skip to content

Commit 7964356

Browse files
authored
[LoopUnroll] Remove redundant debug instructions after blocks have been merged (#91246)
Remove redundant debug instructions after blocks have been merged into the predecessor, It can reduce some compile time in some cases. This change only fixes the situation of loop unrolling, and other situations are not considered. "RemoveRedundantDbgInstrs" seems to be very time-consuming. Thus, we just add here after the "Dest" has been merged into the "Fold", this may be a more targeted solution!!! fixes: #89073
1 parent 1066eb5 commit 7964356

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
377377
const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
378378
SmallVector<WeakTrackingVH, 16> DeadInsts;
379379
for (BasicBlock *BB : L->getBlocks()) {
380+
// Remove repeated debug instructions after loop unrolling.
381+
if (BB->getParent()->getSubprogram())
382+
RemoveRedundantDbgInstrs(BB);
383+
380384
for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
381385
if (Value *V = simplifyInstruction(&Inst, {DL, nullptr, DT, AC}))
382386
if (LI->replacementPreservesLCSSAForm(&Inst, V))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -S -passes=loop-unroll | FileCheck %s
3+
4+
define i64 @d(i1 %tobool.not, i32 %add, i64 %conv23) !dbg !14{
5+
; There should be only one "llvm.dbg.vale" after loop unrolling
6+
; CHECK-LABEL: @d(
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
9+
; CHECK: for.body:
10+
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 0, metadata [[META16:![0-9]+]], metadata !DIExpression()), !dbg [[DBG17:![0-9]+]]
11+
; CHECK-NEXT: ret i64 5
12+
;
13+
entry:
14+
br label %for.body
15+
16+
for.body: ; preds = %for.body, %entry
17+
%k.045 = phi i64 [ 0, %entry ], [ %k.046, %for.body ]
18+
tail call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !17
19+
%k.046 = add nuw nsw i64 %k.045, 1
20+
%exitcond = icmp ne i64 %k.046, 5
21+
br i1 %exitcond, label %for.body, label %for.end22
22+
23+
for.end22: ; preds = %for.body
24+
ret i64 %k.046
25+
}
26+
27+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
28+
declare void @llvm.dbg.value(metadata, metadata, metadata)
29+
30+
!llvm.dbg.cu = !{!0}
31+
!llvm.module.flags = !{!12}
32+
33+
!0 = distinct !DICompileUnit(language: DW_LANG_C89, file: !1, producer: "clang version 19.0.0git (https://github.com/llvm/llvm-project.git ec062f5b33ed22c61742e3c1486f6cba915801e0)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !2, splitDebugInlining: false, nameTableKind: None)
34+
!1 = !DIFile(filename: "unroll-remove-redundant-dbg.c", directory: "", checksumkind: CSK_MD5, checksum: "aa30a1d8c04deb9b0f3885c258d2b674")
35+
!2 = !{!3, !8, !10}
36+
!3 = !DIGlobalVariableExpression(var: !4, expr: !DIExpression())
37+
!4 = distinct !DIGlobalVariable(name: "a", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
38+
!5 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint32_t", file: !6, line: 198, baseType: !7)
39+
!6 = !DIFile(filename: "/usr/include/stdint.h", directory: "", checksumkind: CSK_MD5, checksum: "da031bcff2d0c1d65aa92e7e68a44ef3")
40+
!7 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
41+
!8 = !DIGlobalVariableExpression(var: !9, expr: !DIExpression())
42+
!9 = distinct !DIGlobalVariable(name: "c", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
43+
!10 = !DIGlobalVariableExpression(var: !11, expr: !DIExpression())
44+
!11 = distinct !DIGlobalVariable(name: "b", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
45+
!12 = !{i32 2, !"Debug Info Version", i32 3}
46+
!13 = !DILocalVariable(name: "f", scope: !14, file: !1, line: 4, type: !5)
47+
!14 = distinct !DISubprogram(name: "d", scope: !1, file: !1, line: 3, type: !15, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
48+
!15 = !DISubroutineType(types: !16)
49+
!16 = !{}
50+
!17 = !DILocation(line: 0, scope: !14)

0 commit comments

Comments
 (0)