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 ();
@@ -152,7 +162,7 @@ static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
152
162
// / Parse all \p buffers using \c Executor , \p iteration times, and print out
153
163
// / the measurement to the \c stdout.
154
164
template <typename Executor>
155
- static void
165
+ static llvm::Error
156
166
perform (const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
157
167
ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
158
168
uintmax_t totalLines) {
@@ -161,13 +171,19 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
161
171
llvm::outs () << " parser: " << Executor::name << " \n " ;
162
172
163
173
using duration_t = std::chrono::nanoseconds;
174
+ // Wall clock time.
164
175
auto tDuration = duration_t::zero ();
176
+ // CPU time.
165
177
auto cDuration = duration_t::zero ();
166
178
179
+ llvm::Error err = llvm::Error::success ();
167
180
for (unsigned i = 0 ; i < iteration; i++) {
168
181
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;
171
187
tDuration += elapsed.first ;
172
188
cDuration += elapsed.second ;
173
189
}
@@ -178,13 +194,16 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
178
194
auto cDisplay =
179
195
std::chrono::duration_cast<std::chrono::milliseconds>(cDuration).count ();
180
196
197
+ // Throughputs are based on CPU time.
181
198
auto byteTPS = totalBytes * duration_t ::period::den / cDuration.count ();
182
199
auto lineTPS = totalLines * duration_t ::period::den / cDuration.count ();
183
200
184
201
llvm::outs () << llvm::format (" wall clock time (ms): %8d\n " , tDisplay)
185
202
<< llvm::format (" cpu time (ms): %8d\n " , cDisplay)
186
203
<< llvm::format (" throughput (byte/s): %8d\n " , byteTPS)
187
204
<< llvm::format (" throughput (line/s): %8d\n " , lineTPS);
205
+
206
+ return llvm::Error::success ();
188
207
}
189
208
190
209
} // namespace
@@ -195,7 +214,7 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
195
214
llvm::cl::ParseCommandLineOptions (args.size (), args.data (),
196
215
" Swift parse test\n " );
197
216
198
- unsigned iteration = options.Iteration ;
217
+ unsigned iterations = options.Iterations ;
199
218
ExecuteOptions execOptions;
200
219
if (options.SkipBodies )
201
220
execOptions |= ExecuteOptionFlag::SkipBodies;
@@ -212,23 +231,29 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
212
231
llvm::outs () << llvm::format (" file count: %8d\n " , buffers.size ())
213
232
<< llvm::format (" total bytes: %8d\n " , byteCount)
214
233
<< 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 ();
216
237
for (auto mode : options.Executors ) {
217
238
switch (mode) {
218
239
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);
222
242
break ;
223
- #else
224
- llvm::errs () << " error: SwiftParser is not enabled\n " ;
225
- return 1 ;
226
- #endif
227
243
case Executor::LibParse:
228
- perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount ,
229
- lineCount);
244
+ err = perform<LibParseExecutor>(buffers, execOptions, iterations ,
245
+ byteCount, lineCount);
230
246
break ;
231
247
}
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 ;
232
257
}
233
258
234
259
return 0 ;
0 commit comments