Skip to content

Commit f5211d7

Browse files
authored
[DebugInfo][GVNSink] Fix #77415: GVNSink fails to optimize LLVM IR with debug info (#77602)
This PR fixes issue #77415 and is revised from PR #77419 . PR #77419 breaks the newly added test in the same PR on windows, because GVNSink is non-deterministic when sorting `BasicBlock*` pointers. This is reflected in the failure report. ``` # | C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll:28:10: error: CHECK: expected string not found in input # | ; CHECK: %a.sink = phi i32 [ %a, %if.then ], [ %b, %if.else ] # | ^ # | <stdin>:24:8: note: scanning from here # | if.end: ; preds = %if.else, %if.then # | ^ # | <stdin>:25:2: note: possible intended match here # | %b.sink = phi i32 [ %b, %if.else ], [ %a, %if.then ] # | ^ # | # | Input file: <stdin> # | Check file: C:\src\llvm-project\llvm\test\Transforms\GVNSink\sink-ignore-dbg-intrinsics.ll ``` According to the report, what the CheckFile wants to match is the `%a.sink`, however there is `%b.sink`. But this mismatch does not mean that this commit is wrong, since the occurrence of either `%a.sink` or `%b.sink` is correct. The root cause of this test failure is the strict check rule in the regression test committed. So I refined the regression test with a more general check rule to only detect whether there is an instruction with suffix `.sink` in the `if.end` block. Hope this won't fail the test. If this PR still fails to build, I will close this PR and try to find another right way to fix this.
1 parent b60e628 commit f5211d7

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

llvm/lib/Transforms/Scalar/GVNSink.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class LockstepReverseIterator {
132132
ActiveBlocks.remove(BB);
133133
continue;
134134
}
135-
Insts.push_back(BB->getTerminator()->getPrevNode());
135+
Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
136136
}
137137
if (Insts.empty())
138138
Fail = true;
@@ -168,7 +168,7 @@ class LockstepReverseIterator {
168168
if (Inst == &Inst->getParent()->front())
169169
ActiveBlocks.remove(Inst->getParent());
170170
else
171-
NewInsts.push_back(Inst->getPrevNode());
171+
NewInsts.push_back(Inst->getPrevNonDebugInstruction());
172172
}
173173
if (NewInsts.empty()) {
174174
Fail = true;
@@ -883,7 +883,7 @@ void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks,
883883
BasicBlock *BBEnd) {
884884
SmallVector<Instruction *, 4> Insts;
885885
for (BasicBlock *BB : Blocks)
886-
Insts.push_back(BB->getTerminator()->getPrevNode());
886+
Insts.push_back(BB->getTerminator()->getPrevNonDebugInstruction());
887887
Instruction *I0 = Insts.front();
888888

889889
SmallVector<Value *, 4> NewOperands;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s
2+
3+
; Test that GVNSink correctly performs the sink optimization in the presence of debug information
4+
5+
; Function Attrs: noinline nounwind uwtable
6+
define dso_local i32 @fun(i32 noundef %a, i32 noundef %b) #0 !dbg !10 {
7+
; CHECK-LABEL: define dso_local i32 @fun(
8+
; CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]])
9+
; CHECK: if.end:
10+
; CHECK: [[B_SINK:%.*]] = phi i32 [ [[B]], %if.else ], [ [[A]], %if.then ]
11+
; CHECK: [[ADD1:%.*]] = add nsw i32 [[B_SINK]], 1
12+
; CHECK: [[XOR2:%.*]] = xor i32 [[ADD1]], 1
13+
;
14+
entry:
15+
tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata !DIExpression()), !dbg !16
16+
tail call void @llvm.dbg.value(metadata i32 %b, metadata !17, metadata !DIExpression()), !dbg !16
17+
%cmp = icmp sgt i32 %b, 10, !dbg !18
18+
br i1 %cmp, label %if.then, label %if.else, !dbg !20
19+
20+
if.then: ; preds = %entry
21+
%add = add nsw i32 %a, 1, !dbg !21
22+
tail call void @llvm.dbg.value(metadata i32 %add, metadata !23, metadata !DIExpression()), !dbg !24
23+
%xor = xor i32 %add, 1, !dbg !25
24+
tail call void @llvm.dbg.value(metadata i32 %xor, metadata !26, metadata !DIExpression()), !dbg !24
25+
tail call void @llvm.dbg.value(metadata i32 %xor, metadata !27, metadata !DIExpression()), !dbg !16
26+
br label %if.end, !dbg !28
27+
28+
if.else: ; preds = %entry
29+
%add1 = add nsw i32 %b, 1, !dbg !29
30+
tail call void @llvm.dbg.value(metadata i32 %add1, metadata !31, metadata !DIExpression()), !dbg !32
31+
%xor2 = xor i32 %add1, 1, !dbg !33
32+
tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !34, metadata !DIExpression()), !dbg !32
33+
tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !27, metadata !DIExpression()), !dbg !16
34+
br label %if.end
35+
36+
if.end: ; preds = %if.else, %if.then
37+
%ret.0 = phi i32 [ %xor, %if.then ], [ %xor2, %if.else ], !dbg !35
38+
tail call void @llvm.dbg.value(metadata i32 %ret.0, metadata !27, metadata !DIExpression()), !dbg !16
39+
ret i32 %ret.0, !dbg !36
40+
}
41+
42+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
43+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
44+
45+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
46+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
47+
48+
attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
49+
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
50+
51+
!llvm.dbg.cu = !{!0}
52+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
53+
54+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 18.0.0git (https://github.com/llvm/llvm-project.git 5dfcb3e5d1d16bb4f8fce52b3c089119ed977e7f)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
55+
!1 = !DIFile(filename: "main.c", directory: "/")
56+
!2 = !{i32 7, !"Dwarf Version", i32 5}
57+
!3 = !{i32 2, !"Debug Info Version", i32 3}
58+
!4 = !{i32 1, !"wchar_size", i32 4}
59+
!5 = !{i32 8, !"PIC Level", i32 2}
60+
!6 = !{i32 7, !"PIE Level", i32 2}
61+
!7 = !{i32 7, !"uwtable", i32 2}
62+
!8 = !{i32 7, !"frame-pointer", i32 2}
63+
!10 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
64+
!11 = !DISubroutineType(types: !12)
65+
!12 = !{!13, !13, !13}
66+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
67+
!14 = !{}
68+
!15 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !1, line: 1, type: !13)
69+
!16 = !DILocation(line: 0, scope: !10)
70+
!17 = !DILocalVariable(name: "b", arg: 2, scope: !10, file: !1, line: 1, type: !13)
71+
!18 = !DILocation(line: 3, column: 11, scope: !19)
72+
!19 = distinct !DILexicalBlock(scope: !10, file: !1, line: 3, column: 9)
73+
!20 = !DILocation(line: 3, column: 9, scope: !10)
74+
!21 = !DILocation(line: 4, column: 20, scope: !22)
75+
!22 = distinct !DILexicalBlock(scope: !19, file: !1, line: 3, column: 17)
76+
!23 = !DILocalVariable(name: "a1", scope: !22, file: !1, line: 4, type: !13)
77+
!24 = !DILocation(line: 0, scope: !22)
78+
!25 = !DILocation(line: 5, column: 21, scope: !22)
79+
!26 = !DILocalVariable(name: "a2", scope: !22, file: !1, line: 5, type: !13)
80+
!27 = !DILocalVariable(name: "ret", scope: !10, file: !1, line: 2, type: !13)
81+
!28 = !DILocation(line: 7, column: 5, scope: !22)
82+
!29 = !DILocation(line: 8, column: 20, scope: !30)
83+
!30 = distinct !DILexicalBlock(scope: !19, file: !1, line: 7, column: 12)
84+
!31 = !DILocalVariable(name: "b1", scope: !30, file: !1, line: 8, type: !13)
85+
!32 = !DILocation(line: 0, scope: !30)
86+
!33 = !DILocation(line: 9, column: 21, scope: !30)
87+
!34 = !DILocalVariable(name: "b2", scope: !30, file: !1, line: 9, type: !13)
88+
!35 = !DILocation(line: 0, scope: !19)
89+
!36 = !DILocation(line: 12, column: 5, scope: !10)

0 commit comments

Comments
 (0)