Skip to content

Commit 165c583

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[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. Issue #31798 Change-Id: I174a11b3b812799f399a60af799144c2ba3c26ec Reviewed-on: https://dart-review.googlesource.com/44787 Reviewed-by: Vyacheslav Egorov <[email protected]> Reviewed-by: Régis Crelier <[email protected]>
1 parent 52896af commit 165c583

32 files changed

+1259
-165
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)