Skip to content

Commit 2e3e086

Browse files
authored
[BasicBlockUtils] Remove redundant llvm.dbg instructions after blocks to reduce compile time (llvm#89069)
this patch is to fix the compile time for some cases, before this change, some targets (riscv-64, ve) will spend much more compile time on this case (https://godbolt.org/z/rrov17cTo). With this change, the compile time was reduced a lot. Fixes llvm#89073 PR: llvm#89069
1 parent 213ab96 commit 2e3e086

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
333333
// Finally, erase the old block and update dominator info.
334334
DeleteDeadBlock(BB, DTU);
335335

336+
// Remove redundant "llvm.dbg" instrunctions after blocks have been merged.
337+
if (PredBB->getParent()->getSubprogram())
338+
RemoveRedundantDbgInstrs(PredBB);
339+
336340
return true;
337341
}
338342

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: opt < %s -S -passes=loop-unroll | FileCheck %s
2+
3+
define i64 @d(i1 %tobool.not, i32 %add, i64 %conv23) !dbg !14{
4+
entry:
5+
br label %for.body
6+
7+
for.body: ; preds = %for.body, %entry
8+
; There should be only one "llvm.dbg.vale" after loop unrolling
9+
; CHECK: call void @llvm.dbg.value
10+
; CHECK-NOT: call void @llvm.dbg.value
11+
12+
%k.045 = phi i64 [ 0, %entry ], [ %k.046, %for.body ]
13+
tail call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !17
14+
%k.046 = add nuw nsw i64 %k.045, 1
15+
%exitcond = icmp ne i64 %k.046, 5
16+
br i1 %exitcond, label %for.body, label %for.end22
17+
18+
for.end22: ; preds = %for.body
19+
ret i64 %k.046
20+
}
21+
22+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
23+
declare void @llvm.dbg.value(metadata, metadata, metadata)
24+
25+
!llvm.dbg.cu = !{!0}
26+
!llvm.module.flags = !{!12}
27+
28+
!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)
29+
!1 = !DIFile(filename: "unroll-remove-redundant-dbg.c", directory: "", checksumkind: CSK_MD5, checksum: "aa30a1d8c04deb9b0f3885c258d2b674")
30+
!2 = !{!3, !8, !10}
31+
!3 = !DIGlobalVariableExpression(var: !4, expr: !DIExpression())
32+
!4 = distinct !DIGlobalVariable(name: "a", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
33+
!5 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint32_t", file: !6, line: 198, baseType: !7)
34+
!6 = !DIFile(filename: "/usr/include/stdint.h", directory: "", checksumkind: CSK_MD5, checksum: "da031bcff2d0c1d65aa92e7e68a44ef3")
35+
!7 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
36+
!8 = !DIGlobalVariableExpression(var: !9, expr: !DIExpression())
37+
!9 = distinct !DIGlobalVariable(name: "c", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
38+
!10 = !DIGlobalVariableExpression(var: !11, expr: !DIExpression())
39+
!11 = distinct !DIGlobalVariable(name: "b", scope: !0, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true)
40+
!12 = !{i32 2, !"Debug Info Version", i32 3}
41+
!13 = !DILocalVariable(name: "f", scope: !14, file: !1, line: 4, type: !5)
42+
!14 = distinct !DISubprogram(name: "d", scope: !1, file: !1, line: 3, type: !15, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
43+
!15 = !DISubroutineType(types: !16)
44+
!16 = !{}
45+
!17 = !DILocation(line: 0, scope: !14)

llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue-inlined.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ init:
99

1010
; CHECK: %vala = load i64, ptr %ptr
1111
; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 %vala, metadata [[MD:![0-9]*]]
12-
; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 %vala, metadata [[MD]]
1312
; CHECK-NEXT: %valbmasked = and i64 %vala, 1
1413

1514
a: ; preds = %init

llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ define i1 @hoist_with_debug2(i32 %x) !dbg !22 {
4747
; CHECK-NEXT: entry:
4848
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp ugt i32 [[X:%.*]], 2
4949
; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 [[X]], metadata [[META21:![0-9]+]], metadata !DIExpression()), !dbg [[DBG23:![0-9]+]]
50-
; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 [[X]], metadata [[META21]], metadata !DIExpression()), !dbg [[DBG23]]
5150
; CHECK-NEXT: [[DOT:%.*]] = select i1 [[TOBOOL_NOT]], i1 false, i1 true
5251
; CHECK-NEXT: ret i1 [[DOT]]
5352
;

0 commit comments

Comments
 (0)