Commit Graph

79 Commits

Author SHA1 Message Date
Ömer Ağacan 26a065c60d [wasm_builder] Update names of instructions extern.internalize/externalize
These instructions were renamed to any.convert_extern and extern.convert_any in
Wasm GC spec commit:
https://github.com/WebAssembly/gc/commit/5bca3f7de6ad117a0d4349c138b5e32981c88d2f

Change-Id: Ie696541f75974c69828fb317754e3b613691eb4d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/420141
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2025-04-03 14:24:31 -07:00
Ömer Ağacan 82282b6287 [dart2wasm] Generate local names in the names section
Local names for function value parameters and for "precise this", return
values are generated, state indices in `async` and `sync*` functions are
generated.

We can generate names for more locals as needed.

Change-Id: Ie919f030f0bfae8adbca90408509dd04a7414278
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419200
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2025-04-02 01:52:18 -07:00
Ömer Sinan Ağacan 99d5a32aa1 [dart2wasm] Generate field names in the names section
Change-Id: Ia2fdbb63c20cde2fed432fd89f93acb9373239f4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381840
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2025-03-31 04:52:11 -07:00
Ömer Ağacan c0e8f907e3 [wasm_builder] Add new exception handling instructions
These instructions are not used yet as they're not enabled by default
in Chrome yet.

This CL is mainly tested by the child CL, which uses instructions added
in this CL for exception handling.

Change-Id: I04d767599f47cdb6abc9cca02974647d9e5421fb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411581
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2025-03-20 10:10:22 -07:00
Sigurd Meldgaard 2fe05bd568 Reland "Migrate to use pub workspace"
This is a reland of commit b9b77058a9

Original change's description:
> Migrate to use pub workspace
>
> Use `pub get` to generate `.dart_tool/package_config.json` on gclient sync.
>
> All pkg/ (and a few third_party) packages that are developed inside the sdk repo are included in the workspace from the root `pubspec.yaml`.
>
> All dependencies that are pulled in via DEPS are added as path dependencies via `dependency_overrides` in the root `pubspec.yaml`.
>
> Bug: https://github.com/dart-lang/sdk/issues/56220
> Change-Id: I38c12b608c68da54c57821116cf9aa6696936746
> Tested: relies on CQ of existing tests. Should have no effect on functionality
> CoreLibraryReviewExempt: only core library change is adding a `// ignore:` comment. Should have no influence on functionality
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397164
> Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
> Reviewed-by: Alexander Thomas <athom@google.com>

Bug: https://github.com/dart-lang/sdk/issues/56220
Change-Id: I29afabade2d2447dea05121cb87ff50bb21a4b76
Cq-Include-Trybots: luci.dart.try:flutter-linux-try,flutter-web-try
Tested: relies on CQ of existing tests. Should have no effect on functionality
CoreLibraryReviewExempt: only core library change is adding a `//
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415561
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2025-03-20 06:19:16 -07:00
Sigurd Meldgaard 6c4de1ab7e Revert "Migrate to use pub workspace"
This reverts commit b9b77058a9.


Revert "Add missing sample pubspec to workspace"

This reverts commit 892ea15ac7.


These seem to break the engine.

Change-Id: Ieee26deb7928c3869a1b6265326c3ce568ffe731
Tested: this is a revert.
CoreLibraryReviewExempt: this is a revert
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415582
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2025-03-14 09:08:20 -07:00
Sigurd Meldgaard b9b77058a9 Migrate to use pub workspace
Use `pub get` to generate `.dart_tool/package_config.json` on gclient sync.

All pkg/ (and a few third_party) packages that are developed inside the sdk repo are included in the workspace from the root `pubspec.yaml`.

All dependencies that are pulled in via DEPS are added as path dependencies via `dependency_overrides` in the root `pubspec.yaml`.

Bug: https://github.com/dart-lang/sdk/issues/56220
Change-Id: I38c12b608c68da54c57821116cf9aa6696936746
Tested: relies on CQ of existing tests. Should have no effect on functionality
CoreLibraryReviewExempt: only core library change is adding a `// ignore:` comment. Should have no influence on functionality
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397164
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2025-03-14 05:21:40 -07:00
Nate Biggs 7b28c5a94f [dart2wasm] Update serialization strategy for dynamic modules.
Introduces some more machinery to simplify the serialization pipeline. Now the logic to index kernel nodes is hidden away in the serializer. Now throughout the compiler if entities (i.e. classes, members and references) need to be serialized they can be passed to the serializer directly.

Also adds support for tracking brand type assignments from the main module to the dynamic module. This is necessary to decouple the execution order of both compilations which is a WIP.

Change-Id: I38dd0ed11811ea12a2230959588b261719976592
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412461
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2025-03-07 16:45:35 -08:00
Sigurd Meldgaard a9ab52bcbb Language version bumps for publish_to: none pkgs
Also a separate pubspec for the tools/ folder

Extracted from https://dart-review.googlesource.com/c/sdk/+/397164

Change-Id: If49a6ede07e4864d0c0fcb210c04c18c804849df
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412041
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2025-02-27 00:48:40 -08:00
Ömer Sinan Ağacan 26f08dd036 [wasm_builder] Replace parts with libs
Parts make it difficult to see dependencies between files and which
files can be imported. Replace them with libs.

Change-Id: I0a07b48bc3c4a545a420f3efae23f61c4918bdcf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410400
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2025-02-18 02:35:11 -08: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
Martin Kustermann d704828c34 [dart2wasm] Use Uint8List for representing bytes
We should use `Uint8List` to represent bytes in the wasm serialization
code. For cases where we only write two fixed bytes, we use `writeByte`
instead (which is faster than `Uint8List.setRange(<int>[a, b])`. We use
a cached `ByteData` to write little-endian float32/float64.

Change-Id: Id5662c1b7a3ba15f087341b13a99bbfdfa37059e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403864
Reviewed-by: Slava Egorov <vegorov@google.com>
2025-01-10 02:21:33 -08:00
Nate Biggs 59499d362a [dart2wasm] Add support for declaring functions as module elements.
Allows functions to be referenced in a constant context (via ref_func) without needing to bind the function to a global.

Uses format 3 supported by the element section:
https://webassembly.github.io/gc/core/binary/modules.html#element-section

Change-Id: Ifb68800c480b14acfa3432eae80396be4e9c8a6e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400900
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-12-17 12:05:57 -08:00
Nate Biggs 4fc0590bd1 [dart2wasm] Add asserts ensuring exportable entities are only directly accessed from their own modules.
Change-Id: Ia5d5493e4db2710164d6966e97a0854fe2582f28
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391860
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2024-10-25 07:41:59 +00:00
Ömer Sinan Ağacan 643d1ba3d6 [dart2wasm] Check const-ness of instructions added to const exprs
A constant expression in Wasm is an instruction sequence that consists
of only the constant instructions.

Currently if I generate a non-constant instruction in a constant
expression I get an error during Wasm compilation. I then have to find
where and how I generate the instruction, possibly by using `--watch
<instruction offset>` passed to dart2wasm.

With this change we check in an assertion that an instruction added to
an instruction sequence for a constant expression can only be a constant
expression.

This helps with debugging as I notice the error earlier, and get a stack
trace of the compiler function that generates the invalid instruction.

Changes:

- Add `bool get isConstant => false` member to `Instruction`.

  A method instead of field to avoid adding one more word to
  `Instruction` objects.

- Override the member in constant instructions.

- When creating an instructions builder, mark the builder as "constant",
  with an optional named constructor argument that defaults to `false`.

- When adding an instruction to an instructions builder, check that if
  the instruction block is for a constant expression, the instruction is
  constant.

Spec:
https://webassembly.github.io/gc/core/valid/instructions.html#constant-expressions

Change-Id: I59454f0634ab419a62f9455c84aa9ce631db8b63
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391440
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2024-10-22 14:47:08 +00:00
Nate Biggs b79a06d99b [dart2wasm] Add last few fixes to translator to allow module test mode.
- Declare dummy value globals as needed per module. We cannot share these across modules as they can be used in a const context which limits  how we can reference them. The other option is to declare the dummy values for all heap types in the main module. However, declaring as needed per-module is more in line with our approach elsewhere and will work better for dynamic modules.

Change-Id: Ib2cd0a9300610ff8aa86d904902815d4fe9042d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385401
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-23 18:56:24 +00:00
Nate Biggs 9cd9071a7b [dart2wasm] Add support for module name in wasm NameSection.
This annotates each code location in wasm stacktraces with the name of the referenced module. By default v8 only includes an opaque hash to name each module.

For tests and debugging where we want to analyze stacktraces the opaque hash alone makes it impossible to associate each code location back to a file. With multiple modules we will need this additional information.

Change-Id: I464780d9234e0685d08f4f1925f65ce073b3f322
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385400
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-23 18:56:24 +00:00
Nate Biggs 7deaf2053d [dart2wasm] Update imported table elements map to avoid duplicate entries clobbering each other.
Specifically for the dispatch table, a single function can map to multiple indicies so the function -> index map is not injective and we end up overwriting entries.

Change-Id: I6ca69d5220b539328bafd215d45c52dac9d2e72e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385181
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2024-09-13 15:58:12 +00:00
Nate Biggs 4775207e51 [dart2wasm] Add deferred loading support to dart2wasm (12/X).
Add map-like entity that handles importing/exporting wasm entities as they are needed across modules.

Expands the Exportable interface to support this functionality since these are the entities that can be shared across modules.

Change-Id: Iefc9320af48c36ea039355a573943c9dbcc4ed8a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383222
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2024-09-04 21:58:12 +00:00
Nate Biggs 7d0742c9e1 [dart2wasm] Add deferred loading support to dart2wasm (9/X).
Update intrinsic field table allocation to support importing tables across modules. The tables will always be defined in the main module but can be imported into subsequent modules.

Change-Id: I09c8f316f528fa28c02fddcf356a33583a88ef56
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381442
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-04 21:58:12 +00:00
Nate Biggs c5bd4d05db [dart2wasm] Add deferred loading support to dart2wasm (6/X).
Wasm globals serve a few purposes in the compiler such as storing static fields and closure vtables. Sometimes the access of these globals will be from a different module than the ones they're defined in. We need some indirection to be able to access them in these cross-module situations.

This change adds getter and setter functions that can be called via the StaticTable when a global needs to be accessed from a different module.

We use References to track the owning module for each global to determine if we can access it directly or not.

Change-Id: I93191c83dee1b7a47171c5808e64b071479cdeea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381324
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-04 21:58:12 +00:00
Nate Biggs a7f5845a4e [dart2wasm] Add deferred loading support to dart2wasm (5/X).
Add support for a StaticTable which holds references to known functions that need to be called across modules. For calls that target the DispatchTable we will still go through there if possible. But for any functions not referenced in that table (including any compiler generated functions) we add a separate static table.

Also adds import/export support to both the DispatchTable and the StaticTable. The table will always be defined in the main module and imported into subsequent modules.

Change-Id: Iedc683d1ecfe721393900913826010cdd9b2c3c4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381323
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-09-04 21:58:12 +00:00
Nate Biggs 04f3e7a740 [dart2wasm] Use brand types for wasm minimal recursion groups.
Brand types encode an integer as a struct type and allow us to break the equivalence relation between similar types in different rec groups.

We now compute equivalence classes at the rec group level reducing the number of brand types we have to assign.

We also improves the equivalence class computation by exiting earlier when a group/type is found to be part of an equivalence class. This is enough information to tell us which brand type it needs so we can exit the inner loop early.

Change-Id: I8c5e1cfc679f1fcb6b7f0e79bd71719f345784d2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381960
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2024-08-28 23:44:04 +00:00
Sigmund Cherem b6565dad99 [dart2wasm] Reduce cost of building recursion groups.
Dart classes that are structurally equivalent are currently
merged into the same recursive group to allow binaryen to distinguish
them and ptimize them independently.

This change reduces the cost of checking for structurally equivalence:
* We skip it during the discovery of available types
* We skip trivial comparisons (self, reverse, irrelevant types)

This recovers the compile-time regression mentioned in
https://dart-review.googlesource.com/c/sdk/+/378764/comment/f7990317_f4b34810/

Change-Id: I5c97b1498f261c8549b70fd3622c0762499eda21
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/379754
Reviewed-by: Nate Biggs <natebiggs@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-08-14 00:18:22 +00:00
Ömer Sinan Ağacan 1b1740e941 [dart2wasm] Pass source maps to wasm-opt when optimizing
To be able to know when we are generating a source map, make `dart
compile wasm` aware of the `--no-source-maps` flag.

The "name" segments of source mappings are also made `null` with this
patch. Browsers don't use that segment and binaryen doesn't support it.

Change-Id: I7b52c8fb7cef92ed60547e97ad137e0cd3967f26
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378421
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-08-09 14:23:29 +00:00
Nate Biggs d9b5eec829 [dart2wasm] Modify wasm_builder to support multi-module deferred loading (2/2).
Golem results verifying no regressions from this change:
https://golem.corp.goog/Revision?repository=dart&revision=111273&patch=19368

This modifies the wasm_builder to emit types in minimally sized recursive groups.

The main motivation of these changes is to minimize the size of the type section when dart2wasm produces multiple modules. If we included every type in every module, for small modules the type section would be a very significant % of the total file size (in a small example it was ~98% for a deferred module).

The two main changes are:
1) Type tree-shaking for each module.

We detect which types are actually used in that module (via instructions, function signatures, etc.) and only include the detected types in the module's type section. This simplifies the compilation pipeline as we don't need to worry about assigning types to modules, we can just build the module as before and post-process the IR to collect the set of used types.

2) Create minimally-sized recursive groups.

In order for wasm type-checking to work across modules, equal types have to be in equal recursive groups. Tree-shaking therefore has to occur at the rec group level as opposed to the individual type level. So to make tree-shaking effective, we need the smallest possible rec groups.

We achieve this by creating a graph of the wasm types' dependencies and then calculating the set of strongly connected components for that graph. Each component represents a minimally-sized recursive group. The DAG formed by the components is the order to emit them so that definitions come before usages.

Importantly, by separating types into different rec groups, we are also changing the equivalence relationship between them. This can have a meaningful impact on binaryen's ability to optimize the module, two types that were distinguishable might not be anymore. To avoid this regression we group together any types that are structurally equivalent. This way binaryen will differentiate them as separate types as they were in the original Dart source.


Change-Id: I67acdd21a89ff2718e8bbd6360f342c150494a9a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378764
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2024-08-07 17:27:38 +00:00
Nate Biggs 1e3fb80199 [dart2wasm] Modify wasm_builder to support multi-module deferred loading (1/2).
None of these changes modify the generated wasm in any way yet. But all these changes allow dart2wasm to generate multiple modules to implement deferred loading and eventually dynamic modules.

1) Exposes tag export functionality.

This functionality is supported by the exception handling spec/proposal but just wasn't added to wasm_builder originally:
https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md

The exception tag needs to be exported from the main module and then imported into subsequent modules so that exception handling works in a multi-module environment.

2) Support updates to imported tables via the element section.

The element section provides an easy way to assign functions to indices in a table. This assignment can be applied to either a defined table or an imported table. wasm_builder only supported tracking element assignments for defined tables but deferred loading will make use of imported tables to which we will also want to assign functions.

Change-Id: If23fbbb1e60a67d80d11924aaa718c50e68ecb98
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378840
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2024-08-07 17:27:38 +00:00
Martin Kustermann 15818c52d8 [dart2wasm] Decouple various code from the current function that is being compiled
There's various components in the code generation that should not depend
on the current function. This is especially relevant when inlining, as
we want to emit wasm for the inlined body in the context of an unrelated
caller function. The codegen for the inlined body should therefore not
be aware or require knowing anything about the function it's being
inlined into.

Also results in net removal of code.

Change-Id: I808b6f10fa3f262ec7e0b5a4b2b6e42d60ff6582
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378144
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2024-08-01 08:59:39 +00:00
Ömer Sinan Ağacan 10742d9a12 [dart2wasm] Generate source maps
This implements generating source maps for the generated Wasm files.

Copying dart2js's command line interface, a source map file with the
name `<program name>.wasm.map` is generated unless `--no-source-maps` is
passed.

When a source map is generated, the generated .wasm file gets a new
section `sourceMappingURL` with the contents `<program name>.wasm.map`.

This section seems to be undocumented, but Chrome and binaryen recognize
it as the URI to the source map file. Chrome is then loads it
automatically in the DevTools.

## Implementation

- `wasm_builder` package is updated with the new `source_map` library,
  which describes the source mapping entries.

- `wasm_builder`'s `InstructionsBuilder` is updated with the new public
  members:

  - `startSourceMapping`: starts mapping the instructions generated to
    the given source code.

  - `stopSourceMapping`: stops mapping the instructions generated to a
    source code. These instructions won't have a mapping in the source
    map.

- `CodeGenerator` sets the source file URI and location in the file
  when:

  - Starting compiling a new member
  - Compiling an expression and statement

Change-Id: Ic8f723f7a154402c0d34710689db57d640b83b86
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370500
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-07-04 09:25:39 +00:00
Ömer Sinan Ağacan 0b027bc1ba [wasm_builder] Avoid generating unreachable instructions
After an instruction like `unreachable` or `throw`, stop generating
instructions until the end of the block.

This doesn't change wasm-opt output, but helps when debugging.

jsc test failures are probably because of
https://dart-review.googlesource.com/c/sdk/+/373240.

Closes #56082.

Change-Id: Iaa86c4eff3e1d5769667d9cdaa6de68f89c099ba
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373280
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-06-28 07:51:26 +00:00
Ömer Sinan Ağacan da4bf60a09 [wasm_builder] Update incorrect docs, some minor refactoring
- Imported things (modules, globals, tables, functions) don't need to
  declared before defining things, as we don't assign indices to things
  before finalizing the IR, and when finalizing we assign ids to
  imported things before defined things, in
  `finalizeImportsAndBuilders`.

  Remove documentation saying imports should be declared before
  definitions.

  `FunctionsBuilder._functions` list was used in the name section, and
  required imports to come before definitions. This list is now removed,
  instead we pass `[...imported, ...defined]` to the names section.

- Stop upcasting imported things to `Import` before we need to upcast.

- Fix global lists passed to `NameSection`.

  These lists need to include imports as well (even though we never name
  them, wasm_builder doesn't allow naming them yet) otherwise the
  indices in the name section will be incorrect.

  wasm_builder doesn't allow importing types yet, so we don't need to do
  the same for the types list.

Change-Id: Id05632c3af7937bd66d7581d89d538137020f6e6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366601
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2024-05-15 09:49:02 +00:00
Ömer Sinan Ağacan 0307423810 [wasm_builder] Remove array.new_data handling in global sections
As of 2025/05/14, `array.new_data` is not a constant instruction, so
can't be used in a global initializer.

Also, data count section needs to appear right before the code section.
Without `array.new_data` we don't need to generate it before the global
section.

Remove the code related to handling `array.new_data` in global sections.

When `array.new_data` becomes constant, we can generate the data count
section in its new place always, without having to specially handle it
when it's used in a global section.

Change-Id: If11824e171d7f21848e82c102f0a4e8257f02c6f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366341
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-05-15 07:55:13 +00:00
Ömer Sinan Ağacan 511bffedca [dart2wasm] Allow giving names to globals in the module globals section
Currently no globals are named, so the subsection will be empty. Three
extra bytes will be generated in the names section for the empty
subsection.

When debugging we can now name globals with the optional positional
argument:

    // "my global" is new:
    m.globals.define(w.GlobalType(type), "my global")

wasm-opt and v8 (d8, wami) support the subsection, so the global will
now appear as "my global" in the debugger and Wast outputs.

Change-Id: I5988ab792209c5c82593b85c48fead65ad536031
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/355120
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2024-03-07 11:51:57 +00:00
Martin Kustermann ab09f99638 [dart2wasm] Outline <obj> is/as <type> checks to shared helper functions
This reduces in Flute size of optimized wasm by

  * 1.5% in -O3
  * 0.7% in -O4

We rely on binaryen to inline small enough checks (e.g. `is` check
with a single cid-range)

Change-Id: I8f2c093daebe47d6e066b8681c1e285fa5fa0ea0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/351780
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2024-02-12 12:06:07 +00:00
Ömer Sinan Ağacan 338f217fb7 [dart2wasm] Fix table.get/set instruction builders
Change-Id: Ife3a14b3b930e4f4684cbc3bae1d18ca48509096
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340404
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-07 15:05:14 +00:00
Alexander Thomas 565afe4237 [wasm_builder] Remove duplicate LICENSE file
The top level LICENSE file already covers wasm_builder and this is not
meant to be published separately from the Dart SDK right now.

Bug: b/286184681
Change-Id: Id11bdf5c4939a94e1074d94b803e3281f0d2c153
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/331049
Auto-Submit: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-10-23 12:16:14 +00:00
Jackson Gardner 13a425d941 [dart2wasm] Switch to final GC encodings & Roll Chrome 119 + D8
Change-Id: If63a5b3caa8fe300d97a722c3d42929457bae1e2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326300
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-10-13 17:09:19 +00:00
Devon Carew e2fe203adc [deps] roll package:lints to the latest
Change-Id: I582f956cd4b712203c2f6dd630b4e1384040446d
Tested: analysis clean (this is a lint only related change)
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/329400
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
2023-10-05 03:36:59 +00:00
Ömer Sinan Ağacan fa1fdc8667 [dart2wasm] Simplify watchPoints field types
There's no difference between a module watchpoints field being `null` or
`[]`, so the type `List<int>? watchPoints` can be simplified as
`List<int> watchPoints` and by treating empty list the same way as
previous `null`.

Change-Id: I9be8c872c4336b1c0bf5157af5e8587136cf2088
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325261
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-09-27 04:37:38 +00:00
Devon Carew 3b0b1bd863 Bump package:lints to the latest; address instances of new lints.
Changes:
```
> git log --format="%C(auto) %h %s" 8d5f750..b044aca
 https://dart.googlesource.com/lints.git/+/b044aca add several rules to core and recommended (150)
 https://dart.googlesource.com/lints.git/+/81100a2 fix a dangling table link (146)
```

Diff: https://dart.googlesource.com/lints.git/+/8d5f7500024320654adb1e799e49fc10c5304ae7..b044acab9f6669b3d8e781923a8ff86877801177/
Change-Id: I031333ade99af700a7009b14a36d3aadba12fc94
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/327321
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
2023-09-25 21:15:15 +00:00
Aske Simon Christensen 27d321e86c [wasm_builder] Fix --watch option to work with instructions
The `--watch` debug option to print the stack trace leading to a
particular byte in the output Wasm file didn't work for instructions
after the instruction encoder was changed to produce intermediate
instruction objects.

This is fixed by keeping a map from instruction objects to stack
traces in the instructions builder and registering those stack traces
when serializing the instructions.

Change-Id: I90d665753813452d07783c7f47e1e6bf63a3a18e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324681
Reviewed-by: Jess Lally <jessicalally@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-09-07 10:43:49 +00:00
Aske Simon Christensen 3df48faff1 [wasm_builder] Fix failure type of br_on_cast[_fail]
The Wasm validation rules state that the failure type is sharpened to
non-nullable if the cast target type is nullable.

Change-Id: I77596c31ea7443c5dace543d13024262879a5fb5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/318660
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2023-08-09 08:24:37 +00:00
Joshua Litt 0332c70638 [wasm_builder] Lift import / definition restrictions.
Change-Id: Id0651e8e2ad270be5cc3788d361257769088faa6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316627
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-08-07 17:08:13 +00:00
Aske Simon Christensen 698b5448e1 [dart2wasm] Use new br_on_cast[_fail] encoding with input type
Change-Id: Ie00cdaf30b97f009b4474a032a6b00bddb848ff7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/317541
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-08-03 16:56:15 +00:00
Joshua Litt 96d6c2e0d3 [wasm_builder] Refactor to a builder / built pattern.
This CL mostly just moves code around. There are three broad changes in this CL:
1) Reify the builder / built pattern that exists implicitly in the existing code. Builders now live in `src/builder`, while the built ir lives in `src/ir`.
2) Reify the module subsections.
3) `pkg/dart2wasm` has been updated to use the new API.

There is only one minor logic change in the entire CL, we now defer serialization of a module until the bytes are actually required, as opposed to serializing eagerly.

This change is designed to make the wasm_builder more robust. By clearly delineating which parts of the AST are mutable and which parts are immutable, then it should make it easier for users of the wasm_builder to avoid undefined behavior, i.e. holding on to something that can change.

Change-Id: I676107b867aa74fabf413108673e170126bdb5c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316280
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-07-31 15:27:58 +00:00
Joshua Litt d1660236f1 [wasm_builder] Defer encoding instructions until Module.encode.
This CL lays the foundation for allowing us to emit instructions without finalizing imports, which will in turn enable more flexibility in code generation. For example, we will no longer need to do a pre-code generation pass to collect imports. In addition, it will pave the way for other optimizations as suggested in the `TODO` at the top of `instructions.dart`.

Perhaps counter-intuitively, even without any optimizations, this doesn't seem to be a measurable regression:

Before:
  Completed compilation of dart2wasm-html-engine in 52652ms.
  Completed compilation of dart2wasm-html-html in 51827ms.
  Completed compilation of dart2wasm-html-ui in 20399ms.
  Completed compilation of dart2wasm-canvaskit-canvaskit in 32899ms.
  Completed compilation of dart2wasm-canvaskit-ui in 20695ms.
  Completed compilation of dart2wasm-skwasm-ui in 20319ms.

After:
  Completed compilation of dart2wasm-html-engine in 51476ms.
  Completed compilation of dart2wasm-html-html in 48845ms.
  Completed compilation of dart2wasm-html-ui in 19676ms.
  Completed compilation of dart2wasm-canvaskit-canvaskit in 31933ms.
  Completed compilation of dart2wasm-canvaskit-ui in 19733ms.
  Completed compilation of dart2wasm-skwasm-ui in 19962ms.

Change-Id: Ib3740f88db56070fc3ccdde484675267e4bf40c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315580
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-07-25 13:55:10 +00:00
Ömer Sinan Ağacan 8b709ded96 [dart2wasm] Generate type names in names section
Change-Id: I35879dba0e5e4ecee279bdd8bf725798cd806595
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315402
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-07-21 17:36:47 +00:00
Aske Simon Christensen bac7e9b8c0 [dart2wasm] Emit encoding for final types.
This marks all types without subtypes as final and all types with
subtypes as non-final. This makes the emitted types maximally final
and is appropriate for a closed-world module.

V8 currently ignores the finality of types for the temporary WasmGC
encoding, which make our current encoding work even though it
implicitly marks some types as final that shouldn't be. For the final
WasmGC instruction encoding, V8 is likely to reject modules containing
final types with subtypes.

Change-Id: If3030c49e8fe60fec8099b731b6fcf618d6a7e64
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310163
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-07-21 15:52:38 +00:00
Ömer Sinan Ağacan 463e251334 [dart2wasm] Optimize some list operations
- Use `array.copy` and `array.fill` when possible.

- Use the Wasm array directly (instead of `[]` and `[]=`) when possible
  to avoid redundant bounds checks.

- `_GrowableList.insert` is rewritten to avoid a redundant write when
  the element is not added to the end.

- Remove redundant bounds checks when calling `Lists.copy`.

Change-Id: I08d96b56201cbb4ff24ca969b7fde8bcc1315e4b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315120
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-07-21 10:29:46 +00:00
Ömer Sinan Ağacan 1d8dde36e1 [dart2wasm] Add array.copy instruction and intrinsics
`array.copy` is used in [1] in `setRange` when source and destinations
are both typed arrays with the same Wasm array type.

[1]: https://dart-review.googlesource.com/c/sdk/+/312907

Change-Id: Iaeecea43c22805eca64b7d98751a52e607210a70
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/314080
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-07-17 16:51:25 +00:00