Commit Graph

81 Commits

Author SHA1 Message Date
Nate Biggs d43d9df1a7 [dart2wasm] Remove dynamic modules support from the dart2wasm compiler.
Change-Id: If92f55296dfe83b64165a2bd07eaefb7d137198c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/497341
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2026-04-24 12:04:23 -07:00
Martin Kustermann 4ee6a66270 [dart2wasm] Format pkg/dart2wasm after language version was increased
The change in [0] increased the language version of pkg/dart2wasm. That
in return changes how the package is formatted by the autoformatter.

This CL runs now the formatter to re-format the code. Unfortunately this
makes blame lists worse. But not doing it will make us have to disable
auto-formatting before saving files which is very annoying.

[0] https://dart-review.googlesource.com/c/sdk/+/487944

Change-Id: I6953fe0d6a824b2b79a26bbadb0bb977cec70b7a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/490821
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2026-03-26 04:32:19 -07:00
Nate Biggs d0eb238f1f [dart2wasm] Update deferred module embedding to faciltate batched module fetching.
With `loadDeferredModule` there was no easy way for the embedder to batch module loads that were part of the same load ID. The loop happened within the wasm runtime and so the best the embedder could do was a `setTimeout(f, 0)` and collect the modules before making a request. This can lead to unintended delays though.

Instead we now pass the full list of modules to the embedder's registered loader along with an instantiator function so that the emebedder can start compiling concurrently before all the modules are done loading.

Added a changelog entry since this API is new but might be in use.

Change-Id: I4ae3a9fc28fc726909f63a1dd4ea1d98d5fc3fc0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/481360
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2026-02-18 08:58:28 -08:00
Martin Kustermann 053f0ed005 [dart2wasm] Make IR tests more stable
To avoid depending on exact ordering binaryen uses in the wasm module,
we start

* enqueue functions/types/... based on sort name instead of index
  in wasm module

* only assign increasing ids in `Namer` if we actually want to print
  the name

* print functions/types/... in sorted order

for IR tests.

We also update the `pkg/dart2wasm/bin/wasm2wat.dart` tool to be
able to dump wat for multiple wasm modules.

Change-Id: Ifbfc9149a629cf5ceacd95b7cb4892e9de3d4d9c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465341
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2025-12-02 03:06:51 -08:00
Martin Kustermann 92f500b7b7 [dart2wasm] Make IR test framework more flexible, add deferred loading baseline test
Changes to IR printing implementation:

* Emit omitted `<...>` marker to make difference between function
  without body and function with omitted body clear.
* Extend omtting capability to globals, types and tables
* Extend filtering capability to globals, types, tables
* Better table names: Try to use import or export names if available.
* Option to print globals & types always multiline
* Option to scrub absolute file uris (which aren't stable across machines)

Changes to IR binary / text:

* Extend binary parsing of element section to support imported tables
* Add printing of those (which shows how imported tables are patched)

(The patching of importing tables is used in deferred modules)

Changes to IR test framework:

* Allow more filters (see above) in the test files.
* Allow tests to use helper libraries (containing `.h.` in their name)
  without them being considered tests themselves.
* Allow using deferred loading in tests and write expectation files for
  main & deferred modules as wat files.

Other things:

* Share more code between `pkg/dart2wasm/bin/wasm2dart.dart` and
  `pkg/dart2wasm/test/ir_test.dart`
* Add baseline test for deferred loading using tear off constants

Change-Id: Ica6666f23f5aa6fb2174f414c9082039940f8dc5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/456460
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
2025-10-23 01:11:22 -07:00
Martin Kustermann e63dcafc0c [dart2wasm] Add wasm module printing functionality.
This adds support for printing module IR as text format.
For convenience we add a `pkg/dart2wasm/bin/wami.dart` that
produces very similar output to V8's `wami`.

The goal is to use this to write size/perf optimization tests
by dumping IR into expectation files (will add this
infrastructure in a future CL)

Issue https://github.com/dart-lang/sdk/issues/60928

Change-Id: I42d19c2b8c6242f55693d6ed5d844a5c1ecb1f39
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/454600
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2025-10-15 12:36:50 -07:00
Nate Biggs a20f1bcb40 [dart2wasm] Introduce "load ids" to dart2wasm deferred loading.
Load ids provide a way to reduce the overhead of deferred loading. By
default deferred loading requires mapping `loadLibrary` calls to a list
of modules. This requires including (1) library uris, (2) prefix names
and (3) module names directly in the main module. With this loading
modules is easier as the loading function gets the exact filename.

Load ids provide an alternative approach where the compiler emits a
separate file mapping a load ID to the module set required for that ID.
An app could store this mapping on the server allowing the frontend to
include only the load ID in its request and have the server figure out
which modules to send back.

Internal serving infra uses module sets like this so this change allows
easier integration into that tooling. Dart2js already supports emitting
this deferred mapping JSON and internal infra is using that today.

Other changes include:
- Run the deferred loading transformer after TFA. This will exclude unused libraries in the resulting deferred loading map. Mark deferred helpers as entry points so that they don't get tree-shaken.
- Some changes to naming conventions of JS helpers.
- Use filename as module name to simplify JS helpers

Change-Id: I5e2f5e374de77c87095d08bfff6534506cb10652
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/454240
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2025-10-13 13:42:47 -07:00
Nate Biggs 4f818d498b [dart2wasm] Generate additional mjs module with JS support values.
This updates the dynamic module compile to emit an extra mjs file containing JS-defined functions and values (i.e. Strings). The runtime then loads this JS module along with the wasm module and links the two.

Change-Id: Iedddf378822f3abd957f0ca9016a9f7883854973
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422341
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2025-04-15 13:06:11 -07:00
Nate Biggs a4a4ca8a41 [dart2wasm] Dynamic modules
Missing from this implementation:
- Closure/dynamic calls with differing signatures
- Overrides with extra optional parameters
- Records with same shape defined in different dynamic modules
- Avoiding running TFA on dynamic module.
- Recompilation of only updateable functions from main module.
- Persist wasm def types from main module.

Testing is currently done locally via the dynamic_modules package test suite:
dart pkg/dynamic_modules/test/runner/main.dart --runtime=dart2wasm

Immediately after this lands we can introduce a new step to one of the wasm test matrix configurations that runs the above test suite (the VM has a similar configuration).

Change-Id: I3386d84be11b773842d45f4268a62a54c47e352b
Tested: Tested via new tests in dynamic_modules package. Tests run locally but will add to existing config.
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397721
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2025-02-11 13:59:46 -08:00
Ömer Sinan Ağacan f80214eb86 [dart2wasm] Fix Uri.base in d8
Currently when the .wasm file path passed to run_wasm.js is a relative
path, `Uri.base` becomes something like `file://test.wasm`, which is not
a valid file URI, so it causes crashes in `Uri.toFilePath`.

When the file path is relative add a omit `file://` prefix.

`Uri.base` values before and after:

- Before, relative .wasm path: `file://test.wasm` (invalid)
- Before, absolute .wasm path: `file:///home/user/test.wasm`

- After, relative .wasm path: `test.wasm` (fixed)
- After, absolute .wasm path: `file:///home/user/test.wasm` (same as before)

Change-Id: I0d1c43716e07a9ee926e7feeeab514c2c66bac16
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385700
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-09-24 10:21:52 +00:00
Nate Biggs 1d392255ef [dart2wasm] Add wasm module test mode.
Adds a mode that puts every library (outside of the SDK) into its own wasm module. Each module is then loaded before the program executes.

This allows us to get better coverage on multi-module logic.

I chose to augment the SDK `_invokeMain` entry point because it allows us to push the user-defined main into a second module ensuring we always have at least 2 modules. The SDK alone is then the main module and all user-code is in deferred modules.

The next step is to make a test configuration for this and decide what frequency to execute it with.

Change-Id: I01bf3a5fee4604a890ef376ebd35113281a36a5c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/384404
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-23 18:56:24 +00:00
Martin Kustermann bad285fafd [dart2wasm] Make deferred loading embedder call be based on bytes
Change-Id: I56d331028ad08c176fdcbe14655e036a6191d26c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/384260
Reviewed-by: Nate Biggs <natebiggs@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-09-10 18:17:13 +00:00
Martin Kustermann 702176cbd4 [dart2wasm] Remove traces of JSC_useWebAssembly* in test runner & run_wasm.js
Those environment variables are no longer needed for newer versions of
JSC.

Change-Id: I416535ce116e2d0f1911131d52789362ce951fef
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383863
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2024-09-05 12:23:28 +00:00
Nate Biggs ed7e1d8be4 [dart2wasm] Add deferred loading support to dart2wasm (1/X).
Adds a 'enable-deferred-loading' option to dart2wasm.

Also updates the js shells' run_wasm.js script to support upcoming deferred loading. Adds support for the "fetch" API which will be used to get deferred modules. And adds the JS implementation of the deferred loading helper which will gets imported into wasm.

Change-Id: I6f3cd4d83f5c987dc529ef8f34b3b289ab555d89
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/379900
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-04 21:58:12 +00:00
Martin Kustermann 3deaa76d71 [dart2wasm] Migrate our embedders to new mjs API
To test that the deprecated mjs API functions still work we landed the
new mjs API in [0] seperately.

This CL then migrates our 2 different users of the mjs api
in the Dart SDK (for testing and benchmarking) to the new API.

[0] https://dart-review.googlesource.com/c/sdk/+/383242

Change-Id: Ibbe58c9a977326a8fbff780d328d7f54784bd323
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/382888
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
2024-09-03 06:47:58 +00:00
Martin Kustermann 56178c2e9d [dart2wasm] Make mjs API be object oriented without exposing wasm compiled module & module instance
This encapsulates the way we compile, instantiate and run and
avoids exposing the compiled module and the instantiated module.

It also allows us to carry state from compilation, instantiation
and running that can be accessed later during execution.

For example an embedder could supply extra
* builtins for compiling that we can store on `CompiledApp`
* imports for instantiation that we can store on `InstantiatedApp`

This extra information may be required later on at runtime,
e.g. when loading deferred modules that need to be compiled
and instantiated the same way as the main module.

An embedder can use this as follows:

    const compiledApp = await compileStreaming(fetch(...));
    const instantiatedApp = await compiledApp.instantiate({ <extra imports> });
    instantiatedApp.invokeMain(...);

We mark the top-level `instantiate` and `invoke` methods as deprecated,
allowing flutter to migrate without manuall engine roll.

Change-Id: Ie0297909c1d2b8f8dc8c12670d5476ad4ff772cd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383242
Reviewed-by: Nate Biggs <natebiggs@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-09-03 06:46:12 +00:00
Ömer Sinan Ağacan 4cd6096da4 [dart2wasm] Move Wasm compilation functions into generated runtime blob
To be able to control how dart2wasm-generated modules are compiled, add
a `WebAssembly.compile` and `WebAssmebly.compileStreaming` wrapper to
the generated runtime blob.

These wrappers do js-string built-in detection and enable the built-in
when it's available.

Once the SDK is rolled into engine we will update the engine dart2wasm
module loader to use `compileStreaming` from the runtime blob, in
https://github.com/flutter/engine/pull/51488.

This will allow enabling the built-in without duplicating the feature
detection and compilation code in the engine.

Change-Id: Icb939f16a2b0ae1d348ec5a814371e9ffde4fd68
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381581
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-08-21 11:19:40 +00:00
Martin Kustermann 09361f0f16 [dart2wasm] Make test runner use pkg/dart2wasm/tool/run_benchmark (just as golem does)
* We make the test runner use the same runner script as golem uses
* We make the runner script work for different JS shells

=> The runner script encapsulates how to invoke different JS shells, the
   commandline format, environment variables, ...

=> Golem can then continue to use same runner script, but also with new
   shells

Change-Id: Iad63fe6c0563cfeac28cbfb25eb41ee1b7c13f3a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/365740
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-05-08 11:00:33 +00:00
Martin Kustermann 149db7eced [dart2wasm] Make commandline JS runner (run_wasm.js) initialize location.href
By initializing `location.href` in the commandline JS runners we allow
programs running in D8 to use `Uri.base` (which depends on
`location.href`).

That in return will allow running some Dart programs in D8 that would
otherwise not run (e.g. code using `package:test/test.dart` - which uses
`Uri.base`)

TEST=tests/web/wasm/location_href_test.dart

Change-Id: Ie219f8d9ece3b92f2442200539557f116d2c84ab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363700
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-04-19 12:40:23 +00:00
Ömer Sinan Ağacan 637f3b216a [dart2wasm,dart2js] Fix setTimeout and setInterval impls in d8 scripts
HTML spec says the timeout argument in `setInterval` and `setTimeout`
will be 0 when negative [1], update our mocked versions in the d8 script
to implement this.

Fixes tests on dart2wasm-linux-d8:

- co19/LibTest/async/Timer/Timer.periodic_A02_t01
- co19/LibTest/async/Timer/Timer_A02_t01

These tests already pass on Firefox and Chrome.

Fixes #54598.

[1]: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval-dev
in "Timer initialization steps" step 4: "If timeout is less than 0, then
set timeout to 0."

Change-Id: I716f5942b5dff0fbd5a45c2bb6e0ef2732a5ea67
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362260
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
2024-04-19 07:39:55 +00:00
Ömer Sinan Ağacan 44c2e17600 [dart2wasm] Update built-in string imports according to the latest spec
Spec: https://github.com/WebAssembly/js-string-builtins

Change-Id: Idb387e6dd27b203ddca52291c44fb2aa15ee75cf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/357560
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-03-19 08:20:59 +00:00
Martin Kustermann c625797f8a [dart2wasm] Implement dart:developer in dart2wasm
* We migrate the `dart:developer` implementation of dart2js to
static interop.

* We make dart2wasm use the same implementation as dart2js.

Closes https://github.com/dart-lang/sdk/issues/54991

Change-Id: I7873edc7e804500c8eca878367d9045c98a1c2e8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354101
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-02-26 11:23:29 +00:00
Martin Kustermann 3a314331a7 [dart2wasm] Add support for testing dart2wasm with JSC
JSC only supports `print()` but not `console.log()`.

=> The changes to `printToConsole()` are therefore extended
to check for `console.log()` as well as `print()`.
=> This is extending it to a broader subset of dart2js's print

Change-Id: I7efa697477aa60e473d01716b104fc1526035c67
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347283
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2024-01-22 12:04:37 +00:00
Michael Thomsen 0df58fc2d0 Add new dart compile wasm command.
Marked as experimental, and only shown when verbose flag is passed:

```
$ dart help compile -v
Compile Dart to various formats.

Usage: dart [vm-options] compile <subcommand> [arguments]
-h, --help    Print this usage information.

Available subcommands:
  aot-snapshot   Compile Dart to an AOT snapshot.
  exe            Compile Dart to a self-contained executable.
  jit-snapshot   Compile Dart to a JIT snapshot.
  js             Compile Dart to JavaScript.
  kernel         Compile Dart to a kernel snapshot.
  wasm           Compile Dart to a WebAssembly/WasmGC module (EXPERIMENTAL).
```

Change-Id: I6a0e65d4fbdd7b2782406b8b9969e14036bf0711
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304860
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Michael Thomsen <mit@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-12-05 15:44:53 +00:00
Martin Kustermann 6e771a1ce9 [dart2wasm] Make tools/test.py -n dart2wasm-linux-jsshell ... work
Change-Id: I0a2678a5b141ab52597860380df7d44fd1f1db74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/338381
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-11-27 15:51:55 +00:00
Ömer Sinan Ağacan 0d50d07a08 [dart2wasm] Update d8 flags
Change-Id: Ibe1e6c32d1a46055d35dab32878ac958801c550b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335800
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-11-14 09:19:08 +00:00
Ömer Sinan Ağacan 87a3de41b1 [dart2wasm] Implement micro-task scheduling
Use JS `setTimeout` for events and `queueMicrotask` for micro-tasks.

dart2js event loop implementation is copied in `run_wasm.js` to be able
to use `clearTimeout`, `setInterval`, `clearInterval`, and
`scheduleMicrotask`, which are not not available in d8, and `setTimeout`
in d8 does not wait before calling a callback.

New passing d8 tests:

- co19/LibTest/async/Future/Future.delayed_A01_t02
- co19/LibTest/async/Stream/Stream.periodic_A01_t01
- co19/LibTest/async/Stream/Stream.periodic_all_t01
- co19/LibTest/async/Stream/Stream.periodic_all_t02
- co19/LibTest/async/Stream/timeout_A04_t01
- co19/LibTest/async/StreamController/stream_all_A01_t01
- co19/LibTest/async/StreamController/stream_all_A01_t02
- co19/LibTest/async/StreamController/stream_all_A02_t01
- co19/LibTest/async/StreamController/stream_all_A02_t02
- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A01_t01
- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A01_t02
- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A02_t01
- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A02_t02
- co19/LibTest/async/Timer/Timer.periodic_A01_t01
- co19/LibTest/async/Timer/Timer_A01_t01
- co19/LibTest/core/Stopwatch/elapsedTicks_A01_t01
- language/async/call_test
- language/regress/regress21795_test
- lib/async/multiple_timer_test
- lib/async/periodic_timer2_test
- lib/async/periodic_timer3_test
- lib/async/periodic_timer4_test
- lib/async/schedule_microtask3_test
- lib/async/schedule_microtask_test
- lib/async/stream_timeout_test
- lib/async/timer_isActive_test
- lib/async/timer_repeat_test
- lib/async/timer_test

New passing Chrome tests:

- co19/LibTest/async/Stream/timeout_A04_t01
- language/async/call_test
- lib/async/schedule_microtask3_test

Tests below fail because of async* desugaring issues and will be fixed
separately:

- language/async_star/no_cancel2_test
- language/async_star/no_cancel_test

Tests below fail because of an existing issue (#29615):

- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A01_t03
- co19/LibTest/async/StreamController/StreamController.broadcast_Stream_all_A02_t03
- co19/LibTest/async/StreamController/stream_all_A02_t03

Fixes #51599.

Change-Id: Ib313e99bf3b3cb3bebeddc9e47dc77425ef94481
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/305201
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-06-19 10:21:35 +00:00
Ömer Sinan Ağacan c74387a3a2 [dart2wasm] New async implementation
This CL re-implements the async function compilation without using JSPI
or any other platform features.

This implementation is faster than the JSPI-based one in all benchmarks,
in some cases up to 200x (benchmark results at the end). So we remove
the JSPI-based implementation as there's no point in maintaining a much
slower implementation and supporting two implementations at the same
time (which is tricky because these implementations need different
libraries, all scripts need to support two modes etc.) that also
requires experimental platform features.

# Main changes

- A new pass `AwaitTransformer` transforms `await` expressions to
  top-level statements in form `var <fresh variable> = await <simple
  expr>`, where `<simple expr>` is an expression without `await`.

  After this pass all `await` expressions have the simple continuation
  of "assign the value of the awaited future to this variable and
  continue with the next statement". This simplifies `await`
  compilation.

- A new code generator `AsyncCodeGenerator` (inherits from
  `CodeGenerator`) compiles `async` functions. The `_YieldFinder` class
  is copied from `sync*` code generator but modified to handle `async`
  expressions.

- Mentions to the V8 flag `--experimental-wasm-stack-switching` is
  removed from all scripts and documents.

# Future work

- Control flow handling in `AsyncCodeGenerator` needs to be implemented
  in a similar way in `SyncStarCodeGenerator`. Doing this without
  duplicating a lot of code will require some refactoring.

# New passing tests

- co19/Language/Statements/Yield_and_Yield_Each/Yield/execution_async_A05_t01
- co19/Language/Statements/For/Asynchronous_For_in/execution_A02_t02
- language/regress/regress23996_test
- language/sync_star/dcall_type_test

# Benchmarks

Current implementation:

```
AsyncLiveVars.LiveObj1(RunTime): 1586000.0 us.
AsyncLiveVars.LiveObj2(RunTime): 2114000.0 us.
AsyncLiveVars.LiveObj4(RunTime): 1972500.0 us.
AsyncLiveVars.LiveObj8(RunTime): 2212000.0 us.
AsyncLiveVars.LiveObj16(RunTime): 2238000.0 us.
AsyncLiveVars.LiveInt1(RunTime): 2362000.0 us.
AsyncLiveVars.LiveInt4(RunTime): 2470000.0 us.
AsyncLiveVars.LiveObj2Int2(RunTime): 2575000.0 us.
AsyncLiveVars.LiveObj4Int4(RunTime): 2820000.0 us.
Calls.AwaitAsyncCall(RunTimeRaw): 35676.15658362989 ns.
Calls.AwaitAsyncCallClosureTargetPolymorphic(RunTimeRaw): 38934.108527131786 ns.
Calls.AwaitAsyncCallInstanceTargetPolymorphic(RunTimeRaw): 42617.02127659575 ns.
Calls.AwaitFutureCall(RunTimeRaw): 2832.058906825262 ns.
Calls.AwaitFutureCallClosureTargetPolymorphic(RunTimeRaw): 3665.8125915080527 ns.
Calls.AwaitFutureCallInstanceTargetPolymorphic(RunTimeRaw): 4420.449537241076 ns.
Calls.AwaitFutureOrCall(RunTimeRaw): 3692.7621861152143 ns.
Calls.AwaitFutureOrCallClosureTargetPolymorphic(RunTimeRaw): 4625.346901017576 ns.
Calls.AwaitFutureOrCallInstanceTargetPolymorphic(RunTimeRaw): 4514.6726862302485 ns.
Calls.AwaitFutureOrCallInstanceTargetPolymorphicManyAwaits(RunTimeRaw): 345172.4137931034 ns.
Calls.AwaitForAsyncStarStreamPolymorphic(RunTimeRaw): 697000.0 ns.
Calls.AwaitForAsyncStarStreamPolymorphicManyYields(RunTimeRaw): 704666.6666666666 ns.
Calls.AwaitForManualStreamPolymorphic(RunTimeRaw): 11010.989010989011 ns.
Calls.SyncCall(RunTimeRaw): 0.40275240996973316 ns.
Calls.SyncCallClosureTarget(RunTimeRaw): 0.3989591156672242 ns.
Calls.SyncCallInstanceTargetPolymorphic(RunTimeRaw): 3.2632549336335526 ns.
Calls.IterableSyncStarIterablePolymorphic(RunTimeRaw): 353.3980582524272 ns.
Calls.IterableManualIterablePolymorphic(RunTimeRaw): 332.1161825726141 ns.
Calls.IterableManualIterablePolymorphicManyYields(RunTimeRaw): 354.28067078552516 ns.
```

New implementation:

```
AsyncLiveVars.LiveObj1(RunTime): 11327.683615819209 us.
AsyncLiveVars.LiveObj2(RunTime): 10923.91304347826 us.
AsyncLiveVars.LiveObj4(RunTime): 10956.284153005465 us.
AsyncLiveVars.LiveObj8(RunTime): 11286.516853932584 us.
AsyncLiveVars.LiveObj16(RunTime): 11445.714285714286 us.
AsyncLiveVars.LiveInt1(RunTime): 11016.483516483517 us.
AsyncLiveVars.LiveInt4(RunTime): 11327.683615819209 us.
AsyncLiveVars.LiveObj2Int2(RunTime): 10918.478260869566 us.
AsyncLiveVars.LiveObj4Int4(RunTime): 10737.967914438503 us.
Calls.AwaitAsyncCall(RunTimeRaw): 1082.2510822510822 ns.
Calls.AwaitAsyncCallClosureTargetPolymorphic(RunTimeRaw): 1056.4124234100993 ns.
Calls.AwaitAsyncCallInstanceTargetPolymorphic(RunTimeRaw): 1134.1726210729273 ns.
Calls.AwaitFutureCall(RunTimeRaw): 865.6509695290858 ns.
Calls.AwaitFutureCallClosureTargetPolymorphic(RunTimeRaw): 841.3967185527977 ns.
Calls.AwaitFutureCallInstanceTargetPolymorphic(RunTimeRaw): 839.066957543212 ns.
Calls.AwaitFutureOrCall(RunTimeRaw): 397.9941096871766 ns.
Calls.AwaitFutureOrCallClosureTargetPolymorphic(RunTimeRaw): 406.17384240454913 ns.
Calls.AwaitFutureOrCallInstanceTargetPolymorphic(RunTimeRaw): 393.7472929873607 ns.
Calls.AwaitFutureOrCallInstanceTargetPolymorphicManyAwaits(RunTimeRaw): 1095.0503723171266 ns.
Calls.AwaitForAsyncStarStreamPolymorphic(RunTimeRaw): 6643.426294820717 ns.
Calls.AwaitForAsyncStarStreamPolymorphicManyYields(RunTimeRaw): 7178.750897343863 ns.
Calls.AwaitForManualStreamPolymorphic(RunTimeRaw): 1456.23998835008 ns.
Calls.SyncCall(RunTimeRaw): 0.3919935321067202 ns.
Calls.SyncCallClosureTarget(RunTimeRaw): 0.3906669661780074 ns.
Calls.SyncCallInstanceTargetPolymorphic(RunTimeRaw): 3.1676143112814583 ns.
Calls.IterableSyncStarIterablePolymorphic(RunTimeRaw): 104.4932079414838 ns.
Calls.IterableManualIterablePolymorphic(RunTimeRaw): 104.57516339869281 ns.
Calls.IterableManualIterablePolymorphicManyYields(RunTimeRaw): 116.92487576731949 ns.
```

TEST=ci
CoreLibraryReviewExempt: Added entry-point pragmas.
Change-Id: I02fbd08141f51c00fb37b6fa0304dc25d6afdb71
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301020
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: William Hesse <whesse@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2023-05-22 08:32:12 +00:00
Joshua Litt e82a5a2ff3 [dart2wasm] Move entrypoint into the lib directory.
This CL will make it easier to consume `dart2wasm` as a library. Specifically, users can import `package:dart2wasm/dart2wasm.dart`, and call `main` from Dart libraries.

Change-Id: I9f5d5ab539677af2604df9d4b0e1e2d82db7485f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300780
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-05-03 16:45:32 +00:00
Jackson Gardner 3015fd34fd [dart2wasm] Allow the user to specify the path of the js runtime file.
Change-Id: Iceb0428f70c77d34daa675e199abf670542babb0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/295220
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
2023-04-14 16:51:18 +00:00
Aske Simon Christensen eca8a9a3ee [dart2wasm] Support commandline arguments to Wasm program
Change-Id: Id44fe9b2faf52071c18c81e9be53a4c04eae1735
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291086
Reviewed-by: Jonas Termansen <sortie@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-03-30 16:20:27 +00:00
Aske Simon Christensen c7432bf694 [dart2wasm] Workarounds to compile and run flute
Change-Id: I9180a2a65d47b11ec5e13d9c003c10b428bc15fc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287540
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2023-03-09 21:10:45 +00:00
Mayank Patke bff851ce7e [dart2wasm] Refactor ConstConditionalSimplifier and related classes
This will allow them to be used with other compiler backends, including
dart2wasm.

Change-Id: I42182545eeb035af8a3a6e2eba209ba2601a52d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281923
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
2023-02-23 18:54:29 +00:00
Josh Soref 77978889eb Spelling
Closes https://github.com/dart-lang/sdk/pull/51143

GitOrigin-RevId: 9e21c99a222d588e4fc95980725a2f8c9784965c
Change-Id: If0870e8936c7649935dce7e23cd783d62aa5610c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279916
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
2023-01-30 18:29:59 +00:00
Ömer Sinan Ağacan 2538a8216b [dart2wasm] Add command-line flag for omitting runtime type checks
The new dart2wasm flag `--omit-type-checks` is similar to dart2js's
`--omit-implicit-checks` and comes with the same caveats: program
semantics is only preserved when it doesn't have any runtime type
errors.

An example benchmark profile:

- Without the flag:
  https://pprof.corp.google.com/user-profile?id=c19135b8eeb8867a3b2ce8ba82d32517

- With the flag:
  https://pprof.corp.google.com/user-profile?id=870a61d7fee3fa6c4dbf4ef95c91ab47

List of checks that we omit with the flags:

- Covariance checks
- `as` checks
- Type checks in dynamic invocations
- Type checks in dynamic closure calls

Change-Id: Ia388e76e7defcdc03dd778e1bccf445388568c87
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279965
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-01-30 12:11:35 +00:00
Joshua Litt 9447b6cc51 [dart2wasm] Remove static dart2wasm JS runtime.
Change-Id: Id1f656a90f09ab28b99ab637f80c025521eeae74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278695
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-01-13 01:19:18 +00:00
Joshua Litt 6d0237a79b [dart2wasm] Generate js runtime at compile time.
This is necessary so we can start specializing JS method trampolines. Specializing JS methods will help reduce polymorphic behavior in JS trampolines for JS interop methods.

In the follow on CL we will start injecting specialized JS method trampolines directly into the runtime.

Change-Id: Ib12fb286f54cfb8a64c9f84e238433e8da2eeaa0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/272441
Reviewed-by: Jonas Termansen <sortie@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-01-10 13:16:29 +00:00
Jackson Gardner 061a9fbefb [dart2wasm] Add --enable-asserts flag.
Change-Id: I4cc9a55673374926e8b19cccf2be9888f166553d
Bug: https://github.com/dart-lang/sdk/issues/50801
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276772
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: William Hesse <whesse@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2022-12-21 19:53:14 +00:00
Aske Simon Christensen 6293797db8 [dart2wasm] Option for writing a Ninja depfile
Fixes https://github.com/dart-lang/sdk/issues/50747

Change-Id: I6ea4dcd5bd07ce4e95583f9a962b0abb17392ab2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276062
Auto-Submit: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-12-19 12:19:55 +00:00
Joshua Litt 9b79ab636c [dart2wasm] Chunk copying large strings to JS.
This change is necessary for large strings because otherwise we hit V8's maximum argument limit to a function.

Change-Id: I2473d1c5acd62f16d509752c437678c697e7ebeb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275480
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2022-12-14 17:16:01 +00:00
Jackson Gardner 00ca59a478 [dart2wasm] Support --multi-root and --multi-root-scheme flags.
Change-Id: I111748742677073bb3cf4c1655b69cb21d42f546
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274089
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
2022-12-08 00:59:23 +00:00
Joshua Litt b493cdfc1b [dart2wasm] Add runner script to streamline running wasm in d8.
Change-Id: I0f891d28419d0df55f94e0229ff33e87b791e7d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273280
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2022-12-02 19:55:16 +00:00
Aske Simon Christensen 9cea247af4 [dart2wasm] Remove the --string-data-segments option
In WasmGC MVP, the `array.new_data` instruction will not be a constant
instruction. This means we can't implement the option that places
eager string constants in data segments and reads them using
`array.new_data`. We can still do this for lazy strings.

Also clean out the string function generator (which is dead since we
started using `array.new_data` for lazy strings) and the special case
for the empty string (which is obsolete since we started using
`array.new_fixed` for eager strings).

Change-Id: If28bdf6913ae0cd0f4faf7d87d64cb9412e56bb5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/271120
Reviewed-by: Ömer Ağacan <omersa@google.com>
2022-11-22 15:11:35 +00:00
Aske Simon Christensen 6ddd14696c Update d8 to 10.9.104
This version contains a number of updates to WasmGC:
- `dataref` is replaced by `structref`.
- `call_ref` now takes a function type immediate, both with the proper
  0x14 opcode and the temporary 0x17 opcode.
- Some of the new type test/cast instructions are implemented,
  specifically `ref.test`, `ref.cast`, `ref.test null` and
  `ref.cast null`. The new `br_on_cast` and `br_on_cast_fail` are not
  yet implemented.

Additionally, the `--wasm-gc-js-interop` flag is removed, and interop
is always enabled.

Change-Id: Ibe7a1d9e8800f89096239ff5afa16e80322e01c5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/266161
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-11-11 15:11:54 +00:00
Aske Simon Christensen 74173f0ac0 [dart2wasm] Print exceptions that escape main or callbacks
Change-Id: I5539f93e0db4c2922da873d0826110acb9cde4c8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/267740
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-11-07 13:33:38 +00:00
Joshua Litt 1dc57aca9e [dart2wasm] Handle default arguments to callbacks more consistently.
Change-Id: I88f511ea84640b9078b788eecba0700b878485d1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/266869
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
2022-11-02 19:12:38 +00:00
Joshua Litt 204c03a98e [dart2wasm] Wire up cfe experimental flags.
Change-Id: Iab9b3a5997e63e61a42bc4fb27825b345ae63ab1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/264060
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
2022-10-17 21:01:33 +00:00
Jackson Gardner 47dd60fed0 Refactor run_wasm.js into an ESM module that is reusable in the browser.
Change-Id: I1566febad2b4e1399f50bb923ad0596f04aa1a40
Cq-Include-Trybots: luci.dart.try:dart2wasm-linux-x64-d8-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262268
Reviewed-by: Joshua Litt <joshualitt@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2022-10-07 03:09:39 +00:00
Aske Simon Christensen e08c94a651 [dart2wasm] Partially lazy constants
Make a constant lazy if it involves creating a list that is too long
for the `array.new_fixed` instruction, or, for composite constants, if
one or more of its sub-constants are lazy.

Also read lazy string constants directly from a data segment instead
of writing them to linear memory first.

Change-Id: Iea26460580e6134cd3e26eaaa84cb0dde09016dd
Cq-Include-Trybots: luci.dart.try:dart2wasm-linux-x64-d8-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/261402
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-09-28 15:20:46 +00:00
Joshua Litt 8429f01593 [dart2wasm] Implement optional parameters for JS interop callbacks.
Also added tests of optional parameters for static interop functions.

Change-Id: Id23237b96d0de5a4a4b948b3f23fd1bfe40b218e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259101
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2022-09-14 22:33:02 +00:00