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