Skip to content

Commit 76d56d7

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Refactor the way we handle elements segments
Until now the element segments were hidden from the core IR & Builder classes. This has a few issues as outlined here: * It required `ir.DefinedTable` and `ir.ImportedTable` to have entries => Having element segments as core concept means they can refer to a `ir.Table` (irrespective of wether it's a `ir.DefinedTable` or `ir.ImportedTable`) * It assumed that table entries have to be functions => We can have tables of other reference types as well * If we ever wanted to use the `table.init` instruction then we'd be having an issue as that instruction refers to an element segment (which didn't exist in our IR) => Now we represent `ir.ElementSegment` as a concept which a `ir.TableInit` instruction could reference. * The encoding of function tables was so far either using the `vec[ref.func]` form if the table index was 0 or it would use `vec[expr]`. => The active segment encoding with/without table index is somewhat orthogonal to whether to use ref.func/expression. => If we encode a function table with index != 0 now we can still use `vec[ref.func]` (which is smaller encoding) => If we encode non-functions in a table we can now use `vec[expr]` encoding. * Makes IR, serializer & deserializer cleaner. => removes `ir.Functions.withoutDeclared` => removes deserializing functions with side-effects Change-Id: I75544ab3b1f0c721f0585a2c187a81415e978fe3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/459440 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Ömer Ağacan <[email protected]>
1 parent 59c8081 commit 76d56d7

23 files changed

+526
-316
lines changed

pkg/dart2wasm/lib/dispatch_table.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,17 @@ class DispatchTable {
852852
fun = wrappedDynamicSubmoduleImports[fun] ??=
853853
_wrapDynamicSubmoduleFunction(target, fun);
854854
}
855-
_definedWasmTable.setElement(i, fun);
855+
_definedWasmTable.moduleBuilder.elements
856+
.activeFunctionSegmentBuilderFor(_definedWasmTable)
857+
.setFunctionAt(i, fun);
856858
} else {
857859
// This will generate the imported table if it doesn't already
858860
// exist.
859-
(getWasmTable(targetModuleBuilder) as w.ImportedTable)
860-
.setElements[i] = fun;
861+
final importedTable =
862+
getWasmTable(targetModuleBuilder) as w.ImportedTable;
863+
targetModuleBuilder.elements
864+
.activeFunctionSegmentBuilderFor(importedTable)
865+
.setFunctionAt(i, fun);
861866
}
862867
}
863868
}

pkg/dart2wasm/lib/dynamic_modules.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,16 @@ class DynamicModuleInfo {
677677
overridableFunctions.putIfAbsent(index, () {
678678
if (!isSubmodule) {
679679
final mainFunction = translator.mainModule.functions.define(type, name);
680-
translator.mainModule.functions.declare(mainFunction);
680+
translator.mainModule.elements.declarativeSegmentBuilder
681+
.declare(mainFunction);
681682
buildMain(mainFunction);
682683
return mainFunction;
683684
}
684685

685686
if (skipSubmodule) return null;
686687

687688
final submoduleFunction = submodule.functions.define(type, name);
688-
submodule.functions.declare(submoduleFunction);
689+
submodule.elements.declarativeSegmentBuilder.declare(submoduleFunction);
689690
buildSubmodule(submoduleFunction);
690691
return submoduleFunction;
691692
});
@@ -1067,7 +1068,7 @@ class ConstantCanonicalizer extends ConstantVisitor<void> {
10671068

10681069
// Declare the function so it can be used as a ref_func in a constant
10691070
// context.
1070-
b.moduleBuilder.functions.declare(checker);
1071+
b.moduleBuilder.elements.declarativeSegmentBuilder.declare(checker);
10711072

10721073
// Invoke the 'canonicalize' function with the value and checker.
10731074
b.local_get(valueLocal);
@@ -1101,7 +1102,7 @@ class ConstantCanonicalizer extends ConstantVisitor<void> {
11011102

11021103
// Declare the function so it can be used as a ref_func in a constant
11031104
// context.
1104-
b.moduleBuilder.functions.declare(checker);
1105+
b.moduleBuilder.elements.declarativeSegmentBuilder.declare(checker);
11051106

11061107
// Invoke the canonicalizer function with the value and checker.
11071108
b.local_get(valueLocal);

pkg/dart2wasm/lib/static_dispatch_table.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ class StaticDispatchTableForSignature {
7474
_table.forEach((fun, index) {
7575
final targetModule = translator.moduleToBuilder[fun.enclosingModule]!;
7676
if (translator.isMainModule(targetModule)) {
77-
_definedWasmTable.setElement(index, fun);
77+
_definedWasmTable.moduleBuilder.elements
78+
.activeFunctionSegmentBuilderFor(_definedWasmTable)
79+
.setFunctionAt(index, fun);
7880
} else {
7981
// This will generate the imported table if it doesn't already exist.
80-
(getWasmTable(targetModule) as w.ImportedTable).setElements[index] =
81-
fun;
82+
final importedTable = getWasmTable(targetModule) as w.ImportedTable;
83+
targetModule.elements
84+
.activeFunctionSegmentBuilderFor(importedTable)
85+
.setFunctionAt(index, fun);
8286
}
8387
});
8488

pkg/dart2wasm/test/ir_tests/deferred.constant.multi_module_use.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
(type $type1 (func
1616
(param $var0 i32)
1717
(result (ref $MyConstClass))))
18-
(global $"C370 \"bad\"" (ref $JSStringImpl) <...>)
1918
(table $static0-0 (export "static0-0") 2 (ref null $type1))
19+
(global $"C370 \"bad\"" (ref $JSStringImpl) <...>)
2020
(func $Error._throwWithCurrentStackTrace (param $var0 (ref $#Top)) <...>)
2121
(func $"mainImpl <noInline>" (param $var0 i32)
2222
(local $var1 (ref $MyConstClass))

pkg/dart2wasm/test/ir_tests/deferred.constant.multi_module_use_module1.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
(field $b (ref $JSStringImpl)))))
1515
(global $S.h1-nonshared-const (import "S" "h1-nonshared-const") (ref extern))
1616
(global $S.shared-const (import "S" "shared-const") (ref extern))
17-
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $JSStringImpl) <...>)
18-
(table $module0.constant-table1 (import "module0" "constant-table1") 1 (ref null $MyConstClass) <...>)
17+
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $JSStringImpl))
18+
(table $module0.constant-table1 (import "module0" "constant-table1") 1 (ref null $MyConstClass))
1919
(global $"C488 MyConstClass" (ref $MyConstClass)
2020
(i32.const 107)
2121
(i32.const 0)

pkg/dart2wasm/test/ir_tests/deferred.constant.multi_module_use_module3.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
(field $b (ref $JSStringImpl)))))
1515
(global $S.shared-const (import "S" "shared-const") (ref extern))
1616
(global $S.h0-nonshared-const (import "S" "h0-nonshared-const") (ref extern))
17-
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $JSStringImpl) <...>)
18-
(table $module0.constant-table1 (import "module0" "constant-table1") 1 (ref null $MyConstClass) <...>)
17+
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $JSStringImpl))
18+
(table $module0.constant-table1 (import "module0" "constant-table1") 1 (ref null $MyConstClass))
1919
(global $"C492 MyConstClass" (ref $MyConstClass)
2020
(i32.const 107)
2121
(i32.const 0)

pkg/dart2wasm/test/ir_tests/deferred.constant_module1.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
(global $"C306 WasmArray<_NamedParameter>[0]" (import "module0" "global4") (ref $Array<_NamedParameter>))
3232
(global $"C28 _InterfaceType" (import "module0" "global7") (ref $_InterfaceType))
3333
(global $S.globalH1Bar< (import "S" "globalH1Bar<") (ref extern))
34-
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $_FunctionType) <...>)
34+
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $_FunctionType))
3535
(global $global7 (ref $#Vtable-1-1) <...>)
3636
(global $global4 (ref $#DummyStruct) <...>)
3737
(global $"C459 _FunctionType" (ref $_FunctionType) <...>)

pkg/dart2wasm/test/ir_tests/deferred.constant_module2.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
(global $"C62 WasmArray<_Type>[1]" (import "module0" "global3") (ref $Array<_Type>))
2020
(global $"C306 WasmArray<_NamedParameter>[0]" (import "module0" "global4") (ref $Array<_NamedParameter>))
2121
(global $S.globalH0Foo (import "S" "globalH0Foo") (ref extern))
22-
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $_FunctionType) <...>)
22+
(table $module0.constant-table0 (import "module0" "constant-table0") 1 (ref null $_FunctionType))
2323
(global $global6 (ref $#Vtable-0-1) <...>)
2424
(global $global3 (ref $#DummyStruct) <...>)
2525
(global $"C465 globalH0Foo tear-off" (mut (ref null $#Closure-0-1))

pkg/dart2wasm/test/ir_tests/interop.num.wat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
(func $"dart2wasm._275 (import)" (import "dart2wasm" "_275") (param externref) (result externref))
1919
(func $"dart2wasm._147 (import)" (import "dart2wasm" "_147") (param externref) (result f64))
2020
(func $"dart2wasm._148 (import)" (import "dart2wasm" "_148") (param f64) (result externref))
21+
(table $dtable1 666 funcref)
2122
(global $"C66 WasmArray<WasmArray<_Type>>[727]" (ref $Array<WasmArray<_Type>>) <...>)
2223
(global $"C311 _TopType" (ref $_TopType) <...>)
2324
(global $"numValueNullable initialized" (mut i32) <...>)
2425
(global $numValueNullable (mut (ref null $#Top)) <...>)
2526
(global $"ktrue initialized" (mut i32) <...>)
2627
(global $ktrue (mut i32) <...>)
2728
(global $numValue (mut (ref null $#Top)) <...>)
28-
(table $dtable2 666 funcref <...>)
29+
(elem $dtable1 <...>)
2930
(func $_TypeUniverse.isSubtype (param $var0 (ref $_Type)) (param $var1 (ref null $_Environment)) (param $var2 (ref $_Type)) (param $var3 (ref null $_Environment)) (result i32) <...>)
3031
(func $_TypeUniverse._checkSubclassRelationshipViaTable (param $var0 i32) (param $var1 i32) (result i32) <...>)
3132
(func $_TypeUniverse.substituteTypeArgument (param $var0 (ref $_Type)) (param $var1 (ref $Array<_Type>)) (result (ref $_Type)) <...>)

pkg/wasm_builder/lib/src/builder/builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'globals.dart' show GlobalsBuilder;
1111
export 'global.dart' show GlobalBuilder;
1212
export 'functions.dart' show FunctionsBuilder;
1313
export 'function.dart' show FunctionBuilder;
14+
export 'elements.dart' show ElementsBuilder;
1415
export 'memories.dart' show MemoriesBuilder;
1516
export 'module.dart' show ModuleBuilder;
1617
export 'tables.dart' show TablesBuilder;

0 commit comments

Comments
 (0)