Skip to content

Commit 16f203a

Browse files
committed
[swift-parse-test] Tweak for review
* 'iteration' -> 'iterations' * Comment to clarify cpu time vs wall clock time * Return 'Error' from the executors
1 parent c560267 commit 16f203a

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

lib/DriverTool/swift_parse_test_main.cpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Bridging/ASTGen.h"
2121
#include "swift/Parse/Parser.h"
2222
#include "llvm/Support/CommandLine.h"
23+
#include "llvm/Support/Error.h"
2324

2425
#include <chrono>
2526
#include <ctime>
@@ -46,7 +47,7 @@ struct SwiftParseTestOptions {
4647
clEnumValN(Executor::SwiftParser, "swift-parser", "SwiftParser"),
4748
clEnumValN(Executor::LibParse, "lib-parse", "libParse")));
4849

49-
llvm::cl::opt<unsigned int> Iteration = llvm::cl::opt<unsigned int>(
50+
llvm::cl::opt<unsigned int> Iterations = llvm::cl::opt<unsigned int>(
5051
"n", llvm::cl::desc("iteration"), llvm::cl::init(1));
5152

5253
llvm::cl::opt<bool> SkipBodies = llvm::cl::opt<bool>(
@@ -60,7 +61,8 @@ struct SwiftParseTestOptions {
6061
struct LibParseExecutor {
6162
constexpr static StringRef name = "libParse";
6263

63-
static void performParse(llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
64+
static llvm::Error performParse(llvm::MemoryBufferRef buffer,
65+
ExecuteOptions opts) {
6466
SourceManager SM;
6567
unsigned bufferID =
6668
SM.addNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(buffer));
@@ -87,19 +89,26 @@ struct LibParseExecutor {
8789
Parser parser(bufferID, *SF, /*SILParserState=*/nullptr);
8890
SmallVector<ASTNode> items;
8991
parser.parseTopLevelItems(items);
92+
93+
return llvm::Error::success();
9094
}
9195
};
9296

9397
struct SwiftParserExecutor {
9498
constexpr static StringRef name = "SwiftParser";
9599

96-
static void performParse(llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
100+
static llvm::Error performParse(llvm::MemoryBufferRef buffer,
101+
ExecuteOptions opts) {
97102
#if SWIFT_BUILD_SWIFT_SYNTAX
98103
// TODO: Implement 'ExecuteOptionFlag::SkipBodies'
99104
auto sourceFile = swift_ASTGen_parseSourceFile(
100105
buffer.getBufferStart(), buffer.getBufferSize(), /*moduleName=*/"",
101106
buffer.getBufferIdentifier().data(), /*ASTContext=*/nullptr);
102107
swift_ASTGen_destroySourceFile(sourceFile);
108+
return llvm::Error::success();
109+
#else
110+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
111+
"SwiftParser is not supported")
103112
#endif
104113
}
105114
};
@@ -131,7 +140,8 @@ loadSources(ArrayRef<std::string> filePaths,
131140
}
132141
}
133142

134-
/// Measure the duration of \p body execution.
143+
/// Measure the duration of \p body execution. Returns a pair of "wall clock
144+
/// time" and "CPU time".
135145
template <typename Duration>
136146
static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
137147
auto cStart = std::clock();
@@ -154,7 +164,7 @@ static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
154164
/// Parse all \p buffers using \c Executor , \p iteration times, and print out
155165
/// the measurement to the \c stdout.
156166
template <typename Executor>
157-
static void
167+
static llvm::Error
158168
perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
159169
ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
160170
uintmax_t totalLines) {
@@ -163,13 +173,19 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
163173
llvm::outs() << "parser: " << Executor::name << "\n";
164174

165175
using duration_t = std::chrono::nanoseconds;
176+
// Wall clock time.
166177
auto tDuration = duration_t::zero();
178+
// CPU time.
167179
auto cDuration = duration_t::zero();
168180

181+
llvm::Error err = llvm::Error::success();
169182
for (unsigned i = 0; i < iteration; i++) {
170183
for (auto &buffer : buffers) {
171-
std::pair<duration_t, duration_t> elapsed = measure<duration_t>(
172-
[&] { Executor::performParse(buffer->getMemBufferRef(), opts); });
184+
std::pair<duration_t, duration_t> elapsed = measure<duration_t>([&] {
185+
err = Executor::performParse(buffer->getMemBufferRef(), opts);
186+
});
187+
if (err)
188+
return err;
173189
tDuration += elapsed.first;
174190
cDuration += elapsed.second;
175191
}
@@ -183,12 +199,15 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
183199
<< llvm::format("cpu time (ms): %8d\n", cDisplay);
184200

185201
if (cDuration.count() > 0) {
202+
// Throughputs are based on CPU time.
186203
auto byteTPS = totalBytes * duration_t::period::den / cDuration.count();
187204
auto lineTPS = totalLines * duration_t::period::den / cDuration.count();
188205

189206
llvm::outs() << llvm::format("throughput (byte/s): %8d\n", byteTPS)
190207
<< llvm::format("throughput (line/s): %8d\n", lineTPS);
191208
}
209+
210+
return llvm::Error::success();
192211
}
193212

194213
} // namespace
@@ -199,7 +218,7 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
199218
llvm::cl::ParseCommandLineOptions(args.size(), args.data(),
200219
"Swift parse test\n");
201220

202-
unsigned iteration = options.Iteration;
221+
unsigned iterations = options.Iterations;
203222
ExecuteOptions execOptions;
204223
if (options.SkipBodies)
205224
execOptions |= ExecuteOptionFlag::SkipBodies;
@@ -216,23 +235,29 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
216235
llvm::outs() << llvm::format("file count: %8d\n", buffers.size())
217236
<< llvm::format("total bytes: %8d\n", byteCount)
218237
<< llvm::format("total lines: %8d\n", lineCount)
219-
<< llvm::format("iterations: %8d\n", iteration);
238+
<< llvm::format("iterations: %8d\n", iterations);
239+
240+
llvm::Error err = llvm::Error::success();
220241
for (auto mode : options.Executors) {
221242
switch (mode) {
222243
case Executor::SwiftParser:
223-
#if SWIFT_BUILD_SWIFT_SYNTAX
224-
perform<SwiftParserExecutor>(buffers, execOptions, iteration, byteCount,
225-
lineCount);
244+
err = perform<SwiftParserExecutor>(buffers, execOptions, iterations,
245+
byteCount, lineCount);
226246
break;
227-
#else
228-
llvm::errs() << "error: SwiftParser is not enabled\n";
229-
return 1;
230-
#endif
231247
case Executor::LibParse:
232-
perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount,
233-
lineCount);
248+
err = perform<LibParseExecutor>(buffers, execOptions, iterations,
249+
byteCount, lineCount);
234250
break;
235251
}
252+
if (err)
253+
break;
254+
}
255+
256+
if (err) {
257+
llvm::handleAllErrors(std::move(err), [](llvm::ErrorInfoBase &info) {
258+
llvm::errs() << "error: " << info.message() << "\n";
259+
});
260+
return 1;
236261
}
237262

238263
return 0;

0 commit comments

Comments
 (0)