Skip to content

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Mar 12, 2025

For NativeAOT, we implemented a "managed" typemap that is trimmer-safe.

In order to test its performance characteristics, we can make this typemap useable for Mono and CoreCLR as well:

  • Move NativeAotTypeManager, NativeAotValueManager, and TypeMapping types to Mono.Android.dll

  • Rename NativeAot* to Managed*

  • Add a new private $(_AndroidTypeMapImplementation) MSBuild property that can be set to llvm-ir or managed.

  • Add a new trimmer feature flag Microsoft.Android.Runtime.RuntimeFeature.ManagedTypeMap that when true uses the managed typemap on any runtime.

I added a test that verifies dotnet run succeeds for all typemap implementations.

Note that NativeAOT will only support the managed typemap.

@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/runtime-agnostic-typemap branch from 79061b4 to 32a898f Compare March 12, 2025 21:28
Comment on lines +86 to +89
if (TypemapImplementation != "llvm-ir") {
Log.LogDebugMessage ($"TypemapImplementation='{TypemapImplementation}' will write an empty native typemap.");
state = new NativeCodeGenState (state.TargetArch, new TypeDefinitionCache (), state.Resolver, [], [], state.Classifier);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Instead of returning early, I made this write the native assembly with a typemap of 0 entries, this prevents the crash at runtime:

03-12 12:36:53.682 29155 29155 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "java_type_count" referenced by "/data/app/~~sPVK03Ot__92Sd7QEgoNXw==/com.companyname.helloandroid-EN9cYvEYQtDuCTIXF_jW0Q==/split_config.arm64_v8a.apk!/lib/arm64-v8a/libmonodroid.so"...

@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/runtime-agnostic-typemap branch 3 times, most recently from a4a2896 to 5468780 Compare March 26, 2025 15:43
@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/runtime-agnostic-typemap branch from 827ff6b to ffef891 Compare March 27, 2025 13:56
@jonathanpeppers jonathanpeppers marked this pull request as ready for review March 27, 2025 16:10
Copy link
Contributor

@dellis1972 dellis1972 left a comment

Choose a reason for hiding this comment

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

Looks ok , what are the benefits of being able to use the managed typemap over the other one?

@jonathanpeppers
Copy link
Member Author

Looks ok , what are the benefits of being able to use the managed typemap over the other one?

We want to be able to test the performance of the managed typemap vs the native one. Ideally, we'd do this using the same runtime, so either Mono or CoreCLR.

@jonathanpeppers
Copy link
Member Author

jonathanpeppers commented Mar 28, 2025

This test is failing on all PRs currently:

image

Fixed by:

ObjectReferenceManager = new AndroidObjectReferenceManager ();
TypeManager = typeManager ?? new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
ValueManager = valueManager ?? new AndroidValueManager ();
if (RuntimeFeature.ManagedTypeMap) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should "harmonize" with the AndroidRuntime and related changes in e347cd3#diff-fbdea37924116ed116877313911e8766b915953b4a9c026bd49f11ba37deb815R26

For example, if this should only be controlled by RuntimeFeature.ManagedTypeMap, then do we still need the JniRuntime.JniTypeManager? typeManager, JniRuntime.JniValueManager? valueManager parameters to AndroidRuntimeOptions and AndroidRuntime? If not, we can remove those parameters.

Alternatively, maybe the RuntimeFeature.ManagedTypeMap check should instead happen in JNIEnvInit.cs?

androidRuntime = new AndroidRuntime (
args->env,
args->javaVm,
args->grefLoader,
null,
RuntimeType != DotNetRuntimeType.MonoVM ? new ManagedValueManager () : null,
args->jniAddNativeMethodRegistrationAttributePresent != 0
);

should perhaps instead be:

JniRuntime.JniTypeManager? typeManager;
JniRuntime.JniValueManager? valueManager;
if (RuntimeFeature.ManagedTypeMap || RuntimeType != DotNetRuntimeType.MonoVM) {
	typeManager = new ManagedTypeManager ();
	valueManager = new ManagedValueManager ();
} else {
	typeManager = new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
	valueManager = new AndroidValueManager ();
}
androidRuntime = new AndroidRuntime (
		args->env,
		args->javaVm,
		args->grefLoader,
		typeManager,
		valueManager,
		args->jniAddNativeMethodRegistrationAttributePresent != 0
);

@jonpryor
Copy link
Contributor

Draft commit message:

Context: 684ede6a79da5b95eb9347c319683d8855f84d09
Context: b11471bc574876e148ff40757c5d273fd6fbde9e

For NativeAOT, we implemented a "managed" typemap that is trimmer-safe;
see 684ede6a and b11471bc.

In order to test its performance characteristics, make this typemap
useable for Mono and CoreCLR as well:

  * Move `NativeAotTypeManager`, `NativeAotValueManager`, and
    `TypeMapping` types to `Mono.Android.dll`

  * Rename `NativeAot*` types to `Managed*`

  * Add a new private `$(_AndroidTypeMapImplementation)` MSBuild
    property that can be set to `llvm-ir` or `managed`.

  * Add a new trimmer feature flag
    `Microsoft.Android.Runtime.RuntimeFeature.ManagedTypeMap`;
    when `true` uses the managed typemap on any runtime.

I added a test that verifies `dotnet run` succeeds for all typemap
implementations.

Note that NativeAOT will *only* support the managed typemap.

Update `JNIEnvInit.RegisterJniNatives()` to *not* require that
`androidRuntime.TypeManager` be an `AndroidTypeManager`; this allows
`ManagedTypeManager` to be used on MonoVM and CoreCLR.

For NativeAOT, we implemented a "managed" typemap that is trimmer-safe.

In order to test its performance characteristics, we can make this
typemap useable for Mono and CoreCLR as well:

* Move `NativeAotTypeManager`, `NativeAotValueManager`, and
  `TypeMapping` types to `Mono.Android.dll`

* Rename `NativeAot*` to `Managed*`

* Add a new private `$(_AndroidTypeMapImplementation)` MSBuild
  property that can be set to `llvm-ir` or `managed`.

* Add a new trimmer feature flag
  `Android.Runtime.RuntimeFeature.ManagedTypeMap` that when `true`
  uses the managed typemap on any runtime.

I added a test that verifies `dotnet run` succeeds for all typemap
implementations.

Note that NativeAOT will *only* support the managed typemap.
@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/runtime-agnostic-typemap branch from b164936 to a827552 Compare March 31, 2025 21:00
@jonpryor jonpryor merged commit 9121bea into main Apr 1, 2025
1 of 21 checks passed
@jonpryor jonpryor deleted the dev/peppers/runtime-agnostic-typemap branch April 1, 2025 16:06
@github-actions github-actions bot locked and limited conversation to collaborators May 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants