Skip to content

Commit 261fd62

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/bytecode] Enumerate modified libraries in bytecode for hot reload
This fixes certain vm/cc/IsolateReload tests which stopped working after kernel AST was removed from kernel binaries with bytecode in kernel service (in https://dart.googlesource.com/sdk/+/9b3c693335effe6d156df5d90ce59977985ea9f5). Change-Id: Ibfaccf520d6d422f17c95d36db8e84980364263f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112660 Reviewed-by: Régis Crelier <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 1dbcf24 commit 261fd62

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

runtime/vm/compiler/frontend/bytecode_reader.cc

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ void BytecodeMetadataHelper::ReadLibrary(const Library& library) {
142142
library, bytecode_component.GetNumLibraries());
143143
}
144144

145+
bool BytecodeMetadataHelper::FindModifiedLibrariesForHotReload(
146+
BitVector* modified_libs,
147+
bool* is_empty_program,
148+
intptr_t* p_num_classes,
149+
intptr_t* p_num_procedures) {
150+
ASSERT(Thread::Current()->IsMutatorThread());
151+
152+
if (translation_helper_.GetBytecodeComponent() == Array::null()) {
153+
return false;
154+
}
155+
156+
BytecodeComponentData bytecode_component(
157+
&Array::Handle(helper_->zone_, GetBytecodeComponent()));
158+
BytecodeReaderHelper bytecode_reader(&H, active_class_, &bytecode_component);
159+
AlternativeReadingScope alt(&bytecode_reader.reader(),
160+
bytecode_component.GetLibraryIndexOffset());
161+
bytecode_reader.FindModifiedLibrariesForHotReload(
162+
modified_libs, bytecode_component.GetNumLibraries());
163+
164+
if (is_empty_program != nullptr) {
165+
*is_empty_program = (bytecode_component.GetNumLibraries() == 0);
166+
}
167+
if (p_num_classes != nullptr) {
168+
*p_num_classes = (bytecode_component.GetNumClasses() == 0);
169+
}
170+
if (p_num_procedures != nullptr) {
171+
*p_num_procedures = (bytecode_component.GetNumCodes() == 0);
172+
}
173+
return true;
174+
}
175+
145176
RawLibrary* BytecodeMetadataHelper::GetMainLibrary() {
146177
const intptr_t md_offset = GetComponentMetadataPayloadOffset();
147178
if (md_offset < 0) {
@@ -1148,6 +1179,7 @@ RawArray* BytecodeReaderHelper::ReadBytecodeComponent(intptr_t md_offset) {
11481179
intptr_t num_libraries = 0;
11491180
intptr_t library_index_offset = 0;
11501181
intptr_t libraries_offset = 0;
1182+
intptr_t num_classes = 0;
11511183
intptr_t classes_offset = 0;
11521184
static_assert(KernelBytecode::kMinSupportedBytecodeFormatVersion < 10,
11531185
"Cleanup condition");
@@ -1158,14 +1190,14 @@ RawArray* BytecodeReaderHelper::ReadBytecodeComponent(intptr_t md_offset) {
11581190
reader_.ReadUInt32(); // Skip libraries.numItems
11591191
libraries_offset = start_offset + reader_.ReadUInt32();
11601192

1161-
reader_.ReadUInt32(); // Skip classes.numItems
1193+
num_classes = reader_.ReadUInt32();
11621194
classes_offset = start_offset + reader_.ReadUInt32();
11631195
}
11641196

11651197
reader_.ReadUInt32(); // Skip members.numItems
11661198
const intptr_t members_offset = start_offset + reader_.ReadUInt32();
11671199

1168-
reader_.ReadUInt32(); // Skip codes.numItems
1200+
const intptr_t num_codes = reader_.ReadUInt32();
11691201
const intptr_t codes_offset = start_offset + reader_.ReadUInt32();
11701202

11711203
reader_.ReadUInt32(); // Skip sourcePositions.numItems
@@ -1214,8 +1246,8 @@ RawArray* BytecodeReaderHelper::ReadBytecodeComponent(intptr_t md_offset) {
12141246
Z, BytecodeComponentData::New(
12151247
Z, version, num_objects, string_table_offset,
12161248
strings_contents_offset, objects_contents_offset, main_offset,
1217-
num_libraries, library_index_offset, libraries_offset,
1218-
classes_offset, members_offset, codes_offset,
1249+
num_libraries, library_index_offset, libraries_offset, num_classes,
1250+
classes_offset, members_offset, num_codes, codes_offset,
12191251
source_positions_offset, source_files_offset, line_starts_offset,
12201252
local_variables_offset, annotations_offset, Heap::kOld));
12211253

@@ -2663,6 +2695,23 @@ void BytecodeReaderHelper::FindAndReadSpecificLibrary(const Library& library,
26632695
}
26642696
}
26652697

2698+
void BytecodeReaderHelper::FindModifiedLibrariesForHotReload(
2699+
BitVector* modified_libs,
2700+
intptr_t num_libraries) {
2701+
auto& uri = String::Handle(Z);
2702+
auto& lib = Library::Handle(Z);
2703+
for (intptr_t i = 0; i < num_libraries; ++i) {
2704+
uri ^= ReadObject();
2705+
reader_.ReadUInt(); // Skip offset.
2706+
2707+
lib = Library::LookupLibrary(thread_, uri);
2708+
if (!lib.IsNull() && !lib.is_dart_scheme()) {
2709+
// This is a library that already exists so mark it as being modified.
2710+
modified_libs->Add(lib.index());
2711+
}
2712+
}
2713+
}
2714+
26662715
void BytecodeReaderHelper::ReadParameterCovariance(
26672716
const Function& function,
26682717
BitVector* is_covariant,
@@ -3028,6 +3077,10 @@ intptr_t BytecodeComponentData::GetLibrariesOffset() const {
30283077
return Smi::Value(Smi::RawCast(data_.At(kLibrariesOffset)));
30293078
}
30303079

3080+
intptr_t BytecodeComponentData::GetNumClasses() const {
3081+
return Smi::Value(Smi::RawCast(data_.At(kNumClasses)));
3082+
}
3083+
30313084
intptr_t BytecodeComponentData::GetClassesOffset() const {
30323085
return Smi::Value(Smi::RawCast(data_.At(kClassesOffset)));
30333086
}
@@ -3036,6 +3089,10 @@ intptr_t BytecodeComponentData::GetMembersOffset() const {
30363089
return Smi::Value(Smi::RawCast(data_.At(kMembersOffset)));
30373090
}
30383091

3092+
intptr_t BytecodeComponentData::GetNumCodes() const {
3093+
return Smi::Value(Smi::RawCast(data_.At(kNumCodes)));
3094+
}
3095+
30393096
intptr_t BytecodeComponentData::GetCodesOffset() const {
30403097
return Smi::Value(Smi::RawCast(data_.At(kCodesOffset)));
30413098
}
@@ -3078,8 +3135,10 @@ RawArray* BytecodeComponentData::New(Zone* zone,
30783135
intptr_t num_libraries,
30793136
intptr_t library_index_offset,
30803137
intptr_t libraries_offset,
3138+
intptr_t num_classes,
30813139
intptr_t classes_offset,
30823140
intptr_t members_offset,
3141+
intptr_t num_codes,
30833142
intptr_t codes_offset,
30843143
intptr_t source_positions_offset,
30853144
intptr_t source_files_offset,
@@ -3115,12 +3174,18 @@ RawArray* BytecodeComponentData::New(Zone* zone,
31153174
smi_handle = Smi::New(libraries_offset);
31163175
data.SetAt(kLibrariesOffset, smi_handle);
31173176

3177+
smi_handle = Smi::New(num_classes);
3178+
data.SetAt(kNumClasses, smi_handle);
3179+
31183180
smi_handle = Smi::New(classes_offset);
31193181
data.SetAt(kClassesOffset, smi_handle);
31203182

31213183
smi_handle = Smi::New(members_offset);
31223184
data.SetAt(kMembersOffset, smi_handle);
31233185

3186+
smi_handle = Smi::New(num_codes);
3187+
data.SetAt(kNumCodes, smi_handle);
3188+
31243189
smi_handle = Smi::New(codes_offset);
31253190
data.SetAt(kCodesOffset, smi_handle);
31263191

runtime/vm/compiler/frontend/bytecode_reader.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class BytecodeMetadataHelper : public MetadataHelper {
3737
// Read specific library declaration.
3838
void ReadLibrary(const Library& library);
3939

40+
// Scan through libraries in the bytecode component and figure out if any of
41+
// them will replace libraries which are already loaded.
42+
// Return true if bytecode component is found.
43+
bool FindModifiedLibrariesForHotReload(BitVector* modified_libs,
44+
bool* is_empty_program,
45+
intptr_t* p_num_classes,
46+
intptr_t* p_num_procedures);
47+
4048
RawLibrary* GetMainLibrary();
4149

4250
RawArray* GetBytecodeComponent();
@@ -70,6 +78,8 @@ class BytecodeReaderHelper : public ValueObject {
7078
void ReadLibraryDeclarations(intptr_t num_libraries);
7179
void FindAndReadSpecificLibrary(const Library& library,
7280
intptr_t num_libraries);
81+
void FindModifiedLibrariesForHotReload(BitVector* modified_libs,
82+
intptr_t num_libraries);
7383

7484
void ParseBytecodeFunction(ParsedFunction* parsed_function,
7585
const Function& function);
@@ -252,8 +262,10 @@ class BytecodeComponentData : ValueObject {
252262
kNumLibraries,
253263
kLibraryIndexOffset,
254264
kLibrariesOffset,
265+
kNumClasses,
255266
kClassesOffset,
256267
kMembersOffset,
268+
kNumCodes,
257269
kCodesOffset,
258270
kSourcePositionsOffset,
259271
kSourceFilesOffset,
@@ -275,8 +287,10 @@ class BytecodeComponentData : ValueObject {
275287
intptr_t GetNumLibraries() const;
276288
intptr_t GetLibraryIndexOffset() const;
277289
intptr_t GetLibrariesOffset() const;
290+
intptr_t GetNumClasses() const;
278291
intptr_t GetClassesOffset() const;
279292
intptr_t GetMembersOffset() const;
293+
intptr_t GetNumCodes() const;
280294
intptr_t GetCodesOffset() const;
281295
intptr_t GetSourcePositionsOffset() const;
282296
intptr_t GetSourceFilesOffset() const;
@@ -298,8 +312,10 @@ class BytecodeComponentData : ValueObject {
298312
intptr_t num_libraries,
299313
intptr_t library_index_offset,
300314
intptr_t libraries_offset,
315+
intptr_t num_classes,
301316
intptr_t classes_offset,
302317
intptr_t members_offset,
318+
intptr_t num_codes,
303319
intptr_t codes_offset,
304320
intptr_t source_positions_offset,
305321
intptr_t source_files_offset,

runtime/vm/kernel_loader.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,12 @@ void KernelLoader::walk_incremental_kernel(BitVector* modified_libs,
915915
bool* is_empty_program,
916916
intptr_t* p_num_classes,
917917
intptr_t* p_num_procedures) {
918+
if (FLAG_enable_interpreter || FLAG_use_bytecode_compiler) {
919+
if (bytecode_metadata_helper_.FindModifiedLibrariesForHotReload(
920+
modified_libs, is_empty_program, p_num_classes, p_num_procedures)) {
921+
return;
922+
}
923+
}
918924
intptr_t length = program_->library_count();
919925
*is_empty_program = *is_empty_program && (length == 0);
920926
bool collect_library_stats =

0 commit comments

Comments
 (0)