Skip to content

Commit 7e4145d

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Assign names to globals/initializer functions for constants
* Make globals for constants have names which allows reading code easier as the name of the global for a constant will have the type/kind in it * Make lazy initializer functions for globals have explicit names in most cases, which avoids arbitrary length names for list constants * Both globals and lazy initializer functions will get names that include C<number> (for ease grep'ping navigating in wat files) and a descriptive name that includes the type and/or value of the constant. * Make list constant implementation lower to wasm array constants, just as we do for map constants. This avoids code duplication and allows canonicalization of the wasm arrays. Closes #55409 Change-Id: I7ccbdd3d44cf916bf147fdd1053ed52ddd1c0dc5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435420 Reviewed-by: Ömer Ağacan <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent bed1101 commit 7e4145d

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

pkg/dart2wasm/lib/constants.dart

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,55 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
501501
static String _dynamicModuleConstantExportName(int id) => '#c$id';
502502
static String _dynamicModuleInitFunctionExportName(int id) => '#cf$id';
503503

504+
static int _nextGlobalId = 0;
505+
String _constantName(Constant constant) {
506+
final id = _nextGlobalId++;
507+
if (constant is StringConstant) {
508+
var value = constant.value;
509+
final newline = value.indexOf('\n');
510+
if (newline != -1) value = value.substring(0, newline);
511+
if (value.length > 30) value = '${value.substring(0, 30)}<...>';
512+
return 'C$id "$value"';
513+
}
514+
if (constant is BoolConstant) {
515+
return 'C$id ${constant.value}';
516+
}
517+
if (constant is IntConstant) {
518+
return 'C$id ${constant.value}';
519+
}
520+
if (constant is DoubleConstant) {
521+
return 'C$id ${constant.value}';
522+
}
523+
if (constant is InstanceConstant) {
524+
final klass = constant.classNode;
525+
final name = klass.name;
526+
if (constant.typeArguments.isEmpty) {
527+
return 'C$id $name';
528+
}
529+
final typeArguments = constant.typeArguments.map(_nameType).join(', ');
530+
if (klass == translator.wasmArrayClass ||
531+
klass == translator.immutableWasmArrayClass) {
532+
final entries =
533+
(constant.fieldValues.values.single as ListConstant).entries;
534+
return 'C$id $name<$typeArguments>[${entries.length}]';
535+
}
536+
return 'C$id $name<$typeArguments>';
537+
}
538+
if (constant is TearOffConstant) {
539+
return 'C$id ${constant.target.name} tear-off';
540+
}
541+
return 'C$id $constant';
542+
}
543+
544+
String _nameType(DartType type) {
545+
if (type is InterfaceType) {
546+
final name = type.classNode.name;
547+
if (type.typeArguments.isEmpty) return name;
548+
return '$name<${type.typeArguments.map((t) => _nameType(t)).join(', ')}>';
549+
}
550+
return '$type';
551+
}
552+
504553
ConstantInfo createConstant(
505554
Constant constant, w.RefType type, ConstantCodeGenerator generator,
506555
{bool lazy = false}) {
@@ -535,12 +584,14 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
535584
_dynamicModuleInitFunctionExportName(mainModuleExportId),
536585
ftype);
537586
} else {
538-
final definedGlobal = global = targetModule.globals.define(globalType);
587+
final name = _constantName(constant);
588+
final definedGlobal =
589+
global = targetModule.globals.define(globalType, name);
539590
definedGlobal.initializer.ref_null(w.HeapType.none);
540591
definedGlobal.initializer.end();
541592

542-
final function =
543-
initFunction = targetModule.functions.define(ftype, "$constant");
593+
final function = initFunction =
594+
targetModule.functions.define(ftype, '$name (lazy initializer)}');
544595

545596
if (isShareableAcrossModules) {
546597
final exportId = dynamicModuleConstantIdMap[constant] =
@@ -575,7 +626,8 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
575626
_dynamicModuleConstantExportName(mainModuleExportId), globalType);
576627
} else {
577628
constants.currentlyCreating = true;
578-
final definedGlobal = global = targetModule.globals.define(globalType);
629+
final definedGlobal = global =
630+
targetModule.globals.define(globalType, _constantName(constant));
579631
generator(definedGlobal.initializer);
580632
definedGlobal.initializer.end();
581633
constants.currentlyCreating = false;
@@ -830,48 +882,21 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
830882

831883
@override
832884
ConstantInfo? visitListConstant(ListConstant constant) {
833-
Constant typeArgConstant =
834-
constants._lowerTypeConstant(constant.typeArgument);
835-
ensureConstant(typeArgConstant);
836-
bool lazy = constant.entries.length > maxArrayNewFixedLength;
837-
for (Constant subConstant in constant.entries) {
838-
lazy |= ensureConstant(subConstant)?.isLazy ?? false;
839-
}
840-
841-
ClassInfo info = translator.classInfo[translator.immutableListClass]!;
842-
translator.functions.recordClassAllocation(info.classId);
843-
w.RefType type = info.nonNullableType;
844-
return createConstant(constant, type, lazy: lazy, (b) {
845-
w.ArrayType arrayType = translator.listArrayType;
846-
w.ValueType elementType = arrayType.elementType.type.unpacked;
847-
int length = constant.entries.length;
848-
b.pushObjectHeaderFields(translator, info);
849-
constants.instantiateConstant(
850-
b, typeArgConstant, constants.typeInfo.nullableType);
851-
b.i64_const(length);
852-
if (lazy) {
853-
// Allocate array and set each entry to the corresponding sub-constant.
854-
w.Local arrayLocal =
855-
b.addLocal(w.RefType.def(arrayType, nullable: false));
856-
b.i32_const(length);
857-
b.array_new_default(arrayType);
858-
b.local_set(arrayLocal);
859-
for (int i = 0; i < length; i++) {
860-
b.local_get(arrayLocal);
861-
b.i32_const(i);
862-
constants.instantiateConstant(b, constant.entries[i], elementType);
863-
b.array_set(arrayType);
864-
}
865-
b.local_get(arrayLocal);
866-
} else {
867-
// Push all sub-constants on the stack and initialize array from them.
868-
for (int i = 0; i < length; i++) {
869-
constants.instantiateConstant(b, constant.entries[i], elementType);
870-
}
871-
b.array_new_fixed(arrayType, length);
872-
}
873-
b.struct_new(info.struct);
885+
final instanceConstant =
886+
InstanceConstant(translator.immutableListClass.reference, [
887+
constant.typeArgument,
888+
], {
889+
translator.listBaseLengthField.fieldReference:
890+
IntConstant(constant.entries.length),
891+
translator.listBaseDataField.fieldReference:
892+
InstanceConstant(translator.wasmArrayClass.reference, [
893+
translator.coreTypes.objectNullableRawType
894+
], {
895+
translator.wasmArrayValueField.fieldReference: ListConstant(
896+
translator.coreTypes.objectNullableRawType, constant.entries)
897+
}),
874898
});
899+
return ensureConstant(instanceConstant);
875900
}
876901

877902
@override

pkg/dart2wasm/lib/kernel_nodes.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ mixin KernelNodes {
3939
index.getClass("dart:_boxed_int", "BoxedInt");
4040
late final Class closureClass = index.getClass("dart:core", "_Closure");
4141
late final Class listBaseClass = index.getClass("dart:_list", "WasmListBase");
42+
late final Field listBaseLengthField =
43+
index.getField("dart:_list", "WasmListBase", "_length");
44+
late final Field listBaseDataField =
45+
index.getField("dart:_list", "WasmListBase", "_data");
4246
late final Procedure listBaseIndexOperator =
4347
index.getProcedure("dart:_list", "WasmListBase", "[]");
4448
late final Class fixedLengthListClass =

0 commit comments

Comments
 (0)