Skip to content

Commit af2bea8

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Make int, double, bool box fields immutable
Make the `value` fields of `BoxedInt`, `BoxedDouble` and `BoxedBool` `final`, to generate immutable Wasm fields for them. Immutable fields can potentially generate better code, as loads from the same object will always generate the same value. To allow making the `value` fields `final`, add a constructor. To allow adding a constructor, "implement` base classes instead of extending them. With `extends` the front-end wants us to call the superclass constructors, even though they don't have any constructors. Implementing `bool` (instead of extending) causes issues in TFA as it currently assumes bool literals are compiled as the `bool` class. Update TFA to treat bool literals the same way as `int` and `double` literals. Tested: existing tests Change-Id: I3282e188d784fa7a22421edc79ed47f9d85faf19 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393320 Commit-Queue: Ömer Ağacan <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 8a1c020 commit af2bea8

File tree

8 files changed

+49
-18
lines changed

8 files changed

+49
-18
lines changed

pkg/dart2wasm/lib/class_info.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ class ClassInfoCollector {
311311
// directly below the public classes they implement.
312312
// All other classes sit below their superclass.
313313
ClassInfo superInfo = cls == translator.coreTypes.boolClass ||
314-
cls == translator.coreTypes.numClass
314+
cls == translator.coreTypes.numClass ||
315+
cls == translator.boxedIntClass ||
316+
cls == translator.boxedDoubleClass ||
317+
cls == translator.boxedBoolClass
315318
? topInfo
316319
: (!translator.options.jsCompatibility &&
317320
cls == translator.wasmStringBaseClass) ||

pkg/dart2wasm/lib/target.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class WasmTarget extends Target {
112112
Class? _closure;
113113
Class? _boxedInt;
114114
Class? _boxedDouble;
115+
Class? _boxedBool;
115116
Map<String, Class>? _nativeClasses;
116117

117118
@override
@@ -546,6 +547,10 @@ class WasmTarget extends Target {
546547
_boxedDouble ??=
547548
coreTypes.index.getClass("dart:_boxed_double", "BoxedDouble");
548549

550+
@override
551+
Class concreteBoolLiteralClass(CoreTypes coreTypes, bool value) =>
552+
_boxedBool ??= coreTypes.index.getClass("dart:_boxed_bool", "BoxedBool");
553+
549554
@override
550555
DartLibrarySupport get dartLibrarySupport => CustomizedDartLibrarySupport(
551556
unsupported: {if (!enableExperimentalFfi) 'ffi'});

pkg/dart2wasm/lib/translator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class Translator with KernelNodes {
240240
late final Map<Class, Class> valueClasses = {
241241
boxedIntClass: boxedIntClass,
242242
boxedDoubleClass: boxedDoubleClass,
243-
boxedBoolClass: coreTypes.boolClass,
243+
boxedBoolClass: boxedBoolClass,
244244
if (!options.jsCompatibility) ...{
245245
oneByteStringClass: stringBaseClass,
246246
twoByteStringClass: stringBaseClass

pkg/kernel/lib/target/targets.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ abstract class Target {
507507
Class? concreteIntLiteralClass(CoreTypes coreTypes, int value) => null;
508508
Class? concreteDoubleLiteralClass(CoreTypes coreTypes, double value) => null;
509509
Class? concreteStringLiteralClass(CoreTypes coreTypes, String value) => null;
510+
Class? concreteBoolLiteralClass(CoreTypes coreTypes, bool value) => null;
510511

511512
Class? concreteAsyncResultClass(CoreTypes coreTypes) => null;
512513
Class? concreteSyncStarResultClass(CoreTypes coreTypes) => null;

pkg/vm/lib/transformations/type_flow/summary_collector.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,6 @@ class SummaryCollector extends RecursiveResultVisitor<TypeExpr?> {
14251425

14261426
Class get _superclass => _staticTypeContext!.thisType!.classNode.superclass!;
14271427

1428-
Type _boolLiteralType(bool value) => value ? _boolTrue : _boolFalse;
1429-
14301428
Type _intLiteralType(int value, Constant? constant) {
14311429
final Class? concreteClass =
14321430
target.concreteIntLiteralClass(_environment.coreTypes, value);
@@ -1466,6 +1464,19 @@ class SummaryCollector extends RecursiveResultVisitor<TypeExpr?> {
14661464
return _stringType;
14671465
}
14681466

1467+
Type _boolLiteralType(bool value, Constant? constant) {
1468+
final Class? concreteClass =
1469+
target.concreteBoolLiteralClass(_environment.coreTypes, value);
1470+
if (concreteClass != null) {
1471+
constant ??= BoolConstant(value);
1472+
return _entryPointsListener
1473+
.addAllocatedClass(concreteClass)
1474+
.cls
1475+
.constantConcreteType(constant);
1476+
}
1477+
return value ? _boolTrue : _boolFalse;
1478+
}
1479+
14691480
TypeExpr _closureType(LocalFunction node) {
14701481
final Class? concreteClass =
14711482
target.concreteClosureClass(_environment.coreTypes);
@@ -1682,7 +1693,7 @@ class SummaryCollector extends RecursiveResultVisitor<TypeExpr?> {
16821693

16831694
@override
16841695
TypeExpr visitBoolLiteral(BoolLiteral node) {
1685-
return _boolLiteralType(node.value);
1696+
return _boolLiteralType(node.value, null);
16861697
}
16871698

16881699
@override
@@ -2845,7 +2856,7 @@ class ConstantAllocationCollector implements ConstantVisitor<Type> {
28452856

28462857
@override
28472858
Type visitBoolConstant(BoolConstant constant) {
2848-
return summaryCollector._boolLiteralType(constant.value);
2859+
return summaryCollector._boolLiteralType(constant.value, constant);
28492860
}
28502861

28512862
@override

sdk/lib/_internal/wasm/lib/boxed_bool.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
@pragma("wasm:entry-point")
6-
final class BoxedBool extends bool {
6+
final class BoxedBool implements bool {
77
// A boxed bool contains an unboxed bool.
88
@pragma("wasm:entry-point")
9-
bool value = false;
9+
final bool value;
1010

11-
/// Dummy factory to silence error about missing superclass constructor.
12-
external factory BoxedBool();
11+
@pragma("wasm:entry-point")
12+
BoxedBool._(this.value);
13+
14+
@override
15+
int get hashCode => this ? 1231 : 1237;
16+
17+
@override
18+
String toString() => this ? "true" : "false";
1319

1420
@override
1521
bool operator ==(Object other) {
@@ -18,7 +24,12 @@ final class BoxedBool extends bool {
1824
: false;
1925
}
2026

27+
@override
2128
bool operator &(bool other) => this & other; // Intrinsic &
29+
30+
@override
2231
bool operator ^(bool other) => this ^ other; // Intrinsic ^
32+
33+
@override
2334
bool operator |(bool other) => this | other; // Intrinsic |
2435
}

sdk/lib/_internal/wasm/lib/boxed_double.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import 'dart:_string';
99
import 'dart:_wasm';
1010

1111
@pragma("wasm:entry-point")
12-
final class BoxedDouble extends double {
12+
final class BoxedDouble implements double {
1313
// A boxed double contains an unboxed double.
1414
@pragma("wasm:entry-point")
15-
double value = 0.0;
15+
final double value;
1616

17-
/// Dummy factory to silence error about missing superclass constructor.
18-
external factory BoxedDouble();
17+
@pragma("wasm:entry-point")
18+
BoxedDouble._(this.value);
1919

2020
static const int _mantissaBits = 52;
2121
static const int _exponentBits = 11;

sdk/lib/_internal/wasm/lib/boxed_int.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import 'dart:_internal';
77
import 'dart:_wasm';
88

99
@pragma("wasm:entry-point")
10-
final class BoxedInt extends int {
10+
final class BoxedInt implements int {
1111
// A boxed int contains an unboxed int.
1212
@pragma("wasm:entry-point")
13-
int value = 0;
13+
final int value;
1414

15-
/// Dummy factory to silence error about missing superclass constructor.
16-
external factory BoxedInt();
15+
@pragma("wasm:entry-point")
16+
BoxedInt._(this.value);
1717

1818
external num operator +(num other);
1919
external num operator -(num other);

0 commit comments

Comments
 (0)