Skip to content

Commit e05a423

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2wasm] Avoid extra global definitions for int constants that are wasmI32.
In a few places we create IntConstants to represent constant fields that are typed as WasmI32. If these are nested within other constants we end up hitting ConstantCreator.visitIntConstant for them. This always generates a global for a BoxedInt constant, whether it's used or not. The global we create goes unused because in constant intiailizer for the outer constant we use the wasmI32 value directly. At -O0 this reduced the wasm binary size by ~24k for a simple Flutter app. Binaryen mostly treeshakes these unused globals but I still see a slight improvement even with binaryen running. Change-Id: I89b8392138269d7322196fa415e56e3062a46088 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408121 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 07b7cfe commit e05a423

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

pkg/dart2wasm/lib/constants.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ class Constants {
109109
Types get types => translator.types;
110110
CoreTypes get coreTypes => translator.coreTypes;
111111

112+
Constant makeWasmI32(int value) {
113+
return InstanceConstant(translator.wasmI32Class.reference, const [],
114+
{translator.wasmI32Value.fieldReference: IntConstant(value)});
115+
}
116+
112117
/// Makes a `WasmArray<_Type>` [InstanceConstant].
113118
InstanceConstant makeTypeArray(Iterable<DartType> types) {
114119
return makeArrayOf(
@@ -225,7 +230,7 @@ class Constants {
225230
InstanceConstant _makeInterfaceTypeConstant(InterfaceType type) {
226231
return _makeTypeConstant(translator.interfaceTypeClass, type.nullability, {
227232
translator.interfaceTypeClassIdField.fieldReference:
228-
IntConstant(translator.classIdNumbering.classIds[type.classNode]!),
233+
makeWasmI32(translator.classIdNumbering.classIds[type.classNode]!),
229234
translator.interfaceTypeTypeArguments.fieldReference:
230235
makeTypeArray(type.typeArguments),
231236
});
@@ -593,6 +598,9 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
593598
if (cls == translator.immutableWasmArrayClass) {
594599
return _makeWasmArrayLiteral(constant, mutable: false);
595600
}
601+
if (cls == translator.wasmI32Class) {
602+
return null;
603+
}
596604

597605
ClassInfo info = translator.classInfo[cls]!;
598606
translator.functions.recordClassAllocation(info.classId);

pkg/dart2wasm/lib/kernel_nodes.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ mixin KernelNodes {
158158
late final wasmI8Class = index.getClass("dart:_wasm", "WasmI8");
159159
late final wasmI16Class = index.getClass("dart:_wasm", "WasmI16");
160160
late final wasmI32Class = index.getClass("dart:_wasm", "WasmI32");
161+
late final wasmI32Value = index.getField("dart:_wasm", "WasmI32", "_value");
161162
late final wasmI64Class = index.getClass("dart:_wasm", "WasmI64");
162163
late final wasmF32Class = index.getClass("dart:_wasm", "WasmF32");
163164
late final wasmF64Class = index.getClass("dart:_wasm", "WasmF64");

pkg/dart2wasm/lib/types.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ class RuntimeTypeInformation {
12191219
final table = buildRowDisplacementTable(rows, firstAvailable: 1);
12201220
typeRowDisplacementTable = translator.constants.makeArrayOf(wasmI32, [
12211221
for (final entry in table)
1222-
IntConstant(entry == null
1222+
translator.constants.makeWasmI32(entry == null
12231223
? 0
12241224
: (entry.$2 == noSubstitutionIndex ? -entry.$1 : entry.$1)),
12251225
]);
@@ -1233,7 +1233,8 @@ class RuntimeTypeInformation {
12331233

12341234
typeRowDisplacementOffsets = translator.constants.makeArrayOf(wasmI32, [
12351235
for (int classId = 0; classId < translator.classes.length; ++classId)
1236-
IntConstant(rowForSuperclass[classId]?.offset ?? -1),
1236+
translator.constants
1237+
.makeWasmI32(rowForSuperclass[classId]?.offset ?? -1),
12371238
]);
12381239
}
12391240

0 commit comments

Comments
 (0)