1616//
1717// ===----------------------------------------------------------------------===//
1818
19+ #define DEBUG_TYPE " sil-opt-remarks"
20+
1921#include " swift/SIL/OptimizationRemark.h"
2022#include " swift/AST/DiagnosticEngine.h"
2123#include " swift/AST/DiagnosticsSIL.h"
3032#include " swift/SIL/SILRemarkStreamer.h"
3133#include " llvm/ADT/StringExtras.h"
3234#include " llvm/Support/CommandLine.h"
35+ #include " llvm/Support/Debug.h"
3336#include " llvm/Support/raw_ostream.h"
3437
3538using namespace swift ;
@@ -145,15 +148,29 @@ Emitter::Emitter(StringRef passName, SILFunction &fn)
145148 fn.getASTContext().LangOpts.OptimizationRemarkMissedPattern->match(
146149 passName))) {}
147150
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+
148163// / The user has passed us an instruction that for some reason has a source loc
149164// / that can not be used. Search down the current block for an instruction with
150165// / a valid source loc and use that instead.
151- static SourceLoc inferOptRemarkSearchForwards (SILInstruction &i) {
166+ static SourceLoc
167+ inferOptRemarkSearchForwards (SILInstruction &i,
168+ SourceLocPresentationKind presentationKind) {
152169 for (auto &inst :
153170 llvm::make_range (std::next (i.getIterator ()), i.getParent ()->end ())) {
154- auto newLoc = inst.getLoc (). getSourceLoc ( );
171+ auto newLoc = getLocForPresentation ( inst.getLoc (), presentationKind );
155172 if (auto inlinedLoc = inst.getDebugScope ()->getOutermostInlineLocation ())
156- newLoc = inlinedLoc. getSourceLoc ( );
173+ newLoc = getLocForPresentation (inlinedLoc, presentationKind );
157174 if (newLoc.isValid ())
158175 return newLoc;
159176 }
@@ -165,18 +182,16 @@ static SourceLoc inferOptRemarkSearchForwards(SILInstruction &i) {
165182// / that can not be used. Search up the current block for an instruction with
166183// / a valid SILLocation and use the end SourceLoc of the SourceRange for the
167184// / instruction.
168- static SourceLoc inferOptRemarkSearchBackwards (SILInstruction &i) {
185+ static SourceLoc
186+ inferOptRemarkSearchBackwards (SILInstruction &i,
187+ SourceLocPresentationKind presentationKind) {
169188 for (auto &inst : llvm::make_range (std::next (i.getReverseIterator ()),
170189 i.getParent ()->rend ())) {
171190 auto loc = inst.getLoc ();
172191 if (auto inlinedLoc = inst.getDebugScope ()->getOutermostInlineLocation ())
173192 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;
180195 }
181196
182197 return SourceLoc ();
@@ -195,34 +210,56 @@ static llvm::cl::opt<bool> IgnoreAlwaysInferForTesting(
195210// (retain, release) and other situations where we are ok with original source
196211// locs if we are not inlined (alloc_ref, alloc_stack).
197212SourceLoc 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);
199216 // If we are only supposed to infer in inline contexts, see if we have a valid
200217 // loc and if that loc is an inlined call site.
201218 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+ }
207234
208235 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 " );
211240 return newLoc;
241+ }
212242 }
213243
214244 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 " );
217249 return newLoc;
250+ }
218251 }
219252
220253 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 " );
223258 return newLoc;
259+ }
224260 }
225261
262+ LLVM_DEBUG (llvm::dbgs () << " Failed to find good loc!\n " );
226263 return SourceLoc ();
227264}
228265
0 commit comments