- 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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
- 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>
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>
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>
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>
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>
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>
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>
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>
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>
- 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>
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>
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>
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>
- 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>
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>
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>
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>
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>
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>
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>