Skip to content

Commit 1bbbf26

Browse files
fishythefishCommit Queue
authored andcommitted
[dart2js] Refactor FlatTypeMask cache.
As the powerset grows, the cache needs to be keyed on both the base and the full flags. While we're at it, we ensure that all FlatTypeMask allocations go via the cache. Change-Id: Ifdd400bf4ea23a936c4d37be91a5d527102aa3b4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413700 Reviewed-by: Nate Biggs <[email protected]> Commit-Queue: Mayank Patke <[email protected]>
1 parent d0e09ea commit 1bbbf26

30 files changed

+784
-766
lines changed

pkg/compiler/lib/src/inferrer/abstract_value_strategy.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import 'abstract_value_domain.dart';
88

99
/// Strategy for the abstraction of runtime values used by the global type
1010
/// inference.
11-
abstract class AbstractValueStrategy {
11+
abstract class AbstractValueStrategy<T extends AbstractValueDomain> {
1212
/// Creates the abstract value domain for [closedWorld].
13-
AbstractValueDomain createDomain(covariant JClosedWorld closedWorld);
13+
T createDomain(JClosedWorld closedWorld);
1414

1515
/// Creates the [SelectorConstraintsStrategy] used by the backend enqueuer.
16-
SelectorConstraintsStrategy createSelectorStrategy();
16+
SelectorConstraintsStrategy createSelectorStrategy(T domain);
1717
}

pkg/compiler/lib/src/inferrer/computable.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,18 +708,22 @@ class ComputableAbstractValueDomain with AbstractValueDomain {
708708
}
709709
}
710710

711-
class ComputableAbstractValueStrategy implements AbstractValueStrategy {
711+
class ComputableAbstractValueStrategy
712+
implements AbstractValueStrategy<ComputableAbstractValueDomain> {
712713
final AbstractValueStrategy _wrappedStrategy;
713714

714715
const ComputableAbstractValueStrategy(this._wrappedStrategy);
715716

716717
@override
717-
AbstractValueDomain createDomain(JClosedWorld closedWorld) =>
718+
ComputableAbstractValueDomain createDomain(JClosedWorld closedWorld) =>
718719
ComputableAbstractValueDomain(_wrappedStrategy.createDomain(closedWorld));
719720

720721
@override
721-
SelectorConstraintsStrategy createSelectorStrategy() =>
722-
ComputableSelectorStrategy(_wrappedStrategy.createSelectorStrategy());
722+
SelectorConstraintsStrategy createSelectorStrategy(
723+
ComputableAbstractValueDomain domain,
724+
) => ComputableSelectorStrategy(
725+
_wrappedStrategy.createSelectorStrategy(domain._wrappedDomain),
726+
);
723727
}
724728

725729
class ComputableSelectorStrategy implements SelectorConstraintsStrategy {

pkg/compiler/lib/src/inferrer/powersets/powersets.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,22 +1019,24 @@ class PowersetDomain with AbstractValueDomain {
10191019
}
10201020
}
10211021

1022-
class PowersetStrategy implements AbstractValueStrategy {
1022+
class PowersetStrategy implements AbstractValueStrategy<PowersetDomain> {
10231023
final AbstractValueStrategy _abstractValueStrategy;
10241024
const PowersetStrategy(this._abstractValueStrategy);
10251025

10261026
@override
1027-
AbstractValueDomain createDomain(JClosedWorld closedWorld) {
1027+
PowersetDomain createDomain(JClosedWorld closedWorld) {
10281028
return PowersetDomain(
10291029
_abstractValueStrategy.createDomain(closedWorld),
10301030
PowersetBitsDomain(closedWorld),
10311031
);
10321032
}
10331033

10341034
@override
1035-
SelectorConstraintsStrategy createSelectorStrategy() {
1035+
SelectorConstraintsStrategy createSelectorStrategy(PowersetDomain domain) {
10361036
return PowersetsSelectorStrategy(
1037-
_abstractValueStrategy.createSelectorStrategy(),
1037+
_abstractValueStrategy.createSelectorStrategy(
1038+
domain._abstractValueDomain,
1039+
),
10381040
);
10391041
}
10401042
}

pkg/compiler/lib/src/inferrer/trivial.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,19 @@ class TrivialAbstractValueDomain with AbstractValueDomain {
483483
AbstractValue get typeType => const TrivialAbstractValue();
484484
}
485485

486-
class TrivialAbstractValueStrategy implements AbstractValueStrategy {
486+
class TrivialAbstractValueStrategy
487+
implements AbstractValueStrategy<TrivialAbstractValueDomain> {
487488
const TrivialAbstractValueStrategy();
488489

489490
@override
490-
AbstractValueDomain createDomain(JClosedWorld closedWorld) {
491+
TrivialAbstractValueDomain createDomain(JClosedWorld closedWorld) {
491492
return const TrivialAbstractValueDomain();
492493
}
493494

494495
@override
495-
SelectorConstraintsStrategy createSelectorStrategy() {
496+
SelectorConstraintsStrategy createSelectorStrategy(
497+
TrivialAbstractValueDomain domain,
498+
) {
496499
return const TrivialSelectorStrategy();
497500
}
498501
}

pkg/compiler/lib/src/inferrer/typemasks/constants.dart

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,39 @@ import '../../js_model/js_world.dart' show JClosedWorld;
1010
import 'masks.dart';
1111

1212
/// Computes the [TypeMask] for the constant [value].
13-
TypeMask computeTypeMask(
14-
CommonMasks abstractValueDomain,
15-
JClosedWorld closedWorld,
16-
ConstantValue value,
17-
) {
18-
return value.accept(ConstantValueTypeMasks(abstractValueDomain), closedWorld);
13+
TypeMask computeTypeMask(CommonMasks abstractValueDomain, ConstantValue value) {
14+
return value.accept(ConstantValueTypeMasks(abstractValueDomain), null);
1915
}
2016

21-
class ConstantValueTypeMasks
22-
extends ConstantValueVisitor<TypeMask, JClosedWorld> {
17+
class ConstantValueTypeMasks extends ConstantValueVisitor<TypeMask, Null> {
2318
final CommonMasks _abstractValueDomain;
2419
const ConstantValueTypeMasks(this._abstractValueDomain);
2520

21+
JClosedWorld get closedWorld => _abstractValueDomain.closedWorld;
22+
2623
@override
27-
TypeMask visitConstructed(
28-
ConstructedConstantValue constant,
29-
JClosedWorld closedWorld,
30-
) {
24+
TypeMask visitConstructed(ConstructedConstantValue constant, _) {
3125
if (closedWorld.interceptorData.isInterceptedClass(constant.type.element)) {
3226
return _abstractValueDomain.nonNullType;
3327
}
34-
return TypeMask.nonNullExact(constant.type.element, closedWorld);
28+
return TypeMask.nonNullExact(constant.type.element, _abstractValueDomain);
3529
}
3630

3731
@override
38-
TypeMask visitRecord(RecordConstantValue constant, JClosedWorld closedWorld) {
32+
TypeMask visitRecord(RecordConstantValue constant, _) {
3933
final representation = closedWorld.recordData.representationForShape(
4034
constant.shape,
4135
);
4236
if (representation == null) return _abstractValueDomain.recordType;
43-
return TypeMask.nonNullExact(representation.cls, closedWorld);
37+
return TypeMask.nonNullExact(representation.cls, _abstractValueDomain);
4438
}
4539

4640
@override
47-
TypeMask visitDeferredGlobal(
48-
DeferredGlobalConstantValue constant,
49-
JClosedWorld closedWorld,
50-
) {
51-
return constant.referenced.accept(this, closedWorld);
52-
}
41+
TypeMask visitDeferredGlobal(DeferredGlobalConstantValue constant, _) =>
42+
constant.referenced.accept(this, null);
5343

5444
@override
55-
TypeMask visitDouble(DoubleConstantValue constant, JClosedWorld closedWorld) {
45+
TypeMask visitDouble(DoubleConstantValue constant, _) {
5646
// We have to recognize double constants that are 'is int'.
5747
if (constant_system.isInt(constant)) {
5848
if (constant.isMinusZero) {
@@ -65,105 +55,71 @@ class ConstantValueTypeMasks
6555
}
6656

6757
@override
68-
TypeMask visitDummyInterceptor(
69-
DummyInterceptorConstantValue constant,
70-
JClosedWorld closedWorld,
71-
) {
72-
return _abstractValueDomain.dynamicType;
73-
}
58+
TypeMask visitDummyInterceptor(DummyInterceptorConstantValue constant, _) =>
59+
_abstractValueDomain.dynamicType;
7460

7561
@override
76-
TypeMask visitLateSentinel(
77-
LateSentinelConstantValue constant,
78-
JClosedWorld closedWorld,
79-
) => _abstractValueDomain.lateSentinelType;
62+
TypeMask visitLateSentinel(LateSentinelConstantValue constant, _) =>
63+
_abstractValueDomain.lateSentinelType;
8064

8165
@override
82-
TypeMask visitUnreachable(
83-
UnreachableConstantValue constant,
84-
JClosedWorld closedWorld,
85-
) {
86-
return _abstractValueDomain.emptyType;
87-
}
66+
TypeMask visitUnreachable(UnreachableConstantValue constant, _) =>
67+
_abstractValueDomain.emptyType;
8868

8969
@override
90-
TypeMask visitJsName(JsNameConstantValue constant, JClosedWorld closedWorld) {
91-
return _abstractValueDomain.stringType;
92-
}
70+
TypeMask visitJsName(JsNameConstantValue constant, _) =>
71+
_abstractValueDomain.stringType;
9372

9473
@override
95-
TypeMask visitBool(BoolConstantValue constant, JClosedWorld closedWorld) {
96-
return _abstractValueDomain.boolType;
97-
}
74+
TypeMask visitBool(BoolConstantValue constant, _) =>
75+
_abstractValueDomain.boolType;
9876

9977
@override
100-
TypeMask visitFunction(
101-
FunctionConstantValue constant,
102-
JClosedWorld closedWorld,
103-
) {
104-
return _abstractValueDomain.functionType;
105-
}
78+
TypeMask visitFunction(FunctionConstantValue constant, _) =>
79+
_abstractValueDomain.functionType;
10680

10781
@override
108-
TypeMask visitInstantiation(
109-
InstantiationConstantValue constant,
110-
JClosedWorld closedWorld,
111-
) {
112-
return _abstractValueDomain.functionType;
113-
}
82+
TypeMask visitInstantiation(InstantiationConstantValue constant, _) =>
83+
_abstractValueDomain.functionType;
11484

11585
@override
116-
TypeMask visitInt(IntConstantValue constant, JClosedWorld closedWorld) {
86+
TypeMask visitInt(IntConstantValue constant, _) {
11787
if (constant.isUInt31()) return _abstractValueDomain.uint31Type;
11888
if (constant.isUInt32()) return _abstractValueDomain.uint32Type;
11989
if (constant.isPositive()) return _abstractValueDomain.positiveIntType;
12090
return _abstractValueDomain.intType;
12191
}
12292

12393
@override
124-
TypeMask visitInterceptor(
125-
InterceptorConstantValue constant,
126-
JClosedWorld closedWorld,
127-
) {
128-
return _abstractValueDomain.nonNullType;
129-
}
94+
TypeMask visitInterceptor(InterceptorConstantValue constant, _) =>
95+
_abstractValueDomain.nonNullType;
13096

13197
@override
132-
TypeMask visitList(ListConstantValue constant, JClosedWorld closedWorld) {
133-
return _abstractValueDomain.constListType;
134-
}
98+
TypeMask visitList(ListConstantValue constant, _) =>
99+
_abstractValueDomain.constListType;
135100

136101
@override
137-
TypeMask visitSet(SetConstantValue constant, JClosedWorld closedWorld) {
138-
return _abstractValueDomain.constSetType;
139-
}
102+
TypeMask visitSet(SetConstantValue constant, _) =>
103+
_abstractValueDomain.constSetType;
140104

141105
@override
142-
TypeMask visitMap(MapConstantValue constant, JClosedWorld closedWorld) {
143-
return _abstractValueDomain.constMapType;
144-
}
106+
TypeMask visitMap(MapConstantValue constant, _) =>
107+
_abstractValueDomain.constMapType;
145108

146109
@override
147-
TypeMask visitNull(NullConstantValue constant, JClosedWorld closedWorld) {
148-
return _abstractValueDomain.nullType;
149-
}
110+
TypeMask visitNull(NullConstantValue constant, _) =>
111+
_abstractValueDomain.nullType;
150112

151113
@override
152-
TypeMask visitString(StringConstantValue constant, JClosedWorld closedWorld) {
153-
return _abstractValueDomain.stringType;
154-
}
114+
TypeMask visitString(StringConstantValue constant, _) =>
115+
_abstractValueDomain.stringType;
155116

156117
@override
157-
TypeMask visitType(TypeConstantValue constant, JClosedWorld closedWorld) {
158-
return _abstractValueDomain.typeType;
159-
}
118+
TypeMask visitType(TypeConstantValue constant, _) =>
119+
_abstractValueDomain.typeType;
160120

161121
@override
162-
TypeMask visitJavaScriptObject(
163-
JavaScriptObjectConstantValue constant,
164-
JClosedWorld closedWorld,
165-
) {
166-
// TODO(sra): Change to plain JavaScript object.
167-
return _abstractValueDomain.dynamicType;
168-
}
122+
TypeMask visitJavaScriptObject(JavaScriptObjectConstantValue constant, _) =>
123+
// TODO(sra): Change to plain JavaScript object.
124+
_abstractValueDomain.dynamicType;
169125
}

pkg/compiler/lib/src/inferrer/typemasks/container_type_mask.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ class ContainerTypeMask extends AllocationTypeMask {
6969
}
7070

7171
@override
72-
ContainerTypeMask withPowerset(Bitset powerset) {
72+
ContainerTypeMask withPowerset(Bitset powerset, CommonMasks domain) {
7373
if (powerset == this.powerset) return this;
7474
return ContainerTypeMask(
75-
forwardTo.withPowerset(powerset),
75+
forwardTo.withPowerset(powerset, domain),
7676
allocationNode,
7777
allocationElement,
7878
elementType,

pkg/compiler/lib/src/inferrer/typemasks/dictionary_type_mask.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class DictionaryTypeMask extends MapTypeMask {
6767
}
6868

6969
@override
70-
DictionaryTypeMask withPowerset(Bitset powerset) {
70+
DictionaryTypeMask withPowerset(Bitset powerset, CommonMasks domain) {
7171
if (powerset == this.powerset) return this;
7272
return DictionaryTypeMask(
73-
forwardTo.withPowerset(powerset),
73+
forwardTo.withPowerset(powerset, domain),
7474
allocationNode,
7575
allocationElement,
7676
keyType,
@@ -99,14 +99,14 @@ class DictionaryTypeMask extends MapTypeMask {
9999
Map<String, TypeMask> mappings = {};
100100
_typeMap.forEach((k, v) {
101101
if (!other._typeMap.containsKey(k)) {
102-
mappings[k] = v.nullable();
102+
mappings[k] = v.nullable(domain);
103103
}
104104
});
105105
other._typeMap.forEach((k, v) {
106106
if (_typeMap.containsKey(k)) {
107107
mappings[k] = v.union(_typeMap[k]!, domain);
108108
} else {
109-
mappings[k] = v.nullable();
109+
mappings[k] = v.nullable(domain);
110110
}
111111
});
112112
return DictionaryTypeMask(

0 commit comments

Comments
 (0)