-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathOptimizationRemark.cpp
255 lines (218 loc) · 8.31 KB
/
OptimizationRemark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//===--- OptimizationRemark.cpp - Optimization diagnostics ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
/// \file
/// This file defines the remark type and the emitter class that passes can use
/// to emit optimization diagnostics.
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/OptimizationRemark.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/Demangling/Demangler.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILRemarkStreamer.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
using namespace OptRemark;
Argument::Argument(StringRef key, int n)
: key(ArgumentKeyKind::Default, key), val(llvm::itostr(n)) {}
Argument::Argument(StringRef key, long n)
: key(ArgumentKeyKind::Default, key), val(llvm::itostr(n)) {}
Argument::Argument(StringRef key, long long n)
: key(ArgumentKeyKind::Default, key), val(llvm::itostr(n)) {}
Argument::Argument(StringRef key, unsigned n)
: key(ArgumentKeyKind::Default, key), val(llvm::utostr(n)) {}
Argument::Argument(StringRef key, unsigned long n)
: key(ArgumentKeyKind::Default, key), val(llvm::utostr(n)) {}
Argument::Argument(StringRef key, unsigned long long n)
: key(ArgumentKeyKind::Default, key), val(llvm::utostr(n)) {}
Argument::Argument(ArgumentKey key, SILFunction *f) : key(key) {
auto options = Demangle::DemangleOptions::SimplifiedUIDemangleOptions();
// Enable module names so that we have a way of filtering out
// stdlib-related remarks.
options.DisplayModuleNames = true;
val = (Twine("\"") + Demangle::demangleSymbolAsString(f->getName(), options) +
"\"")
.str();
if (f->hasLocation())
loc = f->getLocation().getSourceLoc();
}
Argument::Argument(StringRef key, SILType ty)
: key(ArgumentKeyKind::Default, key) {
llvm::raw_string_ostream stream(val);
ty.print(stream);
}
Argument::Argument(StringRef key, CanType ty)
: key(ArgumentKeyKind::Default, key) {
llvm::raw_string_ostream stream(val);
ty.print(stream);
}
template <typename DerivedT>
std::string Remark<DerivedT>::getMsg() const {
std::string str;
llvm::raw_string_ostream stream(str);
// Go through our args and if we are not emitting for diagnostics *OR* we are
// emitting for diagnostics and this argument is not intended to be emitted as
// a diagnostic separate from our main remark, emit the arg value here.
for (const Argument &arg : args) {
if (arg.key.kind.isSeparateDiagnostic())
continue;
stream << arg.val;
}
return stream.str();
}
template <typename DerivedT>
std::string Remark<DerivedT>::getDebugMsg() const {
std::string str;
llvm::raw_string_ostream stream(str);
if (indentDebugWidth)
stream << std::string(" ", indentDebugWidth);
for (const Argument &arg : args)
stream << arg.val;
stream << "\n";
return stream.str();
}
Emitter::Emitter(StringRef passName, SILFunction &fn)
: fn(fn), passName(passName),
passedEnabled(
fn.getASTContext().LangOpts.OptimizationRemarkPassedPattern &&
fn.getASTContext().LangOpts.OptimizationRemarkPassedPattern->match(
passName)),
missedEnabled(
fn.getASTContext().LangOpts.OptimizationRemarkMissedPattern &&
fn.getASTContext().LangOpts.OptimizationRemarkMissedPattern->match(
passName)) {}
/// The user has passed us an instruction that for some reason has a source loc
/// that can not be used. Search down the current block for an instruction with
/// a valid source loc and use that instead.
static SourceLoc inferOptRemarkSearchForwards(SILInstruction &i) {
for (auto &inst :
llvm::make_range(std::next(i.getIterator()), i.getParent()->end())) {
auto newLoc = inst.getLoc().getSourceLoc();
if (newLoc.isValid())
return newLoc;
}
return SourceLoc();
}
/// The user has passed us an instruction that for some reason has a source loc
/// that can not be used. Search up the current block for an instruction with
/// a valid SILLocation and use the end SourceLoc of the SourceRange for the
/// instruction.
static SourceLoc inferOptRemarkSearchBackwards(SILInstruction &i) {
for (auto &inst : llvm::make_range(std::next(i.getReverseIterator()),
i.getParent()->rend())) {
auto loc = inst.getLoc();
if (!bool(loc))
continue;
auto range = loc.getSourceRange();
if (range.isValid())
return range.End;
}
return SourceLoc();
}
SourceLoc swift::OptRemark::inferOptRemarkSourceLoc(
SILInstruction &i, SourceLocInferenceBehavior inferBehavior) {
auto loc = i.getLoc().getSourceLoc();
// Do a quick check if we already have a valid loc. In such a case, just
// return. Otherwise, we try to infer using one of our heuristics below.
if (loc.isValid())
return loc;
// Otherwise, try to handle the individual behavior cases, returning loc at
// the end of each case (its invalid, so it will get ignored). If loc is not
// returned, we hit an assert at the end to make it easy to identify a case
// was missed.
switch (inferBehavior) {
case SourceLocInferenceBehavior::None:
return loc;
case SourceLocInferenceBehavior::ForwardScanOnly: {
SourceLoc newLoc = inferOptRemarkSearchForwards(i);
if (newLoc.isValid())
return newLoc;
return loc;
}
case SourceLocInferenceBehavior::BackwardScanOnly: {
SourceLoc newLoc = inferOptRemarkSearchBackwards(i);
if (newLoc.isValid())
return newLoc;
return loc;
}
case SourceLocInferenceBehavior::ForwardThenBackward: {
SourceLoc newLoc = inferOptRemarkSearchForwards(i);
if (newLoc.isValid())
return newLoc;
newLoc = inferOptRemarkSearchBackwards(i);
if (newLoc.isValid())
return newLoc;
return loc;
}
case SourceLocInferenceBehavior::BackwardThenForward: {
SourceLoc newLoc = inferOptRemarkSearchBackwards(i);
if (newLoc.isValid())
return newLoc;
newLoc = inferOptRemarkSearchForwards(i);
if (newLoc.isValid())
return newLoc;
return loc;
}
}
llvm_unreachable("Covered switch isn't covered?!");
}
template <typename RemarkT, typename... ArgTypes>
static void emitRemark(SILFunction &fn, const Remark<RemarkT> &remark,
Diag<ArgTypes...> id, bool diagEnabled) {
if (remark.getLocation().isInvalid())
return;
auto &module = fn.getModule();
if (auto *remarkStreamer = module.getSILRemarkStreamer())
remarkStreamer->emit(remark);
// If diagnostics are enabled, first emit the main diagnostic and then loop
// through our arguments and allow the arguments to add additional diagnostics
// if they want.
if (!diagEnabled)
return;
auto &de = module.getASTContext().Diags;
de.diagnoseWithNotes(
de.diagnose(remark.getLocation(), id, remark.getMsg()), [&]() {
for (auto &arg : remark.getArgs()) {
switch (arg.key.kind) {
case ArgumentKeyKind::Default:
continue;
case ArgumentKeyKind::Note:
de.diagnose(arg.loc, diag::opt_remark_note, arg.val);
continue;
case ArgumentKeyKind::ParentLocNote:
de.diagnose(remark.getLocation(), diag::opt_remark_note, arg.val);
continue;
}
llvm_unreachable("Unhandled case?!");
}
});
}
void Emitter::emit(const RemarkPassed &remark) {
emitRemark(fn, remark, diag::opt_remark_passed, isEnabled<RemarkPassed>());
}
void Emitter::emit(const RemarkMissed &remark) {
emitRemark(fn, remark, diag::opt_remark_missed, isEnabled<RemarkMissed>());
}
void Emitter::emitDebug(const RemarkPassed &remark) {
llvm::dbgs() << remark.getDebugMsg();
}
void Emitter::emitDebug(const RemarkMissed &remark) {
llvm::dbgs() << remark.getDebugMsg();
}