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

Commit 8054409

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: I44a1d5d4b27454ae026aef2a301aada3dd399ea0 Reviewed-on: https://dart-review.googlesource.com/49861 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]>
1 parent ef926f9 commit 8054409

Some content is hidden

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

55 files changed

+3370
-234
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: 138 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,35 @@ 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 [Type::dynamic_type()] object is not serialized, so we manually send
3099+
// the type testing stub for it.
3100+
if (s->kind() == Snapshot::kFullAOT && s->for_vm_isolate()) {
3101+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3102+
Type::dynamic_type().type_test_stub_entry_point());
3103+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3104+
s->Write<int32_t>(text_offset);
30833105
}
30843106
}
30853107

30863108
private:
30873109
GrowableArray<RawType*> canonical_objects_;
30883110
GrowableArray<RawType*> objects_;
3111+
const TypeTestingStubFinder& type_testing_stubs_;
30893112
};
30903113
#endif // !DART_PRECOMPILED_RUNTIME
30913114

30923115
class TypeDeserializationCluster : public DeserializationCluster {
30933116
public:
3094-
TypeDeserializationCluster() {}
3117+
TypeDeserializationCluster()
3118+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
30953119
virtual ~TypeDeserializationCluster() {}
30963120

30973121
void ReadAlloc(Deserializer* d) {
@@ -3126,6 +3150,12 @@ class TypeDeserializationCluster : public DeserializationCluster {
31263150
}
31273151
type->ptr()->token_pos_ = d->ReadTokenPosition();
31283152
type->ptr()->type_state_ = d->Read<int8_t>();
3153+
if (d->kind() == Snapshot::kFullAOT) {
3154+
const int32_t text_offset = d->Read<int32_t>();
3155+
instr_ = d->GetInstructionsAt(text_offset);
3156+
type_ = type;
3157+
type_.SetTypeTestingStub(instr_);
3158+
}
31293159
}
31303160

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

31453207
private:
31463208
intptr_t canonical_start_index_;
31473209
intptr_t canonical_stop_index_;
3210+
AbstractType& type_;
3211+
Instructions& instr_;
31483212
};
31493213

31503214
#if !defined(DART_PRECOMPILED_RUNTIME)
31513215
class TypeRefSerializationCluster : public SerializationCluster {
31523216
public:
3153-
TypeRefSerializationCluster() : SerializationCluster("TypeRef") {}
3217+
explicit TypeRefSerializationCluster(const TypeTestingStubFinder& ttsf)
3218+
: SerializationCluster("TypeRef"), type_testing_stubs_(ttsf) {}
31543219
virtual ~TypeRefSerializationCluster() {}
31553220

31563221
void Trace(Serializer* s, RawObject* object) {
@@ -3183,17 +3248,25 @@ class TypeRefSerializationCluster : public SerializationCluster {
31833248
for (RawObject** p = from; p <= to; p++) {
31843249
s->WriteRef(*p);
31853250
}
3251+
if (s->kind() == Snapshot::kFullAOT) {
3252+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3253+
type->ptr()->type_test_stub_entry_point_);
3254+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3255+
s->Write<int32_t>(text_offset);
3256+
}
31863257
}
31873258
}
31883259

31893260
private:
31903261
GrowableArray<RawTypeRef*> objects_;
3262+
const TypeTestingStubFinder& type_testing_stubs_;
31913263
};
31923264
#endif // !DART_PRECOMPILED_RUNTIME
31933265

31943266
class TypeRefDeserializationCluster : public DeserializationCluster {
31953267
public:
3196-
TypeRefDeserializationCluster() {}
3268+
TypeRefDeserializationCluster()
3269+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
31973270
virtual ~TypeRefDeserializationCluster() {}
31983271

31993272
void ReadAlloc(Deserializer* d) {
@@ -3218,14 +3291,26 @@ class TypeRefDeserializationCluster : public DeserializationCluster {
32183291
for (RawObject** p = from; p <= to; p++) {
32193292
*p = d->ReadRef();
32203293
}
3294+
if (d->kind() == Snapshot::kFullAOT) {
3295+
const int32_t text_offset = d->Read<int32_t>();
3296+
instr_ = d->GetInstructionsAt(text_offset);
3297+
type_ = type;
3298+
type_.SetTypeTestingStub(instr_);
3299+
}
32213300
}
32223301
}
3302+
3303+
private:
3304+
AbstractType& type_;
3305+
Instructions& instr_;
32233306
};
32243307

32253308
#if !defined(DART_PRECOMPILED_RUNTIME)
32263309
class TypeParameterSerializationCluster : public SerializationCluster {
32273310
public:
3228-
TypeParameterSerializationCluster() : SerializationCluster("TypeParameter") {}
3311+
explicit TypeParameterSerializationCluster(const TypeTestingStubFinder& ttsf)
3312+
: SerializationCluster("TypeParameter"), type_testing_stubs_(ttsf) {}
3313+
32293314
virtual ~TypeParameterSerializationCluster() {}
32303315

32313316
void Trace(Serializer* s, RawObject* object) {
@@ -3263,17 +3348,25 @@ class TypeParameterSerializationCluster : public SerializationCluster {
32633348
s->WriteTokenPosition(type->ptr()->token_pos_);
32643349
s->Write<int16_t>(type->ptr()->index_);
32653350
s->Write<int8_t>(type->ptr()->type_state_);
3351+
if (s->kind() == Snapshot::kFullAOT) {
3352+
RawInstructions* instr = type_testing_stubs_.LookupByAddresss(
3353+
type->ptr()->type_test_stub_entry_point_);
3354+
const int32_t text_offset = s->GetTextOffset(instr, Code::null());
3355+
s->Write<int32_t>(text_offset);
3356+
}
32663357
}
32673358
}
32683359

32693360
private:
32703361
GrowableArray<RawTypeParameter*> objects_;
3362+
const TypeTestingStubFinder& type_testing_stubs_;
32713363
};
32723364
#endif // !DART_PRECOMPILED_RUNTIME
32733365

32743366
class TypeParameterDeserializationCluster : public DeserializationCluster {
32753367
public:
3276-
TypeParameterDeserializationCluster() {}
3368+
TypeParameterDeserializationCluster()
3369+
: type_(AbstractType::Handle()), instr_(Instructions::Handle()) {}
32773370
virtual ~TypeParameterDeserializationCluster() {}
32783371

32793372
void ReadAlloc(Deserializer* d) {
@@ -3303,8 +3396,28 @@ class TypeParameterDeserializationCluster : public DeserializationCluster {
33033396
type->ptr()->token_pos_ = d->ReadTokenPosition();
33043397
type->ptr()->index_ = d->Read<int16_t>();
33053398
type->ptr()->type_state_ = d->Read<int8_t>();
3399+
if (d->kind() == Snapshot::kFullAOT) {
3400+
const int32_t text_offset = d->Read<int32_t>();
3401+
instr_ = d->GetInstructionsAt(text_offset);
3402+
type_ = type;
3403+
type_.SetTypeTestingStub(instr_);
3404+
}
3405+
}
3406+
}
3407+
3408+
void PostLoad(const Array& refs, Snapshot::Kind kind, Zone* zone) {
3409+
if (kind != Snapshot::kFullAOT) {
3410+
for (intptr_t id = start_index_; id < stop_index_; id++) {
3411+
type_ ^= refs.At(id);
3412+
instr_ = TypeTestingStubGenerator::DefaultCodeForType(type_);
3413+
type_.SetTypeTestingStub(instr_);
3414+
}
33063415
}
33073416
}
3417+
3418+
private:
3419+
AbstractType& type_;
3420+
Instructions& instr_;
33083421
};
33093422

33103423
#if !defined(DART_PRECOMPILED_RUNTIME)
@@ -4556,7 +4669,8 @@ Serializer::Serializer(Thread* thread,
45564669
uint8_t** buffer,
45574670
ReAlloc alloc,
45584671
intptr_t initial_size,
4559-
ImageWriter* image_writer)
4672+
ImageWriter* image_writer,
4673+
bool vm_isolate)
45604674
: StackResource(thread),
45614675
heap_(thread->isolate()->heap()),
45624676
zone_(thread->zone()),
@@ -4568,7 +4682,8 @@ Serializer::Serializer(Thread* thread,
45684682
num_cids_(0),
45694683
num_base_objects_(0),
45704684
num_written_objects_(0),
4571-
next_ref_index_(1)
4685+
next_ref_index_(1),
4686+
vm_isolate_(vm_isolate)
45724687
#if defined(SNAPSHOT_BACKTRACE)
45734688
,
45744689
current_parent_(Object::null()),
@@ -4668,11 +4783,11 @@ SerializationCluster* Serializer::NewClusterForClass(intptr_t cid) {
46684783
case kLibraryPrefixCid:
46694784
return new (Z) LibraryPrefixSerializationCluster();
46704785
case kTypeCid:
4671-
return new (Z) TypeSerializationCluster();
4786+
return new (Z) TypeSerializationCluster(type_testing_stubs_);
46724787
case kTypeRefCid:
4673-
return new (Z) TypeRefSerializationCluster();
4788+
return new (Z) TypeRefSerializationCluster(type_testing_stubs_);
46744789
case kTypeParameterCid:
4675-
return new (Z) TypeParameterSerializationCluster();
4790+
return new (Z) TypeParameterSerializationCluster(type_testing_stubs_);
46764791
case kBoundedTypeCid:
46774792
return new (Z) BoundedTypeSerializationCluster();
46784793
case kClosureCid:
@@ -5120,7 +5235,8 @@ Deserializer::Deserializer(Thread* thread,
51205235
const uint8_t* buffer,
51215236
intptr_t size,
51225237
const uint8_t* instructions_buffer,
5123-
const uint8_t* data_buffer)
5238+
const uint8_t* data_buffer,
5239+
bool vm_isolate)
51245240
: StackResource(thread),
51255241
heap_(thread->isolate()->heap()),
51265242
zone_(thread->zone()),
@@ -5129,7 +5245,8 @@ Deserializer::Deserializer(Thread* thread,
51295245
image_reader_(NULL),
51305246
refs_(NULL),
51315247
next_ref_index_(1),
5132-
clusters_(NULL) {
5248+
clusters_(NULL),
5249+
vm_isolate_(vm_isolate) {
51335250
if (Snapshot::IncludesCode(kind)) {
51345251
ASSERT(instructions_buffer != NULL);
51355252
ASSERT(data_buffer != NULL);
@@ -5716,7 +5833,8 @@ intptr_t FullSnapshotWriter::WriteVMSnapshot() {
57165833

57175834
ASSERT(vm_snapshot_data_buffer_ != NULL);
57185835
Serializer serializer(thread(), kind_, vm_snapshot_data_buffer_, alloc_,
5719-
kInitialSize, vm_image_writer_);
5836+
kInitialSize, vm_image_writer_,
5837+
/*vm_isolate=*/true);
57205838

57215839
serializer.ReserveHeader();
57225840
serializer.WriteVersionAndFeatures(true);
@@ -5746,7 +5864,8 @@ void FullSnapshotWriter::WriteIsolateSnapshot(intptr_t num_base_objects) {
57465864
thread(), Timeline::GetIsolateStream(), "WriteIsolateSnapshot"));
57475865

57485866
Serializer serializer(thread(), kind_, isolate_snapshot_data_buffer_, alloc_,
5749-
kInitialSize, isolate_image_writer_);
5867+
kInitialSize, isolate_image_writer_,
5868+
/*vm_isolate=*/false);
57505869
ObjectStore* object_store = isolate()->object_store();
57515870
ASSERT(object_store != NULL);
57525871

@@ -5816,7 +5935,8 @@ FullSnapshotReader::FullSnapshotReader(const Snapshot* snapshot,
58165935

58175936
RawApiError* FullSnapshotReader::ReadVMSnapshot() {
58185937
Deserializer deserializer(thread_, kind_, buffer_, size_,
5819-
instructions_buffer_, data_buffer_);
5938+
instructions_buffer_, data_buffer_,
5939+
/*vm_isolate=*/true);
58205940

58215941
RawApiError* error = deserializer.VerifyVersionAndFeatures(/*isolate=*/NULL);
58225942
if (error != ApiError::null()) {
@@ -5840,7 +5960,8 @@ RawApiError* FullSnapshotReader::ReadVMSnapshot() {
58405960

58415961
RawApiError* FullSnapshotReader::ReadIsolateSnapshot() {
58425962
Deserializer deserializer(thread_, kind_, buffer_, size_,
5843-
instructions_buffer_, data_buffer_);
5963+
instructions_buffer_, data_buffer_,
5964+
/*vm_isolate=*/false);
58445965

58455966
RawApiError* error =
58465967
deserializer.VerifyVersionAndFeatures(thread_->isolate());

0 commit comments

Comments
 (0)