Skip to content

Commit faa2f96

Browse files
authored
[DebugInfo] Handle dbg.assigns in FastISel (#80734)
There are some rare circumstances where dbg.assign intrinsics can reach FastISel. They are a more specialised kind of dbg.value intrinsic with more information about the originating alloca. They only occur during optimisation, but might reach FastISel through always_inlining an optimised function into an optnone function. This is a slight problem as it's not safe (for debug-info accuracy) to ignore any intrinsics, and for RemoveDIs (the intrinsic-replacement project) it causes a crash through an unhandled switch case. To get around this, we can just treat the dbg.assign as a dbg.value (it's an actual subclass) and use the variable location information from the dbg.value fields. This loses a small amount of debug-info about stack locations, but is more accurate than just ignoring the intrinsic. (This has popped up deep in an LTO build of a large codebase while testing RemoveDIs, I figured it'd be good to fix it for the intrinsic-form at the same time, just to demonstrate the correct behaviour).
1 parent 1a42b38 commit faa2f96

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,13 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
21202120
ListSize, Alignment));
21212121
return true;
21222122
}
2123+
case Intrinsic::dbg_assign:
2124+
// A dbg.assign is a dbg.value with more information about stack locations,
2125+
// typically produced during optimisation of variables with leaked
2126+
// addresses. We can treat it like a normal dbg_value intrinsic here; to
2127+
// benefit from the full analysis of stack/SSA locations, GlobalISel would
2128+
// need to register for and use the AssignmentTrackingAnalysis pass.
2129+
LLVM_FALLTHROUGH;
21232130
case Intrinsic::dbg_value: {
21242131
// This form of DBG_VALUE is target-independent.
21252132
const DbgValueInst &DI = cast<DbgValueInst>(CI);

llvm/lib/CodeGen/SelectionDAG/FastISel.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,8 @@ void FastISel::handleDbgInfo(const Instruction *II) {
11971197
V = DPV.getVariableLocationOp(0);
11981198

11991199
bool Res = false;
1200-
if (DPV.getType() == DPValue::LocationType::Value) {
1200+
if (DPV.getType() == DPValue::LocationType::Value ||
1201+
DPV.getType() == DPValue::LocationType::Assign) {
12011202
Res = lowerDbgValue(V, DPV.getExpression(), DPV.getVariable(),
12021203
DPV.getDebugLoc());
12031204
} else {
@@ -1393,6 +1394,13 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
13931394

13941395
return true;
13951396
}
1397+
case Intrinsic::dbg_assign:
1398+
// A dbg.assign is a dbg.value with more information, typically produced
1399+
// during optimisation. If one reaches fastisel then something odd has
1400+
// happened (such as an optimised function being always-inlined into an
1401+
// optnone function). We will not be using the extra information in the
1402+
// dbg.assign in that case, just use its dbg.value fields.
1403+
LLVM_FALLTHROUGH;
13961404
case Intrinsic::dbg_value: {
13971405
// This form of DBG_VALUE is target-independent.
13981406
const DbgValueInst *DI = cast<DbgValueInst>(II);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: llc %s -fast-isel -start-after=codegenprepare -stop-before=finalize-isel -o - | FileCheck %s
2+
; RUN: llc %s -fast-isel -start-after=codegenprepare -stop-before=finalize-isel -o - --try-experimental-debuginfo-iterators | FileCheck %s
3+
; RUN: llc %s -global-isel -start-after=codegenprepare -stop-before=finalize-isel -o - | FileCheck %s
4+
; RUN: llc %s -global-isel -start-after=codegenprepare -stop-before=finalize-isel -o - --try-experimental-debuginfo-iterators | FileCheck %s
5+
6+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
7+
target triple = "x86_64-unknown-unknown"
8+
9+
; CHECK: DBG_VALUE
10+
11+
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
12+
13+
define dso_local i32 @foo(i32 %a, i32 %b) local_unnamed_addr !dbg !8 {
14+
entry:
15+
call void @llvm.dbg.assign(metadata !DIArgList(i32 %a, i32 %b), metadata !16, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus), metadata !21, metadata ptr undef, metadata !DIExpression()), !dbg !17
16+
%mul = mul nsw i32 %b, %a, !dbg !18
17+
ret i32 %mul, !dbg !18
18+
}
19+
20+
21+
!llvm.dbg.cu = !{!0}
22+
!llvm.module.flags = !{!3, !4, !5, !19, !6}
23+
!llvm.ident = !{!7}
24+
25+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
26+
!1 = !DIFile(filename: "debug_value_list_selectiondag.cpp", directory: "/")
27+
!2 = !{}
28+
!3 = !{i32 2, !"CodeView", i32 1}
29+
!4 = !{i32 2, !"Debug Info Version", i32 3}
30+
!5 = !{i32 1, !"wchar_size", i32 2}
31+
!6 = !{i32 7, !"PIC Level", i32 2}
32+
!7 = !{!"clang version 11.0.0"}
33+
!8 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !9, file: !9, line: 1, type: !10, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13)
34+
!9 = !DIFile(filename: ".\\debug_value_list.cpp", directory: "/tmp")
35+
!10 = !DISubroutineType(types: !11)
36+
!11 = !{!12, !12, !12}
37+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
38+
!13 = !{!14, !15, !16}
39+
!14 = !DILocalVariable(name: "b", arg: 2, scope: !8, file: !9, line: 1, type: !12)
40+
!15 = !DILocalVariable(name: "a", arg: 1, scope: !8, file: !9, line: 1, type: !12)
41+
!16 = !DILocalVariable(name: "c", scope: !8, file: !9, line: 2, type: !12)
42+
!17 = !DILocation(line: 0, scope: !8)
43+
!18 = !DILocation(line: 3, scope: !8)
44+
!19 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
45+
!20 = !DILocalVariable(name: "d", scope: !8, file: !9, line: 2, type: !12)
46+
!21 = distinct !DIAssignID()

0 commit comments

Comments
 (0)