Skip to content

Commit

Permalink
[dart2wasm] Represent class ids as WasmI32 in RTT
Browse files Browse the repository at this point in the history
Until binaryen does more optimizations on WasmI32<->WasmI64 that e.g. remove
conversions, perform operations on a narrower type, etc we manually use
WasmI32

Change-Id: Ic367f209daed963c236ad52087a9ddec140c100b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/374421
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
  • Loading branch information
mkustermann authored and Commit Queue committed Jul 4, 2024
1 parent 2943f43 commit 0ac19a1
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 133 deletions.
57 changes: 45 additions & 12 deletions pkg/dart2wasm/lib/intrinsics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,43 @@ class Intrinsifier {
b.i32_const(0);
b.i32_ne();
return w.NumType.i32;
}
codeGen.wrap(node.arguments.positional[0], w.NumType.i32);
switch (name) {
case "toIntSigned":
b.i64_extend_i32_s();
return w.NumType.i64;
case "toIntUnsigned":
b.i64_extend_i32_u();
return w.NumType.i64;
case "toBool":
b.i32_const(0);
b.i32_ne();
return w.NumType.i32;
case "+":
b.i32_add();
return w.NumType.i32;
case "-":
b.i32_sub();
return w.NumType.i32;
case ">>":
b.i32_shr_s();
return w.NumType.i32;
case "<":
b.i32_lt_s();
return boolType;
case "<=":
b.i32_le_s();
return boolType;
case "==":
b.i32_eq();
return boolType;
case ">=":
b.i32_ge_s();
return boolType;
case ">":
b.i32_gt_s();
return boolType;
default:
throw 'Unknown WasmI32 member $name';
}
Expand Down Expand Up @@ -428,14 +465,14 @@ class Intrinsifier {
throw 'Class $className not found in library $lib '
'(${target.location})');
int classId = translator.classInfo[cls]!.classId;
b.i64_const(classId);
return w.NumType.i64;
b.i32_const(classId);
return w.NumType.i32;
}

if (target.name.text == 'firstNonMasqueradedInterfaceClassCid') {
b.i64_const(
b.i32_const(
translator.classIdNumbering.firstNonMasqueradedInterfaceClassCid);
return w.NumType.i64;
return w.NumType.i32;
}
}

Expand Down Expand Up @@ -716,8 +753,7 @@ class Intrinsifier {
final objectClassId = translator
.classIdNumbering.classIds[translator.coreTypes.objectClass]!;

codeGen.wrap(classId, w.NumType.i64);
b.i32_wrap_i64();
codeGen.wrap(classId, w.NumType.i32);
b.emitClassIdRangeCheck([Range(objectClassId, objectClassId)]);
return w.NumType.i32;
case "_isClosureClassId":
Expand All @@ -727,8 +763,7 @@ class Intrinsifier {
.getConcreteClassIdRanges(translator.coreTypes.functionClass);
assert(ranges.length <= 1);

codeGen.wrap(classId, w.NumType.i64);
b.i32_wrap_i64();
codeGen.wrap(classId, w.NumType.i32);
b.emitClassIdRangeCheck(ranges);
return w.NumType.i32;
case "_isRecordClassId":
Expand All @@ -738,8 +773,7 @@ class Intrinsifier {
.getConcreteClassIdRanges(translator.coreTypes.recordClass);
assert(ranges.length <= 1);

codeGen.wrap(classId, w.NumType.i64);
b.i32_wrap_i64();
codeGen.wrap(classId, w.NumType.i32);
b.emitClassIdRangeCheck(ranges);
return w.NumType.i32;
}
Expand Down Expand Up @@ -803,8 +837,7 @@ class Intrinsifier {
ClassInfo info = translator.topInfo;
codeGen.wrap(node.arguments.positional.single, info.nonNullableType);
b.struct_get(info.struct, FieldIndex.classId);
b.i64_extend_i32_u();
return w.NumType.i64;
return w.NumType.i32;
case "makeListFixedLength":
return _changeListClassID(node, translator.fixedLengthListClass);
case "makeFixedListUnmodifiable":
Expand Down
6 changes: 3 additions & 3 deletions pkg/dart2wasm/lib/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Types {
final b = codeGen.b;
ClassInfo typeInfo = translator.classInfo[type.classNode]!;
b.i32_const(encodedNullability(type));
b.i64_const(typeInfo.classId);
b.i32_const(typeInfo.classId);
_makeTypeArray(codeGen, type.typeArguments);
}

Expand Down Expand Up @@ -400,7 +400,7 @@ class Types {
translator.classInfo[testedAgainstType.classNode]!;
final typeArguments = testedAgainstType.typeArguments;
b.i32_const(encodedNullability(testedAgainstType));
b.i64_const(typeClassInfo.classId);
b.i32_const(typeClassInfo.classId);
if (typeArguments.isEmpty) {
codeGen.call(translator.isInterfaceSubtype0.reference);
} else if (typeArguments.length == 1) {
Expand Down Expand Up @@ -455,7 +455,7 @@ class Types {
final typeClassInfo = translator.classInfo[testedAgainstType.classNode]!;
final typeArguments = testedAgainstType.typeArguments;
b.i32_const(encodedNullability(testedAgainstType));
b.i64_const(typeClassInfo.classId);
b.i32_const(typeClassInfo.classId);
if (typeArguments.isEmpty) {
outputsToDrop = codeGen.call(translator.asInterfaceSubtype0.reference);
} else if (typeArguments.length == 1) {
Expand Down
66 changes: 33 additions & 33 deletions sdk/lib/_internal/wasm/lib/class_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,75 @@ part of "internal_patch.dart";

@pragma("wasm:entry-point")
class ClassID {
external static int getID(Object? value);
external static WasmI32 getID(Object? value);

@pragma("wasm:class-id", "dart.typed_data#_ExternalUint8Array")
external static int get cidExternalUint8Array;
external static WasmI32 get cidExternalUint8Array;
@pragma("wasm:class-id", "dart.typed_data#_Uint8List")
external static int get cidUint8Array;
external static WasmI32 get cidUint8Array;
@pragma("wasm:class-id", "dart.typed_data#_Uint8ArrayView")
external static int get cidUint8ArrayView;
external static WasmI32 get cidUint8ArrayView;
@pragma("wasm:class-id", "dart.typed_data#Uint8ClampedList")
external static int get cidUint8ClampedList;
external static WasmI32 get cidUint8ClampedList;
@pragma("wasm:class-id", "dart.typed_data#_Uint8ClampedList")
external static int get cid_Uint8ClampedList;
external static WasmI32 get cid_Uint8ClampedList;
@pragma("wasm:class-id", "dart.typed_data#_Uint8ClampedArrayView")
external static int get cidUint8ClampedArrayView;
external static WasmI32 get cidUint8ClampedArrayView;
@pragma("wasm:class-id", "dart.typed_data#Int8List")
external static int get cidInt8List;
external static WasmI32 get cidInt8List;
@pragma("wasm:class-id", "dart.typed_data#_Int8List")
external static int get cid_Int8List;
external static WasmI32 get cid_Int8List;
@pragma("wasm:class-id", "dart.typed_data#_Int8ArrayView")
external static int get cidInt8ArrayView;
external static WasmI32 get cidInt8ArrayView;
@pragma("wasm:class-id", "dart.async#Future")
external static int get cidFuture;
external static WasmI32 get cidFuture;
@pragma("wasm:class-id", "dart.core#Function")
external static int get cidFunction;
external static WasmI32 get cidFunction;
@pragma("wasm:class-id", "dart.core#_Closure")
external static int get cid_Closure;
external static WasmI32 get cid_Closure;
@pragma("wasm:class-id", "dart.core#List")
external static int get cidList;
external static WasmI32 get cidList;
@pragma("wasm:class-id", "dart._list#ModifiableFixedLengthList")
external static int get cidFixedLengthList;
external static WasmI32 get cidFixedLengthList;
@pragma("wasm:class-id", "dart._list#WasmListBase")
external static int get cidListBase;
external static WasmI32 get cidListBase;
@pragma("wasm:class-id", "dart._list#GrowableList")
external static int get cidGrowableList;
external static WasmI32 get cidGrowableList;
@pragma("wasm:class-id", "dart._list#ImmutableList")
external static int get cidImmutableList;
external static WasmI32 get cidImmutableList;
@pragma("wasm:class-id", "dart.core#Record")
external static int get cidRecord;
external static WasmI32 get cidRecord;
@pragma("wasm:class-id", "dart.core#Symbol")
external static int get cidSymbol;
external static WasmI32 get cidSymbol;

// Class IDs for RTI Types.
@pragma("wasm:class-id", "dart.core#_BottomType")
external static int get cidBottomType;
external static WasmI32 get cidBottomType;
@pragma("wasm:class-id", "dart.core#_TopType")
external static int get cidTopType;
external static WasmI32 get cidTopType;
@pragma("wasm:class-id", "dart.core#_FutureOrType")
external static int get cidFutureOrType;
external static WasmI32 get cidFutureOrType;
@pragma("wasm:class-id", "dart.core#_InterfaceType")
external static int get cidInterfaceType;
external static WasmI32 get cidInterfaceType;
@pragma("wasm:class-id", "dart.core#_AbstractFunctionType")
external static int get cidAbstractFunctionType;
external static WasmI32 get cidAbstractFunctionType;
@pragma("wasm:class-id", "dart.core#_FunctionType")
external static int get cidFunctionType;
external static WasmI32 get cidFunctionType;
@pragma("wasm:class-id", "dart.core#_FunctionTypeParameterType")
external static int get cidFunctionTypeParameterType;
external static WasmI32 get cidFunctionTypeParameterType;
@pragma("wasm:class-id", "dart.core#_InterfaceTypeParameterType")
external static int get cidInterfaceTypeParameterType;
external static WasmI32 get cidInterfaceTypeParameterType;
@pragma("wasm:class-id", "dart.core#_AbstractRecordType")
external static int get cidAbstractRecordType;
external static WasmI32 get cidAbstractRecordType;
@pragma("wasm:class-id", "dart.core#_RecordType")
external static int get cidRecordType;
external static WasmI32 get cidRecordType;
@pragma("wasm:class-id", "dart.core#_NamedParameter")
external static int get cidNamedParameter;
external static WasmI32 get cidNamedParameter;

// From this class id onwards, all concrete classes are interface classes and
// do not need to be masqueraded.
external static int get firstNonMasqueradedInterfaceClassCid;
external static WasmI32 get firstNonMasqueradedInterfaceClassCid;

// Dummy, only used by VM-specific hash table code.
static final int numPredefinedCids = 1;
static final WasmI32 numPredefinedCids = 1.toWasmI32();
}
2 changes: 1 addition & 1 deletion sdk/lib/_internal/wasm/lib/compact_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mixin _IdenticalAndIdentityHashCode implements _EqualsAndHashCode {

mixin _OperatorEqualsAndCanonicalHashCode implements _EqualsAndHashCode {
int _hashCode(Object? e) {
final int cid = ClassID.getID(e);
final cid = ClassID.getID(e);
if (cid < ClassID.numPredefinedCids || cid == ClassID.cidSymbol) {
return e.hashCode;
}
Expand Down
Loading

0 comments on commit 0ac19a1

Please sign in to comment.