Skip to content

Commit caa265e

Browse files
authored
[DebugInfo][InstCombine] Do not overwrite prior DILocation for new Insts (#108565)
When InstCombine replaces an old instruction with a new instruction, it copies !dbg and !annotation metadata from old to new. For some InstCombine patterns we set a specific DILocation on the new instruction prior to insertion, however, which more accurately reflects the new instruction. This more specific DILocation may be overwritten on insertion by a less appropriate one, resulting in a less correct line mapping. This patch changes this behaviour to only copy the DILocation from old to new if the new instruction has no existing DILocation (which will always be the case for a new instruction unless InstCombine has specifically set one).
1 parent def9550 commit caa265e

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5170,8 +5170,13 @@ bool InstCombinerImpl::run() {
51705170
LLVM_DEBUG(dbgs() << "IC: Old = " << *I << '\n'
51715171
<< " New = " << *Result << '\n');
51725172

5173-
Result->copyMetadata(*I,
5174-
{LLVMContext::MD_dbg, LLVMContext::MD_annotation});
5173+
// We copy the old instruction's DebugLoc to the new instruction, unless
5174+
// InstCombine already assigned a DebugLoc to it, in which case we
5175+
// should trust the more specifically selected DebugLoc.
5176+
if (!Result->getDebugLoc())
5177+
Result->setDebugLoc(I->getDebugLoc());
5178+
// We also copy annotation metadata to the new instruction.
5179+
Result->copyMetadata(*I, LLVMContext::MD_annotation);
51755180
// Everything uses the new instruction now.
51765181
I->replaceAllUsesWith(Result);
51775182

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals smart
2+
;; Tests that when InstCombine sets a DILocation on the new instruction when it
3+
;; is created, we do not try to overwrite that DILocation when we later insert
4+
;; the new instruction.
5+
;; In this test, InstCombine replaces two loads joined by a PHI into a PHI of
6+
;; the addresses followed by a load, and gives the new load a merge of the two
7+
;; incoming load DILocations. This test verifies that the new load keeps that
8+
;; DILocation, and doesn't have it overwritten by the DILocation of the original
9+
;; PHI. It will, however, receive the !annotation attached to the original PHI.
10+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
11+
12+
define void @test(ptr %xfA, ptr %xfB, i1 %cmp5) {
13+
; CHECK-LABEL: @test(
14+
; CHECK-NEXT: entry:
15+
; CHECK-NEXT: br i1 [[CMP5:%.*]], label [[IF_ELSE:%.*]], label [[IF_THEN6:%.*]]
16+
; CHECK: if.then6:
17+
; CHECK-NEXT: br label [[IF_END11:%.*]]
18+
; CHECK: if.else:
19+
; CHECK-NEXT: br label [[IF_END11]]
20+
; CHECK: if.end11:
21+
; CHECK-NEXT: [[XFA_PN:%.*]] = phi ptr [ [[XFA:%.*]], [[IF_ELSE]] ], [ [[XFB:%.*]], [[IF_THEN6]] ]
22+
; CHECK-NEXT: [[XF1_SROA_8_0_IN:%.*]] = getelementptr i8, ptr [[XFA_PN]], i64 4
23+
; CHECK-NEXT: [[XF1_SROA_8_0:%.*]] = load float, ptr [[XF1_SROA_8_0_IN]], align 4, !dbg [[DBG3:![0-9]+]], !annotation [[META7:![0-9]+]]
24+
; CHECK-NEXT: [[CMP_I:%.*]] = fcmp ugt float [[XF1_SROA_8_0]], 0.000000e+00
25+
; CHECK-NEXT: br i1 [[CMP_I]], label [[IF_END_I:%.*]], label [[IF_THEN_I:%.*]]
26+
; CHECK: if.then.i:
27+
; CHECK-NEXT: br label [[IF_END_I]]
28+
; CHECK: if.end.i:
29+
; CHECK-NEXT: ret void
30+
;
31+
entry:
32+
br i1 %cmp5, label %if.else, label %if.then6
33+
34+
if.then6: ; preds = %entry
35+
%xf1.sroa.8.0.xfB.sroa_idx = getelementptr i8, ptr %xfB, i64 4
36+
%xf1.sroa.8.0.copyload = load float, ptr %xf1.sroa.8.0.xfB.sroa_idx, align 4, !dbg !3
37+
br label %if.end11
38+
39+
if.else: ; preds = %entry
40+
%xf1.sroa.8.0.xfA.sroa_idx = getelementptr i8, ptr %xfA, i64 4
41+
%xf1.sroa.8.0.copyload494 = load float, ptr %xf1.sroa.8.0.xfA.sroa_idx, align 4, !dbg !7
42+
br label %if.end11
43+
44+
if.end11: ; preds = %if.else, %if.then6
45+
%xf1.sroa.8.0 = phi float [ %xf1.sroa.8.0.copyload494, %if.else ], [ %xf1.sroa.8.0.copyload, %if.then6 ], !dbg !8, !annotation !9
46+
%cmp.i = fcmp ugt float %xf1.sroa.8.0, 0.000000e+00
47+
br i1 %cmp.i, label %if.end.i, label %if.then.i
48+
49+
if.then.i: ; preds = %if.end11
50+
br label %if.end.i
51+
52+
if.end.i: ; preds = %if.then.i, %if.end11
53+
ret void
54+
}
55+
56+
!llvm.dbg.cu = !{!0}
57+
!llvm.module.flags = !{!2}
58+
59+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
60+
!1 = !DIFile(filename: "test.cpp", directory: "/tmp")
61+
!2 = !{i32 2, !"Debug Info Version", i32 3}
62+
!3 = !DILocation(line: 13, column: 12, scope: !4)
63+
!4 = distinct !DISubprogram(name: "operator=", linkageName: "_ZN11btMatrix3x3aSERKS_", scope: null, file: !1, line: 61, type: !5, scopeLine: 62, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !6)
64+
!5 = distinct !DISubroutineType(types: !6)
65+
!6 = !{}
66+
!7 = !DILocation(line: 13, column: 15, scope: !4)
67+
!8 = !DILocation(line: 100, scope: !4)
68+
!9 = !{!"Test String"}
69+
;.
70+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
71+
; CHECK: [[META1]] = !DIFile(filename: "test.cpp", directory: {{.*}})
72+
; CHECK: [[DBG3]] = !DILocation(line: 13, scope: [[META4:![0-9]+]])
73+
; CHECK: [[META4]] = distinct !DISubprogram(name: "operator=", linkageName: "_ZN11btMatrix3x3aSERKS_", scope: null, file: [[META1]], line: 61, type: [[META5:![0-9]+]], scopeLine: 62, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META6:![0-9]+]])
74+
; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6]])
75+
; CHECK: [[META6]] = !{}
76+
; CHECK: [[META7]] = !{!"Test String"}
77+
;.

0 commit comments

Comments
 (0)