Skip to content

Commit 5e8f36b

Browse files
authored
Add dart2wasm targets to benchmark builder (#806)
This updates benchmark builder with two dart2wasm targets: - wasm: Default optimized build. - wasm-omit-checks: Optimized build with `--omit-checks`. README is updated with instructions to build and run new targets.
1 parent 41d0156 commit 5e8f36b

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

benchmarks/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@
1313
- AOT: `./out/from_binary.exe`
1414
- JS: `d8 $DART_SDK/lib/_internal/js_runtime/lib/preambles/d8.js out/from_binary.js`
1515

16+
- **Wasm:**
17+
18+
- Get the Dart SDK source code following instructions [here][1], and build
19+
`dart2wasm_platform` target with `tools/build.py -m release
20+
dart2wasm_platform`.
21+
22+
- Make sure `$DART_SDK` environment variable set to the path of [Dart SDK's
23+
"sdk" directory][1].
24+
25+
- Compile benchmarks with `./tool/compile_benchmarks.dart --target=<target>`
26+
where `<target>` is one of:
27+
28+
- `wasm`: Default optimized build
29+
- `wasm-omit-checks`: Optimized build with `--omit-checks`
30+
31+
- Run with: `$DART_SDK/bin/run_dart2wasm_d8 out/from_binary.wasm`, or use
32+
`.omit-checks.wasm` extension for the `wasm-omit-checks` target:
33+
`from_binary.omit-checks.wasm`.
34+
35+
[1]: https://github.com/dart-lang/sdk/wiki/Building
36+
[2]: https://github.com/dart-lang/sdk/tree/main/sdk
37+
1638
## Development
1739

1840
`protoc_version` file specifies the version of protoc Golem will use when

benchmarks/lib/readfile.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
export 'readfile_vm.dart' if (dart.library.js) 'readfile_d8.dart' show readfile;
5+
export 'readfile_vm.dart'
6+
if (dart.library.js) 'readfile_js.dart'
7+
if (dart.library.wasm) 'readfile_wasm.dart' show readfile;
File renamed without changes.

benchmarks/lib/readfile_wasm.dart

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:js_interop';
6+
import 'dart:typed_data';
7+
8+
/// Read the file at the given [path].
9+
///
10+
/// This relies on the `readbuffer` function provided by d8.
11+
@JS()
12+
external ByteBuffer readbuffer(String path);
13+
14+
/// Read the file at the given [path].
15+
Uint8List readfile(String path) {
16+
return Uint8List.view(readbuffer(path));
17+
}

benchmarks/tool/compile_benchmarks.dart

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env dart
2+
// ignore_for_file: only_throw_errors
23

34
import 'dart:io' show Directory, Platform, Process, ProcessResult, exit;
45

@@ -42,9 +43,17 @@ Future<void> main(List<String> args) async {
4243
targets.add(jsProductionTarget);
4344
break;
4445

46+
case 'wasm':
47+
targets.add(wasmTarget);
48+
break;
49+
50+
case 'wasm-omit-checks':
51+
targets.add(wasmOmitChecksTarget);
52+
break;
53+
4554
default:
46-
print(
47-
'Unsupported target: $targetStr. Supported targets: aot, exe, jit, js, js-production');
55+
print('Unsupported target: $targetStr. Supported targets: aot, exe, '
56+
'jit, js, js-production, wasm, wasm-omit-checks');
4857
exit(1);
4958
}
5059
}
@@ -135,6 +144,9 @@ const exeTarget = Target('exe', exeProcessArgs);
135144
const jitTarget = Target('jit', jitProcessArgs);
136145
const jsTarget = Target('js', jsProcessArgs);
137146
const jsProductionTarget = Target('js-production', jsProductionProcessArgs);
147+
const wasmTarget = Target('wasm', wasmProcessArgs);
148+
const wasmOmitChecksTarget =
149+
Target('wasm-omit-checks', wasmOmitChecksProcessArgs);
138150

139151
List<String> aotProcessArgs(String sourceFile) {
140152
final baseName = path.basename(sourceFile);
@@ -185,3 +197,32 @@ List<String> jsProductionProcessArgs(String sourceFile) {
185197
'out/$baseNameNoExt.production.js'
186198
];
187199
}
200+
201+
List<String> wasmProcessArgs(String sourceFile) {
202+
final sdkPath = Platform.environment['DART_SDK'];
203+
if (sdkPath == null) {
204+
throw '\$DART_SDK environment variable is not set';
205+
}
206+
final baseName = path.basename(sourceFile);
207+
final baseNameNoExt = path.withoutExtension(baseName);
208+
return [
209+
'$sdkPath/../pkg/dart2wasm/tool/compile_benchmark',
210+
sourceFile,
211+
'out/$baseNameNoExt.wasm',
212+
];
213+
}
214+
215+
List<String> wasmOmitChecksProcessArgs(String sourceFile) {
216+
final sdkPath = Platform.environment['DART_SDK'];
217+
if (sdkPath == null) {
218+
throw '\$DART_SDK environment variable is not set';
219+
}
220+
final baseName = path.basename(sourceFile);
221+
final baseNameNoExt = path.withoutExtension(baseName);
222+
return [
223+
'$sdkPath/../pkg/dart2wasm/tool/compile_benchmark',
224+
sourceFile,
225+
'out/$baseNameNoExt.omit-checks.wasm',
226+
'--omit-checks',
227+
];
228+
}

0 commit comments

Comments
 (0)