16
16
//
17
17
// ===----------------------------------------------------------------------===//
18
18
19
+ #define DEBUG_TYPE " sil-opt-remarks"
20
+
19
21
#include " swift/SIL/OptimizationRemark.h"
20
22
#include " swift/AST/DiagnosticEngine.h"
21
23
#include " swift/AST/DiagnosticsSIL.h"
30
32
#include " swift/SIL/SILRemarkStreamer.h"
31
33
#include " llvm/ADT/StringExtras.h"
32
34
#include " llvm/Support/CommandLine.h"
35
+ #include " llvm/Support/Debug.h"
33
36
#include " llvm/Support/raw_ostream.h"
34
37
35
38
using namespace swift ;
@@ -145,15 +148,29 @@ Emitter::Emitter(StringRef passName, SILFunction &fn)
145
148
fn.getASTContext().LangOpts.OptimizationRemarkMissedPattern->match(
146
149
passName))) {}
147
150
151
+ static SourceLoc getLocForPresentation (SILLocation loc,
152
+ SourceLocPresentationKind kind) {
153
+ if (!loc)
154
+ return SourceLoc ();
155
+ switch (kind) {
156
+ case SourceLocPresentationKind::StartRange:
157
+ return loc.getSourceLoc ();
158
+ case SourceLocPresentationKind::EndRange:
159
+ return loc.getEndSourceLoc ();
160
+ }
161
+ }
162
+
148
163
// / The user has passed us an instruction that for some reason has a source loc
149
164
// / that can not be used. Search down the current block for an instruction with
150
165
// / a valid source loc and use that instead.
151
- static SourceLoc inferOptRemarkSearchForwards (SILInstruction &i) {
166
+ static SourceLoc
167
+ inferOptRemarkSearchForwards (SILInstruction &i,
168
+ SourceLocPresentationKind presentationKind) {
152
169
for (auto &inst :
153
170
llvm::make_range (std::next (i.getIterator ()), i.getParent ()->end ())) {
154
- auto newLoc = inst.getLoc (). getSourceLoc ( );
171
+ auto newLoc = getLocForPresentation ( inst.getLoc (), presentationKind );
155
172
if (auto inlinedLoc = inst.getDebugScope ()->getOutermostInlineLocation ())
156
- newLoc = inlinedLoc. getSourceLoc ( );
173
+ newLoc = getLocForPresentation (inlinedLoc, presentationKind );
157
174
if (newLoc.isValid ())
158
175
return newLoc;
159
176
}
@@ -165,18 +182,16 @@ static SourceLoc inferOptRemarkSearchForwards(SILInstruction &i) {
165
182
// / that can not be used. Search up the current block for an instruction with
166
183
// / a valid SILLocation and use the end SourceLoc of the SourceRange for the
167
184
// / instruction.
168
- static SourceLoc inferOptRemarkSearchBackwards (SILInstruction &i) {
185
+ static SourceLoc
186
+ inferOptRemarkSearchBackwards (SILInstruction &i,
187
+ SourceLocPresentationKind presentationKind) {
169
188
for (auto &inst : llvm::make_range (std::next (i.getReverseIterator ()),
170
189
i.getParent ()->rend ())) {
171
190
auto loc = inst.getLoc ();
172
191
if (auto inlinedLoc = inst.getDebugScope ()->getOutermostInlineLocation ())
173
192
loc = inlinedLoc;
174
- if (!loc.getSourceLoc ().isValid ())
175
- continue ;
176
-
177
- auto range = loc.getSourceRange ();
178
- if (range.isValid ())
179
- return range.End ;
193
+ if (auto result = getLocForPresentation (loc, presentationKind))
194
+ return result;
180
195
}
181
196
182
197
return SourceLoc ();
@@ -195,34 +210,56 @@ static llvm::cl::opt<bool> IgnoreAlwaysInferForTesting(
195
210
// (retain, release) and other situations where we are ok with original source
196
211
// locs if we are not inlined (alloc_ref, alloc_stack).
197
212
SourceLoc swift::OptRemark::inferOptRemarkSourceLoc (
198
- SILInstruction &i, SourceLocInferenceBehavior inferBehavior) {
213
+ SILInstruction &i, SourceLocInferenceBehavior inferBehavior,
214
+ SourceLocPresentationKind presentationKind) {
215
+ LLVM_DEBUG (llvm::dbgs () << " Begin infer source loc for: " << i);
199
216
// If we are only supposed to infer in inline contexts, see if we have a valid
200
217
// loc and if that loc is an inlined call site.
201
218
auto loc = i.getLoc ();
202
- if (loc.getSourceLoc ().isValid () &&
203
- !(bool (inferBehavior & SourceLocInferenceBehavior::AlwaysInfer) &&
204
- !IgnoreAlwaysInferForTesting) &&
205
- !(i.getDebugScope () && i.getDebugScope ()->InlinedCallSite ))
206
- return loc.getSourceLoc ();
219
+ if (!(bool (inferBehavior & SourceLocInferenceBehavior::AlwaysInfer) &&
220
+ !IgnoreAlwaysInferForTesting)) {
221
+ LLVM_DEBUG (llvm::dbgs () << " Testing insts own source loc?!\n " );
222
+ if (loc.getSourceLoc ().isValid ()) {
223
+ LLVM_DEBUG (llvm::dbgs () << " Found initial valid loc!\n " );
224
+ if (!(i.getDebugScope () && i.getDebugScope ()->InlinedCallSite )) {
225
+ LLVM_DEBUG (llvm::dbgs () << " Found debug scope!\n " );
226
+ return getLocForPresentation (loc, presentationKind);
227
+ } else {
228
+ LLVM_DEBUG (llvm::dbgs () << " Did not find debug scope!\n " );
229
+ }
230
+ } else {
231
+ LLVM_DEBUG (llvm::dbgs () << " Failed to find initial valid loc!\n " );
232
+ }
233
+ }
207
234
208
235
if (bool (inferBehavior & SourceLocInferenceBehavior::ForwardScan)) {
209
- SourceLoc newLoc = inferOptRemarkSearchForwards (i);
210
- if (newLoc.isValid ())
236
+ LLVM_DEBUG (llvm::dbgs () << " Inferring Source Loc Forward!\n " );
237
+ SourceLoc newLoc = inferOptRemarkSearchForwards (i, presentationKind);
238
+ if (newLoc.isValid ()) {
239
+ LLVM_DEBUG (llvm::dbgs () << " Found loc!\n " );
211
240
return newLoc;
241
+ }
212
242
}
213
243
214
244
if (bool (inferBehavior & SourceLocInferenceBehavior::BackwardScan)) {
215
- SourceLoc newLoc = inferOptRemarkSearchBackwards (i);
216
- if (newLoc.isValid ())
245
+ LLVM_DEBUG (llvm::dbgs () << " Inferring Source Loc Backwards!\n " );
246
+ SourceLoc newLoc = inferOptRemarkSearchBackwards (i, presentationKind);
247
+ if (newLoc.isValid ()) {
248
+ LLVM_DEBUG (llvm::dbgs () << " Found loc!\n " );
217
249
return newLoc;
250
+ }
218
251
}
219
252
220
253
if (bool (inferBehavior & SourceLocInferenceBehavior::ForwardScan2nd)) {
221
- SourceLoc newLoc = inferOptRemarkSearchForwards (i);
222
- if (newLoc.isValid ())
254
+ LLVM_DEBUG (llvm::dbgs () << " Inferring Source Loc Forward Scan 2nd!\n " );
255
+ SourceLoc newLoc = inferOptRemarkSearchForwards (i, presentationKind);
256
+ if (newLoc.isValid ()) {
257
+ LLVM_DEBUG (llvm::dbgs () << " Found loc!\n " );
223
258
return newLoc;
259
+ }
224
260
}
225
261
262
+ LLVM_DEBUG (llvm::dbgs () << " Failed to find good loc!\n " );
226
263
return SourceLoc ();
227
264
}
228
265
0 commit comments