Skip to content

Commit 9984701

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 a8d43c7 commit 9984701

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();
@@ -152,7 +162,7 @@ static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
152162
/// Parse all \p buffers using \c Executor , \p iteration times, and print out
153163
/// the measurement to the \c stdout.
154164
template <typename Executor>
155-
static void
165+
static llvm::Error
156166
perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
157167
ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
158168
uintmax_t totalLines) {
@@ -161,13 +171,19 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
161171
llvm::outs() << "parser: " << Executor::name << "\n";
162172

163173
using duration_t = std::chrono::nanoseconds;
174+
// Wall clock time.
164175
auto tDuration = duration_t::zero();
176+
// CPU time.
165177
auto cDuration = duration_t::zero();
166178

179+
llvm::Error err = llvm::Error::success();
167180
for (unsigned i = 0; i < iteration; i++) {
168181
for (auto &buffer : buffers) {
169-
std::pair<duration_t, duration_t> elapsed = measure<duration_t>(
170-
[&] { Executor::performParse(buffer->getMemBufferRef(), opts); });
182+
std::pair<duration_t, duration_t> elapsed = measure<duration_t>([&] {
183+
err = Executor::performParse(buffer->getMemBufferRef(), opts);
184+
});
185+
if (err)
186+
return err;
171187
tDuration += elapsed.first;
172188
cDuration += elapsed.second;
173189
}
@@ -178,13 +194,16 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
178194
auto cDisplay =
179195
std::chrono::duration_cast<std::chrono::milliseconds>(cDuration).count();
180196

197+
// Throughputs are based on CPU time.
181198
auto byteTPS = totalBytes * duration_t::period::den / cDuration.count();
182199
auto lineTPS = totalLines * duration_t::period::den / cDuration.count();
183200

184201
llvm::outs() << llvm::format("wall clock time (ms): %8d\n", tDisplay)
185202
<< llvm::format("cpu time (ms): %8d\n", cDisplay)
186203
<< llvm::format("throughput (byte/s): %8d\n", byteTPS)
187204
<< llvm::format("throughput (line/s): %8d\n", lineTPS);
205+
206+
return llvm::Error::success();
188207
}
189208

190209
} // namespace
@@ -195,7 +214,7 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
195214
llvm::cl::ParseCommandLineOptions(args.size(), args.data(),
196215
"Swift parse test\n");
197216

198-
unsigned iteration = options.Iteration;
217+
unsigned iterations = options.Iterations;
199218
ExecuteOptions execOptions;
200219
if (options.SkipBodies)
201220
execOptions |= ExecuteOptionFlag::SkipBodies;
@@ -212,23 +231,29 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
212231
llvm::outs() << llvm::format("file count: %8d\n", buffers.size())
213232
<< llvm::format("total bytes: %8d\n", byteCount)
214233
<< llvm::format("total lines: %8d\n", lineCount)
215-
<< llvm::format("iterations: %8d\n", iteration);
234+
<< llvm::format("iterations: %8d\n", iterations);
235+
236+
llvm::Error err = llvm::Error::success();
216237
for (auto mode : options.Executors) {
217238
switch (mode) {
218239
case Executor::SwiftParser:
219-
#if SWIFT_BUILD_SWIFT_SYNTAX
220-
perform<SwiftParserExecutor>(buffers, execOptions, iteration, byteCount,
221-
lineCount);
240+
err = perform<SwiftParserExecutor>(buffers, execOptions, iterations,
241+
byteCount, lineCount);
222242
break;
223-
#else
224-
llvm::errs() << "error: SwiftParser is not enabled\n";
225-
return 1;
226-
#endif
227243
case Executor::LibParse:
228-
perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount,
229-
lineCount);
244+
err = perform<LibParseExecutor>(buffers, execOptions, iterations,
245+
byteCount, lineCount);
230246
break;
231247
}
248+
if (err)
249+
break;
250+
}
251+
252+
if (err) {
253+
llvm::handleAllErrors(std::move(err), [](llvm::ErrorInfoBase &info) {
254+
llvm::errs() << "error: " << info.message() << "\n";
255+
});
256+
return 1;
232257
}
233258

234259
return 0;

0 commit comments

Comments
 (0)