1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
#include <asmjit/host.h>
#include <asmjit-testing/commons/asmjitutils.h>
#include <asmjit-testing/commons/cmdline.h>
#include <asmjit-testing/commons/performancetimer.h>
#include <stdint.h>
using namespace asmjit;
static void print_app_info(size_t n) noexcept {
printf("AsmJit Benchmark Overhead v%u.%u.%u [Arch=%s] [Mode=%s]\n\n",
unsigned((ASMJIT_LIBRARY_VERSION >> 16) ),
unsigned((ASMJIT_LIBRARY_VERSION >> 8) & 0xFF),
unsigned((ASMJIT_LIBRARY_VERSION ) & 0xFF),
asmjit_arch_as_string(Arch::kHost),
asmjit_build_type()
);
printf("This benchmark was designed to benchmark the cost of initialization and\n"
"reset (or reinitialization) of CodeHolder and Emitters; and the cost of\n"
"moving a minimal assembled function to executable memory. Each output line\n"
"provides the following columns:\n"
"\n"
" - <Test> - test case name - either 'CodeHolder' only or an emitter\n"
" - Strategy - reusability strategy - whether init/reset or reinit is used\n"
" - Reuse Only - no code generation, no use of emitter except for init/reuse\n"
" - Func - function was assembled\n"
" - RA - function was compiled (registers allocated) (Compiler)\n"
" - Asm - function was finalized & serialized (Builder/Compiler)\n"
" - RT - function was added to JitRuntime and then removed from it\n"
"\n"
"Essentially the output provides an insight into the cost of reusing\n"
"CodeHolder and other emitters, and the cost of assembling, finalizing,\n"
"and moving the assembled code into executable memory by separating each\n"
"phase.\n\n"
);
printf("The number of iterations benchmarked: %zu (override by --count=n)\n", n);
printf("\n");
}
enum class AssemblerOp {
kNone,
kRT
};
enum class BuilderOp {
kNone,
kFinalize,
kFinalize_RT
};
enum class CompilerOp {
kNone,
kCompile,
kFinalize,
kFinalize_RT
};
#if !defined(ASMJIT_NO_JIT)
class MyErrorHandler : public ErrorHandler {
public:
void handle_error(asmjit::Error err, const char* message, asmjit::BaseEmitter* origin) override {
Support::maybe_unused(err, origin);
fprintf(stderr, "AsmJit error: %s\n", message);
}
};
enum class InitStrategy : uint32_t {
kInitReset,
kReinit
};
static inline void bench_codeholder(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.reset();
}
}
else {
code.init(rt.environment());
for (size_t i = 0; i < count; i++) {
code.reinit();
}
}
}
#if ASMJIT_ARCH_X86 != 0 && !defined(ASMJIT_NO_X86)
template<typename EmitterT>
static ASMJIT_INLINE void emit_raw_func(EmitterT& emitter) {
emitter.mov(x86::eax, 0);
emitter.ret();
}
template<typename CompilerT>
static ASMJIT_INLINE void compile_raw_func(CompilerT& cc) {
x86::Gp r = cc.new_gp32();
cc.mov(r, 0);
cc.ret(r);
}
#endif
#if ASMJIT_ARCH_ARM == 64 && !defined(ASMJIT_NO_AARCH64)
template<typename EmitterT>
static ASMJIT_INLINE void emit_raw_func(EmitterT& emitter) {
emitter.mov(a64::w0, 0);
emitter.ret(a64::x30);
}
template<typename CompilerT>
static ASMJIT_INLINE void compile_raw_func(CompilerT& cc) {
a64::Gp gp = cc.new_gp32();
cc.mov(gp, 0);
cc.ret(gp);
}
#endif
#if defined(ASMJIT_HAS_HOST_BACKEND)
template<typename AssemblerT>
static inline void bench_assembler(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
AssemblerT a;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&a);
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&a);
for (size_t i = 0; i < count; i++) {
code.reinit();
}
}
}
template<typename AssemblerT, AssemblerOp op>
static inline void bench_assembler_func(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
AssemblerT a;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&a);
emit_raw_func(a);
if constexpr (op == AssemblerOp::kRT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&a);
for (size_t i = 0; i < count; i++) {
code.reinit();
emit_raw_func(a);
if constexpr (op == AssemblerOp::kRT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
}
}
}
#endif
#if defined(ASMJIT_HAS_HOST_BACKEND) && !defined(ASMJIT_NO_BUILDER)
template<typename BuilderT>
static inline void bench_builder(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
BuilderT b;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&b);
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&b);
for (size_t i = 0; i < count; i++) {
code.reinit();
}
}
}
template<typename BuilderT, BuilderOp op>
static inline void bench_builder_func(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
BuilderT b;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&b);
emit_raw_func(b);
if constexpr (op >= BuilderOp::kFinalize) {
b.finalize();
if constexpr (op == BuilderOp::kFinalize_RT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
}
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&b);
for (size_t i = 0; i < count; i++) {
code.reinit();
emit_raw_func(b);
if constexpr (op >= BuilderOp::kFinalize) {
b.finalize();
if constexpr (op == BuilderOp::kFinalize_RT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
}
}
}
}
#endif // ASMJIT_HAS_HOST_BACKEND && !ASMJIT_NO_BUILDER
#if defined(ASMJIT_HAS_HOST_BACKEND) && !defined(ASMJIT_NO_COMPILER)
template<typename CompilerT>
static inline void bench_compiler(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
CompilerT cc;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&cc);
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&cc);
for (size_t i = 0; i < count; i++) {
code.reinit();
}
}
}
template<typename CompilerT, CompilerOp op>
static inline void bench_compiler_func(InitStrategy strategy, size_t count) {
JitRuntime rt;
CodeHolder code;
CompilerT cc;
MyErrorHandler eh;
if (strategy == InitStrategy::kInitReset) {
for (size_t i = 0; i < count; i++) {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&cc);
(void)cc.add_func(FuncSignature::build<uint32_t>());
compile_raw_func(cc);
cc.end_func();
if constexpr (op == CompilerOp::kCompile) {
cc.run_passes();
}
else if constexpr (op >= CompilerOp::kFinalize) {
cc.finalize();
if constexpr (op == CompilerOp::kFinalize_RT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
}
code.reset();
}
}
else {
code.init(rt.environment());
code.set_error_handler(&eh);
code.attach(&cc);
for (size_t i = 0; i < count; i++) {
code.reinit();
(void)cc.add_func(FuncSignature::build<uint32_t>());
compile_raw_func(cc);
cc.end_func();
if constexpr (op == CompilerOp::kCompile) {
cc.run_passes();
}
else if constexpr (op >= CompilerOp::kFinalize) {
cc.finalize();
if constexpr (op == CompilerOp::kFinalize_RT) {
using Func = uint32_t(*)(void);
Func fn;
rt.add(&fn, &code);
rt.release(fn);
}
}
}
}
}
#endif // ASMJIT_HAS_HOST_BACKEND && !ASMJIT_NO_COMPILER
template<typename Lambda>
static inline void test_perf(const char* group, const char* params, InitStrategy strategy, size_t n, Lambda&& fn) {
PerformanceTimer timer;
const char* strategy_name = strategy == InitStrategy::kInitReset ? "init/reset" : "reinit";
timer.start();
fn(strategy, n);
timer.stop();
printf("| %-10s | %-10s | %-23s| %8.1f [ms] |\n", group, strategy_name, params, timer.duration());
}
static inline void test_perf_all(InitStrategy strategy, size_t n) {
using IS = InitStrategy;
const char frame[] = "+------------+------------+------------------------+---------------+\n";
const char header[] = "| Group | Strategy | Features Used | Time [ms] |\n";
printf(frame);
printf(header);
printf(frame);
test_perf("CodeHolder", "Reuse Only" , strategy, n, [](IS s, size_t n) { bench_codeholder(s, n); });
#if defined(ASMJIT_HAS_HOST_BACKEND)
test_perf("Assembler ", "Reuse Only" , strategy, n, [](IS s, size_t n) { bench_assembler<host::Assembler>(s, n); });
test_perf("Assembler ", "Func" , strategy, n, [](IS s, size_t n) { bench_assembler_func<host::Assembler, AssemblerOp::kNone>(s, n); });
test_perf("Assembler ", "Func + RT" , strategy, n, [](IS s, size_t n) { bench_assembler_func<host::Assembler, AssemblerOp::kRT>(s, n); });
#endif
#if defined(ASMJIT_HAS_HOST_BACKEND) && !defined(ASMJIT_NO_BUILDER)
test_perf("Builder ", "Reuse Only" , strategy, n, [](IS s, size_t n) { bench_builder<host::Builder>(s, n); });
test_perf("Builder ", "Func" , strategy, n, [](IS s, size_t n) { bench_builder_func<host::Builder, BuilderOp::kNone>(s, n); });
test_perf("Builder ", "Func + Asm" , strategy, n, [](IS s, size_t n) { bench_builder_func<host::Builder, BuilderOp::kFinalize>(s, n); });
test_perf("Builder ", "Func + Asm + RT" , strategy, n, [](IS s, size_t n) { bench_builder_func<host::Builder, BuilderOp::kFinalize_RT>(s, n); });
#endif
#if defined(ASMJIT_HAS_HOST_BACKEND) && !defined(ASMJIT_NO_COMPILER)
test_perf("Compiler ", "Reuse Only" , strategy, n, [](IS s, size_t n) { bench_compiler<host::Compiler>(s, n); });
test_perf("Compiler ", "Func" , strategy, n, [](IS s, size_t n) { bench_compiler_func<host::Compiler, CompilerOp::kNone>(s, n); });
test_perf("Compiler ", "Func + RA" , strategy, n, [](IS s, size_t n) { bench_compiler_func<host::Compiler, CompilerOp::kCompile>(s, n); });
test_perf("Compiler ", "Func + RA + Asm" , strategy, n, [](IS s, size_t n) { bench_compiler_func<host::Compiler, CompilerOp::kFinalize>(s, n); });
test_perf("Compiler ", "Func + RA + Asm + RT", strategy, n, [](IS s, size_t n) { bench_compiler_func<host::Compiler, CompilerOp::kFinalize_RT>(s, n); });
#endif
printf(frame);
}
int main(int argc, char* argv[]) {
CmdLine cmd_line(argc, argv);
size_t n = cmd_line.value_as_uint("--count", 1000000);
print_app_info(n);
test_perf_all(InitStrategy::kInitReset, n);
printf("\n");
test_perf_all(InitStrategy::kReinit, n);
return 0;
}
#else
int main() {
print_app_info(0);
printf("!!AsmJit Benchmark Reuse is currently disabled: <ASMJIT_NO_JIT> or unsuitable target architecture !!\n");
return 0;
}
#endif
|