Skip to content

Commit

Permalink
Allow ddc in kernel mode to output used inputs
Browse files Browse the repository at this point in the history
This change basically consists of these steps:
* Enable the incremental compiler to trace used inputs.
* Translate used libraries into used dill inputs.
* Output the list of used dills.

Bug: #37788
Change-Id: Icbc3942226f50ba733196586fac8715e21d6ed34
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112385
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Vijay Menon <vsm@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
  • Loading branch information
jensjoha authored and commit-bot@chromium.org committed Aug 13, 2019
1 parent aeb864a commit e47f354
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
33 changes: 31 additions & 2 deletions pkg/dev_compiler/lib/src/kernel/command.dart
Expand Up @@ -101,7 +101,9 @@ Future<CompilerResult> _compile(List<String> args,
..addFlag('compile-sdk',
help: 'Build an SDK module.', defaultsTo: false, hide: true)
..addOption('libraries-file',
help: 'The path to the libraries.json file for the sdk.');
help: 'The path to the libraries.json file for the sdk.')
..addOption('used-inputs-file',
help: 'If set, the file to record inputs used.', hide: true);
SharedCompilerOptions.addArguments(argParser);

var declaredVariables = parseAndRemoveDeclaredVariables(args);
Expand Down Expand Up @@ -237,6 +239,7 @@ Future<CompilerResult> _compile(List<String> args,
List<Component> doneInputSummaries;
fe.IncrementalCompiler incrementalCompiler;
fe.WorkerInputComponent cachedSdkInput;
bool recordUsedInputs = argResults['used-inputs-file'] != null;
if (useAnalyzer || !useIncrementalCompiler) {
compilerState = await fe.initializeCompiler(
oldCompilerState,
Expand Down Expand Up @@ -267,7 +270,8 @@ Future<CompilerResult> _compile(List<String> args,
TargetFlags(trackWidgetCreation: trackWidgetCreation)),
fileSystem: fileSystem,
experiments: experiments,
environmentDefines: declaredVariables);
environmentDefines: declaredVariables,
trackNeededDillLibraries: recordUsedInputs);
incrementalCompiler = compilerState.incrementalCompiler;
cachedSdkInput =
compilerState.workerInputCache[sourcePathToUri(sdkSummaryPath)];
Expand Down Expand Up @@ -386,6 +390,31 @@ Future<CompilerResult> _compile(List<String> args,
}
}

if (recordUsedInputs) {
Set<Uri> usedOutlines = Set<Uri>();
if (!useAnalyzer && useIncrementalCompiler) {
compilerState.incrementalCompiler
.updateNeededDillLibrariesWithHierarchy(result.classHierarchy, null);
for (Library lib
in compilerState.incrementalCompiler.neededDillLibraries) {
if (lib.importUri.scheme == "dart") continue;
Uri uri = compilerState.libraryToInputDill[lib.importUri];
if (uri == null) {
throw StateError("Library ${lib.importUri} was recorded as used, "
"but was not in the list of known libraries.");
}
usedOutlines.add(uri);
}
} else {
// Used inputs wasn't recorded: Say we used everything.
usedOutlines.addAll(summaryModules.keys);
}

var outputUsedFile = File(argResults['used-inputs-file'] as String);
outputUsedFile.createSync(recursive: true);
outputUsedFile.writeAsStringSync(usedOutlines.join("\n"));
}

await Future.wait(outFiles);
return CompilerResult(0, kernelState: compilerState);
}
Expand Down
20 changes: 18 additions & 2 deletions pkg/front_end/lib/src/api_unstable/ddc.dart
Expand Up @@ -142,7 +142,8 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
Target target,
{FileSystem fileSystem,
Map<ExperimentalFlag, bool> experiments,
Map<String, String> environmentDefines}) async {
Map<String, String> environmentDefines,
bool trackNeededDillLibraries: false}) async {
inputSummaries.sort((a, b) => a.toString().compareTo(b.toString()));

IncrementalCompiler incrementalCompiler;
Expand Down Expand Up @@ -189,6 +190,7 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
workerInputCache[sdkSummary] = cachedSdkInput;
incrementalCompiler = new IncrementalCompiler.fromComponent(
new CompilerContext(processedOpts), cachedSdkInput.component);
incrementalCompiler.trackNeededDillLibraries = trackNeededDillLibraries;
} else {
options = oldState.options;
options.inputSummaries = inputSummaries;
Expand All @@ -205,6 +207,7 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
// Reuse the incremental compiler, but reset as needed.
incrementalCompiler = oldState.incrementalCompiler;
incrementalCompiler.invalidateAllSources();
incrementalCompiler.trackNeededDillLibraries = trackNeededDillLibraries;
options.packagesFileUri = packagesFile;
options.fileSystem = fileSystem;
processedOpts.clearFileSystemCache();
Expand All @@ -215,6 +218,10 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
incrementalCompiler: incrementalCompiler);

CanonicalName nameRoot = cachedSdkInput.component.root;
Map<Uri, Uri> libraryToInputDill;
if (trackNeededDillLibraries) {
libraryToInputDill = new Map<Uri, Uri>();
}
List<int> loadFromDillIndexes = new List<int>();

// Notice that the ordering of the input summaries matter, so we need to
Expand All @@ -238,6 +245,9 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
var component = cachedInput.component;
for (var lib in component.libraries) {
lib.isExternal = cachedInput.externalLibs.contains(lib.importUri);
if (trackNeededDillLibraries) {
libraryToInputDill[lib.importUri] = inputSummary;
}
}
component.computeCanonicalNames();
doneInputSummaries[i] = component;
Expand All @@ -258,13 +268,19 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
.loadComponent(bytes, nameRoot, alwaysCreateNewNamedNodes: true));
workerInputCache[summary] = cachedInput;
doneInputSummaries[index] = cachedInput.component;
if (trackNeededDillLibraries) {
for (var lib in cachedInput.component.libraries) {
libraryToInputDill[lib.importUri] = summary;
}
}
}

incrementalCompiler.setModulesToLoadOnNextComputeDelta(doneInputSummaries);

return new InitializedCompilerState(options, processedOpts,
workerInputCache: workerInputCache,
incrementalCompiler: incrementalCompiler);
incrementalCompiler: incrementalCompiler,
libraryToInputDill: libraryToInputDill);
}

Future<DdcResult> compile(InitializedCompilerState compilerState,
Expand Down

0 comments on commit e47f354

Please sign in to comment.