Skip to content

Commit 08b2294

Browse files
creliercommit-bot@chromium.org
authored andcommitted
[kernel] In strong mode, change covariant parameter types to Object in the
implicit closure of a method compiled by kernel (fixes #31305). Change-Id: Iabeee2e382bf07ff78645054e453e0bd1afd02ab Reviewed-on: https://dart-review.googlesource.com/24362 Commit-Queue: Régis Crelier <[email protected]> Reviewed-by: Samir Jindel <[email protected]>
1 parent 48a927a commit 08b2294

File tree

8 files changed

+75
-44
lines changed

8 files changed

+75
-44
lines changed

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,12 @@ StreamingScopeBuilder::StreamingScopeBuilder(ParsedFunction* parsed_function)
727727
needs_expr_temp_(false),
728728
builder_(new StreamingFlowGraphBuilder(
729729
&translation_helper_,
730-
parsed_function->function().script(),
730+
Script::Handle(Z, parsed_function->function().script()),
731731
zone_,
732732
TypedData::Handle(Z, parsed_function->function().KernelData()),
733733
parsed_function->function().KernelDataProgramOffset())),
734734
type_translator_(builder_, /*finalize=*/true) {
735-
Script& script = Script::Handle(Z, parsed_function->function().script());
736-
H.InitFromScript(script);
735+
H.InitFromScript(builder_->script());
737736
type_translator_.active_class_ = &active_class_;
738737
}
739738

@@ -2506,7 +2505,7 @@ StreamingConstantEvaluator::StreamingConstantEvaluator(
25062505
zone_(builder_->zone_),
25072506
translation_helper_(builder_->translation_helper_),
25082507
type_translator_(builder_->type_translator_),
2509-
script_(Script::Handle(zone_, builder_->Script())),
2508+
script_(builder_->script()),
25102509
result_(Instance::Handle(zone_)) {}
25112510

25122511
bool StreamingConstantEvaluator::IsCached(intptr_t offset) {
@@ -6442,8 +6441,7 @@ intptr_t StreamingFlowGraphBuilder::ArgumentCheckBitsForSetter(
64426441
AlternativeReadingScope r(reader_, &kernel_data,
64436442
target_field.kernel_offset());
64446443
AlternativeScriptScope s(&translation_helper_,
6445-
Script::Handle(target_field.Script()),
6446-
Script::Handle(Script()));
6444+
Script::Handle(target_field.Script()), script());
64476445

64486446
FieldHelper helper(this);
64496447
helper.ReadUntilIncluding(FieldHelper::kFlags);
@@ -6510,7 +6508,7 @@ void StreamingFlowGraphBuilder::ArgumentCheckBitsForInvocation(
65106508
interface_target.kernel_offset());
65116509
AlternativeScriptScope _s(&translation_helper_,
65126510
Script::Handle(interface_target.script()),
6513-
Script::Handle(Script()));
6511+
script());
65146512
ReadUntilFunctionNode();
65156513

65166514
FunctionNodeHelper fn_helper(this);
@@ -8888,10 +8886,6 @@ Fragment StreamingFlowGraphBuilder::BuildFunctionNode(
88888886
return instructions;
88898887
}
88908888

8891-
RawScript* StreamingFlowGraphBuilder::Script() {
8892-
return script_;
8893-
}
8894-
88958889
void StreamingFlowGraphBuilder::LoadAndSetupTypeParameters(
88968890
ActiveClass* active_class,
88978891
const Object& set_on,

runtime/vm/compiler/frontend/kernel_binary_flowgraph.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class VariableDeclarationHelper {
106106
enum Flag {
107107
kFinal = 1 << 0,
108108
kConst = 1 << 1,
109+
kCovariant = 1 << 3,
109110
kIsGenericCovariantImpl = 1 << 5,
110111
kIsGenericCovariantInterface = 1 << 6
111112
};
@@ -126,6 +127,7 @@ class VariableDeclarationHelper {
126127

127128
bool IsConst() { return (flags_ & kConst) != 0; }
128129
bool IsFinal() { return (flags_ & kFinal) != 0; }
130+
bool IsCovariant() { return (flags_ & kCovariant) != 0; }
129131

130132
bool IsGenericCovariantInterface() {
131133
return (flags_ & kIsGenericCovariantInterface) != 0;
@@ -839,7 +841,7 @@ class StreamingConstantEvaluator {
839841
TranslationHelper& translation_helper_;
840842
StreamingDartTypeTranslator& type_translator_;
841843

842-
Script& script_;
844+
const Script& script_;
843845
Instance& result_;
844846
};
845847

@@ -852,7 +854,7 @@ class StreamingFlowGraphBuilder {
852854
translation_helper_(flow_graph_builder->translation_helper_),
853855
zone_(flow_graph_builder->zone_),
854856
reader_(new Reader(data)),
855-
script_(parsed_function()->function().script()),
857+
script_(Script::Handle(zone_, parsed_function()->function().script())),
856858
constant_evaluator_(this),
857859
type_translator_(this, /* finalize= */ true),
858860
data_program_offset_(data_program_offset),
@@ -872,7 +874,7 @@ class StreamingFlowGraphBuilder {
872874
translation_helper_(*translation_helper),
873875
zone_(zone),
874876
reader_(new Reader(data_buffer, buffer_length)),
875-
script_(Script::null()),
877+
script_(Script::Handle(zone_)),
876878
constant_evaluator_(this),
877879
type_translator_(this, /* finalize= */ true),
878880
data_program_offset_(data_program_offset),
@@ -884,7 +886,7 @@ class StreamingFlowGraphBuilder {
884886
metadata_scanned_(false) {}
885887

886888
StreamingFlowGraphBuilder(TranslationHelper* translation_helper,
887-
RawScript* script,
889+
const Script& script,
888890
Zone* zone,
889891
const TypedData& data,
890892
intptr_t data_program_offset)
@@ -921,6 +923,8 @@ class StreamingFlowGraphBuilder {
921923
String& GetSourceFor(intptr_t index);
922924
RawTypedData* GetLineStartsFor(intptr_t index);
923925
void SetOffset(intptr_t offset);
926+
void ReadUntilFunctionNode();
927+
intptr_t ReadListLength();
924928

925929
enum DispatchCategory { Interface, ViaThis, Closure, DynamicDispatch };
926930

@@ -934,7 +938,6 @@ class StreamingFlowGraphBuilder {
934938
const Function& function,
935939
Function* outermost_function);
936940

937-
void ReadUntilFunctionNode();
938941
StringIndex GetNameFromVariableDeclaration(intptr_t kernel_offset,
939942
const Function& function);
940943

@@ -961,7 +964,6 @@ class StreamingFlowGraphBuilder {
961964
uint32_t ReadUInt32();
962965
uint32_t PeekUInt();
963966
uint32_t PeekListLength();
964-
intptr_t ReadListLength();
965967
StringIndex ReadStringReference();
966968
NameIndex ReadCanonicalNameReference();
967969
StringIndex ReadNameAsStringIndex();
@@ -1256,7 +1258,7 @@ class StreamingFlowGraphBuilder {
12561258
intptr_t* argument_bits,
12571259
intptr_t* type_argument_bits);
12581260

1259-
RawScript* Script();
1261+
const Script& script() { return script_; }
12601262

12611263
// Scan through metadata mappings section and cache offsets for recognized
12621264
// metadata kinds.
@@ -1266,7 +1268,7 @@ class StreamingFlowGraphBuilder {
12661268
TranslationHelper& translation_helper_;
12671269
Zone* zone_;
12681270
Reader* reader_;
1269-
RawScript* script_;
1271+
const Script& script_;
12701272
StreamingConstantEvaluator constant_evaluator_;
12711273
StreamingDartTypeTranslator type_translator_;
12721274
// Some items like variables are specified in the kernel binary as

runtime/vm/compiler/frontend/kernel_to_il.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ RawObject* EvaluateMetadata(const Field& metadata_field) {
24922492
helper.InitFromScript(script);
24932493

24942494
StreamingFlowGraphBuilder streaming_flow_graph_builder(
2495-
&helper, metadata_field.Script(), zone_,
2495+
&helper, Script::Handle(Z, metadata_field.Script()), Z,
24962496
TypedData::Handle(Z, metadata_field.KernelData()),
24972497
metadata_field.KernelDataProgramOffset());
24982498
return streaming_flow_graph_builder.EvaluateMetadata(
@@ -2516,7 +2516,7 @@ RawObject* BuildParameterDescriptor(const Function& function) {
25162516
helper.InitFromScript(script);
25172517

25182518
StreamingFlowGraphBuilder streaming_flow_graph_builder(
2519-
&helper, function.script(), zone_,
2519+
&helper, Script::Handle(Z, function.script()), Z,
25202520
TypedData::Handle(Z, function.KernelData()),
25212521
function.KernelDataProgramOffset());
25222522
return streaming_flow_graph_builder.BuildParameterDescriptor(
@@ -2582,7 +2582,7 @@ static void ProcessTokenPositionsEntry(
25822582
}
25832583

25842584
StreamingFlowGraphBuilder streaming_flow_graph_builder(
2585-
helper, script.raw(), zone_, data, data_kernel_offset);
2585+
helper, script, zone_, data, data_kernel_offset);
25862586
streaming_flow_graph_builder.CollectTokenPositionsFor(
25872587
script.kernel_script_index(), entry_script.kernel_script_index(),
25882588
kernel_offset, token_positions, yield_positions);

runtime/vm/kernel.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ bool FieldHasFunctionLiteralInitializer(const Field& field,
1919
TranslationHelper translation_helper(Thread::Current());
2020
translation_helper.InitFromScript(script);
2121

22-
StreamingFlowGraphBuilder builder(&translation_helper, field.Script(), zone,
22+
StreamingFlowGraphBuilder builder(&translation_helper,
23+
Script::Handle(zone, field.Script()), zone,
2324
TypedData::Handle(zone, field.KernelData()),
2425
field.KernelDataProgramOffset());
2526
builder.SetOffset(field.kernel_offset());

runtime/vm/kernel_loader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ KernelLoader::KernelLoader(const Script& script,
347347
kernel_program_info_(
348348
KernelProgramInfo::ZoneHandle(zone_, script.kernel_program_info())),
349349
translation_helper_(this, thread_),
350-
builder_(&translation_helper_, script.raw(), zone_, kernel_data, 0),
350+
builder_(&translation_helper_, script, zone_, kernel_data, 0),
351351
external_name_class_(Class::Handle(Z)),
352352
external_name_field_(Field::Handle(Z)),
353353
potential_natives_(GrowableObjectArray::Handle(Z)) {

runtime/vm/object.cc

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "vm/compiler/aot/precompiler.h"
1515
#include "vm/compiler/assembler/assembler.h"
1616
#include "vm/compiler/assembler/disassembler.h"
17+
#include "vm/compiler/frontend/kernel_binary_flowgraph.h"
1718
#include "vm/compiler/frontend/kernel_to_il.h"
1819
#include "vm/compiler/intrinsifier.h"
1920
#include "vm/compiler/jit/compiler.h"
@@ -82,7 +83,6 @@ DEFINE_FLAG(bool,
8283
"Remove script timestamps to allow for deterministic testing.");
8384

8485
DECLARE_FLAG(bool, show_invisible_frames);
85-
DECLARE_FLAG(bool, strong);
8686
DECLARE_FLAG(bool, trace_deoptimization);
8787
DECLARE_FLAG(bool, trace_deoptimization_verbose);
8888
DECLARE_FLAG(bool, trace_reload);
@@ -6924,6 +6924,11 @@ RawFunction* Function::ImplicitClosureFunction() const {
69246924
if (implicit_closure_function() != Function::null()) {
69256925
return implicit_closure_function();
69266926
}
6927+
#if defined(DART_PRECOMPILED_RUNTIME)
6928+
// In AOT mode all implicit closures are pre-created.
6929+
UNREACHABLE();
6930+
return Function::null();
6931+
#else
69276932
ASSERT(!IsSignatureFunction() && !IsClosureFunction());
69286933
Thread* thread = Thread::Current();
69296934
Zone* zone = thread->zone();
@@ -6985,6 +6990,50 @@ RawFunction* Function::ImplicitClosureFunction() const {
69856990
}
69866991
closure_function.set_kernel_offset(kernel_offset());
69876992

6993+
// In strong mode, change covariant parameter types to Object in the implicit
6994+
// closure of a method compiled by kernel.
6995+
// The VM's parser erases covariant types immediately in strong mode.
6996+
if (thread->isolate()->strong() && !is_static() && (kernel_offset() > 0)) {
6997+
const Script& function_script = Script::Handle(zone, script());
6998+
kernel::TranslationHelper translation_helper(thread);
6999+
kernel::StreamingFlowGraphBuilder builder(
7000+
&translation_helper, function_script, zone,
7001+
TypedData::Handle(zone, KernelData()), KernelDataProgramOffset());
7002+
translation_helper.InitFromScript(function_script);
7003+
builder.SetOffset(kernel_offset());
7004+
7005+
builder.ReadUntilFunctionNode();
7006+
kernel::FunctionNodeHelper fn_helper(&builder);
7007+
7008+
// Check the positional parameters, including the optional positional ones.
7009+
fn_helper.ReadUntilExcluding(
7010+
kernel::FunctionNodeHelper::kPositionalParameters);
7011+
intptr_t num_pos_params = builder.ReadListLength();
7012+
ASSERT(num_pos_params ==
7013+
num_fixed_params - 1 + (has_opt_pos_params ? num_opt_params : 0));
7014+
const Type& object_type = Type::Handle(zone, Type::ObjectType());
7015+
for (intptr_t i = 0; i < num_pos_params; ++i) {
7016+
kernel::VariableDeclarationHelper var_helper(&builder);
7017+
var_helper.ReadUntilExcluding(kernel::VariableDeclarationHelper::kEnd);
7018+
if (var_helper.IsCovariant() || var_helper.IsGenericCovariantImpl()) {
7019+
closure_function.SetParameterTypeAt(i + 1, object_type);
7020+
}
7021+
}
7022+
fn_helper.SetJustRead(kernel::FunctionNodeHelper::kPositionalParameters);
7023+
7024+
// Check the optional named parameters.
7025+
fn_helper.ReadUntilExcluding(kernel::FunctionNodeHelper::kNamedParameters);
7026+
intptr_t num_named_params = builder.ReadListLength();
7027+
ASSERT(num_named_params == (has_opt_pos_params ? 0 : num_opt_params));
7028+
for (intptr_t i = 0; i < num_named_params; ++i) {
7029+
kernel::VariableDeclarationHelper var_helper(&builder);
7030+
var_helper.ReadUntilExcluding(kernel::VariableDeclarationHelper::kEnd);
7031+
if (var_helper.IsCovariant() || var_helper.IsGenericCovariantImpl()) {
7032+
closure_function.SetParameterTypeAt(num_pos_params + 1 + i,
7033+
object_type);
7034+
}
7035+
}
7036+
}
69887037
const Type& signature_type =
69897038
Type::Handle(zone, closure_function.SignatureType());
69907039
if (!signature_type.IsFinalized()) {
@@ -6993,6 +7042,7 @@ RawFunction* Function::ImplicitClosureFunction() const {
69937042
set_implicit_closure_function(closure_function);
69947043
ASSERT(closure_function.IsImplicitClosureFunction());
69957044
return closure_function.raw();
7045+
#endif // defined(DART_PRECOMPILED_RUNTIME)
69967046
}
69977047

69987048
void Function::DropUncompiledImplicitClosureFunction() const {
@@ -7066,7 +7116,7 @@ void Function::DropUncompiledImplicitClosureFunction() const {
70667116
// and take the rest of the arguments from the invocation.
70677117
//
70687118
// Converted closure functions in VM follow same discipline as implicit closure
7069-
// functions, because they are similar in many ways. For further deatils, please
7119+
// functions, because they are similar in many ways. For further details, please
70707120
// refer to the following methods:
70717121
// -> Function::ConvertedClosureFunction
70727122
// -> StreamingFlowGraphBuilder::BuildGraphOfConvertedClosureFunction
@@ -7077,9 +7127,6 @@ void Function::DropUncompiledImplicitClosureFunction() const {
70777127
//
70787128
// Function::ConvertedClosureFunction method follows the logic of
70797129
// Function::ImplicitClosureFunction method.
7080-
//
7081-
// TODO(29181): Adjust the method to correctly pass type parameters after they
7082-
// are enabled in closure conversion.
70837130
RawFunction* Function::ConvertedClosureFunction() const {
70847131
// Return the existing converted closure function if any.
70857132
if (converted_closure_function() != Function::null()) {

0 commit comments

Comments
 (0)