Skip to content

Commit

Permalink
[test/ffi] Test native assets with assembly snapshot
Browse files Browse the repository at this point in the history
Test the assembly snapshot flow with native assets.
This is a prerequisite for using the native linker in follow up CLs.

TEST=tests/ffi/native_assets/asset_relative_test.dart

Bug: #49418
Change-Id: Ib14c3e1edb652ac0eefeefd0b46fa0dd69c7fa67
Cq-Include-Trybots: dart/try:vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64-try,vm-aot-win-debug-x64c-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362501
Reviewed-by: Tess Strickland <sstrickl@google.com>
  • Loading branch information
dcharkes committed Apr 12, 2024
1 parent dd1fc29 commit 3fbf46c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
15 changes: 13 additions & 2 deletions tests/ffi/native_assets/asset_relative_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Future<void> selfInvokes() async {
selfSourceUri: selfSourceUri,
runtime: Runtime.aot,
kernelCombine: KernelCombine.concatenation,
aotCompile: (Platform.isLinux || Platform.isMacOS)
? AotCompile.assembly
: AotCompile.elf,
relativePath: RelativePath.up,
arguments: [runTestsArg],
);
Expand All @@ -78,6 +81,7 @@ Future<void> invokeSelf({
required List<String> arguments,
Runtime runtime = Runtime.jit,
KernelCombine kernelCombine = KernelCombine.source,
AotCompile aotCompile = AotCompile.elf,
RelativePath relativePath = RelativePath.same,
}) async {
await withTempDir((Uri tempUri) async {
Expand Down Expand Up @@ -115,10 +119,17 @@ Future<void> invokeSelf({
nativeAssetsYaml: nativeAssetsYaml,
runtime: runtime,
kernelCombine: kernelCombine,
aotCompile: aotCompile,
runArguments: arguments,
);
print([selfSourceUri.toFilePath(), runtime.name, relativePath.name, 'done']
.join(' '));
print([
selfSourceUri.toFilePath(),
runtime.name,
kernelCombine.name,
if (runtime == Runtime.aot) aotCompile.name,
relativePath.name,
'done',
].join(' '));
});
}

Expand Down
86 changes: 75 additions & 11 deletions tests/ffi/native_assets/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ enum Runtime {
jit,
}

enum AotCompile {
assembly,
elf,
}

Future<void> runGenKernel({
required Runtime runtime,
required Uri outputUri,
Expand Down Expand Up @@ -195,18 +200,69 @@ Future<void> createDillFile({
}

Future<void> runGenSnapshot({
required Uri tempUri,
required Uri dillUri,
required Uri outputUri,
}) =>
runProcess(
executable: genSnapshotUri.toFilePath(),
arguments: [
'--snapshot-kind=app-aot-elf',
'--elf=${outputUri.toFilePath()}',
'--strip',
dillUri.toFilePath(),
],
);
required AotCompile aotCompile,
}) async {
switch (aotCompile) {
case AotCompile.elf:
await runProcess(
executable: genSnapshotUri.toFilePath(),
arguments: [
'--snapshot-kind=app-aot-elf',
'--elf=${outputUri.toFilePath()}',
'--strip',
dillUri.toFilePath(),
],
);
case AotCompile.assembly:
if (!(Platform.isLinux || Platform.isMacOS)) {
// Windows doesn't support assembly snapshots.
throw UnsupportedError('Not yet implemented for MSVC');
}
final assemblyUri = tempUri.resolve('out.S');
await runProcess(
executable: genSnapshotUri.toFilePath(),
arguments: [
'--snapshot-kind=app-aot-assembly',
'--assembly=${assemblyUri.toFilePath()}',
dillUri.toFilePath(),
],
);
if (!await File.fromUri(assemblyUri).exists()) {
throw Error();
}

// Executables and arguments taken from
// pkg/test_runner/lib/src/compiler_configuration.dart
// `computeAssembleCommand`.
if (Platform.isMacOS) {
await runProcess(
executable: 'clang',
arguments: [
'-Wl,-undefined,error',
'-Wl,-no_compact_unwind',
'-dynamiclib',
'-o',
outputUri.toFilePath(),
assemblyUri.toFilePath(),
],
);
} else if (Platform.isLinux) {
await runProcess(
executable: 'gcc',
arguments: [
'-shared',
'-Wl,--no-undefined',
'-o',
outputUri.toFilePath(),
assemblyUri.toFilePath(),
],
);
}
}
}

Future<void> runDart({
required Uri scriptUri,
Expand Down Expand Up @@ -279,6 +335,7 @@ Future<void> compileAndRun({
required String nativeAssetsYaml,
required Runtime runtime,
required KernelCombine kernelCombine,
AotCompile aotCompile = AotCompile.elf,
required List<String> runArguments,
}) async {
final nativeAssetsUri = tempUri.resolve('native_assets.yaml');
Expand All @@ -297,7 +354,12 @@ Future<void> compileAndRun({
switch (runtime) {
case Runtime.aot:
final snapshotUri = tempUri.resolve('out.snapshot');
await runGenSnapshot(dillUri: outDillUri, outputUri: snapshotUri);
await runGenSnapshot(
tempUri: tempUri,
dillUri: outDillUri,
outputUri: snapshotUri,
aotCompile: aotCompile,
);
await runDartAotRuntime(
aotSnapshotUri: snapshotUri, arguments: runArguments);
case Runtime.appjit:
Expand Down Expand Up @@ -350,6 +412,7 @@ Future<void> invokeSelf({
required String nativeAssetsYaml,
Runtime runtime = Runtime.jit,
KernelCombine kernelCombine = KernelCombine.source,
AotCompile aotCompile = AotCompile.elf,
}) async {
await withTempDir((Uri tempUri) async {
await compileAndRun(
Expand All @@ -358,6 +421,7 @@ Future<void> invokeSelf({
nativeAssetsYaml: nativeAssetsYaml,
runtime: runtime,
kernelCombine: kernelCombine,
aotCompile: aotCompile,
runArguments: arguments,
);
print([selfSourceUri.toFilePath(), runtime.name, 'done'].join(' '));
Expand Down

0 comments on commit 3fbf46c

Please sign in to comment.