Skip to content

Commit 40d6497

Browse files
authored
[DebugInfo] Transfer strcmp DILocation to generated inline code (#108531)
When AggressiveInstCombine inlines a strcmp call, we currently copy the strcmp's DILocation only to the br instruction that jumps to the inline code. While this is roughly analogous to the original call, it leaves the generated code without any source location, which is precarious for a memory operation. This patch copies the strcmp call's DILocation to all the generated code. An alternative solution would be to generate a new DILocation with a line 0 location and an inlinedAt pointing to the original call location, but this would still give limited attribution to the generated code without traversing the DIE, whereas the submitted solution allows attribution with just the line table; even though it would be technically more accurate, pragmatically I believe that copying the call's location will be more useful for users.
1 parent 04d8e36 commit 40d6497

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,13 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
10521052
bool Swapped) {
10531053
auto &Ctx = CI->getContext();
10541054
IRBuilder<> B(Ctx);
1055+
// We want these instructions to be recognized as inlined instructions for the
1056+
// compare call, but we don't have a source location for the definition of
1057+
// that function, since we're generating that code now. Because the generated
1058+
// code is a viable point for a memory access error, we make the pragmatic
1059+
// choice here to directly use CI's location so that we have useful
1060+
// attribution for the generated code.
1061+
B.SetCurrentDebugLocation(CI->getDebugLoc());
10551062

10561063
BasicBlock *BBCI = CI->getParent();
10571064
BasicBlock *BBTail =
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
;; Tests that when we replace a call to strcmp with compiler-generated inline
3+
;; code, we pass the strcmp call's dbg location to the inline code.
4+
; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s
5+
6+
@.str = constant [3 x i8] c"-h\00"
7+
8+
define i32 @main() {
9+
; CHECK-LABEL: define i32 @main() {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: br label %[[SUB_0:.*]], !dbg [[DBG4:![0-9]+]]
12+
; CHECK: [[SUB_0]]:
13+
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr null, align 1, !dbg [[DBG4]]
14+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i32, !dbg [[DBG4]]
15+
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 [[TMP1]], 45, !dbg [[DBG4]]
16+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0, !dbg [[DBG4]]
17+
; CHECK-NEXT: br i1 [[TMP3]], label %[[NE:.*]], label %[[SUB_1:.*]], !dbg [[DBG4]]
18+
; CHECK: [[SUB_1]]:
19+
; CHECK-NEXT: [[TMP4:%.*]] = load i8, ptr getelementptr inbounds (i8, ptr null, i64 1), align 1, !dbg [[DBG4]]
20+
; CHECK-NEXT: [[TMP5:%.*]] = zext i8 [[TMP4]] to i32, !dbg [[DBG4]]
21+
; CHECK-NEXT: [[TMP6:%.*]] = sub i32 [[TMP5]], 104, !dbg [[DBG4]]
22+
; CHECK-NEXT: [[TMP7:%.*]] = icmp ne i32 [[TMP6]], 0, !dbg [[DBG4]]
23+
; CHECK-NEXT: br i1 [[TMP7]], label %[[NE]], label %[[SUB_2:.*]], !dbg [[DBG4]]
24+
; CHECK: [[SUB_2]]:
25+
; CHECK-NEXT: br label %[[NE]], !dbg [[DBG4]]
26+
; CHECK: [[NE]]:
27+
; CHECK-NEXT: br label %[[ENTRY_TAIL:.*]], !dbg [[DBG4]]
28+
; CHECK: [[ENTRY_TAIL]]:
29+
; CHECK-NEXT: ret i32 0
30+
;
31+
entry:
32+
%call.i = call i32 @strcmp(ptr null, ptr @.str), !dbg !4
33+
%cmp.i.not = icmp eq i32 %call.i, 0
34+
ret i32 0
35+
}
36+
37+
declare i32 @strcmp(ptr, ptr)
38+
39+
!llvm.dbg.cu = !{!0}
40+
!llvm.module.flags = !{!3}
41+
42+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git", isOptimized: true, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2)
43+
!1 = !DIFile(filename: "test.c", directory: "/tmp")
44+
!2 = !{}
45+
!3 = !{i32 2, !"Debug Info Version", i32 3}
46+
!4 = !DILocation(line: 258, column: 10, scope: !5)
47+
!5 = distinct !DISubprogram(name: "streq", scope: !1, file: !1, line: 257, type: !7, scopeLine: 257, unit: !0, retainedNodes: !2)
48+
!7 = !DISubroutineType(types: !2)
49+
;.
50+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]], retainedTypes: [[META2]], globals: [[META2]])
51+
; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}})
52+
; CHECK: [[META2]] = !{}
53+
; CHECK: [[DBG4]] = !DILocation(line: 258, column: 10, scope: [[META5:![0-9]+]])
54+
; CHECK: [[META5]] = distinct !DISubprogram(name: "streq", scope: [[META1]], file: [[META1]], line: 257, type: [[META6:![0-9]+]], scopeLine: 257, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META2]])
55+
; CHECK: [[META6]] = !DISubroutineType(types: [[META2]])
56+
;.

0 commit comments

Comments
 (0)