Skip to content

Commit 93c194f

Browse files
committed
[Assignment Tracking] Ignore zero-sized fragments
Such dbg.assigns will occur if you write zero-sized memcpys (see https://reviews.llvm.org/D146987#4240016). Handle this in AssignmentTrackingAnalysis (back end) rather than AssignmentTrackingPass (declare-to-assign) in case it is possible to reproduce this as a result of optimisations. Reviewed By: jmorse Differential Revision: https://reviews.llvm.org/D147435
1 parent 4639653 commit 93c194f

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ class AssignmentTrackingLowering {
12721272
/// location information).
12731273
///@{
12741274
void processNonDbgInstruction(Instruction &I, BlockInfo *LiveSet);
1275-
void processDbgInstruction(Instruction &I, BlockInfo *LiveSet);
1275+
void processDbgInstruction(DbgInfoIntrinsic &I, BlockInfo *LiveSet);
12761276
/// Update \p LiveSet after encountering an instruction with a DIAssignID
12771277
/// attachment, \p I.
12781278
void processTaggedInstruction(Instruction &I, BlockInfo *LiveSet);
@@ -1702,8 +1702,22 @@ void AssignmentTrackingLowering::processDbgValue(DbgValueInst &DVI,
17021702
emitDbgValue(LocKind::Val, &DVI, &DVI);
17031703
}
17041704

1705+
static bool hasZeroSizedFragment(DbgVariableIntrinsic &DVI) {
1706+
if (auto F = DVI.getExpression()->getFragmentInfo())
1707+
return F->SizeInBits == 0;
1708+
return false;
1709+
}
1710+
17051711
void AssignmentTrackingLowering::processDbgInstruction(
1706-
Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1712+
DbgInfoIntrinsic &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1713+
auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
1714+
if (!DVI)
1715+
return;
1716+
1717+
// Ignore assignments to zero bits of the variable.
1718+
if (hasZeroSizedFragment(*DVI))
1719+
return;
1720+
17071721
if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
17081722
processDbgAssign(*DAI, LiveSet);
17091723
else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
@@ -1733,10 +1747,11 @@ void AssignmentTrackingLowering::process(BasicBlock &BB, BlockInfo *LiveSet) {
17331747
++II;
17341748
}
17351749
while (II != EI) {
1736-
if (!isa<DbgInfoIntrinsic>(&*II))
1750+
auto *Dbg = dyn_cast<DbgInfoIntrinsic>(&*II);
1751+
if (!Dbg)
17371752
break;
17381753
resetInsertionPoint(*II);
1739-
processDbgInstruction(*II, LiveSet);
1754+
processDbgInstruction(*Dbg, LiveSet);
17401755
assert(LiveSet->isValid());
17411756
++II;
17421757
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
; RUN: llc %s -stop-after=finalize-isel -o - | FileCheck %s
2+
3+
;; Check that a zero-sized fragment (the final dbg.assign) is ignored by
4+
;; AssignmentTrackingAnalysis.
5+
6+
; CHECK: stack:
7+
; CHECK-NEXT: - { id: 0, name: m4, type: default, offset: 0, size: 32, alignment: 8,
8+
; CHECK-NEXT: stack-id: default, callee-saved-register: '', callee-saved-restored: true,
9+
; CHECK-NEXT: debug-info-variable: '![[#]]', debug-info-expression: '!DIExpression()',
10+
; CHECK-NEXT: debug-info-location: '![[#]]' }
11+
12+
target triple = "x86_64-unknown-unknown"
13+
14+
%struct.mm = type { [8 x i32] }
15+
16+
@m1 = local_unnamed_addr global %struct.mm zeroinitializer, align 4, !dbg !0
17+
18+
define dso_local i32 @main() local_unnamed_addr #0 !dbg !23 {
19+
entry:
20+
%m4 = alloca %struct.mm, align 8, !DIAssignID !28
21+
call void @llvm.dbg.assign(metadata i1 undef, metadata !27, metadata !DIExpression(), metadata !28, metadata ptr %m4, metadata !DIExpression()), !dbg !29
22+
call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %m4, ptr noundef nonnull align 4 dereferenceable(32) @m1, i64 32, i1 false), !dbg !31, !DIAssignID !36
23+
call void @llvm.dbg.assign(metadata i1 undef, metadata !27, metadata !DIExpression(), metadata !36, metadata ptr %m4, metadata !DIExpression()), !dbg !29
24+
call void @llvm.dbg.assign(metadata i1 undef, metadata !27, metadata !DIExpression(DW_OP_LLVM_fragment, 64, 0), metadata !43, metadata ptr %m4, metadata !DIExpression(DW_OP_plus_uconst, 8)), !dbg !29
25+
ret i32 0, !dbg !45
26+
}
27+
28+
29+
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2
30+
declare void @foo(i32 noundef) local_unnamed_addr #3
31+
declare i32 @bar(ptr noundef byval(%struct.mm) align 8, ptr noundef byval(%struct.mm) align 8) local_unnamed_addr #3
32+
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) #4
33+
34+
!llvm.dbg.cu = !{!2}
35+
!llvm.module.flags = !{!19, !20, !21}
36+
!llvm.ident = !{!22}
37+
38+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
39+
!1 = distinct !DIGlobalVariable(name: "m1", scope: !2, file: !9, line: 1, type: !10, isLocal: false, isDefinition: true)
40+
!2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 17.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !4, globals: !6, splitDebugInlining: false, nameTableKind: None)
41+
!3 = !DIFile(filename: "<stdin>", directory: "/")
42+
!4 = !{!5}
43+
!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
44+
!6 = !{!0}
45+
!9 = !DIFile(filename: "repro.c", directory: "/")
46+
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "mm", file: !9, line: 1, size: 256, elements: !11)
47+
!11 = !{!12}
48+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !10, file: !9, line: 1, baseType: !13, size: 256)
49+
!13 = !DICompositeType(tag: DW_TAG_array_type, baseType: !14, size: 256, elements: !15)
50+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
51+
!15 = !{!16}
52+
!16 = !DISubrange(count: 8)
53+
!19 = !{i32 2, !"Debug Info Version", i32 3}
54+
!20 = !{i32 1, !"wchar_size", i32 4}
55+
!21 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
56+
!22 = !{!"clang version 17.0.0"}
57+
!23 = distinct !DISubprogram(name: "main", scope: !9, file: !9, line: 7, type: !24, scopeLine: 8, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !26)
58+
!24 = !DISubroutineType(types: !25)
59+
!25 = !{!14}
60+
!26 = !{!27}
61+
!27 = !DILocalVariable(name: "m4", scope: !23, file: !9, line: 9, type: !10)
62+
!28 = distinct !DIAssignID()
63+
!29 = !DILocation(line: 0, scope: !23)
64+
!31 = !DILocation(line: 11, column: 8, scope: !23)
65+
!36 = distinct !DIAssignID()
66+
!43 = distinct !DIAssignID()
67+
!45 = !DILocation(line: 19, column: 3, scope: !23)

0 commit comments

Comments
 (0)