20
20
#include " swift/Bridging/ASTGen.h"
21
21
#include " swift/Parse/Parser.h"
22
22
#include " llvm/Support/CommandLine.h"
23
+ #include " llvm/Support/Error.h"
23
24
24
25
#include < chrono>
25
26
#include < ctime>
@@ -46,7 +47,7 @@ struct SwiftParseTestOptions {
46
47
clEnumValN (Executor::SwiftParser, " swift-parser" , " SwiftParser" ),
47
48
clEnumValN(Executor::LibParse, " lib-parse" , " libParse" )));
48
49
49
- llvm::cl::opt<unsigned int > Iteration = llvm::cl::opt<unsigned int >(
50
+ llvm::cl::opt<unsigned int > Iterations = llvm::cl::opt<unsigned int >(
50
51
" n" , llvm::cl::desc(" iteration" ), llvm::cl::init(1 ));
51
52
52
53
llvm::cl::opt<bool > SkipBodies = llvm::cl::opt<bool >(
@@ -60,7 +61,8 @@ struct SwiftParseTestOptions {
60
61
struct LibParseExecutor {
61
62
constexpr static StringRef name = " libParse" ;
62
63
63
- static void performParse (llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
64
+ static llvm::Error performParse (llvm::MemoryBufferRef buffer,
65
+ ExecuteOptions opts) {
64
66
SourceManager SM;
65
67
unsigned bufferID =
66
68
SM.addNewSourceBuffer (llvm::MemoryBuffer::getMemBuffer (buffer));
@@ -87,19 +89,26 @@ struct LibParseExecutor {
87
89
Parser parser (bufferID, *SF, /* SILParserState=*/ nullptr );
88
90
SmallVector<ASTNode> items;
89
91
parser.parseTopLevelItems (items);
92
+
93
+ return llvm::Error::success ();
90
94
}
91
95
};
92
96
93
97
struct SwiftParserExecutor {
94
98
constexpr static StringRef name = " SwiftParser" ;
95
99
96
- static void performParse (llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
100
+ static llvm::Error performParse (llvm::MemoryBufferRef buffer,
101
+ ExecuteOptions opts) {
97
102
#if SWIFT_BUILD_SWIFT_SYNTAX
98
103
// TODO: Implement 'ExecuteOptionFlag::SkipBodies'
99
104
auto sourceFile = swift_ASTGen_parseSourceFile (
100
105
buffer.getBufferStart (), buffer.getBufferSize (), /* moduleName=*/ " " ,
101
106
buffer.getBufferIdentifier ().data (), /* ASTContext=*/ nullptr );
102
107
swift_ASTGen_destroySourceFile (sourceFile);
108
+ return llvm::Error::success ();
109
+ #else
110
+ return llvm::createStringError (llvm::inconvertibleErrorCode (),
111
+ " SwiftParser is not supported" )
103
112
#endif
104
113
}
105
114
};
@@ -131,7 +140,8 @@ loadSources(ArrayRef<std::string> filePaths,
131
140
}
132
141
}
133
142
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".
135
145
template <typename Duration>
136
146
static std::pair<Duration, Duration> measure (llvm::function_ref<void ()> body) {
137
147
auto cStart = std::clock ();
@@ -154,7 +164,7 @@ static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
154
164
// / Parse all \p buffers using \c Executor , \p iteration times, and print out
155
165
// / the measurement to the \c stdout.
156
166
template <typename Executor>
157
- static void
167
+ static llvm::Error
158
168
perform (const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
159
169
ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
160
170
uintmax_t totalLines) {
@@ -163,13 +173,21 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
163
173
llvm::outs () << " parser: " << Executor::name << " \n " ;
164
174
165
175
using duration_t = std::chrono::nanoseconds;
176
+ // Wall clock time.
166
177
auto tDuration = duration_t::zero ();
178
+ // CPU time.
167
179
auto cDuration = duration_t::zero ();
168
180
181
+ llvm::Error err = llvm::Error::success ();
182
+ (void )bool (err);
183
+
169
184
for (unsigned i = 0 ; i < iteration; i++) {
170
185
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;
173
191
tDuration += elapsed.first ;
174
192
cDuration += elapsed.second ;
175
193
}
@@ -183,12 +201,15 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
183
201
<< llvm::format (" cpu time (ms): %8d\n " , cDisplay);
184
202
185
203
if (cDuration.count () > 0 ) {
204
+ // Throughputs are based on CPU time.
186
205
auto byteTPS = totalBytes * duration_t ::period::den / cDuration.count ();
187
206
auto lineTPS = totalLines * duration_t ::period::den / cDuration.count ();
188
207
189
208
llvm::outs () << llvm::format (" throughput (byte/s): %8d\n " , byteTPS)
190
209
<< llvm::format (" throughput (line/s): %8d\n " , lineTPS);
191
210
}
211
+
212
+ return llvm::Error::success ();
192
213
}
193
214
194
215
} // namespace
@@ -199,7 +220,7 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
199
220
llvm::cl::ParseCommandLineOptions (args.size (), args.data (),
200
221
" Swift parse test\n " );
201
222
202
- unsigned iteration = options.Iteration ;
223
+ unsigned iterations = options.Iterations ;
203
224
ExecuteOptions execOptions;
204
225
if (options.SkipBodies )
205
226
execOptions |= ExecuteOptionFlag::SkipBodies;
@@ -216,23 +237,31 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
216
237
llvm::outs () << llvm::format (" file count: %8d\n " , buffers.size ())
217
238
<< llvm::format (" total bytes: %8d\n " , byteCount)
218
239
<< 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
+
220
245
for (auto mode : options.Executors ) {
221
246
switch (mode) {
222
247
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);
226
250
break ;
227
- #else
228
- llvm::errs () << " error: SwiftParser is not enabled\n " ;
229
- return 1 ;
230
- #endif
231
251
case Executor::LibParse:
232
- perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount ,
233
- lineCount);
252
+ err = perform<LibParseExecutor>(buffers, execOptions, iterations ,
253
+ byteCount, lineCount);
234
254
break ;
235
255
}
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 ;
236
265
}
237
266
238
267
return 0 ;
0 commit comments