Debian Codegraph @

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

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