Skip to content

Commit 46f53d4

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm/aot] Cleanup remnants of unoptimized compilation from AOT
Also, cleanup PrecompileFunctionHelper. TEST=ci Change-Id: I42a12ccbd378f5ed365cd8a90d29ff3b94410b8e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399460 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 5ba97d3 commit 46f53d4

File tree

2 files changed

+65
-124
lines changed

2 files changed

+65
-124
lines changed

runtime/vm/compiler/aot/precompiler.cc

Lines changed: 62 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,15 @@ class RetainedReasonsWriter : public StackResource {
333333
class PrecompileParsedFunctionHelper : public ValueObject {
334334
public:
335335
PrecompileParsedFunctionHelper(Precompiler* precompiler,
336-
ParsedFunction* parsed_function,
337-
bool optimized)
336+
ParsedFunction* parsed_function)
338337
: precompiler_(precompiler),
339338
parsed_function_(parsed_function),
340-
optimized_(optimized),
341339
thread_(Thread::Current()) {}
342340

343341
bool Compile();
344342

345343
private:
346344
ParsedFunction* parsed_function() const { return parsed_function_; }
347-
bool optimized() const { return optimized_; }
348345
Thread* thread() const { return thread_; }
349346

350347
bool GenerateCode(FlowGraph* flow_graph,
@@ -358,7 +355,6 @@ class PrecompileParsedFunctionHelper : public ValueObject {
358355

359356
Precompiler* precompiler_;
360357
ParsedFunction* parsed_function_;
361-
const bool optimized_;
362358
Thread* const thread_;
363359

364360
DISALLOW_COPY_AND_ASSIGN(PrecompileParsedFunctionHelper);
@@ -710,7 +706,7 @@ void Precompiler::PrecompileConstructors() {
710706
THR_Print("Precompiling constructor %s\n", function.ToCString());
711707
}
712708
ASSERT(Class::Handle(zone_, function.Owner()).is_finalized());
713-
CompileFunction(precompiler_, Thread::Current(), zone_, function);
709+
CompileFunction(precompiler_, Thread::Current(), function);
714710
}
715711

716712
private:
@@ -876,10 +872,7 @@ void Precompiler::ProcessFunction(const Function& function) {
876872

877873
ASSERT(!function.is_abstract());
878874

879-
error_ = CompileFunction(this, thread_, zone_, function);
880-
if (!error_.IsNull()) {
881-
Jump(error_);
882-
}
875+
CompileFunction(this, thread_, function);
883876

884877
// Used in the JIT to save type-feedback across compilations.
885878
function.ClearICDataArray();
@@ -3425,6 +3418,7 @@ void PrecompileParsedFunctionHelper::FinalizeCompilation(
34253418
CodeStatistics* stats) {
34263419
const Function& function = parsed_function()->function();
34273420
Zone* const zone = thread()->zone();
3421+
ASSERT(function.IsOptimizable());
34283422

34293423
// CreateDeoptInfo uses the object pool and needs to be done before
34303424
// FinalizeCode.
@@ -3437,14 +3431,9 @@ void PrecompileParsedFunctionHelper::FinalizeCompilation(
34373431
SafepointWriteRwLocker ml(T, T->isolate_group()->program_lock());
34383432
const Code& code = Code::Handle(
34393433
Code::FinalizeCodeAndNotify(function, graph_compiler, assembler,
3440-
pool_attachment, optimized(), stats));
3441-
code.set_is_optimized(optimized());
3434+
pool_attachment, /*optimized=*/true, stats));
3435+
code.set_is_optimized(true);
34423436
code.set_owner(function);
3443-
if (!function.IsOptimizable()) {
3444-
// A function with huge unoptimized code can become non-optimizable
3445-
// after generating unoptimized code.
3446-
function.set_usage_counter(INT32_MIN);
3447-
}
34483437

34493438
graph_compiler->FinalizePcDescriptors(code);
34503439
code.set_deopt_info_array(deopt_info_array);
@@ -3456,14 +3445,9 @@ void PrecompileParsedFunctionHelper::FinalizeCompilation(
34563445
graph_compiler->FinalizeStaticCallTargetsTable(code);
34573446
graph_compiler->FinalizeCodeSourceMap(code);
34583447

3459-
if (optimized()) {
3460-
// Installs code while at safepoint.
3461-
ASSERT(thread()->IsDartMutatorThread());
3462-
function.InstallOptimizedCode(code);
3463-
} else { // not optimized.
3464-
function.set_unoptimized_code(code);
3465-
function.AttachCode(code);
3466-
}
3448+
// Installs code while at safepoint.
3449+
ASSERT(thread()->IsDartMutatorThread());
3450+
function.InstallOptimizedCode(code);
34673451

34683452
if (function.IsFfiCallbackTrampoline()) {
34693453
compiler::ffi::SetFfiCallbackCode(thread(), function, code);
@@ -3523,7 +3507,7 @@ bool PrecompileParsedFunctionHelper::GenerateCode(
35233507
}
35243508

35253509
FlowGraphCompiler graph_compiler(
3526-
&assembler, flow_graph, *parsed_function(), optimized(),
3510+
&assembler, flow_graph, *parsed_function(), /*is_optimizing=*/true,
35273511
pass_state->inline_id_to_function, pass_state->inline_id_to_token_pos,
35283512
pass_state->caller_inline_id, ic_data_array, function_stats);
35293513
pass_state->graph_compiler = &graph_compiler;
@@ -3607,20 +3591,17 @@ bool PrecompileParsedFunctionHelper::GenerateCode(
36073591
// Return false if bailed out.
36083592
bool PrecompileParsedFunctionHelper::Compile() {
36093593
ASSERT(CompilerState::Current().is_aot());
3610-
if (optimized() && !parsed_function()->function().IsOptimizable()) {
3611-
// All functions compiled by precompiler must be optimizable.
3612-
UNREACHABLE();
3613-
return false;
3614-
}
36153594
Zone* const zone = thread()->zone();
36163595
HANDLESCOPE(thread());
36173596

36183597
FlowGraph* flow_graph = nullptr;
36193598
ZoneGrowableArray<const ICData*>* ic_data_array = nullptr;
36203599
const Function& function = parsed_function()->function();
36213600
ASSERT(!function.IsIrregexpFunction());
3601+
ASSERT(function.IsOptimizable());
36223602

3623-
CompilerState compiler_state(thread(), /*is_aot=*/true, optimized(),
3603+
CompilerState compiler_state(thread(), /*is_aot=*/true,
3604+
/*is_optimizing=*/true,
36243605
CompilerState::ShouldTrace(function));
36253606
compiler_state.set_function(function);
36263607

@@ -3631,28 +3612,18 @@ bool PrecompileParsedFunctionHelper::Compile() {
36313612
COMPILER_TIMINGS_TIMER_SCOPE(thread(), BuildGraph);
36323613
kernel::FlowGraphBuilder builder(parsed_function(), ic_data_array,
36333614
/* not building var desc */ nullptr,
3634-
/* not inlining */ nullptr, optimized(),
3615+
/* not inlining */ nullptr,
3616+
/*optimizing=*/true,
36353617
Compiler::kNoOSRDeoptId);
36363618
flow_graph = builder.BuildGraph();
36373619
ASSERT(flow_graph != nullptr);
36383620
}
36393621

3640-
if (optimized()) {
3641-
flow_graph->PopulateWithICData(function);
3642-
}
3643-
3644-
const bool print_flow_graph =
3645-
(FLAG_print_flow_graph ||
3646-
(optimized() && FLAG_print_flow_graph_optimized)) &&
3647-
FlowGraphPrinter::ShouldPrint(function);
3648-
3649-
if (print_flow_graph && !optimized()) {
3650-
FlowGraphPrinter::PrintGraph("Unoptimized Compilation", flow_graph);
3651-
}
3622+
flow_graph->PopulateWithICData(function);
36523623

36533624
CompilerPassState pass_state(thread(), flow_graph, precompiler_);
36543625

3655-
if (optimized()) {
3626+
{
36563627
TIMELINE_DURATION(thread(), CompilerVerbose, "OptimizationPasses");
36573628

36583629
AotCallSpecializer call_specializer(precompiler_, flow_graph);
@@ -3679,93 +3650,64 @@ bool PrecompileParsedFunctionHelper::Compile() {
36793650
return GenerateCode(flow_graph, &pass_state, ic_data_array);
36803651
}
36813652

3682-
static ErrorPtr PrecompileFunctionHelper(Precompiler* precompiler,
3683-
const Function& function,
3684-
bool optimized) {
3685-
// Check that we optimize, except if the function is not optimizable.
3653+
void Precompiler::CompileFunction(Precompiler* precompiler,
3654+
Thread* thread,
3655+
const Function& function) {
3656+
PRECOMPILER_TIMER_SCOPE(precompiler, CompileFunction);
3657+
NoActiveIsolateScope no_isolate_scope;
3658+
3659+
VMTagScope tagScope(thread, VMTag::kCompileUnoptimizedTagId);
3660+
TIMELINE_FUNCTION_COMPILATION_DURATION(thread, "CompileFunction", function);
3661+
36863662
ASSERT(CompilerState::Current().is_aot());
3687-
ASSERT(!function.IsOptimizable() || optimized);
3663+
ASSERT(function.IsOptimizable());
36883664
ASSERT(!function.HasCode());
3689-
LongJumpScope jump;
3690-
if (setjmp(*jump.Set()) == 0) {
3691-
Thread* const thread = Thread::Current();
3692-
StackZone stack_zone(thread);
3693-
Zone* const zone = stack_zone.GetZone();
3694-
const bool trace_compiler =
3695-
FLAG_trace_compiler || (FLAG_trace_optimizing_compiler && optimized);
3696-
Timer per_compile_timer;
3697-
per_compile_timer.Start();
3698-
3699-
ParsedFunction* parsed_function = new (zone)
3700-
ParsedFunction(thread, Function::ZoneHandle(zone, function.ptr()));
3701-
if (trace_compiler) {
3702-
THR_Print("Precompiling %sfunction: '%s' @ token %" Pd ", size %" Pd "\n",
3703-
(optimized ? "optimized " : ""),
3704-
function.ToFullyQualifiedCString(), function.token_pos().Pos(),
3705-
(function.end_token_pos().Pos() - function.token_pos().Pos()));
3706-
}
37073665

3708-
PrecompileParsedFunctionHelper helper(precompiler, parsed_function,
3709-
optimized);
3710-
const bool success = helper.Compile();
3711-
if (!success) {
3712-
// We got an error during compilation.
3713-
const Error& error = Error::Handle(thread->StealStickyError());
3714-
ASSERT(error.IsLanguageError() &&
3715-
LanguageError::Cast(error).kind() != Report::kBailout);
3716-
return error.ptr();
3717-
}
3666+
if (precompiler->is_tracing()) {
3667+
precompiler->tracer_->WriteCompileFunctionEvent(function);
3668+
}
37183669

3719-
per_compile_timer.Stop();
3670+
StackZone stack_zone(thread);
3671+
Zone* const zone = stack_zone.GetZone();
3672+
const bool trace_compiler =
3673+
FLAG_trace_compiler || FLAG_trace_optimizing_compiler;
3674+
Timer per_compile_timer;
3675+
per_compile_timer.Start();
37203676

3721-
if (trace_compiler) {
3722-
THR_Print("--> '%s' entry: %#" Px " size: %" Pd " time: %" Pd64 " us\n",
3723-
function.ToFullyQualifiedCString(),
3724-
Code::Handle(function.CurrentCode()).PayloadStart(),
3725-
Code::Handle(function.CurrentCode()).Size(),
3726-
per_compile_timer.TotalElapsedTime());
3727-
}
3677+
ParsedFunction* parsed_function = new (zone)
3678+
ParsedFunction(thread, Function::ZoneHandle(zone, function.ptr()));
3679+
if (trace_compiler) {
3680+
THR_Print("Precompiling optimized function: '%s' @ token %" Pd ", size %" Pd
3681+
"\n",
3682+
function.ToFullyQualifiedCString(), function.token_pos().Pos(),
3683+
(function.end_token_pos().Pos() - function.token_pos().Pos()));
3684+
}
37283685

3729-
if (FLAG_disassemble && FlowGraphPrinter::ShouldPrint(function)) {
3730-
Code& code = Code::Handle(function.CurrentCode());
3731-
Disassembler::DisassembleCode(function, code, optimized);
3732-
} else if (FLAG_disassemble_optimized && optimized &&
3733-
FlowGraphPrinter::ShouldPrint(function)) {
3734-
Code& code = Code::Handle(function.CurrentCode());
3735-
Disassembler::DisassembleCode(function, code, true);
3736-
}
3737-
return Error::null();
3738-
} else {
3739-
Thread* const thread = Thread::Current();
3740-
StackZone stack_zone(thread);
3686+
PrecompileParsedFunctionHelper helper(precompiler, parsed_function);
3687+
const bool success = helper.Compile();
3688+
if (!success) {
37413689
// We got an error during compilation.
37423690
const Error& error = Error::Handle(thread->StealStickyError());
3743-
// Precompilation may encounter compile-time errors.
3744-
// Do not attempt to optimize functions that can cause errors.
3745-
function.set_is_optimizable(false);
3746-
return error.ptr();
3691+
ASSERT(error.IsLanguageError() &&
3692+
LanguageError::Cast(error).kind() != Report::kBailout);
3693+
Jump(error);
37473694
}
3748-
UNREACHABLE();
3749-
return Error::null();
3750-
}
37513695

3752-
ErrorPtr Precompiler::CompileFunction(Precompiler* precompiler,
3753-
Thread* thread,
3754-
Zone* zone,
3755-
const Function& function) {
3756-
PRECOMPILER_TIMER_SCOPE(precompiler, CompileFunction);
3757-
NoActiveIsolateScope no_isolate_scope;
3758-
3759-
VMTagScope tagScope(thread, VMTag::kCompileUnoptimizedTagId);
3760-
TIMELINE_FUNCTION_COMPILATION_DURATION(thread, "CompileFunction", function);
3696+
per_compile_timer.Stop();
37613697

3762-
ASSERT(CompilerState::Current().is_aot());
3763-
const bool optimized = function.IsOptimizable(); // False for natives.
3764-
if (precompiler->is_tracing()) {
3765-
precompiler->tracer_->WriteCompileFunctionEvent(function);
3698+
if (trace_compiler) {
3699+
THR_Print("--> '%s' entry: %#" Px " size: %" Pd " time: %" Pd64 " us\n",
3700+
function.ToFullyQualifiedCString(),
3701+
Code::Handle(function.CurrentCode()).PayloadStart(),
3702+
Code::Handle(function.CurrentCode()).Size(),
3703+
per_compile_timer.TotalElapsedTime());
37663704
}
37673705

3768-
return PrecompileFunctionHelper(precompiler, function, optimized);
3706+
if ((FLAG_disassemble || FLAG_disassemble_optimized) &&
3707+
FlowGraphPrinter::ShouldPrint(function)) {
3708+
Code& code = Code::Handle(function.CurrentCode());
3709+
Disassembler::DisassembleCode(function, code, /*optimized=*/true);
3710+
}
37693711
}
37703712

37713713
Obfuscator::Obfuscator(Thread* thread, const String& private_key)

runtime/vm/compiler/aot/precompiler.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,9 @@ class Precompiler : public ValueObject {
243243
public:
244244
static ErrorPtr CompileAll();
245245

246-
static ErrorPtr CompileFunction(Precompiler* precompiler,
247-
Thread* thread,
248-
Zone* zone,
249-
const Function& function);
246+
static void CompileFunction(Precompiler* precompiler,
247+
Thread* thread,
248+
const Function& function);
250249

251250
// Returns true if get:runtimeType is not overloaded by any class.
252251
bool get_runtime_type_is_unique() const {

0 commit comments

Comments
 (0)