Skip to content

Commit

Permalink
[vm/kernel] Add a transformation that annotates invocations with rece…
Browse files Browse the repository at this point in the history
…iver type.

Currently we only annotate those call-sites that would result
in generic covariant checks performed on the callee side.

Bug: #31798
Change-Id: Ifcf60032036575f615015d716276484a7c1236b3
Reviewed-on: https://dart-review.googlesource.com/69580
Commit-Queue: Vyacheslav Egorov <[email protected]>
Reviewed-by: Kevin Millikin <[email protected]>
Reviewed-by: Martin Kustermann <[email protected]>
  • Loading branch information
mraleph authored and [email protected] committed Aug 16, 2018
1 parent da428bb commit aa8145a
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 80 deletions.
6 changes: 1 addition & 5 deletions pkg/compiler/lib/src/kernel/dart2js_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ class Dart2jsTarget extends Target {
bool get errorOnUnexactWebIntLiterals => true;

@override
void performModularTransformationsOnLibraries(
void performModularTransformationsOnLibraries(ir.Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<ir.Library> libraries,
{void logger(String msg)}) {}

@override
void performGlobalTransformations(CoreTypes coreTypes, ir.Component component,
{void logger(String msg)}) {}

@override
ir.Expression instantiateInvocation(
CoreTypes coreTypes,
Expand Down
6 changes: 1 addition & 5 deletions pkg/dev_compiler/lib/src/kernel/target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,10 @@ class DevCompilerTarget extends Target {
bool get enableNoSuchMethodForwarders => true;

@override
void performModularTransformationsOnLibraries(
void performModularTransformationsOnLibraries(Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<Library> libraries,
{void logger(String msg)}) {}

@override
void performGlobalTransformations(CoreTypes coreTypes, Component component,
{void logger(String msg)}) {}

@override
Expression instantiateInvocation(CoreTypes coreTypes, Expression receiver,
String name, Arguments arguments, int offset, bool isSuper) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/front_end/lib/src/base/processed_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ class ProcessedOptions {

/// Helper to load a .dill file from [uri] using the existing [nameRoot].
Component loadComponent(List<int> bytes, CanonicalName nameRoot) {
Component component = new Component(nameRoot: nameRoot);
Component component =
target.configureComponent(new Component(nameRoot: nameRoot));
// TODO(ahe): Pass file name to BinaryBuilder.
// TODO(ahe): Control lazy loading via an option.
new BinaryBuilder(bytes, filename: null, disableLazyReading: false)
Expand Down
9 changes: 5 additions & 4 deletions pkg/front_end/lib/src/fasta/incremental_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
}
userCode.loader.builders.clear();
userCode = userCodeOld;
return new Component(
libraries: compiledLibraries, uriToSource: <Uri, Source>{});
return context.options.target.configureComponent(new Component(
libraries: compiledLibraries, uriToSource: <Uri, Source>{}));
}
if (componentWithDill != null) {
this.invalidatedUris.clear();
Expand Down Expand Up @@ -281,7 +281,8 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
}

// This is the incremental component.
return new Component(libraries: outputLibraries, uriToSource: uriToSource)
return context.options.target.configureComponent(
new Component(libraries: outputLibraries, uriToSource: uriToSource))
..mainMethod = mainMethod;
});
}
Expand Down Expand Up @@ -348,7 +349,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {

if (summaryBytes != null) {
ticker.logMs("Read ${c.options.sdkSummary}");
data.component = new Component();
data.component = c.options.target.configureComponent(new Component());
new BinaryBuilder(summaryBytes, disableLazyReading: false)
.readComponent(data.component);
ticker.logMs("Deserialized ${c.options.sdkSummary}");
Expand Down
12 changes: 8 additions & 4 deletions pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,11 @@ class KernelTarget extends TargetImplementation {
this.uriToSource.forEach(copySource);
dillTarget.loader.uriToSource.forEach(copySource);

Component component = new Component(
nameRoot: nameRoot, libraries: libraries, uriToSource: uriToSource);
Component component = CompilerContext.current.options.target
.configureComponent(new Component(
nameRoot: nameRoot,
libraries: libraries,
uriToSource: uriToSource));
if (loader.first != null) {
// TODO(sigmund): do only for full program
Declaration declaration =
Expand Down Expand Up @@ -601,7 +604,8 @@ class KernelTarget extends TargetImplementation {
libraries.add(library.target);
}
}
Component plaformLibraries = new Component();
Component plaformLibraries = CompilerContext.current.options.target
.configureComponent(new Component());
// Add libraries directly to prevent that their parents are changed.
plaformLibraries.libraries.addAll(libraries);
loader.computeCoreTypes(plaformLibraries);
Expand Down Expand Up @@ -728,7 +732,7 @@ class KernelTarget extends TargetImplementation {
/// libraries for the first time.
void runBuildTransformations() {
backendTarget.performModularTransformationsOnLibraries(
loader.coreTypes, loader.hierarchy, loader.libraries,
component, loader.coreTypes, loader.hierarchy, loader.libraries,
logger: (String msg) => ticker.logMs(msg));
}

Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/lib/src/fasta/kernel/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ List<int> serializeProcedure(Procedure procedure) {
procedure.parent = fakeLibrary;
}

// TODO(vegorov) find a way to preserve metadata.
Component program = new Component(libraries: [fakeLibrary]);
return serializeComponent(program);
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/front_end/lib/src/fasta/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Location getLocationFromNode(TreeNode node) {
parent = parent.parent;
}
if (parent is Library) {
Component component =
new Component(uriToSource: CompilerContext.current.uriToSource);
Component component = CompilerContext.current.options.target
.configureComponent(
new Component(uriToSource: CompilerContext.current.uriToSource));
component.libraries.add(parent);
parent.parent = component;
Location result = node.location;
Expand Down
3 changes: 2 additions & 1 deletion pkg/front_end/lib/src/incremental/kernel_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ class KernelDriver {
List<int> bytes = _byteStore.get(kernelKey);
if (bytes != null) {
return _logger.runAsync('Read serialized libraries', () async {
var component = new Component(nameRoot: nameRoot);
var component = _options.target
.configureComponent(new Component(nameRoot: nameRoot));
_readComponent(component, bytes);
await appendNewDillLibraries(component);

Expand Down
12 changes: 3 additions & 9 deletions pkg/front_end/test/fasta/testing/suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,14 @@ class TestVmTarget extends VmTarget {

String get name => "vm";

void performModularTransformationsOnLibraries(
@override
void performModularTransformationsOnLibraries(Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<Library> libraries,
{void logger(String msg)}) {
if (enabled) {
super.performModularTransformationsOnLibraries(
coreTypes, hierarchy, libraries,
component, coreTypes, hierarchy, libraries,
logger: logger);
}
}

void performGlobalTransformations(CoreTypes coreTypes, Component component,
{void logger(String msg)}) {
if (enabled) {
super.performGlobalTransformations(coreTypes, component, logger: logger);
}
}
}
47 changes: 6 additions & 41 deletions pkg/kernel/lib/target/targets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,48 +103,11 @@ abstract class Target {
/// slowing down compilation.
void performOutlineTransformations(Component component) {}

/// Perform target-specific modular transformations on the given component.
///
/// These transformations should not be whole-component transformations. They
/// should expect that the component will contain external libraries.
void performModularTransformationsOnComponent(
CoreTypes coreTypes, ClassHierarchy hierarchy, Component component,
{void logger(String msg)}) {
performModularTransformationsOnLibraries(
coreTypes, hierarchy, component.libraries,
logger: logger);
}

/// Perform target-specific modular transformations on the given libraries.
///
/// The intent of this method is to perform the transformations only on some
/// subset of the component libraries and avoid packing them into a temporary
/// [Component] instance to pass into [performModularTransformationsOnComponent].
///
/// Note that the following should be equivalent:
///
/// target.performModularTransformationsOnComponent(coreTypes, component);
///
/// and
///
/// target.performModularTransformationsOnLibraries(
/// coreTypes, component.libraries);
void performModularTransformationsOnLibraries(
void performModularTransformationsOnLibraries(Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<Library> libraries,
{void logger(String msg)});

/// Perform target-specific whole-program transformations.
///
/// These transformations should be optimizations and not required for
/// correctness. Everything should work if a simple and fast linker chooses
/// not to apply these transformations.
///
/// Note that [performGlobalTransformations] doesn't have -OnComponent and
/// -OnLibraries alternatives, because the global knowledge required by the
/// transformations is assumed to be retrieved from a [Component] instance.
void performGlobalTransformations(CoreTypes coreTypes, Component component,
{void logger(String msg)});

/// Perform target-specific modular transformations on the given program.
///
/// This is used when an individual expression is compiled, e.g. for debugging
Expand Down Expand Up @@ -226,6 +189,10 @@ abstract class Target {
return new InvalidExpression(message)..fileOffset = offset;
}

/// Configure the given [Component] in a target specific way.
/// Returns the configured component.
Component configureComponent(Component component) => component;

String toString() => 'Target($name)';
}

Expand All @@ -237,11 +204,9 @@ class NoneTarget extends Target {
bool get strongMode => flags.strongMode;
String get name => 'none';
List<String> get extraRequiredLibraries => <String>[];
void performModularTransformationsOnLibraries(
void performModularTransformationsOnLibraries(Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<Library> libraries,
{void logger(String msg)}) {}
void performGlobalTransformations(CoreTypes coreTypes, Component component,
{void logger(String msg)}) {}

@override
Expression instantiateInvocation(CoreTypes coreTypes, Expression receiver,
Expand Down
4 changes: 4 additions & 0 deletions pkg/vm/bin/dump_kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import 'package:vm/metadata/procedure_attributes.dart'
import 'package:vm/metadata/unreachable.dart'
show UnreachableNodeMetadataRepository;

import 'package:vm/metadata/call_site_attributes.dart'
show CallSiteAttributesMetadataRepository;

final String _usage = '''
Usage: dump_kernel input.dill output.txt
Dumps kernel binary file with VM-specific metadata.
Expand All @@ -39,6 +42,7 @@ main(List<String> arguments) async {
component.addMetadataRepository(new ProcedureAttributesMetadataRepository());
component.addMetadataRepository(new UnreachableNodeMetadataRepository());
component.addMetadataRepository(new BytecodeMetadataRepository());
component.addMetadataRepository(new CallSiteAttributesMetadataRepository());

final List<int> bytes = new File(input).readAsBytesSync();
new BinaryBuilderWithMetadata(bytes).readComponent(component);
Expand Down
2 changes: 2 additions & 0 deletions pkg/vm/lib/incremental_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class IncrementalCompiler {
combined[library.importUri] = library;
}
}

// TODO(vegorov) this needs to merge metadata repositories from deltas.
return new Component(libraries: combined.values.toList())
..mainMethod = mainMethod;
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/vm/lib/metadata/call_site_attributes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library vm.metadata.call_site_attributes;

import 'package:kernel/ast.dart';

/// Metadata for annotating call sites with various attributes.
class CallSiteAttributesMetadata {
final DartType receiverType;

const CallSiteAttributesMetadata({this.receiverType});

@override
String toString() => "receiverType:$receiverType";
}

/// Repository for [CallSiteAttributesMetadata].
class CallSiteAttributesMetadataRepository
extends MetadataRepository<CallSiteAttributesMetadata> {
static final repositoryTag = 'vm.call-site-attributes.metadata';

@override
final String tag = repositoryTag;

@override
final Map<TreeNode, CallSiteAttributesMetadata> mapping =
<TreeNode, CallSiteAttributesMetadata>{};

@override
void writeToBinary(
CallSiteAttributesMetadata metadata, Node node, BinarySink sink) {
sink.writeDartType(metadata.receiverType);
}

@override
CallSiteAttributesMetadata readFromBinary(Node node, BinarySource source) {
final type = source.readDartType();
return new CallSiteAttributesMetadata(receiverType: type);
}
}
18 changes: 13 additions & 5 deletions pkg/vm/lib/target/vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'package:kernel/transformations/mixin_full_resolution.dart'
import 'package:kernel/transformations/continuation.dart' as transformAsync
show transformLibraries, transformProcedure;

import '../transformations/call_site_annotator.dart' as callSiteAnnotator;

/// Specializes the kernel IR to the Dart VM.
class VmTarget extends Target {
final TargetFlags flags;
Expand Down Expand Up @@ -56,7 +58,7 @@ class VmTarget extends Target {
];

@override
void performModularTransformationsOnLibraries(
void performModularTransformationsOnLibraries(Component component,
CoreTypes coreTypes, ClassHierarchy hierarchy, List<Library> libraries,
{void logger(String msg)}) {
transformMixins.transformLibraries(this, coreTypes, hierarchy, libraries,
Expand All @@ -66,11 +68,11 @@ class VmTarget extends Target {
// TODO(kmillikin): Make this run on a per-method basis.
transformAsync.transformLibraries(coreTypes, libraries, flags.syncAsync);
logger?.call("Transformed async methods");
}

@override
void performGlobalTransformations(CoreTypes coreTypes, Component component,
{void logger(String msg)}) {}
callSiteAnnotator.transformLibraries(
component, libraries, coreTypes, hierarchy);
logger?.call("Annotated call sites");
}

@override
void performTransformationsOnProcedure(
Expand Down Expand Up @@ -284,4 +286,10 @@ class VmTarget extends Target {

@override
bool get nativeExtensionExpectsString => true;

@override
Component configureComponent(Component component) {
callSiteAnnotator.addRepositoryTo(component);
return super.configureComponent(component);
}
}
Loading

0 comments on commit aa8145a

Please sign in to comment.