Skip to content

Commit 86464ed

Browse files
committed
[Assignment Tracking][15/*] Account for assignment tracking in simplifycfg
The Assignment Tracking debug-info feature is outlined in this RFC: https://discourse.llvm.org/t/ rfc-assignment-tracking-a-better-way-of-specifying-variable-locations-in-ir Update simplifycfg: sinkLastInstruction - preserve debug use-before-defs. SpeculativelyExecuteBB - replace the value component of dbg.assign intrinsics when stores are hoisted and merged using a select, and don't delete them. Reviewed By: jmorse Differential Revision: https://reviews.llvm.org/D133310
1 parent d473dac commit 86464ed

File tree

3 files changed

+269
-7
lines changed

3 files changed

+269
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/IR/ConstantRange.h"
4242
#include "llvm/IR/Constants.h"
4343
#include "llvm/IR/DataLayout.h"
44+
#include "llvm/IR/DebugInfo.h"
4445
#include "llvm/IR/DerivedTypes.h"
4546
#include "llvm/IR/Function.h"
4647
#include "llvm/IR/GlobalValue.h"
@@ -1999,9 +2000,15 @@ static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
19992000
}
20002001

20012002
// Finally nuke all instructions apart from the common instruction.
2002-
for (auto *I : Insts)
2003-
if (I != I0)
2004-
I->eraseFromParent();
2003+
for (auto *I : Insts) {
2004+
if (I == I0)
2005+
continue;
2006+
// The remaining uses are debug users, replace those with the common inst.
2007+
// In most (all?) cases this just introduces a use-before-def.
2008+
assert(I->user_empty() && "Inst unexpectedly still has non-dbg users");
2009+
I->replaceAllUsesWith(I0);
2010+
I->eraseFromParent();
2011+
}
20052012

20062013
return true;
20072014
}
@@ -2969,6 +2976,7 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
29692976
// Insert a select of the value of the speculated store.
29702977
if (SpeculatedStoreValue) {
29712978
IRBuilder<NoFolder> Builder(BI);
2979+
Value *OrigV = SpeculatedStore->getValueOperand();
29722980
Value *TrueV = SpeculatedStore->getValueOperand();
29732981
Value *FalseV = SpeculatedStoreValue;
29742982
if (Invert)
@@ -2978,6 +2986,35 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
29782986
SpeculatedStore->setOperand(0, S);
29792987
SpeculatedStore->applyMergedLocation(BI->getDebugLoc(),
29802988
SpeculatedStore->getDebugLoc());
2989+
// The value stored is still conditional, but the store itself is now
2990+
// unconditonally executed, so we must be sure that any linked dbg.assign
2991+
// intrinsics are tracking the new stored value (the result of the
2992+
// select). If we don't, and the store were to be removed by another pass
2993+
// (e.g. DSE), then we'd eventually end up emitting a location describing
2994+
// the conditional value, unconditionally.
2995+
//
2996+
// === Before this transformation ===
2997+
// pred:
2998+
// store %one, %x.dest, !DIAssignID !1
2999+
// dbg.assign %one, "x", ..., !1, ...
3000+
// br %cond if.then
3001+
//
3002+
// if.then:
3003+
// store %two, %x.dest, !DIAssignID !2
3004+
// dbg.assign %two, "x", ..., !2, ...
3005+
//
3006+
// === After this transformation ===
3007+
// pred:
3008+
// store %one, %x.dest, !DIAssignID !1
3009+
// dbg.assign %one, "x", ..., !1
3010+
/// ...
3011+
// %merge = select %cond, %two, %one
3012+
// store %merge, %x.dest, !DIAssignID !2
3013+
// dbg.assign %merge, "x", ..., !2
3014+
for (auto *DAI : at::getAssignmentMarkers(SpeculatedStore)) {
3015+
if (any_of(DAI->location_ops(), [&](Value *V) { return V == OrigV; }))
3016+
DAI->replaceVariableLocationOp(OrigV, S);
3017+
}
29813018
}
29823019

29833020
// Metadata can be dependent on the condition we are hoisting above.
@@ -2987,8 +3024,11 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
29873024
// Similarly strip attributes that maybe dependent on condition we are
29883025
// hoisting above.
29893026
for (auto &I : make_early_inc_range(*ThenBB)) {
2990-
if (!SpeculatedStoreValue || &I != SpeculatedStore)
2991-
I.setDebugLoc(DebugLoc());
3027+
if (!SpeculatedStoreValue || &I != SpeculatedStore) {
3028+
// Don't update the DILocation of dbg.assign intrinsics.
3029+
if (!isa<DbgAssignIntrinsic>(&I))
3030+
I.setDebugLoc(DebugLoc());
3031+
}
29923032
I.dropUndefImplyingAttrsAndUnknownMetadata();
29933033

29943034
// Drop ephemeral values.
@@ -3028,8 +3068,12 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
30283068
// Remove speculated dbg intrinsics.
30293069
// FIXME: Is it possible to do this in a more elegant way? Moving/merging the
30303070
// dbg value for the different flows and inserting it after the select.
3031-
for (Instruction *I : SpeculatedDbgIntrinsics)
3032-
I->eraseFromParent();
3071+
for (Instruction *I : SpeculatedDbgIntrinsics) {
3072+
// We still want to know that an assignment took place so don't remove
3073+
// dbg.assign intrinsics.
3074+
if (!isa<DbgAssignIntrinsic>(I))
3075+
I->eraseFromParent();
3076+
}
30333077

30343078
++NumSpeculations;
30353079
return true;
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
; RUN: opt -S %s -passes=simplifycfg -o - -experimental-assignment-tracking \
2+
; RUN: | FileCheck %s
3+
4+
;; $ cat test.cpp
5+
;; class a {};
6+
;; void operator*(a, float &);
7+
;; class b {
8+
;; public:
9+
;; a c;
10+
;; };
11+
;; int d;
12+
;; class e {
13+
;; b g[3];
14+
;; float f;
15+
;; void i();
16+
;; };
17+
;; void e::i() {
18+
;; float h;
19+
;; g[d].c *h;
20+
;; if (h)
21+
;; h = f;
22+
;; else
23+
;; h = f;
24+
;; }
25+
;; Generated by grabbing IR before simplifycfg in:
26+
;; $ clang++ -O2 -g -c test.cpp -Xclang -fexperimental-assignment-tracking
27+
28+
;; if.then and if.else each only have a dbg.assign and br instruction.
29+
;; SimplifyCFG will remove these blocks. Check that the dbg.assign intrinsics
30+
;; are sunk into the succ beforehand.
31+
32+
; CHECK: entry:
33+
;; -- alloca dbg.assign
34+
; CHECK: call void @llvm.dbg.assign(metadata i1 undef
35+
;; -- sunk dbg.assigns
36+
; CHECK: call void @llvm.dbg.assign(metadata float undef, metadata ![[var:[0-9]+]], metadata !DIExpression(), metadata ![[id:[0-9]+]], metadata ptr %h, metadata !DIExpression()), !dbg
37+
; CHECK-NEXT: call void @llvm.dbg.assign(metadata float undef, metadata ![[var]], metadata !DIExpression(), metadata ![[id]], metadata ptr %h, metadata !DIExpression()), !dbg
38+
; CHECK-NEXT: %storemerge.in = getelementptr
39+
; CHECK-NEXT: %storemerge = load float
40+
; CHECK-NEXT: store float %storemerge, ptr %h, align 4{{.+}}!DIAssignID ![[id]]
41+
; CHECK: ret void
42+
43+
%class.e = type { [3 x %class.b], float }
44+
%class.b = type { %class.a }
45+
%class.a = type { i8 }
46+
47+
@d = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0
48+
49+
; Function Attrs: uwtable
50+
define dso_local void @_ZN1e1iEv(ptr %this) local_unnamed_addr #0 align 2 !dbg !11 {
51+
entry:
52+
%h = alloca float, align 4, !DIAssignID !32
53+
call void @llvm.dbg.assign(metadata i1 undef, metadata !31, metadata !DIExpression(), metadata !32, metadata ptr %h, metadata !DIExpression()), !dbg !33
54+
%0 = bitcast ptr %h to ptr, !dbg !34
55+
call void @llvm.lifetime.start.p0i8(i64 4, ptr nonnull %0) #4, !dbg !34
56+
call void @_Zml1aRf(ptr nonnull align 4 dereferenceable(4) %h), !dbg !35
57+
%1 = load float, ptr %h, align 4, !dbg !36
58+
%tobool = fcmp une float %1, 0.000000e+00, !dbg !36
59+
br i1 %tobool, label %if.then, label %if.else, !dbg !42
60+
61+
if.then: ; preds = %entry
62+
call void @llvm.dbg.assign(metadata float undef, metadata !31, metadata !DIExpression(), metadata !43, metadata ptr %h, metadata !DIExpression()), !dbg !33
63+
br label %if.end, !dbg !44
64+
65+
if.else: ; preds = %entry
66+
call void @llvm.dbg.assign(metadata float undef, metadata !31, metadata !DIExpression(), metadata !43, metadata ptr %h, metadata !DIExpression()), !dbg !33
67+
br label %if.end
68+
69+
if.end: ; preds = %if.else, %if.then
70+
%storemerge.in = getelementptr inbounds %class.e, ptr %this, i64 0, i32 1, !dbg !45
71+
%storemerge = load float, ptr %storemerge.in, align 4, !dbg !45
72+
store float %storemerge, ptr %h, align 4, !dbg !45, !DIAssignID !43
73+
call void @llvm.lifetime.end.p0i8(i64 4, ptr nonnull %0) #4, !dbg !48
74+
ret void, !dbg !48
75+
}
76+
77+
declare void @llvm.lifetime.start.p0i8(i64 immarg, ptr nocapture) #1
78+
declare !dbg !49 dso_local void @_Zml1aRf(ptr nonnull align 4 dereferenceable(4)) local_unnamed_addr #2
79+
declare void @llvm.lifetime.end.p0i8(i64 immarg, ptr nocapture) #1
80+
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) #3
81+
82+
!llvm.dbg.cu = !{!2}
83+
!llvm.module.flags = !{!7, !8, !9}
84+
!llvm.ident = !{!10}
85+
86+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
87+
!1 = distinct !DIGlobalVariable(name: "d", scope: !2, file: !3, line: 7, type: !6, isLocal: false, isDefinition: true)
88+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
89+
!3 = !DIFile(filename: "test.cpp", directory: "/")
90+
!4 = !{}
91+
!5 = !{!0}
92+
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
93+
!7 = !{i32 7, !"Dwarf Version", i32 4}
94+
!8 = !{i32 2, !"Debug Info Version", i32 3}
95+
!9 = !{i32 1, !"wchar_size", i32 4}
96+
!10 = !{!"clang version 12.0.0"}
97+
!11 = distinct !DISubprogram(name: "i", linkageName: "_ZN1e1iEv", scope: !12, file: !3, line: 13, type: !25, scopeLine: 13, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, declaration: !24, retainedNodes: !28)
98+
!12 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "e", file: !3, line: 8, size: 64, flags: DIFlagTypePassByValue, elements: !13, identifier: "_ZTS1e")
99+
!13 = !{!14, !22, !24}
100+
!14 = !DIDerivedType(tag: DW_TAG_member, name: "g", scope: !12, file: !3, line: 9, baseType: !15, size: 24)
101+
!15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !16, size: 24, elements: !20)
102+
!16 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "b", file: !3, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !17, identifier: "_ZTS1b")
103+
!17 = !{!18}
104+
!18 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !16, file: !3, line: 5, baseType: !19, size: 8, flags: DIFlagPublic)
105+
!19 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "a", file: !3, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !4, identifier: "_ZTS1a")
106+
!20 = !{!21}
107+
!21 = !DISubrange(count: 3)
108+
!22 = !DIDerivedType(tag: DW_TAG_member, name: "f", scope: !12, file: !3, line: 10, baseType: !23, size: 32, offset: 32)
109+
!23 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
110+
!24 = !DISubprogram(name: "i", linkageName: "_ZN1e1iEv", scope: !12, file: !3, line: 11, type: !25, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
111+
!25 = !DISubroutineType(types: !26)
112+
!26 = !{null, !27}
113+
!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
114+
!28 = !{!29, !31}
115+
!29 = !DILocalVariable(name: "this", arg: 1, scope: !11, type: !30, flags: DIFlagArtificial | DIFlagObjectPointer)
116+
!30 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
117+
!31 = !DILocalVariable(name: "h", scope: !11, file: !3, line: 14, type: !23)
118+
!32 = distinct !DIAssignID()
119+
!33 = !DILocation(line: 0, scope: !11)
120+
!34 = !DILocation(line: 14, column: 3, scope: !11)
121+
!35 = !DILocation(line: 15, column: 10, scope: !11)
122+
!36 = !DILocation(line: 16, column: 7, scope: !37)
123+
!37 = distinct !DILexicalBlock(scope: !11, file: !3, line: 16, column: 7)
124+
!42 = !DILocation(line: 16, column: 7, scope: !11)
125+
!43 = distinct !DIAssignID()
126+
!44 = !DILocation(line: 17, column: 5, scope: !37)
127+
!45 = !DILocation(line: 0, scope: !37)
128+
!48 = !DILocation(line: 20, column: 1, scope: !11)
129+
!49 = !DISubprogram(name: "operator*", linkageName: "_Zml1aRf", scope: !3, file: !3, line: 2, type: !50, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !4)
130+
!50 = !DISubroutineType(types: !51)
131+
!51 = !{null, !19, !52}
132+
!52 = !DIDerivedType(tag: DW_TAG_reference_type, baseType: !23, size: 64)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
; RUN: opt -passes=simplifycfg %s -S -experimental-assignment-tracking \
2+
; RUN: | FileCheck %s
3+
4+
;; Ensure that we correctly update the value component of dbg.assign intrinsics
5+
;; after merging a conditional block with a store its the predecessor. The
6+
;; value stored is still conditional, but the store itself is now
7+
;; unconditionally run, so we must be sure that any linked dbg.assign intrinsics
8+
;; are tracking the new stored value (the result of the select). If we don't,
9+
;; and the store were to be removed by another pass (e.g. DSE), then we'd
10+
;; eventually end up emitting a location describing the conditional value,
11+
;; unconditionally.
12+
13+
;; Created from the following source and command, with dbg.assign and DIAssignID
14+
;; metadata added and some other metadata removed by hand:
15+
;; $ cat test.c
16+
;; int a;
17+
;; void b() {
18+
;; int c = 0;
19+
;; if (a)
20+
;; c = 1;
21+
;; }
22+
;; $ clang -O2 -g -emit-llvm -S test.c -Xclang -fexperimental-assignment-tracking
23+
24+
; CHECK: %[[SELECT:.*]] = select i1 %tobool
25+
; CHECK-NEXT: store i32 %[[SELECT]], ptr %c{{.*}}, !DIAssignID ![[ID:[0-9]+]]
26+
; CHECK-NEXT: call void @llvm.dbg.assign(metadata i32 %[[SELECT]], metadata ![[VAR_C:[0-9]+]], metadata !DIExpression(), metadata ![[ID]], metadata ptr %c, metadata !DIExpression()), !dbg
27+
; CHECK: ![[VAR_C]] = !DILocalVariable(name: "c",
28+
29+
@a = dso_local global i32 0, align 4, !dbg !0
30+
31+
define dso_local void @b() !dbg !11 {
32+
entry:
33+
%c = alloca i32, align 4
34+
%0 = bitcast ptr %c to ptr, !dbg !16
35+
call void @llvm.lifetime.start.p0i8(i64 4, ptr %0), !dbg !16
36+
store i32 0, ptr %c, align 4, !dbg !17, !DIAssignID !36
37+
call void @llvm.dbg.assign(metadata i32 0, metadata !15, metadata !DIExpression(), metadata !36, metadata ptr %c, metadata !DIExpression()), !dbg !17
38+
%1 = load i32, ptr @a, align 4, !dbg !22
39+
%tobool = icmp ne i32 %1, 0, !dbg !22
40+
br i1 %tobool, label %if.then, label %if.end, !dbg !24
41+
42+
if.then: ; preds = %entry
43+
store i32 1, ptr %c, align 4, !dbg !25, !DIAssignID !37
44+
call void @llvm.dbg.assign(metadata i32 1, metadata !15, metadata !DIExpression(), metadata !37, metadata ptr %c, metadata !DIExpression()), !dbg !17
45+
br label %if.end, !dbg !26
46+
47+
if.end: ; preds = %if.then, %entry
48+
%2 = bitcast ptr %c to ptr, !dbg !27
49+
call void @llvm.lifetime.end.p0i8(i64 4, ptr %2), !dbg !27
50+
ret void, !dbg !27
51+
}
52+
53+
declare void @llvm.lifetime.start.p0i8(i64 immarg, ptr nocapture)
54+
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
55+
declare void @llvm.lifetime.end.p0i8(i64 immarg, ptr nocapture)
56+
57+
!llvm.dbg.cu = !{!2}
58+
!llvm.module.flags = !{!7, !8, !9}
59+
!llvm.ident = !{!10}
60+
61+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
62+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
63+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
64+
!3 = !DIFile(filename: "test.c", directory: "/")
65+
!4 = !{}
66+
!5 = !{!0}
67+
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
68+
!7 = !{i32 7, !"Dwarf Version", i32 4}
69+
!8 = !{i32 2, !"Debug Info Version", i32 3}
70+
!9 = !{i32 1, !"wchar_size", i32 4}
71+
!10 = !{!"clang version 12.0.0"}
72+
!11 = distinct !DISubprogram(name: "b", scope: !3, file: !3, line: 2, type: !12, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !14)
73+
!12 = !DISubroutineType(types: !13)
74+
!13 = !{null}
75+
!14 = !{!15}
76+
!15 = !DILocalVariable(name: "c", scope: !11, file: !3, line: 3, type: !6)
77+
!16 = !DILocation(line: 3, column: 3, scope: !11)
78+
!17 = !DILocation(line: 3, column: 7, scope: !11)
79+
!22 = !DILocation(line: 4, column: 7, scope: !23)
80+
!23 = distinct !DILexicalBlock(scope: !11, file: !3, line: 4, column: 7)
81+
!24 = !DILocation(line: 4, column: 7, scope: !11)
82+
!25 = !DILocation(line: 5, column: 7, scope: !23)
83+
!26 = !DILocation(line: 5, column: 5, scope: !23)
84+
!27 = !DILocation(line: 6, column: 1, scope: !11)
85+
!36 = distinct !DIAssignID()
86+
!37 = distinct !DIAssignID()

0 commit comments

Comments
 (0)