Skip to content

dart2wasm compiles same code multiple times #59676

@osa1

Description

@osa1

While working on #55231 and having trouble with functions getting defined multiple times and getting defined accidentally (when calling unrelated functions) and not being compiled, I noticed that we actually compile many things multiple times. When I add debug prints to print when a function with the same name is defined again, I see thousands of duplicate definitions in the test suite. Some examples:

Initializers:

  • toStringVisiting initializer
  • GrowableList._emptyData initializer
  • BoxedDouble._cache initializer
  • BoxedDouble._cacheEvictIndex initializer

Type test functions:

  • <obj> as _PendingEvents<T0>
  • <obj> is num
  • <obj> as double

Closures:

  • generator1 closure at ...
  • foo closure at ...
  • createErrorStream closure at ...

Closure wrappers:

  • closure wrapper at file://... trampoline
  • closure wrapper at file://... trampoline
  • closure wrapper at file://... trampoline
  • closure wrapper at file://... trampoline

This is not an exhaustive list, there may be other kind of things that we compile multiple times.

The diff that I used to generate this output:

diff --git a/pkg/wasm_builder/lib/src/builder/functions.dart b/pkg/wasm_builder/lib/src/builder/functions.dart
index 2969a5f815f..5aa987e26f0 100644
--- a/pkg/wasm_builder/lib/src/builder/functions.dart
+++ b/pkg/wasm_builder/lib/src/builder/functions.dart
@@ -46,6 +46,15 @@ class FunctionsBuilder with Builder<ir.Functions> {
   FunctionBuilder define(ir.FunctionType type, [String? name]) {
     final function =
         FunctionBuilder(_module, ir.FinalizableIndex(), type, name);
+
+    if (name != null) {
+      for (final builder in _functionBuilders) {
+        if (builder.functionName == name) {
+          print("Generating duplicate name: $name");
+        }
+      }
+    }
+
     _functionBuilders.add(function);
     _addName(name, function);
     return function;

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart2wasmIssues for the dart2wasm compiler.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions