-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDriverIncrementalRanges.cpp
294 lines (255 loc) · 11.5 KB
/
DriverIncrementalRanges.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
//===------ DriverIncrementalRanges.cpp ------------------------------------==//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/Driver/DriverIncrementalRanges.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/SourceComparator.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <string>
#include <unordered_map>
// These are the definitions for managing serializable source locations so that
// the driver can implement incremental compilation based on
// source ranges.
using namespace swift;
using namespace incremental_ranges;
using namespace driver;
//==============================================================================
// MARK: SourceRangeBasedInfo - constructing
//==============================================================================
SourceRangeBasedInfo::SourceRangeBasedInfo(
StringRef primaryInputPath,
SwiftRangesFileContents &&swiftRangesFileContents,
SourceComparator::LRRanges &&changedRanges, Ranges &&nonlocalChangedRanges)
: primaryInputPath(primaryInputPath),
swiftRangesFileContents(std::move(swiftRangesFileContents)),
changedRanges(std::move(changedRanges)),
nonlocalChangedRanges(std::move(nonlocalChangedRanges)) {
assert(!primaryInputPath.empty() && "Must be a compile job");
}
SourceRangeBasedInfo::SourceRangeBasedInfo(SourceRangeBasedInfo &&x)
: primaryInputPath(x.primaryInputPath),
swiftRangesFileContents(std::move(x.swiftRangesFileContents)),
changedRanges(std::move(x.changedRanges)),
nonlocalChangedRanges(std::move(x.nonlocalChangedRanges)) {}
//==============================================================================
// MARK: loading
//==============================================================================
Optional<SourceRangeBasedInfo> SourceRangeBasedInfo::loadInfoForOneJob(
const Job *cmd, const bool showIncrementalBuildDecisions,
DiagnosticEngine &diags) {
StringRef primaryInputPath = cmd->getFirstSwiftPrimaryInput();
if (primaryInputPath.empty())
return None;
const StringRef compiledSourcePath =
cmd->getOutput().getAdditionalOutputForType(
file_types::TY_CompiledSource);
const StringRef swiftRangesPath =
cmd->getOutput().getAdditionalOutputForType(file_types::TY_SwiftRanges);
return loadInfoForOnePrimary(primaryInputPath, compiledSourcePath,
swiftRangesPath, showIncrementalBuildDecisions,
diags);
}
Optional<SourceRangeBasedInfo> SourceRangeBasedInfo::loadInfoForOnePrimary(
StringRef primaryInputPath, StringRef compiledSourcePath,
StringRef swiftRangesPath, bool showIncrementalBuildDecisions,
DiagnosticEngine &diags) {
auto removeSupplementaryPaths = [&] {
if (auto ec = llvm::sys::fs::remove(compiledSourcePath)) {
(void)ec;
llvm::errs() << "WARNING could not remove: " << compiledSourcePath;
}
if (auto ec = llvm::sys::fs::remove(swiftRangesPath)) {
(void)ec;
llvm::errs() << "WARNING could not remove: " << swiftRangesPath;
}
};
assert(!primaryInputPath.empty() && "Must have a primary to load info.");
// nonexistant primary -> it was removed since invoking swift?!
if (!llvm::sys::fs::exists(primaryInputPath)) {
if (showIncrementalBuildDecisions)
llvm::outs() << primaryInputPath << " was removed.";
// so they won't be used if primary gets re-added
removeSupplementaryPaths();
return None;
}
auto swiftRangesFileContents = loadSwiftRangesFileContents(
swiftRangesPath, primaryInputPath, showIncrementalBuildDecisions, diags);
auto changedRanges = loadChangedRanges(compiledSourcePath, primaryInputPath,
showIncrementalBuildDecisions, diags);
if (!swiftRangesFileContents || !changedRanges) {
removeSupplementaryPaths();
return None;
}
Ranges nonlocalChangedRanges = computeNonlocalChangedRanges(
swiftRangesFileContents.getValue(), changedRanges.getValue());
return SourceRangeBasedInfo(
primaryInputPath, std::move(swiftRangesFileContents.getValue()),
std::move(changedRanges.getValue()), std::move(nonlocalChangedRanges));
}
Optional<SwiftRangesFileContents>
SourceRangeBasedInfo::loadSwiftRangesFileContents(
const StringRef swiftRangesPath, const StringRef primaryInputPath,
const bool showIncrementalBuildDecisions, DiagnosticEngine &diags) {
auto bufferOrError = llvm::MemoryBuffer::getFile(swiftRangesPath);
if (auto ec = bufferOrError.getError()) {
diags.diagnose(SourceLoc(), diag::warn_unable_to_load_swift_ranges,
swiftRangesPath, ec.message());
return None;
}
return SwiftRangesFileContents::load(primaryInputPath, *bufferOrError->get(),
showIncrementalBuildDecisions, diags);
}
Optional<SourceComparator::LRRanges> SourceRangeBasedInfo::loadChangedRanges(
const StringRef compiledSourcePath, const StringRef primaryInputPath,
const bool showIncrementalBuildDecisions, DiagnosticEngine &diags) {
// Shortcut the diff if the saved source is newer than the actual source.
auto isPreviouslyCompiledNewer =
isFileNewerThan(compiledSourcePath, primaryInputPath, diags);
if (!isPreviouslyCompiledNewer)
return None;
if (isPreviouslyCompiledNewer.getValue())
return SourceComparator::LRRanges(); // no changes
auto whatWasPreviouslyCompiled =
llvm::MemoryBuffer::getFile(compiledSourcePath);
if (auto ec = whatWasPreviouslyCompiled.getError()) {
diags.diagnose(SourceLoc(), diag::warn_unable_to_load_compiled_swift,
compiledSourcePath, ec.message());
return None;
}
auto whatIsAboutToBeCompiled = llvm::MemoryBuffer::getFile(primaryInputPath);
if (auto ec = whatIsAboutToBeCompiled.getError()) {
diags.diagnose(SourceLoc(), diag::warn_unable_to_load_primary,
primaryInputPath, ec.message());
return None;
}
// SourceComparator::test();
auto comp = SourceComparator(whatWasPreviouslyCompiled->get()->getBuffer(),
whatIsAboutToBeCompiled->get()->getBuffer());
comp.compare();
// lhs in terms of old version
return comp.convertAllMismatches();
}
/// Return true if lhs is newer than rhs, or None for error.
Optional<bool> SourceRangeBasedInfo::isFileNewerThan(StringRef lhs,
StringRef rhs,
DiagnosticEngine &diags) {
auto getModTime = [&](StringRef path) -> Optional<llvm::sys::TimePoint<>> {
llvm::sys::fs::file_status status;
if (auto statError = llvm::sys::fs::status(path, status)) {
diags.diagnose(SourceLoc(), diag::warn_cannot_stat_input,
llvm::sys::path::filename(path), statError.message());
return None;
}
return status.getLastModificationTime();
};
const auto lhsModTime = getModTime(lhs);
const auto rhsModTime = getModTime(rhs);
return !lhsModTime || !rhsModTime
? None
: Optional<bool>(lhsModTime.getValue() > rhsModTime.getValue());
}
Optional<SwiftRangesFileContents> SwiftRangesFileContents::load(
const StringRef primaryPath, const llvm::MemoryBuffer &swiftRangesBuffer,
const bool showIncrementalBuildDecisions, DiagnosticEngine &diags) {
if (!swiftRangesBuffer.getBuffer().startswith(header)) {
diags.diagnose(SourceLoc(), diag::warn_bad_swift_ranges_header,
swiftRangesBuffer.getBufferIdentifier());
return None;
}
llvm::yaml::Input yamlReader(llvm::MemoryBufferRef(swiftRangesBuffer),
nullptr);
SwiftRangesFileContents contents;
yamlReader >> contents;
if (yamlReader.error()) {
diags.diagnose(SourceLoc(), diag::warn_bad_swift_ranges_format,
swiftRangesBuffer.getBufferIdentifier(),
yamlReader.error().message());
return None;
}
return contents;
}
Ranges SourceRangeBasedInfo::computeNonlocalChangedRanges(
const SwiftRangesFileContents &swiftRangesFileContents,
const SourceComparator::LRRanges &changedRanges) {
return SerializableSourceRange::findAllOutliers(
changedRanges.lhs(), swiftRangesFileContents.noninlinableFunctionBodies);
}
//==============================================================================
// MARK: scheduling
//==============================================================================
static std::string rangeStrings(std::string prefix, const Ranges &ranges) {
std::string s = prefix;
interleave(ranges.begin(), ranges.end(),
[&](const SerializableSourceRange &r) { s += r.printString(); },
[&] { s += ", "; });
return s;
}
bool SourceRangeBasedInfo::didInputChangeAtAll(
DiagnosticEngine &,
function_ref<void(bool, StringRef)> noteBuilding) const {
const auto &changesToOldSource = changedRanges.lhs();
if (changesToOldSource.empty())
noteBuilding(/*willBeBuilding=*/false, "Did not change at all");
else
noteBuilding(/*willBeBuilding=*/true,
rangeStrings("changed at ", changesToOldSource));
return !changesToOldSource.empty();
}
bool SourceRangeBasedInfo::didInputChangeNonlocally(
DiagnosticEngine &,
function_ref<void(bool, StringRef)> noteInitiallyCascading) const {
if (nonlocalChangedRanges.empty())
noteInitiallyCascading(false, "did not change outside any function bodies");
else
noteInitiallyCascading(true,
rangeStrings("changed outside a function body at: ",
nonlocalChangedRanges));
return !nonlocalChangedRanges.empty();
}
//==============================================================================
// MARK: SourceRangeBasedInfo - printing
//==============================================================================
void SourceRangeBasedInfo::dump(const bool dumpCompiledSourceDiffs,
const bool dumpSwiftRanges) const {
if (!dumpSwiftRanges && !dumpCompiledSourceDiffs)
return;
if (dumpSwiftRanges)
swiftRangesFileContents.dump(primaryInputPath);
if (dumpCompiledSourceDiffs)
dumpChangedRanges();
}
void SourceRangeBasedInfo::dumpChangedRanges() const {
const auto primaryFilename = llvm::sys::path::filename(primaryInputPath);
auto dumpRangeSet = [&](StringRef which, StringRef wrt,
const Ranges &ranges) {
llvm::errs() << "*** " << which << " changed ranges in '" << primaryFilename
<< "' (w.r.t " << wrt << ") ***\n";
for (const auto &r : ranges)
llvm::errs() << "- " << r.printString() << "\n";
};
if (changedRanges.empty()) {
assert(nonlocalChangedRanges.empty() && "A fortiori.");
dumpRangeSet("no", "previously- or about-to-be-compiled", {});
return;
}
dumpRangeSet("all", "previously-compiled", changedRanges.lhs());
dumpRangeSet("all", "to-be-compiled", changedRanges.rhs());
dumpRangeSet("nonlocal", "previously-compiled", nonlocalChangedRanges);
llvm::errs() << "\n";
}