Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cross-compiling AOT snapshots in benchmark builder #701

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 100 additions & 31 deletions benchmarks/tool/compile_benchmarks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import 'package:pool/pool.dart' show Pool;
Future<void> main(List<String> args) async {
final argParser = ArgParser()
..addOption('target',
mandatory: false, defaultsTo: 'aot,exe,jit,js,js-production')
..addOption('jobs', abbr: 'j', mandatory: false);
mandatory: false, defaultsTo: 'exe,jit,js,js-production')
..addOption('jobs', abbr: 'j', mandatory: false)
..addOption('aot-target', mandatory: false, defaultsTo: 'x64');

final parsedArgs = argParser.parse(args);
final env = Platform.environment;

var jobs = Platform.numberOfProcessors;
if (parsedArgs['jobs'] != null) {
Expand All @@ -23,7 +25,23 @@ Future<void> main(List<String> args) async {
for (final targetStr in parsedArgs['target'].split(',')) {
switch (targetStr) {
case 'aot':
targets.add(aotTarget);
final dartSdkPath = env["DART_SDK"];

if (dartSdkPath == null) {
print('\$DART_SDK needs to be set when generating aot snapshots');
exit(1);
}

final parsedAotTarget = parsedArgs['aot-target'];
final aotTarget = aotTargets[parsedAotTarget];
if (aotTarget == null) {
print('Unsupported aot target: $parsedAotTarget');
print(
'Supported aot targets: ${aotTargets.keys.toList().join(', ')}');
exit(1);
}

targets.add(makeAotTarget(dartSdkPath, aotTarget));
break;

case 'exe':
Expand Down Expand Up @@ -60,7 +78,7 @@ Future<void> main(List<String> args) async {
.toList();
}

final commands = <List<String>>[];
final commands = <ProcessInstructions>[];

if (sourceFiles.isNotEmpty && targets.isNotEmpty) {
try {
Expand All @@ -79,11 +97,21 @@ Future<void> main(List<String> args) async {

final pool = Pool(jobs);

final stream = pool.forEach<List<String>, CompileProcess>(commands,
(List<String> command) async {
final commandStr = command.join(' ');
final stream = pool.forEach<ProcessInstructions, CompileProcess>(commands,
(ProcessInstructions command) async {
var envStr = '';
if (command.environment != null) {
envStr = command.environment!.entries
.map((entry) => '${entry.key}=${entry.value}')
.join(' ') +
' ';
}
final commandStr =
'$envStr${command.executable} ${command.arguments.join(' ')}';
print(commandStr);
final result = await Process.run(command[0], command.sublist(1));

final result = await Process.run(command.executable, command.arguments,
environment: command.environment);
return CompileProcess(commandStr, result);
});

Expand All @@ -107,6 +135,39 @@ Future<void> main(List<String> args) async {
await pool.done;
}

/// Stores [Process.run] arguments
class ProcessInstructions {
final String executable;
final List<String> arguments;
final Map<String, String>? environment;

ProcessInstructions(this.executable, this.arguments, {this.environment});
}

/// Supported aot snapshot targets
enum AotTarget {
x64,
armv7hf,
armv8,
}

/// Maps `--aot-target` arguments to their [AotTarget]s
const aotTargets = <String, AotTarget>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(to avoid duplication you could use AotTarget.values and their string representation)

'x64': AotTarget.x64,
'armv7hf': AotTarget.armv7hf,
'armv8': AotTarget.armv8,
};

/// Maps [AotTarget]s to `DART_CONFIGURATION` env values when invoking `precompiler2`
// TODO: Product or release?
const aotTargetDartConfiguration = <AotTarget, String>{
AotTarget.x64: "ProductX64",
AotTarget.armv7hf: "ProductXARM/clang_x86",
AotTarget.armv8: "ProductXARM64/clang_x64",
};

/// Packs a debug string for a command being run to the command's result, to be
/// able to show which command failed and why
class CompileProcess {
final String command;
final ProcessResult result;
Expand All @@ -116,7 +177,7 @@ class CompileProcess {

class Target {
final String _name;
final List<String> Function(String) _processArgs;
final ProcessInstructions Function(String sourceFile) _processArgs;

const Target(this._name, this._processArgs);

Expand All @@ -125,63 +186,71 @@ class Target {
return 'Target($_name)';
}

List<String> compileArgs(String sourceFile) {
ProcessInstructions compileArgs(String sourceFile) {
return _processArgs(sourceFile);
}
}

Target makeAotTarget(String dartSdkPath, AotTarget aotTarget) {
return Target('aot', (sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
// TODO: Do we need `-Ddart.vm.product=true`?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :)

return ProcessInstructions('$dartSdkPath/pkg/vm/tool/precompiler2', [
sourceFile,
'out/$baseNameNoExt.aot'
], environment: {
'DART_CONFIGURATION': aotTargetDartConfiguration[aotTarget]!
});
});
}

const aotTarget = Target('aot', aotProcessArgs);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this aotTarget and aotProcessArgs unused now?

const exeTarget = Target('exe', exeProcessArgs);
const jitTarget = Target('jit', jitProcessArgs);
const jsTarget = Target('js', jsProcessArgs);
const jsProductionTarget = Target('js-production', jsProductionProcessArgs);

List<String> aotProcessArgs(String sourceFile) {
ProcessInstructions aotProcessArgs(String sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
return [
'dart',
'compile',
'aot-snapshot',
sourceFile,
'-o',
'out/$baseNameNoExt.aot'
];
return ProcessInstructions('dart',
['compile', 'aot-snapshot', sourceFile, '-o', 'out/$baseNameNoExt.aot']);
}

List<String> exeProcessArgs(String sourceFile) {
ProcessInstructions exeProcessArgs(String sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
return ['dart', 'compile', 'exe', sourceFile, '-o', 'out/$baseNameNoExt.exe'];
return ProcessInstructions(
'dart', ['compile', 'exe', sourceFile, '-o', 'out/$baseNameNoExt.exe']);
}

List<String> jitProcessArgs(String sourceFile) {
ProcessInstructions jitProcessArgs(String sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
return [
'dart',
return ProcessInstructions('dart', [
'--snapshot-kind=kernel',
'--snapshot=out/$baseNameNoExt.dill',
sourceFile
];
]);
}

List<String> jsProcessArgs(String sourceFile) {
ProcessInstructions jsProcessArgs(String sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
return ['dart', 'compile', 'js', sourceFile, '-o', 'out/$baseNameNoExt.js'];
return ProcessInstructions(
'dart', ['compile', 'js', sourceFile, '-o', 'out/$baseNameNoExt.js']);
}

List<String> jsProductionProcessArgs(String sourceFile) {
ProcessInstructions jsProductionProcessArgs(String sourceFile) {
final baseName = path.basename(sourceFile);
final baseNameNoExt = path.withoutExtension(baseName);
return [
'dart',
return ProcessInstructions('dart', [
'compile',
'js',
sourceFile,
'-O4',
'-o',
'out/$baseNameNoExt.production.js'
];
]);
}