Skip to content

Commit d4ffb92

Browse files
rmacnak-googlecommit-bot@chromium.org
authored andcommitted
[vm] Reify loading units.
Loading unit metadata is expanded to create an object representing each loading unit, and each library to reference the loading unit it is a part of. To be used at compile-time to help assign objects to snapshots. To be used at runtime to track which units have been loading and the base objects for cross-snapshot references. Bug: #41974 Change-Id: Icd3fa95ffd5949bd8f7f84a6f750c4c515de0dba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151865 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent c76fc43 commit d4ffb92

21 files changed

+307
-14
lines changed

runtime/vm/bootstrap.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ static ErrorPtr BootstrapFromKernel(Thread* thread,
142142
library = Library::LookupLibrary(thread, dart_builtin);
143143
isolate->object_store()->set_builtin_library(library);
144144

145+
if (FLAG_precompiled_mode) {
146+
loader.ReadLoadingUnits();
147+
}
148+
145149
return Error::null();
146150
}
147151

runtime/vm/class_id.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace dart {
4747
V(ICData) \
4848
V(MegamorphicCache) \
4949
V(SubtypeTestCache) \
50+
V(LoadingUnit) \
5051
V(Error) \
5152
V(ApiError) \
5253
V(LanguageError) \

runtime/vm/clustered_snapshot.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,73 @@ class SubtypeTestCacheDeserializationCluster : public DeserializationCluster {
29262926
}
29272927
};
29282928

2929+
#if !defined(DART_PRECOMPILED_RUNTIME)
2930+
class LoadingUnitSerializationCluster : public SerializationCluster {
2931+
public:
2932+
LoadingUnitSerializationCluster() : SerializationCluster("LoadingUnit") {}
2933+
~LoadingUnitSerializationCluster() {}
2934+
2935+
void Trace(Serializer* s, ObjectPtr object) {
2936+
LoadingUnitPtr unit = LoadingUnit::RawCast(object);
2937+
objects_.Add(unit);
2938+
s->Push(unit->ptr()->parent_);
2939+
}
2940+
2941+
void WriteAlloc(Serializer* s) {
2942+
s->WriteCid(kLoadingUnitCid);
2943+
const intptr_t count = objects_.length();
2944+
s->WriteUnsigned(count);
2945+
for (intptr_t i = 0; i < count; i++) {
2946+
LoadingUnitPtr unit = objects_[i];
2947+
s->AssignRef(unit);
2948+
}
2949+
}
2950+
2951+
void WriteFill(Serializer* s) {
2952+
const intptr_t count = objects_.length();
2953+
for (intptr_t i = 0; i < count; i++) {
2954+
LoadingUnitPtr unit = objects_[i];
2955+
AutoTraceObject(unit);
2956+
WriteField(unit, parent_);
2957+
s->Write<int32_t>(unit->ptr()->id_);
2958+
}
2959+
}
2960+
2961+
private:
2962+
GrowableArray<LoadingUnitPtr> objects_;
2963+
};
2964+
#endif // !DART_PRECOMPILED_RUNTIME
2965+
2966+
class LoadingUnitDeserializationCluster : public DeserializationCluster {
2967+
public:
2968+
LoadingUnitDeserializationCluster() {}
2969+
~LoadingUnitDeserializationCluster() {}
2970+
2971+
void ReadAlloc(Deserializer* d) {
2972+
start_index_ = d->next_index();
2973+
PageSpace* old_space = d->heap()->old_space();
2974+
const intptr_t count = d->ReadUnsigned();
2975+
for (intptr_t i = 0; i < count; i++) {
2976+
d->AssignRef(
2977+
AllocateUninitialized(old_space, LoadingUnit::InstanceSize()));
2978+
}
2979+
stop_index_ = d->next_index();
2980+
}
2981+
2982+
void ReadFill(Deserializer* d) {
2983+
for (intptr_t id = start_index_; id < stop_index_; id++) {
2984+
LoadingUnitPtr unit = static_cast<LoadingUnitPtr>(d->Ref(id));
2985+
Deserializer::InitializeHeader(unit, kLoadingUnitCid,
2986+
LoadingUnit::InstanceSize());
2987+
unit->ptr()->parent_ = static_cast<LoadingUnitPtr>(d->ReadRef());
2988+
unit->ptr()->base_objects_ = Array::null();
2989+
unit->ptr()->id_ = d->Read<int32_t>();
2990+
unit->ptr()->loaded_ = false;
2991+
unit->ptr()->load_issued_ = false;
2992+
}
2993+
}
2994+
};
2995+
29292996
#if !defined(DART_PRECOMPILED_RUNTIME)
29302997
class LanguageErrorSerializationCluster : public SerializationCluster {
29312998
public:
@@ -5058,6 +5125,8 @@ SerializationCluster* Serializer::NewClusterForClass(intptr_t cid) {
50585125
return new (Z) MegamorphicCacheSerializationCluster();
50595126
case kSubtypeTestCacheCid:
50605127
return new (Z) SubtypeTestCacheSerializationCluster();
5128+
case kLoadingUnitCid:
5129+
return new (Z) LoadingUnitSerializationCluster();
50615130
case kLanguageErrorCid:
50625131
return new (Z) LanguageErrorSerializationCluster();
50635132
case kUnhandledExceptionCid:
@@ -5934,6 +6003,8 @@ DeserializationCluster* Deserializer::ReadCluster() {
59346003
return new (Z) MegamorphicCacheDeserializationCluster();
59356004
case kSubtypeTestCacheCid:
59366005
return new (Z) SubtypeTestCacheDeserializationCluster();
6006+
case kLoadingUnitCid:
6007+
return new (Z) LoadingUnitDeserializationCluster();
59376008
case kLanguageErrorCid:
59386009
return new (Z) LanguageErrorDeserializationCluster();
59396010
case kUnhandledExceptionCid:

runtime/vm/compiler/frontend/kernel_translation_helper.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,56 @@ void ObfuscationProhibitionsMetadataHelper::ReadMetadata(intptr_t node_offset) {
17981798
return;
17991799
}
18001800

1801+
LoadingUnitsMetadataHelper::LoadingUnitsMetadataHelper(
1802+
KernelReaderHelper* helper)
1803+
: MetadataHelper(helper, tag(), /* precompiler_only = */ true) {}
1804+
1805+
void LoadingUnitsMetadataHelper::ReadMetadata(intptr_t node_offset) {
1806+
intptr_t md_offset = GetNextMetadataPayloadOffset(node_offset);
1807+
if (md_offset < 0) {
1808+
return;
1809+
}
1810+
1811+
AlternativeReadingScopeWithNewData alt(&helper_->reader_,
1812+
&H.metadata_payloads(), md_offset);
1813+
1814+
Thread* T = Thread::Current();
1815+
Zone* Z = T->zone();
1816+
intptr_t unit_count = helper_->ReadUInt();
1817+
Array& loading_units = Array::Handle(Z, Array::New(unit_count + 1));
1818+
LoadingUnit& unit = LoadingUnit::Handle(Z);
1819+
LoadingUnit& parent = LoadingUnit::Handle(Z);
1820+
Library& lib = Library::Handle(Z);
1821+
1822+
for (int i = 0; i < unit_count; i++) {
1823+
intptr_t id = helper_->ReadUInt();
1824+
unit = LoadingUnit::New();
1825+
unit.set_id(id);
1826+
1827+
intptr_t parent_id = helper_->ReadUInt();
1828+
parent ^= loading_units.At(parent_id);
1829+
ASSERT(parent.IsNull() == (parent_id == 0));
1830+
unit.set_parent(parent);
1831+
1832+
intptr_t library_count = helper_->ReadUInt();
1833+
for (intptr_t j = 0; j < library_count; j++) {
1834+
const String& uri =
1835+
translation_helper_.DartSymbolPlain(helper_->ReadStringReference());
1836+
lib = Library::LookupLibrary(T, uri);
1837+
if (lib.IsNull()) {
1838+
FATAL1("Missing library: %s\n", uri.ToCString());
1839+
}
1840+
lib.set_loading_unit(unit);
1841+
}
1842+
1843+
loading_units.SetAt(id, unit);
1844+
}
1845+
1846+
ObjectStore* object_store = Isolate::Current()->object_store();
1847+
ASSERT(object_store->loading_units() == Array::null());
1848+
object_store->set_loading_units(loading_units);
1849+
}
1850+
18011851
CallSiteAttributesMetadataHelper::CallSiteAttributesMetadataHelper(
18021852
KernelReaderHelper* helper,
18031853
TypeTranslator* type_translator)

runtime/vm/compiler/frontend/kernel_translation_helper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,20 @@ class ObfuscationProhibitionsMetadataHelper : public MetadataHelper {
10291029
DISALLOW_COPY_AND_ASSIGN(ObfuscationProhibitionsMetadataHelper);
10301030
};
10311031

1032+
class LoadingUnitsMetadataHelper : public MetadataHelper {
1033+
public:
1034+
static const char* tag() { return "vm.loading-units.metadata"; }
1035+
1036+
explicit LoadingUnitsMetadataHelper(KernelReaderHelper* helper);
1037+
1038+
void ReadLoadingUnits() { ReadMetadata(0); }
1039+
1040+
private:
1041+
void ReadMetadata(intptr_t node_offset);
1042+
1043+
DISALLOW_COPY_AND_ASSIGN(LoadingUnitsMetadataHelper);
1044+
};
1045+
10321046
struct CallSiteAttributesMetadata {
10331047
const AbstractType* receiver_type = nullptr;
10341048
};
@@ -1267,6 +1281,7 @@ class KernelReaderHelper {
12671281
friend class UnboxingInfoMetadataHelper;
12681282
friend class VariableDeclarationHelper;
12691283
friend class ObfuscationProhibitionsMetadataHelper;
1284+
friend class LoadingUnitsMetadataHelper;
12701285
friend bool NeedsDynamicInvocationForwarder(const Function& function);
12711286

12721287
private:

runtime/vm/compiler/runtime_api.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ word SubtypeTestCache::NextFieldOffset() {
994994
return -kWordSize;
995995
}
996996

997+
word LoadingUnit::NextFieldOffset() {
998+
return -kWordSize;
999+
}
1000+
9971001
word Context::NextFieldOffset() {
9981002
return -kWordSize;
9991003
}

runtime/vm/compiler/runtime_api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,12 @@ class SubtypeTestCache : public AllStatic {
11771177
static word NextFieldOffset();
11781178
};
11791179

1180+
class LoadingUnit : public AllStatic {
1181+
public:
1182+
static word InstanceSize();
1183+
static word NextFieldOffset();
1184+
};
1185+
11801186
class Context : public AllStatic {
11811187
public:
11821188
static word header_size();

0 commit comments

Comments
 (0)