Skip to content

Commit 3e19d72

Browse files
rmcilroyCommit Bot
authored and
Commit Bot
committed
[Compiler] Move creation of SharedFunctionInfo later in compile.
Moves the creation of shared function infos and the scripts shared function info array (for top-level code) to be in the FinalizeUnoptimizedCompilation step when not compiling with Full-codegen. This is needed in order to avoid accessing the heap between parse and compile. BUG=v8:5203 Change-Id: I4063bb91608fa5a0c3d3564767786776a0e4dd03 Reviewed-on: https://chromium-review.googlesource.com/571812 Commit-Queue: Ross McIlroy <[email protected]> Reviewed-by: Michael Starzinger <[email protected]> Cr-Commit-Position: refs/heads/master@{#46731}
1 parent 2b5a36d commit 3e19d72

File tree

3 files changed

+63
-67
lines changed

3 files changed

+63
-67
lines changed

src/codegen.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
244244

245245
// Print the source code if available.
246246
bool print_source =
247-
info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
248-
code->kind() == Code::FUNCTION);
247+
info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION);
249248
if (print_source) {
250249
Handle<SharedFunctionInfo> shared = info->shared_info();
251250
Handle<Script> script = info->script();

src/compilation-info.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ bool CompilationInfo::is_this_defined() const { return !IsStub(); }
124124
bool CompilationInfo::ShouldSelfOptimize() {
125125
return FLAG_opt && !literal()->dont_self_optimize() &&
126126
!literal()->dont_optimize() &&
127-
literal()->scope()->AllowsLazyCompilation() &&
128-
!shared_info()->optimization_disabled();
127+
literal()->scope()->AllowsLazyCompilation();
129128
}
130129

131130
void CompilationInfo::set_deferred_handles(

src/compiler.cc

+61-63
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ bool UseAsmWasm(DeclarationScope* scope, Handle<SharedFunctionInfo> shared_info,
289289

290290
// Modules that have validated successfully, but were subsequently broken by
291291
// invalid module instantiation attempts are off limit forever.
292-
if (shared_info->is_asm_wasm_broken()) return false;
292+
if (!shared_info.is_null() && shared_info->is_asm_wasm_broken()) return false;
293293

294294
// Compiling for debugging is not supported, fall back.
295295
if (is_debug) return false;
@@ -326,6 +326,18 @@ CompilationJob* GetUnoptimizedCompilationJob(CompilationInfo* info) {
326326

327327
void InstallUnoptimizedCode(CompilationInfo* info) {
328328
Handle<SharedFunctionInfo> shared = info->shared_info();
329+
DCHECK_EQ(info->shared_info()->language_mode(),
330+
info->literal()->language_mode());
331+
332+
// Ensure feedback metadata is installed.
333+
EnsureFeedbackMetadata(info);
334+
335+
// Mark code to be executed once before being aged if necessary.
336+
// TODO(6409): Remove when full-codegen dies.
337+
DCHECK(!info->code().is_null());
338+
if (info->parse_info()->literal()->should_be_used_once_hint()) {
339+
info->code()->MarkToBeExecutedOnce(info->isolate());
340+
}
329341

330342
// Update the shared function info with the scope info.
331343
Handle<ScopeInfo> scope_info = info->scope()->scope_info();
@@ -357,6 +369,19 @@ void InstallUnoptimizedCode(CompilationInfo* info) {
357369
}
358370
}
359371

372+
void EnsureSharedFunctionInfosArrayOnScript(CompilationInfo* info) {
373+
DCHECK(info->parse_info()->is_toplevel());
374+
DCHECK(!info->script().is_null());
375+
if (info->script()->shared_function_infos()->length() > 0) {
376+
DCHECK_EQ(info->script()->shared_function_infos()->length(),
377+
info->parse_info()->max_function_literal_id() + 1);
378+
return;
379+
}
380+
Handle<FixedArray> infos(info->isolate()->factory()->NewFixedArray(
381+
info->parse_info()->max_function_literal_id() + 1));
382+
info->script()->set_shared_function_infos(*infos);
383+
}
384+
360385
void SetSharedFunctionFlagsFromLiteral(FunctionLiteral* literal,
361386
Handle<SharedFunctionInfo> shared_info) {
362387
// Don't overwrite values set by the bootstrapper.
@@ -374,17 +399,33 @@ void SetSharedFunctionFlagsFromLiteral(FunctionLiteral* literal,
374399

375400
CompilationJob::Status FinalizeUnoptimizedCompilationJob(CompilationJob* job) {
376401
CompilationInfo* info = job->info();
402+
ParseInfo* parse_info = info->parse_info();
403+
Isolate* isolate = info->isolate();
404+
405+
if (parse_info->is_toplevel()) {
406+
// Allocate a shared function info and an array for shared function infos
407+
// for inner functions.
408+
EnsureSharedFunctionInfosArrayOnScript(info);
409+
DCHECK_EQ(kNoSourcePosition, info->literal()->function_token_position());
410+
if (!info->has_shared_info()) {
411+
Handle<SharedFunctionInfo> shared =
412+
isolate->factory()->NewSharedFunctionInfoForLiteral(info->literal(),
413+
info->script());
414+
shared->set_is_toplevel(true);
415+
parse_info->set_shared_info(shared);
416+
}
417+
}
377418
SetSharedFunctionFlagsFromLiteral(info->literal(), info->shared_info());
378419

379420
CompilationJob::Status status = job->FinalizeJob();
380421
if (status == CompilationJob::SUCCEEDED) {
381-
EnsureFeedbackMetadata(info);
382-
DCHECK(!info->code().is_null());
383-
if (info->parse_info()->literal()->should_be_used_once_hint()) {
384-
info->code()->MarkToBeExecutedOnce(info->isolate());
385-
}
386422
InstallUnoptimizedCode(info);
387-
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, info);
423+
CodeEventListener::LogEventsAndTags log_tags =
424+
parse_info->is_toplevel() ? parse_info->is_eval()
425+
? CodeEventListener::EVAL_TAG
426+
: CodeEventListener::SCRIPT_TAG
427+
: CodeEventListener::FUNCTION_TAG;
428+
RecordFunctionCompilation(log_tags, info);
388429
job->RecordUnoptimizedCompilationStats();
389430
}
390431
return status;
@@ -418,7 +459,6 @@ bool Renumber(ParseInfo* parse_info,
418459

419460
bool GenerateUnoptimizedCode(CompilationInfo* info) {
420461
if (UseAsmWasm(info->scope(), info->shared_info(), info->is_debug())) {
421-
EnsureFeedbackMetadata(info);
422462
MaybeHandle<FixedArray> wasm_data;
423463
wasm_data = AsmJs::CompileAsmViaWasm(info);
424464
if (!wasm_data.is_null()) {
@@ -475,7 +515,7 @@ bool CompileUnoptimizedInnerFunctions(
475515
ParseInfo parse_info(script);
476516
CompilationInfo info(parse_info.zone(), &parse_info, isolate,
477517
Handle<JSFunction>::null());
478-
518+
parse_info.set_toplevel(false);
479519
parse_info.set_literal(literal);
480520
parse_info.set_shared_info(shared);
481521
parse_info.set_function_literal_id(shared->function_literal_id());
@@ -498,11 +538,11 @@ bool CompileUnoptimizedInnerFunctions(
498538
return true;
499539
}
500540

501-
bool InnerFunctionIsAsmModule(
541+
bool InnerFunctionShouldUseFullCodegen(
502542
ThreadedList<ThreadedListZoneEntry<FunctionLiteral*>>* literals) {
503543
for (auto it : *literals) {
504544
FunctionLiteral* literal = it->value();
505-
if (literal->scope()->IsAsmModule()) return true;
545+
if (ShouldUseFullCodegen(literal)) return true;
506546
}
507547
return false;
508548
}
@@ -524,11 +564,12 @@ bool CompileUnoptimizedCode(CompilationInfo* info,
524564
}
525565
}
526566

527-
// Disable concurrent inner compilation for asm-wasm code.
528-
// TODO(rmcilroy,bradnelson): Remove this AsmWasm check once the asm-wasm
529-
// builder doesn't do parsing when visiting function declarations.
530-
if (info->scope()->IsAsmModule() ||
531-
InnerFunctionIsAsmModule(&inner_literals)) {
567+
if (info->parse_info()->is_toplevel() &&
568+
(ShouldUseFullCodegen(info->literal()) ||
569+
InnerFunctionShouldUseFullCodegen(&inner_literals))) {
570+
// Full-codegen needs to access SFI when compiling, so allocate the array
571+
// now.
572+
EnsureSharedFunctionInfosArrayOnScript(info);
532573
inner_function_mode = ConcurrencyMode::kNotConcurrent;
533574
}
534575

@@ -541,34 +582,16 @@ bool CompileUnoptimizedCode(CompilationInfo* info,
541582
parse_zone->Seal();
542583
}
543584

544-
if (!CompileUnoptimizedInnerFunctions(&inner_literals, inner_function_mode,
545-
parse_zone, info) ||
546-
!GenerateUnoptimizedCode(info)) {
585+
if (!GenerateUnoptimizedCode(info) ||
586+
!CompileUnoptimizedInnerFunctions(&inner_literals, inner_function_mode,
587+
parse_zone, info)) {
547588
if (!isolate->has_pending_exception()) isolate->StackOverflow();
548589
return false;
549590
}
550591

551592
return true;
552593
}
553594

554-
void EnsureSharedFunctionInfosArrayOnScript(ParseInfo* info, Isolate* isolate) {
555-
DCHECK(info->is_toplevel());
556-
DCHECK(!info->script().is_null());
557-
if (info->script()->shared_function_infos()->length() > 0) {
558-
DCHECK_EQ(info->script()->shared_function_infos()->length(),
559-
info->max_function_literal_id() + 1);
560-
return;
561-
}
562-
Handle<FixedArray> infos(
563-
isolate->factory()->NewFixedArray(info->max_function_literal_id() + 1));
564-
info->script()->set_shared_function_infos(*infos);
565-
}
566-
567-
void EnsureSharedFunctionInfosArrayOnScript(CompilationInfo* info) {
568-
return EnsureSharedFunctionInfosArrayOnScript(info->parse_info(),
569-
info->isolate());
570-
}
571-
572595
MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(
573596
CompilationInfo* info, ConcurrencyMode inner_function_mode) {
574597
RuntimeCallTimerScope runtimeTimer(
@@ -591,9 +614,6 @@ MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(
591614
}
592615
}
593616

594-
DCHECK_EQ(info->shared_info()->language_mode(),
595-
info->literal()->language_mode());
596-
597617
// Compile either unoptimized code or bytecode for the interpreter.
598618
if (!CompileUnoptimizedCode(info, inner_function_mode)) {
599619
return MaybeHandle<Code>();
@@ -1042,8 +1062,6 @@ Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
10421062
}
10431063
}
10441064

1045-
EnsureSharedFunctionInfosArrayOnScript(info);
1046-
10471065
// Measure how long it takes to do the compilation; only take the
10481066
// rest of the function into account to avoid overlap with the
10491067
// parsing statistics.
@@ -1054,37 +1072,17 @@ Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
10541072
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
10551073
parse_info->is_eval() ? "V8.CompileEval" : "V8.Compile");
10561074

1057-
// Allocate a shared function info object.
1058-
FunctionLiteral* lit = parse_info->literal();
1059-
DCHECK_EQ(kNoSourcePosition, lit->function_token_position());
1060-
result = isolate->factory()->NewSharedFunctionInfoForLiteral(lit, script);
1061-
result->set_is_toplevel(true);
1062-
parse_info->set_shared_info(result);
1063-
parse_info->set_function_literal_id(result->function_literal_id());
1064-
10651075
// Compile the code.
10661076
if (!CompileUnoptimizedCode(info, inner_function_mode)) {
10671077
return Handle<SharedFunctionInfo>::null();
10681078
}
10691079

1070-
Handle<String> script_name =
1071-
script->name()->IsString()
1072-
? Handle<String>(String::cast(script->name()))
1073-
: isolate->factory()->empty_string();
1074-
CodeEventListener::LogEventsAndTags log_tag =
1075-
parse_info->is_eval()
1076-
? CodeEventListener::EVAL_TAG
1077-
: Logger::ToNativeByScript(CodeEventListener::SCRIPT_TAG, *script);
1078-
1079-
PROFILE(isolate, CodeCreateEvent(log_tag, result->abstract_code(), *result,
1080-
*script_name));
1081-
10821080
if (!script.is_null()) {
10831081
script->set_compilation_state(Script::COMPILATION_STATE_COMPILED);
10841082
}
10851083
}
10861084

1087-
return result;
1085+
return info->shared_info();
10881086
}
10891087

10901088
} // namespace

0 commit comments

Comments
 (0)