Commit Graph

64 Commits

Author SHA1 Message Date
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
Ömer Sinan Ağacan ef01fbf60a [dart2wasm] Close parens in compile-time error messages
Change-Id: I1f4582317f1df26624978f91ad2d52f39001cb7a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293542
Reviewed-by: Joshua Litt <joshualitt@google.com>
Auto-Submit: Ömer Ağacan <omersa@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
2023-04-05 16:36:27 +00:00
Devon Carew 1dcb466924 [pkg/wasm_builder] use package:lints/recommended.yaml
Change-Id: Id5bda808afbc2404e7fdd2586c64dfe9521f5be3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282382
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-02-13 17:57:24 +00:00
Joshua Litt 130903685d [dart2wasm] Avoid implicitly internalizing WasmExternRef.
CoreLibraryReviewExempt: Wasm only changes.
Change-Id: I0e0a6cddffa1c7f2d7c74779f480dd10bf509c1c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279268
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-02-10 16:22:47 +00:00
Ömer Sinan Ağacan 4c89d9f6e2 [dart2wasm] Improve Wasm stack error messages
Currently if I generate a `call` with incorrect number of values on
stack, I get an error like:

    Unhandled exception:
       local.get 0                i64
       i64.const 1073741823       i64, i64
       i64.gt_u                   i32
       L1: if () -> ()
         local.get 0              i64
         i64.const 0              i64, i64
         i64.const 1073741823     i64, i64, i64
         ref.null Object          i64, i64, i64, ref null Object

    Underflowing base stack of innermost block

After this I usually need to check the type of the function being called
to see how many arguments it's expecting, with their types.

With this change we now report:

    Unhandled exception:
       local.get 0                i64
       i64.const 1073741823       i64, i64
       i64.gt_u                   i32
       L1: if () -> ()
         local.get 0              i64
         i64.const 0              i64, i64
         i64.const 1073741823     i64, i64, i64
         ref.null Object          i64, i64, i64, ref null Object

    Underflowing base stack of innermost block: expected [i64, i64, i64, ref null Object, ref null Object], but stack contained [i64, i64, i64, ref null Object]

Change-Id: I3191ebf6112e90d341cd121a2afd2fc858282c68
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279089
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-01-18 10:13:19 +00:00
Aske Simon Christensen b8f0fda3dd [dart2wasm] Switch to the new WasmGC test/cast instructions
Also implements various related changes to the WasmGC spec:
- Remove `dataref` and add `structref` and `arrayref`.
- Use new encoding for `array.len` without immediate.

Change-Id: Ic1ba67fad694c453fbdae277f5a7e4c94695977f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/266200
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-01-12 14:08:56 +00:00
Jackson Gardner 94475e05f3 [dart2wasm] Emit a generic error for non-dart exceptions coming from JS.
Change-Id: I1a3cb5f5c264c6a8ea1d68da480f7672cf399251
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278371
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-01-10 20:36:28 +00:00
Aske Simon Christensen f053d2170d [dart2wasm] Put function types of imports in singleton rec groups
Starting from ba6be821c99db09b0ef2689dea8f0cb863c47ff5 (11.0.194), V8
enforces type equivalence according to the canonical iso-recursive
types for functions imported from other modules or constructed using
`WebAssembly.Function`, which means that to match such a type, a
function type declared in a Wasm module must be declared in a
singleton rec group.

To be compatible with this new canonicalization scheme, we define the
function types for all imported functions each in their own rec group
separately from other types.

Change-Id: I166b4b7d1c8fa48638f816f874f737536a61f15d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274388
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2022-12-13 20:43:22 +00:00
Aske Simon Christensen 2846cb8da6 [dart2wasm] Add Wasm bottom heap types
- Add getters for the top and bottom types of the WasmGC hierarchy
  that a type belongs to.
- Rename the internal common supertype from `top` to `common` to avoid
  confusion with the top type of each hierarchy.
- Always use a bottom type when emitting a `ref.null` instruction.
- Translate the `Null` and `Never` Dart types to WasmGC `nullref`.

Change-Id: I43ba5da222a848214647980f7b4876940546242a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268463
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-11-18 11:43:52 +00:00
Aske Simon Christensen eaa2ea9aab [dart2wasm] Switch back opcode for call_ref
V8 and Binaryen now both accept a function type immediate for the
version of call_ref using the proper opcode value of 0x14.

Change-Id: I1010de82d97df0ff80ff1365e2f827d7fd78eb81
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/266162
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-11-17 12:59:48 +00:00
Ömer Sinan Ağacan d01dd13a17 [dart2wasm] Minor refactoring in ClassInfoCollector
The interaction between `Translator` and other types like
`ClassInfoCollector` is quite complicated. This patch refactors a few
things to make it slightly simpler.

Changes:

- Add documentation to a few fields, minor rewording in a few other
  field documentation.

- Make `ClassInfoCollector`'s `nextClassId`, `computeRepresentation`,
  `addTypedDataFields` private.

- Initialize of `ClassInfo`'s `nullableType`, `nonNullableType` in
  constructor.

Change-Id: I48e55029844ff41d9f043aac004a1abad8df83dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265281
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2022-10-28 17:43:08 +00:00
Aske Simon Christensen c975c5c713 [dart2wasm] Add function type immediate to call_ref
The encoding uses the 0x17 opcode temporarily assigned to `call_ref`
with immediate in V8 and Binaryen for transitioning from `call_ref`
without immediate. After V8 and Binaryen change the proper 0x14
encoding to accepting an immediate, we can switch back to 0x14.

Change-Id: Ib84c62068e73a101d23a1bbd7eb3b893ef7a1493
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258802
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2022-10-28 16:25:58 +00:00
Joshua Litt f0ab6fb6ef [dart2wasm] Fix memory definition.
Size is number of 64kb pages.

Change-Id: I4c2876ebee553d0dc219e8dd8bb6b1169799a465
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257260
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2022-09-06 18:25:36 +00:00
Aske Simon Christensen a85157e233 [dart2wasm] Convert to/from externref via Wasm instructions
This adds support for the newly added `extern.externalize` and
`extern.internalize` instructions and uses these for converting to/from
`externref` instead of a JS round-trip.

Change-Id: If18d8f44ddf013d4c26bf1597be91bcd0db41c5a
Cq-Include-Trybots: luci.dart.try:dart2wasm-linux-x64-d8-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255462
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2022-08-19 07:11:18 +00:00
Aske Simon Christensen b1c0992199 [dart2wasm] Implement the three-pronged WasmGC type hierarchy.
This splits the type hierarchy into three separate hierarchies with
the top types `any`, `func` and `extern`.

Update d8 to 10.6.91, which switches to the new type hierarchy.

Also, all ref shorthands for abstract heap types are now nullable, so
the type emitter in the `wasm_builder` is updated to follow that scheme.

To reduce confusion about the nullability of abstract reference types,
these now all require the nullability to be specified explicitly.

Change-Id: I4774d08cbed18307e481c466b2e3402a8d8fb6bd
Cq-Include-Trybots: luci.dart.try:dart2wasm-linux-x64-d8-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255060
Reviewed-by: Joshua Litt <joshualitt@google.com>
2022-08-19 07:11:18 +00:00
Aske Simon Christensen 5ea5e3f0b2 [dart2wasm] Remove RTT support and emit isorecursive types
Relatedly, remove the --nominal-types and --runtime-types options and
eliminate the instruction and type wrappers that abstracted over these
options.

Rename instructions to the names in the latest WasmGC working draft.

Change-Id: Icd0e4bbcf444d5a3cea1f88f4e3bf8dd2926f70f
Cq-Include-Trybots: luci.dart.try:dart2wasm-linux-x64-d8-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254902
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
2022-08-16 08:31:57 +00:00