Debian Codegraph @

external/llvm/releases/9.0.0/patches_external/Instruction-Add-dropLocation-and-updateLocationAfter.patch

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 /*========================== begin_copyright_notice ============================
10
11 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12 See https://llvm.org/LICENSE.txt for license information.
13 SPDX-License-Identifier: Apache-2.0 with LLVM-exception
14
15 ============================= end_copyright_notice ===========================*/
16
17 From dfc5a9eb57aaaac972764bf731503419284bd3dc Mon Sep 17 00:00:00 2001
18 From: Vedant Kumar <[email protected]>
19 Date: Tue, 8 Sep 2020 13:39:52 -0700
20 Subject: [PATCH] [Instruction] Add dropLocation and updateLocationAfterHoist
21 helpers
22
23 Introduce a helper which can be used to update the debug location of an
24 Instruction after the instruction is hoisted. This can be used to safely
25 drop a source location as recommended by the docs.
26
27 For more context, see the discussion in https://reviews.llvm.org/D60913.
28
29 Differential Revision: https://reviews.llvm.org/D85670
30 ---
31 include/llvm/IR/Instruction.h | 14 ++++
32 lib/IR/DebugInfo.cpp | 32 +++++++++
33 lib/Transforms/Scalar/GVN.cpp | 4 +-
34 lib/Transforms/Scalar/LICM.cpp | 5 +-
35 test/DebugInfo/Generic/licm-hoist-debug-loc.ll | 3 +-
36 test/Transforms/GVN/PRE/phi-translate.ll | 7 +-
37 unittests/IR/InstructionsTest.cpp | 72 +++++++++++++++++++
38 8 files changed, 125 insertions(+), 14 deletions(-)
39
40 diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
41 index ceec001dccf4..24f8e0d6a4b1 100644
42 --- a/llvm/include/llvm/IR/Instruction.h
43 +++ b/llvm/include/llvm/IR/Instruction.h
44 @@ -492,6 +492,20 @@ public:
45 /// merged DebugLoc.
46 void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);
47
48 + /// Updates the debug location given that the instruction has been hoisted
49 + /// from a block to a predecessor of that block.
50 + /// Note: it is undefined behavior to call this on an instruction not
51 + /// currently inserted into a function.
52 + void updateLocationAfterHoist();
53 +
54 + /// Drop the instruction's debug location. This does not guarantee removal
55 + /// of the !dbg source location attachment, as it must set a line 0 location
56 + /// with scope information attached on call instructions. To guarantee
57 + /// removal of the !dbg attachment, use the \ref setDebugLoc() API.
58 + /// Note: it is undefined behavior to call this on an instruction not
59 + /// currently inserted into a function.
60 + void dropLocation();
61 +
62 private:
63 /// Return true if we have an entry in the on-the-side metadata hash.
64 bool hasMetadataHashEntry() const {
65 diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
66 index 190b220dc9aa..603b88200001 100644
67 --- a/llvm/lib/IR/DebugInfo.cpp
68 +++ b/llvm/lib/IR/DebugInfo.cpp
69 @@ -696,6 +696,38 @@ void Instruction::applyMergedLocation(const DILocation *LocA,
70 setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
71 }
72
73 +void Instruction::updateLocationAfterHoist() { dropLocation(); }
74 +
75 +void Instruction::dropLocation() {
76 + const DebugLoc &DL = getDebugLoc();
77 + if (!DL)
78 + return;
79 +
80 + // If this isn't a call, drop the location to allow a location from a
81 + // preceding instruction to propagate.
82 + if (!isa<CallBase>(this)) {
83 + setDebugLoc(DebugLoc());
84 + return;
85 + }
86 +
87 + // Set a line 0 location for calls to preserve scope information in case
88 + // inlining occurs.
89 + const DISubprogram *SP = getFunction()->getSubprogram();
90 + if (SP)
91 + // If a function scope is available, set it on the line 0 location. When
92 + // hoisting a call to a predecessor block, using the function scope avoids
93 + // making it look like the callee was reached earlier than it should be.
94 + setDebugLoc(DebugLoc::get(0, 0, SP));
95 + else
96 + // The parent function has no scope. Go ahead and drop the location. If
97 + // the parent function is inlined, and the callee has a subprogram, the
98 + // inliner will attach a location to the call.
99 + //
100 + // One alternative is to set a line 0 location with the existing scope and
101 + // inlinedAt info. The location might be sensitive to when inlining occurs.
102 + setDebugLoc(DebugLoc());
103 +}
104 +
105 //===----------------------------------------------------------------------===//
106 // LLVM C API implementations.
107 //===----------------------------------------------------------------------===//
108 diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
109 index f8e8e2c773f9..c25fdd44dcf9 100644
110 --- a/llvm/lib/Transforms/Scalar/GVN.cpp
111 +++ b/llvm/lib/Transforms/Scalar/GVN.cpp
112 @@ -48,7 +48,6 @@
113 #include "llvm/IR/Constant.h"
114 #include "llvm/IR/Constants.h"
115 #include "llvm/IR/DataLayout.h"
116 -#include "llvm/IR/DebugInfoMetadata.h"
117 #include "llvm/IR/DebugLoc.h"
118 #include "llvm/IR/Dominators.h"
119 #include "llvm/IR/Function.h"
120 @@ -1323,8 +1322,7 @@ bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
121 // Instructions that have been inserted in predecessor(s) to materialize
122 // the load address do not retain their original debug locations. Doing
123 // so could lead to confusing (but correct) source attributions.
124 - if (const DebugLoc &DL = I->getDebugLoc())
125 - I->setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
126 + I->updateLocationAfterHoist();
127
128 // FIXME: We really _ought_ to insert these value numbers into their
129 // parent's availability map. However, in doing so, we risk getting into
130 diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
131 index 1b1679e1b03d..631fa2f27c5b 100644
132 --- a/llvm/lib/Transforms/Scalar/LICM.cpp
133 +++ b/llvm/lib/Transforms/Scalar/LICM.cpp
134 @@ -1736,10 +1736,7 @@ static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
135 // Move the new node to the destination block, before its terminator.
136 moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE);
137
138 - // Apply line 0 debug locations when we are moving instructions to different
139 - // basic blocks because we want to avoid jumpy line tables.
140 - if (const DebugLoc &DL = I.getDebugLoc())
141 - I.setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
142 + I.updateLocationAfterHoist();
143
144 if (isa<LoadInst>(I))
145 ++NumMovedLoads;
146 diff --git a/llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll b/llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll
147 index 0a13e9604f0c..37c642170aa3 100644
148 --- a/llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll
149 +++ b/llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll
150 @@ -18,9 +18,8 @@
151 ; We make sure that the instruction that is hoisted into the preheader
152 ; does not have a debug location.
153 ; CHECK: for.body.lr.ph:
154 -; CHECK: getelementptr{{.*}}%p.addr, i64 4{{.*}} !dbg [[zero:![0-9]+]]
155 +; CHECK: getelementptr{{.*}}%p.addr, i64 4{{$}}
156 ; CHECK: for.body:
157 -; CHECK: [[zero]] = !DILocation(line: 0
158 ;
159 ; ModuleID = 't.ll'
160 source_filename = "test.c"
161 diff --git a/llvm/test/Transforms/GVN/PRE/phi-translate.ll b/llvm/test/Transforms/GVN/PRE/phi-translate.ll
162 index f80e002eab0a..2b6a577d6678 100644
163 --- a/llvm/test/Transforms/GVN/PRE/phi-translate.ll
164 +++ b/llvm/test/Transforms/GVN/PRE/phi-translate.ll
165 @@ -4,8 +4,8 @@ target datalayout = "e-p:64:64:64"
166
167 ; CHECK-LABEL: @foo(
168 ; CHECK: entry.end_crit_edge:
169 -; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{.*}} !dbg [[ZERO_LOC:![0-9]+]]
170 -; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{.*}} !dbg [[ZERO_LOC]]
171 +; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{$}}
172 +; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{$}}
173 ; CHECK: %n.pre = load i32, i32* %[[ADDRESS]], !dbg [[N_LOC:![0-9]+]]
174 ; CHECK: br label %end
175 ; CHECK: then:
176 @@ -14,8 +14,7 @@ target datalayout = "e-p:64:64:64"
177 ; CHECK: %n = phi i32 [ %n.pre, %entry.end_crit_edge ], [ %z, %then ], !dbg [[N_LOC]]
178 ; CHECK: ret i32 %n
179
180 -; CHECK-DAG: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
181 -; CHECK-DAG: [[ZERO_LOC]] = !DILocation(line: 0
182 +; CHECK: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
183
184 @G = external global [100 x i32]
185 define i32 @foo(i32 %x, i32 %z) !dbg !6 {
186 diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp
187 index b25eee7279af..1324fe2d1dd7 100644
188 --- a/llvm/unittests/IR/InstructionsTest.cpp
189 +++ b/llvm/unittests/IR/InstructionsTest.cpp
190 @@ -13,6 +13,7 @@
191 #include "llvm/IR/BasicBlock.h"
192 #include "llvm/IR/Constants.h"
193 #include "llvm/IR/DataLayout.h"
194 +#include "llvm/IR/DebugInfoMetadata.h"
195 #include "llvm/IR/DerivedTypes.h"
196 #include "llvm/IR/Function.h"
197 #include "llvm/IR/IRBuilder.h"
198 @@ -1304,5 +1305,76 @@ TEST(InstructionsTest, UnaryOperator) {
199 I->deleteValue();
200 }
201
202 +TEST(InstructionsTest, DropLocation) {
203 + LLVMContext C;
204 + std::unique_ptr<Module> M = parseIR(C,
205 + R"(
206 + declare void @callee()
207 +
208 + define void @no_parent_scope() {
209 + call void @callee() ; I1: Call with no location.
210 + call void @callee(), !dbg !11 ; I2: Call with location.
211 + ret void, !dbg !11 ; I3: Non-call with location.
212 + }
213 +
214 + define void @with_parent_scope() !dbg !8 {
215 + call void @callee() ; I1: Call with no location.
216 + call void @callee(), !dbg !11 ; I2: Call with location.
217 + ret void, !dbg !11 ; I3: Non-call with location.
218 + }
219 +
220 + !llvm.dbg.cu = !{!0}
221 + !llvm.module.flags = !{!3, !4}
222 + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
223 + !1 = !DIFile(filename: "t2.c", directory: "foo")
224 + !2 = !{}
225 + !3 = !{i32 2, !"Dwarf Version", i32 4}
226 + !4 = !{i32 2, !"Debug Info Version", i32 3}
227 + !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
228 + !9 = !DISubroutineType(types: !10)
229 + !10 = !{null}
230 + !11 = !DILocation(line: 2, column: 7, scope: !8, inlinedAt: !12)
231 + !12 = !DILocation(line: 3, column: 8, scope: !8)
232 + )");
233 + ASSERT_TRUE(M);
234 +
235 + {
236 + Function *NoParentScopeF =
237 + cast<Function>(M->getNamedValue("no_parent_scope"));
238 + BasicBlock &BB = NoParentScopeF->front();
239 +
240 + auto *I1 = BB.getFirstNonPHI();
241 + auto *I2 = I1->getNextNode();
242 + auto *I3 = BB.getTerminator();
243 +
244 + EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
245 + I1->dropLocation();
246 + EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
247 +
248 + EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
249 + I2->dropLocation();
250 + EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
251 +
252 + EXPECT_EQ(I3->getDebugLoc().getLine(), 2U);
253 + I3->dropLocation();
254 + EXPECT_EQ(I3->getDebugLoc(), DebugLoc());
255 + }
256 +
257 + {
258 + Function *WithParentScopeF =
259 + cast<Function>(M->getNamedValue("with_parent_scope"));
260 + BasicBlock &BB = WithParentScopeF->front();
261 +
262 + auto *I2 = BB.getFirstNonPHI()->getNextNode();
263 +
264 + MDNode *Scope = cast<MDNode>(WithParentScopeF->getSubprogram());
265 + EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
266 + I2->dropLocation();
267 + EXPECT_EQ(I2->getDebugLoc().getLine(), 0U);
268 + EXPECT_EQ(I2->getDebugLoc().getScope(), Scope);
269 + EXPECT_EQ(I2->getDebugLoc().getInlinedAt(), nullptr);
270 + }
271 +}
272 +
273 } // end anonymous namespace
274 } // end namespace llvm
275 --
276 2.29.2.windows.2
277