Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 34763bc

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
Reland "[VM] Introduction of type testing stubs - Part 1-4"
Relands 165c583 [VM] Introduction of type testing stubs - Part 1 This CL: * Adds a field to [RawAbstractType] which will always hold a pointer to the entrypoint of a type testing stub * Makes this new field be initialized to a default stub whenever a instances are created (e.g. via Type::New(), snapshot reader, ...) * Makes the clustered snapshotter write a reference to the corresponding [RawInstructions] object when writing the field and do the reverse when reading it. * Makes us call the type testing stub for performing assert-assignable checks. To reduce unnecessary loads on callsites, we store the entrypoint of the type testing stubs directly in the type objects. This means that the caller of type testing stubs can simply branch there without populating a code object first. This also means that the type testing stubs themselves have no access to a pool and we therefore also don't hold on to the [Code] object, only the [Instruction] object is necessary. The type testing stubs do not setup a frame themselves and also have no safepoint. In the case when the type testing stubs could not determine a positive answer they will tail-call a general-purpose stub. The general-purpose stub sets up a stub frame, tries to consult a [SubtypeTestCache] and bails out to runtime if this was unsuccessful. This CL is just the the first, for ease of reviewing. The actual type-specialized type testing stubs will be generated in later CLs. Reviewed-on: https://dart-review.googlesource.com/44787 Relands f226c22 [VM] Introduction of type testing stubs - Part 2 This CL starts building type testing stubs specialzed for [Type] objects we test against. More specifically, it adds support for: * Handling obvious fast cases on the call sites (while still having a call to stub for negative case) * Handling type tests against type parameters, by loading the value of the type parameter on the call sites and invoking it's type testing stub. * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subtype-checks. ==> e.g. String/List<dynamic> * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the type arguments. ==> e.g. Widget<State>, where we know [Widget] is only extended and not implemented. * Specialzed type testing stubs for certain non-instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the instantiated type arguments and cid based comparisons for type parameters. (Note that this fast-case migth result in some false-negatives!) ==> e.g. _HashMapEntry<K, V>, where we know [_HashMapEntry] is only extended and not implemented. This optimizes cases where the caller uses `new HashMap<A, B>()` and only uses `A` and `B` as key/values (and not subclasses of it). The false-negative can occur when subtypes of A or B are used. In such cases we fall back to the [SubtypeTestCache]-based imlementation. Reviewed-on: https://dart-review.googlesource.com/44788 Relands 25f98bc [VM] Introduction of type testing stubs - Part 3 The changes include: * Make AssertAssignableInstr no longer have a call-summary, which helps methods with several parameter checks by not having to re-load/re-initialize type arguments registers * Lazily create SubtypeTestCaches: We already go to runtime to warm up the caches, so we now also create the caches on the first runtime call and patch the pool entries. * No longer load the destination name into a register: We only need the name when we throw an exception, so it is not on the hot path. Instead we let the runtime look at the call site, decoding a pool index from the instructions stream. The destination name will be available in the pool, at a consecutive index to the subtype cache. * Remove the fall-through to N=1 case for probing subtypeing tests, since those will always be handled by the optimized stubs. * Do not generate optimized stubs for FutureOr<T> (so far it just falled-through to TTS). We can make optimzed version of that later, but it requires special subtyping rules. * Local code quality improvement in the type-testing-stubs: Avoid extra jump at last case of cid-class-range checks. There are still a number of optimization opportunities we can do in future changes. Reviewed-on: https://dart-review.googlesource.com/46984 Relands 2c52480 [VM] Introduction of type testing stubs - Part 4 In order to avoid generating type testing stubs for too many types in the system - and thereby potentially cause an increase in code size - this change introduces a smarter way to decide for which types we should generate optimized type testing stubs. The precompiler creates a [TypeUsageInfo] which we use to collect information. More specifically: a) We collect the destination types for all type checks we emit (we do this inside AssertAssignableInstr::EmitNativeCode). -> These are types we might want to generate optimized type testing stubs for. b) We collect type argument vectors used in instance creations (we do this inside AllocateObjectInstr::EmitNativeCode) and keep a set of of used type argument vectors for each class. After the precompiler has finished compiling normal code we scan the set of destination types collected in a) for uninstantiated types (or more specifically, type parameter types). We then propagate the type argument vectors used on object allocation sites, which were collected in b), in order to find out what kind of types are flowing into those type parameters. This allows us to extend the set of types which we test against, by adding the types that flow into type parameters. We use this final augmented set of destination types as a "filter" when making the decision whether to generate an optimized type testing stub for a given type. Reviewed-on: https://dart-review.googlesource.com/48640 Issue dart-lang/sdk#32603 Change-Id: I6d33d4ca3d5187a1eb1664078c003061855f0160 Reviewed-on: https://dart-review.googlesource.com/50482 Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent a86b864 commit 34763bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3391
-237
lines changed

runtime/vm/class_finalizer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "vm/symbols.h"
1919
#include "vm/timeline.h"
2020
#include "vm/type_table.h"
21+
#include "vm/type_testing_stubs.h"
2122

2223
namespace dart {
2324

@@ -589,6 +590,10 @@ void ClassFinalizer::ResolveType(const Class& cls, const AbstractType& type) {
589590
}
590591
}
591592
}
593+
594+
// After resolving, we re-initialize the type testing stub.
595+
type.SetTypeTestingStub(
596+
Instructions::Handle(TypeTestingStubGenerator::DefaultCodeForType(type)));
592597
}
593598

594599
void ClassFinalizer::FinalizeTypeParameters(const Class& cls,

runtime/vm/clustered_snapshot.cc

Lines changed: 143 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,8 @@ class LibraryPrefixDeserializationCluster : public DeserializationCluster {
30143014
#if !defined(DART_PRECOMPILED_RUNTIME)
30153015
class TypeSerializationCluster : public SerializationCluster {
30163016
public:
3017-
TypeSerializationCluster() : SerializationCluster("Type") {}
3017+
explicit TypeSerializationCluster(const TypeTestingStubFinder& ttsf)
3018+
: SerializationCluster("Type"), type_testing_stubs_(ttsf) {}
30183019
virtual ~TypeSerializationCluster() {}
30193020

30203021
void Trace(Serializer* s, RawObject* object) {
@@ -3069,6 +3070,12 @@ class TypeSerializationCluster : public SerializationCluster {
30693070
}
30703071
s->WriteTokenPosition(type->ptr()->token_pos_);
30713072
s->Write<int8_t>(type->ptr()->type_state_);
3073+
if (s->kind() == Snapshot::kFullAOT) {
3074+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3075+
type->ptr()->type_test_stub_entry_point_);
3076+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3077+
s->Write<int32_t>(text_offset);
3078+
}
30723079
}
30733080
count = objects_.length();
30743081
for (intptr_t i = 0; i < count; i++) {
@@ -3080,18 +3087,38 @@ class TypeSerializationCluster : public SerializationCluster {
30803087
}
30813088
s->WriteTokenPosition(type->ptr()->token_pos_);
30823089
s->Write<int8_t>(type->ptr()->type_state_);
3090+
if (s->kind() == Snapshot::kFullAOT) {
3091+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3092+
type->ptr()->type_test_stub_entry_point_);
3093+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3094+
s->Write<int32_t>(text_offset);
3095+
}
3096+
}
3097+
3098+
// The dynamic/void types are not serialized, so we manually send
3099+
// the type testing stubs for them.
3100+
if (s->kind() == Snapshot::kFullAOT && s->for_vm_isolate()) {
3101+
RawInstructions* dynamic_instr = type_testing_stubs_.LookupByAddresss(
3102+
Type::dynamic_type().type_test_stub_entry_point());
3103+
s->Write<int32_t>(s->GetTextOffset(dynamic_instr, Code::null()));
3104+
3105+
RawInstructions* void_instr = type_testing_stubs_.LookupByAddresss(
3106+
Type::void_type().type_test_stub_entry_point());
3107+
s->Write<int32_t>(s->GetTextOffset(void_instr, Code::null()));
30833108
}
30843109
}
30853110

30863111
private:
30873112
GrowableArray<RawType*> canonical_objects_;
30883113
GrowableArray<RawType*> objects_;
3114+
const TypeTestingStubFinder& type_testing_stubs_;
30893115
};
30903116
#endif // !DART_PRECOMPILED_RUNTIME
30913117

30923118
class TypeDeserializationCluster : public DeserializationCluster {
30933119
public:
3094-
TypeDeserializationCluster() {}
3120+
TypeDeserializationCluster()
3121+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
30953122
virtual ~TypeDeserializationCluster() {}
30963123

30973124
void ReadAlloc(Deserializer* d) {
@@ -3126,6 +3153,12 @@ class TypeDeserializationCluster : public DeserializationCluster {
31263153
}
31273154
type->ptr()->token_pos_ = d->ReadTokenPosition();
31283155
type->ptr()->type_state_ = d->Read<int8_t>();
3156+
if (d->kind() == Snapshot::kFullAOT) {
3157+
const int32_t text_offset = d->Read<int32_t>();
3158+
instr_ = d->GetInstructionsAt(text_offset);
3159+
type_ = type;
3160+
type_.SetTypeTestingStub(instr_);
3161+
}
31293162
}
31303163

31313164
for (intptr_t id = start_index_; id < stop_index_; id++) {
@@ -3139,18 +3172,55 @@ class TypeDeserializationCluster : public DeserializationCluster {
31393172
}
31403173
type->ptr()->token_pos_ = d->ReadTokenPosition();
31413174
type->ptr()->type_state_ = d->Read<int8_t>();
3175+
if (d->kind() == Snapshot::kFullAOT) {
3176+
const int32_t text_offset = d->Read<int32_t>();
3177+
instr_ = d->GetInstructionsAt(text_offset);
3178+
type_ = type;
3179+
type_.SetTypeTestingStub(instr_);
3180+
}
3181+
}
3182+
3183+
// The [Type::dynamic_type()] object is not serialized, so we manually send
3184+
// the type testing stub for it.
3185+
if (d->kind() == Snapshot::kFullAOT && d->for_vm_isolate()) {
3186+
const int32_t text_offset = d->Read<int32_t>();
3187+
Dart::vm_isolate()->heap()->WriteProtect(false);
3188+
instr_ = d->GetInstructionsAt(text_offset);
3189+
Type::dynamic_type().SetTypeTestingStub(instr_);
3190+
instr_ = d->GetInstructionsAt(text_offset);
3191+
Type::void_type().SetTypeTestingStub(instr_);
3192+
Dart::vm_isolate()->heap()->WriteProtect(true);
3193+
}
3194+
}
3195+
3196+
void PostLoad(const Array& refs, Snapshot::Kind kind, Zone* zone) {
3197+
if (kind != Snapshot::kFullAOT) {
3198+
for (intptr_t id = canonical_start_index_; id < canonical_stop_index_;
3199+
id++) {
3200+
type_ ^= refs.At(id);
3201+
instr_ = TypeTestingStubGenerator::DefaultCodeForType(type_);
3202+
type_.SetTypeTestingStub(instr_);
3203+
}
3204+
for (intptr_t id = start_index_; id < stop_index_; id++) {
3205+
type_ ^= refs.At(id);
3206+
instr_ = TypeTestingStubGenerator::DefaultCodeForType(type_);
3207+
type_.SetTypeTestingStub(instr_);
3208+
}
31423209
}
31433210
}
31443211

31453212
private:
31463213
intptr_t canonical_start_index_;
31473214
intptr_t canonical_stop_index_;
3215+
AbstractType& type_;
3216+
Instructions& instr_;
31483217
};
31493218

31503219
#if !defined(DART_PRECOMPILED_RUNTIME)
31513220
class TypeRefSerializationCluster : public SerializationCluster {
31523221
public:
3153-
TypeRefSerializationCluster() : SerializationCluster("TypeRef") {}
3222+
explicit TypeRefSerializationCluster(const TypeTestingStubFinder& ttsf)
3223+
: SerializationCluster("TypeRef"), type_testing_stubs_(ttsf) {}
31543224
virtual ~TypeRefSerializationCluster() {}
31553225

31563226
void Trace(Serializer* s, RawObject* object) {
@@ -3183,17 +3253,25 @@ class TypeRefSerializationCluster : public SerializationCluster {
31833253
for (RawObject** p = from; p <= to; p++) {
31843254
s->WriteRef(*p);
31853255
}
3256+
if (s->kind() == Snapshot::kFullAOT) {
3257+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3258+
type->ptr()->type_test_stub_entry_point_);
3259+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3260+
s->Write<int32_t>(text_offset);
3261+
}
31863262
}
31873263
}
31883264

31893265
private:
31903266
GrowableArray<RawTypeRef*> objects_;
3267+
const TypeTestingStubFinder& type_testing_stubs_;
31913268
};
31923269
#endif // !DART_PRECOMPILED_RUNTIME
31933270

31943271
class TypeRefDeserializationCluster : public DeserializationCluster {
31953272
public:
3196-
TypeRefDeserializationCluster() {}
3273+
TypeRefDeserializationCluster()
3274+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
31973275
virtual ~TypeRefDeserializationCluster() {}
31983276

31993277
void ReadAlloc(Deserializer* d) {
@@ -3218,14 +3296,26 @@ class TypeRefDeserializationCluster : public DeserializationCluster {
32183296
for (RawObject** p = from; p <= to; p++) {
32193297
*p = d->ReadRef();
32203298
}
3299+
if (d->kind() == Snapshot::kFullAOT) {
3300+
const int32_t text_offset = d->Read<int32_t>();
3301+
instr_ = d->GetInstructionsAt(text_offset);
3302+
type_ = type;
3303+
type_.SetTypeTestingStub(instr_);
3304+
}
32213305
}
32223306
}
3307+
3308+
private:
3309+
AbstractType& type_;
3310+
Instructions& instr_;
32233311
};
32243312

32253313
#if !defined(DART_PRECOMPILED_RUNTIME)
32263314
class TypeParameterSerializationCluster : public SerializationCluster {
32273315
public:
3228-
TypeParameterSerializationCluster() : SerializationCluster("TypeParameter") {}
3316+
explicit TypeParameterSerializationCluster(const TypeTestingStubFinder& ttsf)
3317+
: SerializationCluster("TypeParameter"), type_testing_stubs_(ttsf) {}
3318+
32293319
virtual ~TypeParameterSerializationCluster() {}
32303320

32313321
void Trace(Serializer* s, RawObject* object) {
@@ -3263,17 +3353,25 @@ class TypeParameterSerializationCluster : public SerializationCluster {
32633353
s->WriteTokenPosition(type->ptr()->token_pos_);
32643354
s->Write<int16_t>(type->ptr()->index_);
32653355
s->Write<int8_t>(type->ptr()->type_state_);
3356+
if (s->kind() == Snapshot::kFullAOT) {
3357+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3358+
type->ptr()->type_test_stub_entry_point_);
3359+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3360+
s->Write<int32_t>(text_offset);
3361+
}
32663362
}
32673363
}
32683364

32693365
private:
32703366
GrowableArray<RawTypeParameter*> objects_;
3367+
const TypeTestingStubFinder& type_testing_stubs_;
32713368
};
32723369
#endif // !DART_PRECOMPILED_RUNTIME
32733370

32743371
class TypeParameterDeserializationCluster : public DeserializationCluster {
32753372
public:
3276-
TypeParameterDeserializationCluster() {}
3373+
TypeParameterDeserializationCluster()
3374+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
32773375
virtual ~TypeParameterDeserializationCluster() {}
32783376

32793377
void ReadAlloc(Deserializer* d) {
@@ -3303,8 +3401,28 @@ class TypeParameterDeserializationCluster : public DeserializationCluster {
33033401
type->ptr()->token_pos_ = d->ReadTokenPosition();
33043402
type->ptr()->index_ = d->Read<int16_t>();
33053403
type->ptr()->type_state_ = d->Read<int8_t>();
3404+
if (d->kind() == Snapshot::kFullAOT) {
3405+
const int32_t text_offset = d->Read<int32_t>();
3406+
instr_ = d->GetInstructionsAt(text_offset);
3407+
type_ = type;
3408+
type_.SetTypeTestingStub(instr_);
3409+
}
3410+
}
3411+
}
3412+
3413+
void PostLoad(const Array& refs, Snapshot::Kind kind, Zone* zone) {
3414+
if (kind != Snapshot::kFullAOT) {
3415+
for (intptr_t id = start_index_; id < stop_index_; id++) {
3416+
type_ ^= refs.At(id);
3417+
instr_ = TypeTestingStubGenerator::DefaultCodeForType(type_);
3418+
type_.SetTypeTestingStub(instr_);
3419+
}
33063420
}
33073421
}
3422+
3423+
private:
3424+
AbstractType& type_;
3425+
Instructions& instr_;
33083426
};
33093427

33103428
#if !defined(DART_PRECOMPILED_RUNTIME)
@@ -4556,7 +4674,8 @@ Serializer::Serializer(Thread* thread,
45564674
uint8_t** buffer,
45574675
ReAlloc alloc,
45584676
intptr_t initial_size,
4559-
ImageWriter* image_writer)
4677+
ImageWriter* image_writer,
4678+
bool vm_isolate)
45604679
: StackResource(thread),
45614680
heap_(thread->isolate()->heap()),
45624681
zone_(thread->zone()),
@@ -4568,7 +4687,8 @@ Serializer::Serializer(Thread* thread,
45684687
num_cids_(0),
45694688
num_base_objects_(0),
45704689
num_written_objects_(0),
4571-
next_ref_index_(1)
4690+
next_ref_index_(1),
4691+
vm_isolate_(vm_isolate)
45724692
#if defined(SNAPSHOT_BACKTRACE)
45734693
,
45744694
current_parent_(Object::null()),
@@ -4668,11 +4788,11 @@ SerializationCluster* Serializer::NewClusterForClass(intptr_t cid) {
46684788
case kLibraryPrefixCid:
46694789
return new (Z) LibraryPrefixSerializationCluster();
46704790
case kTypeCid:
4671-
return new (Z) TypeSerializationCluster();
4791+
return new (Z) TypeSerializationCluster(type_testing_stubs_);
46724792
case kTypeRefCid:
4673-
return new (Z) TypeRefSerializationCluster();
4793+
return new (Z) TypeRefSerializationCluster(type_testing_stubs_);
46744794
case kTypeParameterCid:
4675-
return new (Z) TypeParameterSerializationCluster();
4795+
return new (Z) TypeParameterSerializationCluster(type_testing_stubs_);
46764796
case kBoundedTypeCid:
46774797
return new (Z) BoundedTypeSerializationCluster();
46784798
case kClosureCid:
@@ -5120,7 +5240,8 @@ Deserializer::Deserializer(Thread* thread,
51205240
const uint8_t* buffer,
51215241
intptr_t size,
51225242
const uint8_t* instructions_buffer,
5123-
const uint8_t* data_buffer)
5243+
const uint8_t* data_buffer,
5244+
bool vm_isolate)
51245245
: StackResource(thread),
51255246
heap_(thread->isolate()->heap()),
51265247
zone_(thread->zone()),
@@ -5129,7 +5250,8 @@ Deserializer::Deserializer(Thread* thread,
51295250
image_reader_(NULL),
51305251
refs_(NULL),
51315252
next_ref_index_(1),
5132-
clusters_(NULL) {
5253+
clusters_(NULL),
5254+
vm_isolate_(vm_isolate) {
51335255
if (Snapshot::IncludesCode(kind)) {
51345256
ASSERT(instructions_buffer != NULL);
51355257
ASSERT(data_buffer != NULL);
@@ -5716,7 +5838,8 @@ intptr_t FullSnapshotWriter::WriteVMSnapshot() {
57165838

57175839
ASSERT(vm_snapshot_data_buffer_ != NULL);
57185840
Serializer serializer(thread(), kind_, vm_snapshot_data_buffer_, alloc_,
5719-
kInitialSize, vm_image_writer_);
5841+
kInitialSize, vm_image_writer_,
5842+
/*vm_isolate=*/true);
57205843

57215844
serializer.ReserveHeader();
57225845
serializer.WriteVersionAndFeatures(true);
@@ -5746,7 +5869,8 @@ void FullSnapshotWriter::WriteIsolateSnapshot(intptr_t num_base_objects) {
57465869
thread(), Timeline::GetIsolateStream(), "WriteIsolateSnapshot"));
57475870

57485871
Serializer serializer(thread(), kind_, isolate_snapshot_data_buffer_, alloc_,
5749-
kInitialSize, isolate_image_writer_);
5872+
kInitialSize, isolate_image_writer_,
5873+
/*vm_isolate=*/false);
57505874
ObjectStore* object_store = isolate()->object_store();
57515875
ASSERT(object_store != NULL);
57525876

@@ -5816,7 +5940,8 @@ FullSnapshotReader::FullSnapshotReader(const Snapshot* snapshot,
58165940

58175941
RawApiError* FullSnapshotReader::ReadVMSnapshot() {
58185942
Deserializer deserializer(thread_, kind_, buffer_, size_,
5819-
instructions_buffer_, data_buffer_);
5943+
instructions_buffer_, data_buffer_,
5944+
/*vm_isolate=*/true);
58205945

58215946
RawApiError* error = deserializer.VerifyVersionAndFeatures(/*isolate=*/NULL);
58225947
if (error != ApiError::null()) {
@@ -5840,7 +5965,8 @@ RawApiError* FullSnapshotReader::ReadVMSnapshot() {
58405965

58415966
RawApiError* FullSnapshotReader::ReadIsolateSnapshot() {
58425967
Deserializer deserializer(thread_, kind_, buffer_, size_,
5843-
instructions_buffer_, data_buffer_);
5968+
instructions_buffer_, data_buffer_,
5969+
/*vm_isolate=*/false);
58445970

58455971
RawApiError* error =
58465972
deserializer.VerifyVersionAndFeatures(thread_->isolate());

0 commit comments

Comments
 (0)