Skip to content

Commit fdeb9dc

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2wasm] Introduce an opt phase to dart2wasm.
To help facilitate this we move all IO into a separate helper library/class. This makes it easier to have symmetric read/write functions and to do IO within compile.dart where necessary. Adding the new `opt` phase allows us to remove the duplicated code between dartdev and compile_benchmark simplifying those two files a lot. It will also allow us to more easily invoke wasm-opt within our internal build pipeline. For compile_benchmark we still run the opt phase independently (but through dart2wasm) to keep the benchmark data as consistent as possible. Change-Id: Iaa855dbc3a05abfedbc3eea4af32e3ba27e84600 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464640 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 836465d commit fdeb9dc

File tree

14 files changed

+606
-542
lines changed

14 files changed

+606
-542
lines changed

pkg/dart2wasm/benchmark/self_compile_benchmark.dart

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,70 @@
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+
import 'dart:typed_data';
6+
57
import 'package:_fe_analyzer_shared/src/util/colors.dart' as colors;
68
import 'package:dart2wasm/compile.dart';
79
import 'package:dart2wasm/compiler_options.dart';
10+
import 'package:dart2wasm/io_util.dart';
811

912
import 'filesystem_io.dart' if (dart.library.js_interop) 'filesystem_js.dart';
1013

14+
class _BenchmarkIOManager extends CompilerPhaseInputOutputManager {
15+
final WasmCompilerFileSystem benchmarkFileSystem;
16+
Uint8List? moduleBytes;
17+
18+
_BenchmarkIOManager(this.benchmarkFileSystem, WasmCompilerOptions options)
19+
: super(benchmarkFileSystem, options);
20+
21+
@override
22+
Future<void> writeWasmModule(Uint8List wasmModule, String moduleName) async {
23+
moduleBytes = wasmModule;
24+
}
25+
26+
@override
27+
Future<void> writeWasmSourceMap(String sourceMap, String moduleName) async {}
28+
29+
@override
30+
Future<void> writeJsRuntime(String jsRuntime) async {}
31+
32+
@override
33+
Future<void> writeSupportJs(String supportJs) async {}
34+
35+
void flushWasmModules(String wasmFile) {
36+
benchmarkFileSystem.writeBytesSync(wasmFile, moduleBytes!);
37+
}
38+
}
39+
1140
Future main(List<String> args) async {
1241
final sw = Stopwatch()..start();
1342
final fileSystem = WasmCompilerFileSystem();
14-
final result = await compileBenchmark(
15-
fileSystem, 'pkg/dart2wasm/benchmark/self_compile_benchmark.dart');
43+
final mainFile = 'pkg/dart2wasm/benchmark/self_compile_benchmark.dart';
44+
final main = Uri.file('${fileSystem.sdkRoot}/$mainFile');
45+
46+
final options = WasmCompilerOptions(mainUri: main, outputFile: 'out.wasm');
47+
final ioManager = _BenchmarkIOManager(fileSystem, options);
48+
49+
options.librariesSpecPath =
50+
Uri.file('${fileSystem.sdkRoot}/sdk/lib/libraries.json');
51+
52+
await compileBenchmark(options, ioManager);
1653
print('Dart2WasmSelfCompile(RunTimeRaw): ${sw.elapsed.inMilliseconds} ms.');
1754

1855
if (args.isNotEmpty) {
19-
final module = result.wasmModules.values.single;
2056
final wasmFile = args.single;
21-
fileSystem.writeBytesSync(wasmFile, module.moduleBytes);
57+
ioManager.flushWasmModules(wasmFile);
2258
}
2359
}
2460

25-
Future<CodegenResult> compileBenchmark(
26-
WasmCompilerFileSystem fileSystem, String mainFile) async {
61+
Future<CodegenResult> compileBenchmark(WasmCompilerOptions options,
62+
CompilerPhaseInputOutputManager ioManager) async {
2763
// Avoid CFE self-detecting whether `stdout`/`stderr` is terminal and supports
2864
// colors (as we don't have `dart:io` available when we run dart2wasm in a
2965
// wasm runtime).
3066
colors.enableColors = false;
3167

32-
final main = Uri.file('${fileSystem.sdkRoot}/$mainFile');
33-
34-
final options = WasmCompilerOptions(mainUri: main, outputFile: 'out.wasm');
35-
options.librariesSpecPath =
36-
Uri.file('${fileSystem.sdkRoot}/sdk/lib/libraries.json');
37-
38-
final result = await compile(
39-
options, fileSystem, (mod) => Uri.parse('$mod.maps'), (diag) {
68+
final result = await compile(options, ioManager, (diag) {
4069
print('Diagnostics: ${diag.severity} ${diag.plainTextFormatted}');
4170
});
4271
if (result is! CompilationSuccess) {

0 commit comments

Comments
 (0)