forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.cpp
356 lines (300 loc) · 10.7 KB
/
Evaluator.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//===--- Evaluator.cpp - Request Evaluator Implementation -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the Evaluator class that evaluates and caches
// requests.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/Evaluator.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/SourceManager.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
using namespace swift;
AnyRequest::HolderBase::~HolderBase() { }
std::string AnyRequest::getAsString() const {
std::string result;
{
llvm::raw_string_ostream out(result);
simple_display(out, *this);
}
return result;
}
AbstractRequestFunction *
Evaluator::getAbstractRequestFunction(uint8_t zoneID, uint8_t requestID) const {
for (const auto &zone : requestFunctionsByZone) {
if (zone.first == zoneID) {
if (requestID < zone.second.size())
return zone.second[requestID];
return nullptr;
}
}
return nullptr;
}
void Evaluator::registerRequestFunctions(
uint8_t zoneID,
ArrayRef<AbstractRequestFunction *> functions) {
#ifndef NDEBUG
for (const auto &zone : requestFunctionsByZone) {
assert(zone.first != zoneID);
}
#endif
requestFunctionsByZone.push_back({zoneID, functions});
}
Evaluator::Evaluator(DiagnosticEngine &diags, bool debugDumpCycles)
: diags(diags), debugDumpCycles(debugDumpCycles) { }
void Evaluator::emitRequestEvaluatorGraphViz(llvm::StringRef graphVizPath) {
std::error_code error;
llvm::raw_fd_ostream out(graphVizPath, error, llvm::sys::fs::F_Text);
printDependenciesGraphviz(out);
}
bool Evaluator::checkDependency(const AnyRequest &request) {
// If there is an active request, record it's dependency on this request.
if (!activeRequests.empty())
dependencies[activeRequests.back()].push_back(request);
// Record this as an active request.
if (activeRequests.insert(request)) {
return false;
}
// Diagnose cycle.
diagnoseCycle(request);
if (debugDumpCycles) {
llvm::errs() << "===CYCLE DETECTED===\n";
llvm::DenseSet<AnyRequest> visitedAnywhere;
llvm::SmallVector<AnyRequest, 4> visitedAlongPath;
std::string prefixStr;
printDependencies(activeRequests.front(), llvm::errs(), visitedAnywhere,
visitedAlongPath, activeRequests.getArrayRef(),
prefixStr, /*lastChild=*/true);
}
return true;
}
void Evaluator::diagnoseCycle(const AnyRequest &request) {
request.diagnoseCycle(diags);
for (const auto &step : reversed(activeRequests)) {
if (step == request) return;
step.noteCycleStep(diags);
}
llvm_unreachable("Diagnosed a cycle but it wasn't represented in the stack");
}
void Evaluator::printDependencies(
const AnyRequest &request,
llvm::raw_ostream &out,
llvm::DenseSet<AnyRequest> &visitedAnywhere,
llvm::SmallVectorImpl<AnyRequest> &visitedAlongPath,
llvm::ArrayRef<AnyRequest> highlightPath,
std::string &prefixStr,
bool lastChild) const {
out << prefixStr << " `--";
// Determine whether this node should be highlighted.
bool isHighlighted = false;
if (std::find(highlightPath.begin(), highlightPath.end(), request)
!= highlightPath.end()) {
isHighlighted = true;
out.changeColor(llvm::buffer_ostream::Colors::GREEN);
}
// Print this node.
simple_display(out, request);
// Turn off the highlight.
if (isHighlighted) {
out.resetColor();
}
// Print the cached value, if known.
auto cachedValue = cache.find(request);
if (cachedValue != cache.end()) {
out << " -> ";
printEscapedString(cachedValue->second.getAsString(), out);
}
if (!visitedAnywhere.insert(request).second) {
// We've already seed this node. Check whether it's part of a cycle.
if (std::find(visitedAlongPath.begin(), visitedAlongPath.end(), request)
!= visitedAlongPath.end()) {
// We have a cyclic dependency.
out.changeColor(llvm::raw_ostream::RED);
out << " (cyclic dependency)\n";
} else {
// We have seen this node before, but it's not a cycle. Elide its
// children.
out << " (elided)\n";
}
out.resetColor();
} else if (dependencies.count(request) == 0) {
// We have not seen this node before, so we don't know its dependencies.
out.changeColor(llvm::raw_ostream::GREEN);
out << " (dependency not evaluated)\n";
out.resetColor();
// Remove from the visited set.
visitedAnywhere.erase(request);
} else {
// Print children.
out << "\n";
// Setup the prefix to print the children.
prefixStr += ' ';
prefixStr += (lastChild ? ' ' : '|');
prefixStr += " ";
// Note that this request is along the path.
visitedAlongPath.push_back(request);
// Print the children.
auto &dependsOn = dependencies.find(request)->second;
for (unsigned i : indices(dependsOn)) {
printDependencies(dependsOn[i], out, visitedAnywhere, visitedAlongPath,
highlightPath, prefixStr, i == dependsOn.size()-1);
}
// Drop our changes to the prefix.
prefixStr.erase(prefixStr.end() - 4, prefixStr.end());
// Remove from the visited set and path.
visitedAnywhere.erase(request);
assert(visitedAlongPath.back() == request);
visitedAlongPath.pop_back();
}
}
void Evaluator::dumpDependencies(const AnyRequest &request) const {
printDependencies(request, llvm::dbgs());
}
void Evaluator::printDependenciesGraphviz(llvm::raw_ostream &out) const {
// Form a list of all of the requests we know about.
std::vector<AnyRequest> allRequests;
for (const auto &knownRequest : dependencies) {
allRequests.push_back(knownRequest.first);
}
// Sort the list of requests based on the display strings, so we get
// deterministic output.
auto getDisplayString = [&](const AnyRequest &request) {
std::string result;
{
llvm::raw_string_ostream out(result);
simple_display(out, request);
}
return result;
};
std::sort(allRequests.begin(), allRequests.end(),
[&](const AnyRequest &lhs, const AnyRequest &rhs) {
return getDisplayString(lhs) < getDisplayString(rhs);
});
// Manage request IDs to use in the resulting output graph.
llvm::DenseMap<AnyRequest, unsigned> requestIDs;
unsigned nextID = 0;
// Prepopulate the known requests.
for (const auto &request : allRequests) {
requestIDs[request] = nextID++;
}
auto getRequestID = [&](const AnyRequest &request) {
auto known = requestIDs.find(request);
if (known != requestIDs.end()) return known->second;
// We discovered a new request; record it's ID and add it to the list of
// all requests.
allRequests.push_back(request);
requestIDs[request] = nextID;
return nextID++;
};
auto getNodeName = [&](const AnyRequest &request) {
std::string result;
{
llvm::raw_string_ostream out(result);
out << "request_" << getRequestID(request);
}
return result;
};
// Emit the graph header.
out << "digraph Dependencies {\n";
// Emit the edges.
llvm::DenseMap<AnyRequest, unsigned> inDegree;
for (const auto &source : allRequests) {
auto known = dependencies.find(source);
assert(known != dependencies.end());
for (const auto &target : known->second) {
out << " " << getNodeName(source) << " -> " << getNodeName(target)
<< ";\n";
++inDegree[target];
}
}
out << "\n";
static const char *colorNames[] = {
"aquamarine",
"blueviolet",
"brown",
"burlywood",
"cadetblue",
"chartreuse",
"chocolate",
"coral",
"cornflowerblue",
"crimson"
};
const unsigned numColorNames = sizeof(colorNames) / sizeof(const char *);
llvm::DenseMap<unsigned, unsigned> knownBuffers;
auto getColor = [&](const AnyRequest &request) -> Optional<const char *> {
SourceLoc loc = request.getNearestLoc();
if (loc.isInvalid())
return None;
unsigned bufferID = diags.SourceMgr.findBufferContainingLoc(loc);
auto knownId = knownBuffers.find(bufferID);
if (knownId == knownBuffers.end()) {
knownId = knownBuffers.insert({bufferID, knownBuffers.size()}).first;
}
return colorNames[knownId->second % numColorNames];
};
// Emit the nodes.
for (unsigned i : indices(allRequests)) {
const auto &request = allRequests[i];
out << " " << getNodeName(request);
out << " [label=\"";
printEscapedString(request.getAsString(), out);
auto cachedValue = cache.find(request);
if (cachedValue != cache.end()) {
out << " -> ";
printEscapedString(cachedValue->second.getAsString(), out);
}
out << "\"";
if (auto color = getColor(request)) {
out << ", fillcolor=\"" << *color << "\"";
}
out << "];\n";
}
// Emit "fake" nodes for each of the source buffers we encountered, so
// we know which file we're working from.
// FIXME: This approximates a "top level" request for, e.g., type checking
// an entire source file.
std::vector<unsigned> sourceBufferIDs;
for (const auto &element : knownBuffers) {
sourceBufferIDs.push_back(element.first);
}
std::sort(sourceBufferIDs.begin(), sourceBufferIDs.end());
for (unsigned bufferID : sourceBufferIDs) {
out << " buffer_" << bufferID << "[label=\"";
printEscapedString(diags.SourceMgr.getIdentifierForBuffer(bufferID), out);
out << "\"";
out << ", shape=\"box\"";
out << ", fillcolor=\""
<< colorNames[knownBuffers[bufferID] % numColorNames] << "\"";
out << "];\n";
}
// Emit "false" dependencies from source buffer IDs to any requests that (1)
// have no other incomining edges and (2) can be associated with a source
// buffer.
for (const auto &request : allRequests) {
if (inDegree[request] > 0)
continue;
SourceLoc loc = request.getNearestLoc();
if (loc.isInvalid())
continue;
unsigned bufferID = diags.SourceMgr.findBufferContainingLoc(loc);
out << " buffer_" << bufferID << " -> " << getNodeName(request) << ";\n";
}
// Done!
out << "}\n";
}
void Evaluator::dumpDependenciesGraphviz() const {
printDependenciesGraphviz(llvm::dbgs());
}