Commit Graph

452 Commits

Author SHA1 Message Date
Martin Kustermann 83cf40afef [dart2wasm] Add support for compile-time/code-size/memory-use benchmarks of dart2wasm compiler
```
% DART_CONFIGURATION=ReleaseX64 pkg/dart2wasm/tool/compile_benchmark --
CompileFluteComplex.CompileTime.Total(CompileTime): 34.48 s
CompileFluteComplex.CompileTime.Dart2Wasm(CompileTime): 12.74 s
CompileFluteComplex.CompileTime.Wasm2WasmOpt(CompileTime): 21.74 s
CompileFluteComplex.CodeSize.mjs(CodeSize): 11333 bytes
CompileFluteComplex.CodeSize.mjs.gz(CodeSize): 3144 bytes
CompileFluteComplex.CodeSize.wasm(CodeSize): 3462517 bytes
CompileFluteComplex.CodeSize.wasm.gz(CodeSize): 1019112 bytes
CompileFluteComplex.CodeSize.wasm.opt(CodeSize): 1500111 bytes
CompileFluteComplex.CodeSize.wasm.opt.gz(CodeSize): 477204 bytes
CompileFluteComplexMemoryUse.Max(MemoryUse): 574341120 bytes
CompileFluteComplexMemoryUse.Dart2Wasm(MemoryUse): 574341120 bytes
CompileFluteComplexMemoryUse.Wasm2WasmOpt(MemoryUse): 487387136 bytes
```

We measure all metrics (compile-time, code size, memory use) separately
for dart2wasm and wasm-opt/binaryen. We also report total compilation
time as well as maximum memory usage across the tools.

Change-Id: I94c190475443b52d437a21ba85d5c5433f03dc50
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/341486
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-12-14 14:59:33 +00:00
Martin Kustermann fc02d365cc [dart2wasm] Fix caching of array types: The cache has to take into consideration the mutability bit
This fixes web/wasm/wasm_types_test in jscm mode.

TEST=web/wasm/wasm_types_test

Change-Id: I4757707d245d7419dac28ec957d7d76b32e9e4d6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/341500
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2023-12-13 09:51:51 +00:00
Martin Kustermann bdefe5fa7e Move Wasm{Object,Int,Float}Array -> WasmArray, use extensions on WasmArray for static dispatc0h
The dart2wasm compiler treats wasm arrays of objects/ints/floats pretty
much the same, there's no reason to have complicated class hierarchy and
different kinds of wasm array classes.

Also: We rely on all operations on wasm arrays (and other built-in
types) to be handled on the call site. Wasm arrays are not dart objects,
don't support virtual dispatch. So we make operations operating on the
wasm types extensions.

This also makes now loads and stores into the wasm arrays simple `[]` and
`[]=` operations. We still have special extensions for reading/writing
to integer/double arrays that not only read/write but also
sign-extend/zero-extend and operate on Dart's `int` / `double`.

The compiler will allow `WasmArray<X>(length)` for types `X` that have
default-value in wasm arrays (nullable / integer / double types) and
have another `WasmArray<X>.filled(length, X)` for non-nullable reference
types.

Overall this reduces LOCs and simplifies things

Change-Id: I885bed3dfd1c602dc7b0747c69927d464376383d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340881
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-12-12 17:05:49 +00:00
Ömer Sinan Ağacan d548c07996 [dart2wasm] Fix async* stream types when fn return type is not Stream
Fixes #53234.

Change-Id: I97aec5d61bf442ecdaeb2ea2dc61d499ab167dd2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340862
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-12 14:13:08 +00:00
Srujan Gaddam c1ba3cf699 [dart2wasm] Fix handling of generic callbacks in trampoline
Closes https://github.com/dart-lang/sdk/issues/54192

Callbacks were being casted to their static type before being
called. With generic callbacks, we do not know their exact type
at compile time, therefore cannot make that cast. Similarly, a
null check was being done if the static type of the parameter
was potentially non-nullable, whereas it should be done if it's
*not* potentially nullable.

Change-Id: I3d30e901f1ff2ae4c17887564f15368244baeb24
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340390
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2023-12-12 04:50:13 +00:00
Ömer Sinan Ağacan 3873c04638 [dart2wasm] ParamInfo documentation and minor refactoring
Change-Id: I73cdec947c3d7c5b0d1a5d2955b97273c11e1ec5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340920
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2023-12-11 22:21:27 +00:00
Martin Kustermann 72f6db9261 Use WasmObjectArray instead of List in RTT metadata
This reduces flute's complex.dart slightly

  complex.wasm 1596131 -> 1563506 (-2%)
  complex.wasm.unopt 3562573 -> 3535797 (-0.7%)

And makes the code probably slightly faster

Change-Id: Id35f2b156d39b6e77b62240a83f78914999bb744
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340565
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-12-11 12:24:04 +00:00
Ömer Sinan Ağacan 41ee6a5a94 [dart2wasm] Handle JS exceptions in await exprs, finally blocks
Fixes #54024.

Change-Id: I5c92f12a453a550ce76b340116f57b329ffa97f7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335740
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-11 09:34:28 +00:00
Ömer Sinan Ağacan 2f1506822c [dart2wasm] Skip synthetic mixin class names in type name list
Reduces optimized binary size (with names section) of devtools from
10,322,431 to 10,153,197 bytes (-1.6%).

Change-Id: Ife9f803846a054cfbe3910008dc62d51d7a2c7bd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340564
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-09 09:44:44 +00:00
Martin Kustermann e0485a4fc0 Make _FunctionType fields use WasmObjectArray instead of List
This reduces flute's complex.dart slightly

  complex.wasm: 1601793  -> 1596131 (-0.35%)
  complex.wasm.unopt: 3570856 -> 3562573 (-0.23%)

And makes the code probably slightly faster

Change-Id: I5b5cb8f95509930ce23c7d4bc0385740ad9f429b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340562
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2023-12-08 18:48:00 +00:00
Ömer Sinan Ağacan 7cceaebd49 [dart2wasm] Start using WebAssembly.String imports
Refactor `JSStringImpl` to use the js-string-builtins API[1].

When the module `WebAssembly.String` is not available we define a
"polyfill" with the previous imports, so this change is backwards
compatible.

Also updates some of the methods to use avoid multiple `this.length`
calls (which do an FFI call), and use unchecked getters when possible.

A new library `dart:_error_utils` is introduced for faster range and
index checks when the length (or max value) is known to be positive
(e.g. when it's the length of a string).

For now only `JSStringImpl` and `operator []` and `operator []=` of
`JSArrayImpl` are updated to use the new range and index checks. Rest of
the libraries will be updated separately.

[1]: https://github.com/WebAssembly/js-string-builtins

CoreLibraryReviewExempt: dart2wasm specific library change.
Change-Id: I9436def0cfe59c631f6f4e15ea06cc18a47a738e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335043
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-08 10:04:41 +00:00
Aske Simon Christensen d58f1ec00f [dart2wasm] Dispatch type checks to avoid creating runtime type
Change-Id: I4ae9ffa7fa030e0a893178b8061f568e69a7bd79
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336903
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-12-06 10:04:01 +00:00
Michael Thomsen 0df58fc2d0 Add new dart compile wasm command.
Marked as experimental, and only shown when verbose flag is passed:

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

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

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

Change-Id: I6a0e65d4fbdd7b2782406b8b9969e14036bf0711
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304860
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Michael Thomsen <mit@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-12-05 15:44:53 +00:00
Ömer Sinan Ağacan c585bfb7bc [dart2wasm] Avoid duplicate checks in dynamic forwarders
In a forwarder for a method, check class IDs of all classes inheriting
the member and run the shape check once.

For example, in a this class hierarchy:

    class A {
      void printName() { print("A"); }
    }

    class B extends A {
      void printName() { print("B"); }
    }

    class C extends A {}

When generating method forwarder for `printName`, we currently generate
a shape check for all classes.

This this CL we now generate two shape checks: one for A and C (which
share the same `printName`), one for B.

Secondly, when class IDs of a target contains a range of consecutive
class IDs, we now generate a range check, instead of a sequence of
checks for every class ID in the range.

Fixes #53880.

Change-Id: Ie7360075c7930262e9e18c7cd125b83fd32b4bfa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339301
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2023-12-05 13:13:41 +00:00
Simon Binder a44a524bb0 [vm/ffi]: Add Native.addressOf
This adds the `Native.addressOf` function, which takes a constant tear-
off of a method annotated with `@Native` as a parameter and returns a
pointer to the underlying C function.

The CFE will resolve these calls in two steps: First, the existing
transformer for `@Native` methods adds a pragma describing the fully-
resolved native annotation (with the asset id inferred from the library
if not given explicitly). Then, the FFI use sites transformer rewrites
calls to `Native.addressOf` by searching for this pragma on the passed
function and passing the `Native` constants to `Native._addressOf`. The
latter method is implemented in the VM, which can re-use existing parts
already used for `@Native` calls.
An alternative implementation strategy would have been to forward
`addressOf` calls to `Native.ffi_resolver_function` directly without
any special consideration in the VM. However, the chosen approach makes
it easier to support static linking in the future, as this requires
unresolved symbols in the generated assembly.

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

TEST=pkg/vm/testcases/transformations/ffi/ffinative.dart
TEST=tests/ffi/native_assets/asset_*_test.dart
TEST=tests/ffi/vmspecific_static_checks_ffinative_test.dart
TEST=pkg/analyzer/test/src/diagnostics/ffi_native_test.dart

Cq-Include-Trybots: luci.dart.try:vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64c-try,vm-aot-win-release-x64-try,vm-kernel-precomp-linux-release-x64-try
CoreLibraryReviewExempt: VM & dart2wasm only feature
Change-Id: Ic8e3a390146dffd44c95578f975a4472db79a0ee
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333920
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-12-05 13:10:36 +00:00
Ömer Sinan Ağacan c55f85a237 [dart2wasm] Fix looping when inlining constructors
Example recursive constructor call:
https://github.com/flutter/devtools/blob/58c6e1c8fc20c78979ced06c671c2ed6ff51c947/packages/devtools_app/lib/src/screens/inspector/layout_explorer/ui/utils.dart#L247-L256

Change-Id: Ice42e4d0e908336ba6fecfc034d438f4d9c0b747
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339581
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-04 15:01:36 +00:00
Ömer Sinan Ağacan 38e5af5e7d [dart2wasm] Fix nullability of type parameter types
Fixes #53968

Change-Id: I3d4d0ff2d0fc9b196322f967600bf392124a75de
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339400
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-12-04 09:10:39 +00:00
Alexander Markov 927091e8ac [dart2wasm] Fix invalid closure return type in async* lowering
dart2wasm lowering of async* methods includes call to Stream.where
which takes a closure. The return type of that closure was incorrectly
set to Object?, while it should be bool.

This incorrect static type becomes a problem with more precise
handling of closures in TFA.

Issue: https://github.com/dart-lang/sdk/issues/39692
Change-Id: I35a8a6b198413d564e091a935a8beaff15d6d541
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339200
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-12-01 14:06:18 +00:00
Ömer Sinan Ağacan 3f3e4264e2 [dart2wasm] Use browser decoder when possible in non-JSCM mode
- Move `TextDecoder` code to decode JS `Uint8Array`s as UTF-8 to its own
  module `dart:_js_string_convert`.

- Use the module in default mode (non-JSCM) `dart:convert` when decoding
  JS `Uint8Array`s.

Change-Id: I6921b2a99c37ec8920ab79482228ede777bf2b4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/338520
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2023-12-01 09:36:41 +00:00
Srujan Gaddam ae082ef407 [pkg:js] Remove referenceFromIndex lookup in static interop eraser
This was only ever used to handle modular compiles and is no longer
needed now that we erase in DDC instead.

Change-Id: I44577464c29526d4b4fabb7c65c9f5f693edb02a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335407
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
2023-11-28 01:11:30 +00:00
Martin Kustermann 6e771a1ce9 [dart2wasm] Make tools/test.py -n dart2wasm-linux-jsshell ... work
Change-Id: I0a2678a5b141ab52597860380df7d44fd1f1db74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/338381
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-11-27 15:51:55 +00:00
Daco Harkes df4ef72c01 [vm/ffi] Remove deprecated FfiNative
TEST=pkg/analyzer/test/
TEST=CI build with class removed from `dart:ffi`

Closes: https://github.com/dart-lang/sdk/issues/53923
CoreLibraryReviewExempt: VM & dart2wasm only.
Change-Id: I45a39b623754f9f1b65cac55ea9adae390199f5d
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm64c-try,analyzer-analysis-server-linux-try,analyzer-mac-release-try,dart-sdk-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336960
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2023-11-20 13:16:28 +00:00
Aske Simon Christensen 4b880aec60 [dart2wasm] Avoid using _Closure as a type
`_Closure` is the implementation class for function objects. When used
as a type, it translated to the same Wasm type as the `Function` type,
but in the type system it missed the special property of `Function`
that it is a supertype of all functions. Instead, it was translated
into an interface type, leading to incorrect type check results.

Thus, we must always use the `Function` type when referring to any
function.

The problem was hidden by the type check specialization for simple
interface type checks and exposed by `--verify-type-checks`.

Change-Id: I384d35506c0c8cd932ba789e977f8257e684b8d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336423
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-11-16 15:04:58 +00:00
Aske Simon Christensen 80a69223e0 [dart2wasm] Add option to verify type check implementations
This adds a `--verify-type-checks` option to dart2wasm to instrument
the code such that whenever we are able to generate specialized code
for a type check, we generate both the specialized code and also call
the general fallback path, then compare the results.

This can be used to expose bugs in the type check specializations, or
in the reference implementation, as it may be.

Change-Id: I081540a8eedc7d029b332919283810220b21b3ea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336023
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-11-16 15:04:58 +00:00
Ömer Sinan Ağacan 932ed2e364 [dart2wasm] Allocate TextDecoder once when decoding UTF-8 to JSStringImpl
Improves a protobuf decoding benchmark from 348ms to 305ms.

Change-Id: Ib47d4271981c16c196594e435c807fecb8fb02f5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336020
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-11-14 11:42:23 +00:00
Ömer Sinan Ağacan 0d50d07a08 [dart2wasm] Update d8 flags
Change-Id: Ibe1e6c32d1a46055d35dab32878ac958801c550b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335800
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-11-14 09:19:08 +00:00
Ömer Sinan Ağacan 75c975fe6d [dart2wasm] Remove stringref target
stringref target won't be used any time soon (probably ever). To help
with build times and avoid keeping it updated remove it for now.

Change-Id: I0df33b7ab2e19bae5090e8ea32ea6a3dc3751652
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334881
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-11-10 09:49:37 +00:00
Srujan Gaddam a700d5f902 [dart2wasm] Handle extension types that wrap allowed types on external members
Users are allowed to have extension types on types that we allow
on external methods e.g. String. For most types, this doesn't make a
difference as we jsify for parameters and dartify and cast for return
types. The one place it does matter is when we check the static type
to do int conversions. Here, we should use the erased type instead.

Change-Id: Ibfcb0acc7f2c8a1ba3b52aa42000640982f11962
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335120
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
2023-11-09 21:58:33 +00:00
Ömer Sinan Ağacan f78759775e Reland "[dart2wasm] Use DataView in JS typed arrays"
This is a reland of commit b9b7de14e0

Previous version broke engine tests because of incorrect array impl
class (e.g. `JSUint16ArrayImpl`) to JS array externref conversion. Since
array implementation classes now store an `externref` to a `DataView`,
we can't use `toExternRef`, we need to use the new `toJSArrayExternRef`.

Original change's description:
> [dart2wasm] Use DataView in JS typed arrays
>
> Refcator JS typed array classes to store JS `DataView`s (instead of JS
> typed array with the matching type), and use V8's new optimized
> `DataView` imports.
>
> Brings JS typed array class performance from unusable to reasonable
> levels. In `SkeletalAnimation` benchmark:
>
> - Before: 1444 us.
> - After: 101 us.
>
> Future work:
>
> - Make immutable classes extend the mutable ones. This will allow `[]`
>   calls to be always inlined in JSCM.
>
> - Move list mixin applications from the base class to implementation
>   classes. This will allow members like `elementAt`, `forEach` etc. to
>   have direct calls to other methods of the same class.
>
> - Try adding a `length` field to avoid JS calls in bounds checks.
>
> Change-Id: Ief6165fe9c4a825072077db820b105c4fca30dce
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328920
> Reviewed-by: Aske Simon Christensen <askesc@google.com>
> Commit-Queue: Ömer Ağacan <omersa@google.com>

Change-Id: If913aed837d09570b82b0f6dcb9553abb17fd708
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334468
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-11-09 10:23:31 +00:00
Siva Annamalai e64dd81a4a Revert "[dart2wasm] Use DataView in JS typed arrays"
This reverts commit b9b7de14e0.

Reason for revert: Dart to Engine rolls are failing, please see
https://github.com/flutter/engine/runs/18494538711

Original change's description:
> [dart2wasm] Use DataView in JS typed arrays
>
> Refcator JS typed array classes to store JS `DataView`s (instead of JS
> typed array with the matching type), and use V8's new optimized
> `DataView` imports.
>
> Brings JS typed array class performance from unusable to reasonable
> levels. In `SkeletalAnimation` benchmark:
>
> - Before: 1444 us.
> - After: 101 us.
>
> Future work:
>
> - Make immutable classes extend the mutable ones. This will allow `[]`
>   calls to be always inlined in JSCM.
>
> - Move list mixin applications from the base class to implementation
>   classes. This will allow members like `elementAt`, `forEach` etc. to
>   have direct calls to other methods of the same class.
>
> - Try adding a `length` field to avoid JS calls in bounds checks.
>
> Change-Id: Ief6165fe9c4a825072077db820b105c4fca30dce
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328920
> Reviewed-by: Aske Simon Christensen <askesc@google.com>
> Commit-Queue: Ömer Ağacan <omersa@google.com>

Change-Id: Iba46cfc409805bcd6f86bcc52a06ed8d54eea671
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335026
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
2023-11-08 22:44:18 +00:00
Ömer Sinan Ağacan b9b7de14e0 [dart2wasm] Use DataView in JS typed arrays
Refcator JS typed array classes to store JS `DataView`s (instead of JS
typed array with the matching type), and use V8's new optimized
`DataView` imports.

Brings JS typed array class performance from unusable to reasonable
levels. In `SkeletalAnimation` benchmark:

- Before: 1444 us.
- After: 101 us.

Future work:

- Make immutable classes extend the mutable ones. This will allow `[]`
  calls to be always inlined in JSCM.

- Move list mixin applications from the base class to implementation
  classes. This will allow members like `elementAt`, `forEach` etc. to
  have direct calls to other methods of the same class.

- Try adding a `length` field to avoid JS calls in bounds checks.

Change-Id: Ief6165fe9c4a825072077db820b105c4fca30dce
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328920
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-11-08 13:48:59 +00:00
Ömer Sinan Ağacan 1820bae915 [dart2wasm] Remove redundant blocks around unreachables for implicit returns
Change-Id: I212f87c79d7b90e2feb04046f3d2ed517d3d9ca4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334200
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2023-11-08 09:28:48 +00:00
Ömer Sinan Ağacan ab2c2ae8a2 [dart2wasm] Specify concrete int and double literal classes for TFA
Tested: existing tests
Change-Id: I0f800ca3a375525dcd8ca9f9f0e25144bdce80f7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333004
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-11-03 14:28:21 +00:00
Alexander Markov e7c8363900 [dart2wasm/tfa] Improve inference for binary int operations
Because of the special language rules (int +/-/* int = int),
static type for binary int operations can be more precise than
inferred result type (in case of dart2wasm, int binary operations
are declared as external methods returning num).

So, for dart2wasm it is useful to narrow result of a call by its
static type, if it is different from return type of the call target.

Also, this change fixes an incorrect static type which is
created during dart2wasm async* transformation.

TEST=pkg/vm/testcases/transformations/type_flow/transformer/int_operations_dart2wasm.dart
Fixes https://github.com/dart-lang/sdk/issues/53921

Change-Id: Id6a5a2cffac47918c4e92f996267bf7fae713416
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333580
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-11-03 13:39:43 +00:00
Ömer Sinan Ağacan 12e0690dfe [dart2wasm] Generate inline code for int ~/ operator
The implementation of `~/` is a bit more complicated than other
operators as it needs to handle some special cases, so we currently
don't generate inline code for it.

This causes generating indirect calls to `~/` in some cases even when
the static Dart type of the arguments are `int`s, see [1] for details.

Update the intrinsic matcher to specially handle this operator by
calling `_BoxedInt._truncDiv`, which is then inlined.

This leads to huge performance wins in [2] as typed data `length`
getters currently compiled to indirect calls to `~/`.

[1]: https://github.com/dart-lang/sdk/issues/53921
[2]: https://dart-review.googlesource.com/c/sdk/+/328920

Change-Id: I92d89db76af38b1f843e25a841074f00ff2ef30a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333381
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-11-02 14:46:37 +00:00
Johnni Winther 12c4e22a4d [cfe] Handle various cases ExtensionType
Handles ExtensionType in inference of MapLiteral, inference of
ObjectPattern, and exhaustiveness.

TEST=tests/extension_type/*

Change-Id: I3284da2c69d875e192cf3f004ee1156e1aedd98b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333160
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-11-02 10:42:48 +00:00
Ömer Sinan Ağacan fb54ea5f3f [dart2wasm] Remove old special case in binary op intrinsic matching
Change-Id: I1643d2fe8c6b920084a832be2d698d6c48f26eab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333380
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-11-02 10:38:04 +00:00
Ömer Sinan Ağacan 9ed368c2e1 [dart2wasm] Use only JS strings in JSCM
Refactor libraries so that JSCM will only use `JSStringImpl` class for
strings.

The goal is to disentangle the native string classes and `JSStringImpl`
and start testing `JSStringImpl` in isolation.

Changes:

- `dart:_string` is no longer available in JSCM.

- Make `int.toString` external to allow patching it differently in JSCM
  and normal modes.

  `toString` implementations are in `boxed_int_to_string.dart` patch
  files.

- `int.parse` now uses JS `parseInt`. However `parseInt` is not
  compatible with Dart's `int.parse` so this will cause some more test
  failures in JSCM for now.

- Any dependencies to `dart:_string` from JSCM `dart:convert` are
  removed. The library implementation now uses JS `TextDecoder` for
  UTF-8 decoding.

  Note: `TextDecoder` is not available on d8, so text decoding tests
  will fail on d8.

  JSON encoding and decoding in `dart:convert` will be updated in a
  follow-up CL.

- Compiler (translator, constant generator, code generator etc.) is
  updated to allocate the `JSStringImpl`s in JSCM.

Initially this will make some JSCM test fail as `int` parsing is not
quite right, those will be fixed in follow-up CLs.

Co-authored-by: Joshua Litt <joshualitt@google.com>
Change-Id: I366e06f44cdc369d28fe47b24015234260304399
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332680
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-10-31 12:58:26 +00:00
Ömer Sinan Ağacan 3a8d3cdf2f [dart2wasm] Don't generate duplicate selectors for dynamic calls
Change-Id: I8c41e8289400c4673d1268826bfe1cbc161a4d90
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332402
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-10-27 12:31:23 +00:00
Johnni Winther 4b2cf0744d [cfe] Add TypeDeclarationType and asInstanceOf
This add a common sealed superclass TypeDeclarationType for
ExtensionType and InterfaceType and uses it to add a common
asInstanceOf method to ClassHierarchy.

Change-Id: I7294e41069b063305c3bd4e384ff90614a3936a6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/331981
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-10-26 08:46:48 +00:00
Johnni Winther d252bb11a3 [cfe] Add representation field to ExtensionTypeDeclaration
This adds the representation field of an extension type declaration
as an abstract getter in the ExtensionTypeDeclaraiton marking it as
a ProcedureStubKind.RepresentationField

These are never used as interface targets and are therefore skipped
in the type flow analysis.

TEST=existing

Change-Id: Ie645e63e0995a31895e985a2025dccb1476d16bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330782
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-10-19 10:15:25 +00:00
Aske Simon Christensen dd602467dd Reland "[dart2wasm] Use a separate Wasm struct for every Dart class"
This is a reland of commit 2cbb5eabca

This removes the optimization that would reuse the Wasm struct of the
superclass if no new fields were added in a class. Such reuse
interferes with debugging.

Binaryen performs a similar optimization in its TypeMerging pass. It
seems to be doing a better job with the more precise type information
provided after this change. Benchmarks show a positive trend.

The change uncovered a number of latent bugs in the compiler, which
are also fixed in this CL:
- References to `StackTrace` objects must be typed with the
  representation type of `StackTrace`, rather than its struct type.
- The dynamic call vtable entry and dynamic invocation forwarder
  functions must use `_ListBase` rather than `_List` for their
  parameters, since we sometimes pass growable lists to these
  functions.
- Most covariant parameters must use the top type, since any object
  can be passed into such parameters via class type parameter
  covariance or tear-offs.

The change makes us fail a number of tests with very deep class
hierarchies due to the Wasm subtyping depth limit of 63. The
optimization did not absolve us from this limitation. It just made us
only count classes with newly added fields.

The limitation is not expected to cause any problems for real code.
The maximum subclassing depth that occurs in the Flute benchmark is
11. FWIW, Java has a limit of 60.

The limitation is mentioned along with other limitations in
https://github.com/dart-lang/sdk/issues/53703

Change-Id: I9f541049d2020934e98b2042a7d9852305f17ec1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330341
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-10-19 09:45:04 +00:00
Ömer Sinan Ağacan fa976d75be [dart2wasm] Simpify string handling in core
Now that we have our own `dart:convert` implementations in dart2wasm
targets (1) and the internal types are now internally public (2), this
simplifies some of the string implementation code by using the
implementation classes directly.

Note: stringref patch can be ignored, it's currently unused and I'm only
updating it to make it compile. It will be rewritten based on
`JSStringImpl` in [3].

To keep the CLs as small as possible, this CL does not do any of the
optimizations we can do now, such as optimizing
`OneByteString._setRange` to Wasm `array.copy` instructions. These will
be done in follow-up CLs.

[1]: https://dart-review.googlesource.com/c/sdk/+/330781
[2]: https://dart-review.googlesource.com/c/sdk/+/330783
[3]: https://dart-review.googlesource.com/c/sdk/+/316628

Change-Id: I92918d58a565ad32f5a221430e02d81562f03b00
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/331040
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2023-10-18 17:37:33 +00:00
Ömer Sinan Ağacan ce9e332bb1 [dart2wasm] Make typed data implementation classes internally public
This is a part of [1] and a continuation of [2].

- Make typed data implementation file a library, to allow importing it
  in `dart:convert` patch.

- Make SIMD implementation file a library, to allow importing it from
  the typed data library.

- Make typed data implementation classes internally public (available in
  other `dart:...` libraries).

- Relax `mayDefineRestrictedType` to allow implementing restricted types
  anywhere in the standard library implementation.

[1]: https://dart-review.googlesource.com/c/sdk/+/330121
[2]: https://dart-review.googlesource.com/c/sdk/+/330781

Change-Id: I70bc6869a20fd2479bee081cfceef27d6de19974
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330783
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
2023-10-18 06:40:49 +00:00
Alexander Markov 293cc6475a [vm/aot/tfa] Whole-program propagation of closure values
This change adds propagation of closure values to TFA.
For now, inferred closure values are used only when they are
constant (tear-off of a static method or a constructor).

Arbitrary closures can now be referenced from unrelated
members via closure-id metadata.

In addition, this change fixes an incorrect stack trace when
an implicit closure (tear-off) was propagated and its call was
inlined. Inlining interval was not recorded because of the missing
source position of a call to a target method within implicit closure.

TEST=pkg/vm/testcases/transformations/type_flow/transformer/closures.dart
Issue: https://github.com/dart-lang/sdk/issues/39692

Change-Id: I3590da91b6057e0b55a8614382dba1bbcc267b39
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325447
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2023-10-12 16:16:21 +00:00
Aske Simon Christensen 4090d3b909 [dart2wasm] Normalize FutureOr<T> as nullable when T is nullable
The type normalization rules specify that `FutureOr<T>?` is normalized
to `FutureOr<T>` when `T` is nullable. However, it's more practical
for subtype testing if the declared nullability on the runtime
representation of the `FutureOr` type reflects the true nullability
(nullable if the `FutureOr` is declared nullable or its type argument
is nullable), rather than being normalized as per the spec.

This changes the static and dynamic normalization rules in dart2wasm
thus and compensates by computing the proper spec normalization when
the type is converted to a string.

The added test exposed a number of bugs in DDC and the VM:

https://github.com/dart-lang/sdk/issues/53175
https://github.com/dart-lang/sdk/issues/53737
https://github.com/dart-lang/sdk/issues/53738

Change-Id: I0ad0a09fe935ccbd3eb65e6958c958d29e0bb088
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/320821
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
2023-10-12 15:29:26 +00:00
Jackson Gardner 665aaefd8c Revert "[dart2wasm] Use a separate Wasm struct for every Dart class"
This reverts commit 2cbb5eabca.

Reason for revert: https://github.com/flutter/flutter/issues/136363

Original change's description:
> [dart2wasm] Use a separate Wasm struct for every Dart class
>
> This removes the optimization that would reuse the Wasm struct of the
> superclass if no new fields were added in a class. Such reuse
> interferes with debugging.
>
> Binaryen performs a similar optimization in its TypeMerging pass. It
> seems to be doing a better job with the more precise type information
> provided after this change. Benchmarks show a positive trend.
>
> The change uncovered a number of latent bugs in the compiler, which
> are also fixed in this CL:
> - References to `StackTrace` objects must be typed with the
>   representation type of `StackTrace`, rather than its struct type.
> - The dynamic call vtable entry and dynamic invocation forwarder
>   functions must use `_ListBase` rather than `_List` for their
>   parameters, since we sometimes pass growable lists to these
>   functions.
> - Most covariant parameters must use the top type, since any object
>   can be passed into such parameters via class type parameter
>   covariance or tear-offs.
>
> The change makes us fail a number of tests with very deep class
> hierarchies due to the Wasm subtyping depth limit of 63. The
> optimization did not absolve us from this limitation. It just made us
> only count classes with newly added fields.
>
> The limitation is not expected to cause any problems for real code.
> The maximum subclassing depth that occurs in the Flute benchmark is
> 11. FWIW, Java has a limit of 60.
>
> The limitation is mentioned along with other limitations in
> https://github.com/dart-lang/sdk/issues/53703
>
> Change-Id: Ifaa23d8278989e2549c41696d8913e55ed1e80aa
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324460
> Commit-Queue: Aske Simon Christensen <askesc@google.com>
> Reviewed-by: Ömer Ağacan <omersa@google.com>

Change-Id: I1b16936a045f6297f340b5d72344f702446bf955
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330044
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Auto-Submit: Jackson Gardner <jacksongardner@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-10-11 16:55:16 +00:00
Johnni Winther 9a4c9100ae [kernel] Add DartType.extensionTypeErasure
This renames ExtensionType.typeErasure and adds it to DartType. This also fixes the extension type erasure for when extension types are used in the arguments of an extension type.

DartType.extensionTypeErasure can be used by backends to easily
access the type without extension types for any type.

TEST=pkg/kernel/test/extension_type_erasure_test.dart


Change-Id: Ia49d273ed85111e3ae822720860a3e0be5ea0252
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/329960
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-10-11 13:51:22 +00:00
Aske Simon Christensen 2cbb5eabca [dart2wasm] Use a separate Wasm struct for every Dart class
This removes the optimization that would reuse the Wasm struct of the
superclass if no new fields were added in a class. Such reuse
interferes with debugging.

Binaryen performs a similar optimization in its TypeMerging pass. It
seems to be doing a better job with the more precise type information
provided after this change. Benchmarks show a positive trend.

The change uncovered a number of latent bugs in the compiler, which
are also fixed in this CL:
- References to `StackTrace` objects must be typed with the
  representation type of `StackTrace`, rather than its struct type.
- The dynamic call vtable entry and dynamic invocation forwarder
  functions must use `_ListBase` rather than `_List` for their
  parameters, since we sometimes pass growable lists to these
  functions.
- Most covariant parameters must use the top type, since any object
  can be passed into such parameters via class type parameter
  covariance or tear-offs.

The change makes us fail a number of tests with very deep class
hierarchies due to the Wasm subtyping depth limit of 63. The
optimization did not absolve us from this limitation. It just made us
only count classes with newly added fields.

The limitation is not expected to cause any problems for real code.
The maximum subclassing depth that occurs in the Flute benchmark is
11. FWIW, Java has a limit of 60.

The limitation is mentioned along with other limitations in
https://github.com/dart-lang/sdk/issues/53703

Change-Id: Ifaa23d8278989e2549c41696d8913e55ed1e80aa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324460
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
2023-10-10 14:35:53 +00:00
Ömer Sinan Ağacan c72088c1e6 [dart2wasm] Fix prefer-inline annotation checking, handle never-inline
`getPragma` is also slightly refactored to make it clear when it returns
the passed default value.

Change-Id: I5dfc3e7a675141d41cbaf957b2487e34aeb991e4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328921
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
2023-10-03 12:51:45 +00:00