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 {
6061struct 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
9397struct 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".
135145template <typename Duration>
136146static 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.
156166template <typename Executor>
157- static void
167+ static llvm::Error
158168perform (const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
159169 ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
160170 uintmax_t totalLines) {
@@ -163,13 +173,21 @@ 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 ();
182+ (void )bool (err);
183+
169184 for (unsigned i = 0 ; i < iteration; i++) {
170185 for (auto &buffer : buffers) {
171- std::pair<duration_t , duration_t > elapsed = measure<duration_t >(
172- [&] { Executor::performParse (buffer->getMemBufferRef (), opts); });
186+ std::pair<duration_t , duration_t > elapsed = measure<duration_t >([&] {
187+ err = Executor::performParse (buffer->getMemBufferRef (), opts);
188+ });
189+ if (err)
190+ return err;
173191 tDuration += elapsed.first ;
174192 cDuration += elapsed.second ;
175193 }
@@ -183,12 +201,15 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
183201 << llvm::format (" cpu time (ms): %8d\n " , cDisplay);
184202
185203 if (cDuration.count () > 0 ) {
204+ // Throughputs are based on CPU time.
186205 auto byteTPS = totalBytes * duration_t ::period::den / cDuration.count ();
187206 auto lineTPS = totalLines * duration_t ::period::den / cDuration.count ();
188207
189208 llvm::outs () << llvm::format (" throughput (byte/s): %8d\n " , byteTPS)
190209 << llvm::format (" throughput (line/s): %8d\n " , lineTPS);
191210 }
211+
212+ return llvm::Error::success ();
192213}
193214
194215} // namespace
@@ -199,7 +220,7 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
199220 llvm::cl::ParseCommandLineOptions (args.size (), args.data (),
200221 " Swift parse test\n " );
201222
202- unsigned iteration = options.Iteration ;
223+ unsigned iterations = options.Iterations ;
203224 ExecuteOptions execOptions;
204225 if (options.SkipBodies )
205226 execOptions |= ExecuteOptionFlag::SkipBodies;
@@ -216,23 +237,31 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
216237 llvm::outs () << llvm::format (" file count: %8d\n " , buffers.size ())
217238 << llvm::format (" total bytes: %8d\n " , byteCount)
218239 << llvm::format (" total lines: %8d\n " , lineCount)
219- << llvm::format (" iterations: %8d\n " , iteration);
240+ << llvm::format (" iterations: %8d\n " , iterations);
241+
242+ llvm::Error err = llvm::Error::success ();
243+ (void )bool (err);
244+
220245 for (auto mode : options.Executors ) {
221246 switch (mode) {
222247 case Executor::SwiftParser:
223- #if SWIFT_BUILD_SWIFT_SYNTAX
224- perform<SwiftParserExecutor>(buffers, execOptions, iteration, byteCount,
225- lineCount);
248+ err = perform<SwiftParserExecutor>(buffers, execOptions, iterations,
249+ byteCount, lineCount);
226250 break ;
227- #else
228- llvm::errs () << " error: SwiftParser is not enabled\n " ;
229- return 1 ;
230- #endif
231251 case Executor::LibParse:
232- perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount ,
233- lineCount);
252+ err = perform<LibParseExecutor>(buffers, execOptions, iterations ,
253+ byteCount, lineCount);
234254 break ;
235255 }
256+ if (err)
257+ break ;
258+ }
259+
260+ if (err) {
261+ llvm::handleAllErrors (std::move (err), [](llvm::ErrorInfoBase &info) {
262+ llvm::errs () << " error: " << info.message () << " \n " ;
263+ });
264+ return 1 ;
236265 }
237266
238267 return 0 ;
0 commit comments