Skip to content

Commit e47f354

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
Allow ddc in kernel mode to output used inputs
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>
1 parent aeb864a commit e47f354

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pkg/dev_compiler/lib/src/kernel/command.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ Future<CompilerResult> _compile(List<String> args,
101101
..addFlag('compile-sdk',
102102
help: 'Build an SDK module.', defaultsTo: false, hide: true)
103103
..addOption('libraries-file',
104-
help: 'The path to the libraries.json file for the sdk.');
104+
help: 'The path to the libraries.json file for the sdk.')
105+
..addOption('used-inputs-file',
106+
help: 'If set, the file to record inputs used.', hide: true);
105107
SharedCompilerOptions.addArguments(argParser);
106108

107109
var declaredVariables = parseAndRemoveDeclaredVariables(args);
@@ -237,6 +239,7 @@ Future<CompilerResult> _compile(List<String> args,
237239
List<Component> doneInputSummaries;
238240
fe.IncrementalCompiler incrementalCompiler;
239241
fe.WorkerInputComponent cachedSdkInput;
242+
bool recordUsedInputs = argResults['used-inputs-file'] != null;
240243
if (useAnalyzer || !useIncrementalCompiler) {
241244
compilerState = await fe.initializeCompiler(
242245
oldCompilerState,
@@ -267,7 +270,8 @@ Future<CompilerResult> _compile(List<String> args,
267270
TargetFlags(trackWidgetCreation: trackWidgetCreation)),
268271
fileSystem: fileSystem,
269272
experiments: experiments,
270-
environmentDefines: declaredVariables);
273+
environmentDefines: declaredVariables,
274+
trackNeededDillLibraries: recordUsedInputs);
271275
incrementalCompiler = compilerState.incrementalCompiler;
272276
cachedSdkInput =
273277
compilerState.workerInputCache[sourcePathToUri(sdkSummaryPath)];
@@ -386,6 +390,31 @@ Future<CompilerResult> _compile(List<String> args,
386390
}
387391
}
388392

393+
if (recordUsedInputs) {
394+
Set<Uri> usedOutlines = Set<Uri>();
395+
if (!useAnalyzer && useIncrementalCompiler) {
396+
compilerState.incrementalCompiler
397+
.updateNeededDillLibrariesWithHierarchy(result.classHierarchy, null);
398+
for (Library lib
399+
in compilerState.incrementalCompiler.neededDillLibraries) {
400+
if (lib.importUri.scheme == "dart") continue;
401+
Uri uri = compilerState.libraryToInputDill[lib.importUri];
402+
if (uri == null) {
403+
throw StateError("Library ${lib.importUri} was recorded as used, "
404+
"but was not in the list of known libraries.");
405+
}
406+
usedOutlines.add(uri);
407+
}
408+
} else {
409+
// Used inputs wasn't recorded: Say we used everything.
410+
usedOutlines.addAll(summaryModules.keys);
411+
}
412+
413+
var outputUsedFile = File(argResults['used-inputs-file'] as String);
414+
outputUsedFile.createSync(recursive: true);
415+
outputUsedFile.writeAsStringSync(usedOutlines.join("\n"));
416+
}
417+
389418
await Future.wait(outFiles);
390419
return CompilerResult(0, kernelState: compilerState);
391420
}

pkg/front_end/lib/src/api_unstable/ddc.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
142142
Target target,
143143
{FileSystem fileSystem,
144144
Map<ExperimentalFlag, bool> experiments,
145-
Map<String, String> environmentDefines}) async {
145+
Map<String, String> environmentDefines,
146+
bool trackNeededDillLibraries: false}) async {
146147
inputSummaries.sort((a, b) => a.toString().compareTo(b.toString()));
147148

148149
IncrementalCompiler incrementalCompiler;
@@ -189,6 +190,7 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
189190
workerInputCache[sdkSummary] = cachedSdkInput;
190191
incrementalCompiler = new IncrementalCompiler.fromComponent(
191192
new CompilerContext(processedOpts), cachedSdkInput.component);
193+
incrementalCompiler.trackNeededDillLibraries = trackNeededDillLibraries;
192194
} else {
193195
options = oldState.options;
194196
options.inputSummaries = inputSummaries;
@@ -205,6 +207,7 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
205207
// Reuse the incremental compiler, but reset as needed.
206208
incrementalCompiler = oldState.incrementalCompiler;
207209
incrementalCompiler.invalidateAllSources();
210+
incrementalCompiler.trackNeededDillLibraries = trackNeededDillLibraries;
208211
options.packagesFileUri = packagesFile;
209212
options.fileSystem = fileSystem;
210213
processedOpts.clearFileSystemCache();
@@ -215,6 +218,10 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
215218
incrementalCompiler: incrementalCompiler);
216219

217220
CanonicalName nameRoot = cachedSdkInput.component.root;
221+
Map<Uri, Uri> libraryToInputDill;
222+
if (trackNeededDillLibraries) {
223+
libraryToInputDill = new Map<Uri, Uri>();
224+
}
218225
List<int> loadFromDillIndexes = new List<int>();
219226

220227
// Notice that the ordering of the input summaries matter, so we need to
@@ -238,6 +245,9 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
238245
var component = cachedInput.component;
239246
for (var lib in component.libraries) {
240247
lib.isExternal = cachedInput.externalLibs.contains(lib.importUri);
248+
if (trackNeededDillLibraries) {
249+
libraryToInputDill[lib.importUri] = inputSummary;
250+
}
241251
}
242252
component.computeCanonicalNames();
243253
doneInputSummaries[i] = component;
@@ -258,13 +268,19 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
258268
.loadComponent(bytes, nameRoot, alwaysCreateNewNamedNodes: true));
259269
workerInputCache[summary] = cachedInput;
260270
doneInputSummaries[index] = cachedInput.component;
271+
if (trackNeededDillLibraries) {
272+
for (var lib in cachedInput.component.libraries) {
273+
libraryToInputDill[lib.importUri] = summary;
274+
}
275+
}
261276
}
262277

263278
incrementalCompiler.setModulesToLoadOnNextComputeDelta(doneInputSummaries);
264279

265280
return new InitializedCompilerState(options, processedOpts,
266281
workerInputCache: workerInputCache,
267-
incrementalCompiler: incrementalCompiler);
282+
incrementalCompiler: incrementalCompiler,
283+
libraryToInputDill: libraryToInputDill);
268284
}
269285

270286
Future<DdcResult> compile(InitializedCompilerState compilerState,

0 commit comments

Comments
 (0)