Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks, monodroid] LLVM-IR Generator (#7163)
Browse files Browse the repository at this point in the history
Context: e1af958

Commit e1af958 mentioned a TODO:

> * Finish the LLVM code generator so that LLVM Marshal Methods are
>   emitted into `libxamarin-app.so`.

Implement LLVM Marshal Methods LLVM-IR executable code generator:

  * LLVM-IR Generation Infrastructure:
    * function and instruction attributes
    * function parameter (declaration/definition) and argument
      (runtime) handling
    * function variable (including parameters) handling, including
      unnamed local variables
    * support for native function signatures and pointers to functions
    * The ret, store, load, icmp, br, call and phi instructions
  * LLVM-IR Marshal Method generator
    * managed to JNI signature and symbol name translations

When `ENABLE_MARSHAL_METHODS` is defined, LLVM-IR code is generated,
compiled, and linked.  Generated code is minimally tested for runtime
behavior, as not all the infrastructure on the Xamarin.Android side
is implemented yet. This is on purpose, to keep commits smaller.

Support for various LLVM IR instructions is limited only to those we
actually use and only to elements of those constructions that we use.
As such, it's not a general purpose code generator which allows us to
make some assumptions and take some shortcuts, without compromising
correctness and validity of the generated code.

The native type handling system portion of this commit is to be
treated as proof-of-concept: is is not as optimized (design wise) as
it should be. The reason for this limitation is that it requires
modifying the previous LLVM IR data generation code and it would
contribute to this commits' already substantial size.

~~ TODO ~~

Update/rewrite infrastructure to focus on implementing the runtime
side of marshal methods, making it possible to actually run
applications which use marshal methods.

Finish prototyping the infrastructure so that a MAUI app can be run
with LLVm Marshal Methods enabled.
  • Loading branch information
grendello committed Aug 19, 2022
1 parent 31bad9c commit 903ba37
Show file tree
Hide file tree
Showing 28 changed files with 2,477 additions and 54 deletions.
6 changes: 6 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs
Expand Up @@ -237,6 +237,9 @@ void Run (DirectoryAssemblyResolver res)
string managedKey = type.FullName.Replace ('/', '.');
string javaKey = JavaNativeTypeManager.ToJniName (type, cache).Replace ('/', '.');

#if ENABLE_MARSHAL_METHODS
Console.WriteLine ($"##G2: {type.FullName} -> {javaKey}");
#endif
acw_map.Write (type.GetPartialAssemblyQualifiedName (cache));
acw_map.Write (';');
acw_map.Write (javaKey);
Expand Down Expand Up @@ -384,6 +387,9 @@ bool CreateJavaSources (IEnumerable<TypeDefinition> javaTypes, TypeDefinitionCac

bool ok = true;
foreach (var t in javaTypes) {
#if ENABLE_MARSHAL_METHODS
Console.WriteLine ($"##G0: JCW for {t.FullName}");
#endif
if (t.IsInterface) {
// Interfaces are in typemap but they shouldn't have JCW generated for them
continue;
Expand Down
Expand Up @@ -16,6 +16,12 @@ class Arm32LlvmIrGenerator : LlvmIrGenerator
public override int PointerSize => 4;
protected override string Triple => "armv7-unknown-linux-android"; // NDK appends API level, we don't need that

static readonly LlvmFunctionAttributeSet commonAttributes = new LlvmFunctionAttributeSet {
new FramePointerFunctionAttribute ("all"),
new TargetCpuFunctionAttribute ("generic"),
new TargetFeaturesFunctionAttribute ("+armv7-a,+d32,+dsp,+fp64,+neon,+thumb-mode,+vfp2,+vfp2sp,+vfp3,+vfp3d16,+vfp3d16sp,+vfp3sp,-aes,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-sha2,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"),
};

public Arm32LlvmIrGenerator (AndroidTargetArch arch, StreamWriter output, string fileName)
: base (arch, output, fileName)
{}
Expand All @@ -25,5 +31,13 @@ protected override void AddModuleFlagsMetadata (List<LlvmIrMetadataItem> flagsFi
base.AddModuleFlagsMetadata (flagsFields);
flagsFields.Add (MetadataManager.AddNumbered (LlvmIrModuleMergeBehavior.Error, "min_enum_size", 4));
}

protected override void InitFunctionAttributes ()
{
base.InitFunctionAttributes ();

FunctionAttributes[FunctionAttributesXamarinAppInit].Add (commonAttributes);
FunctionAttributes[FunctionAttributesJniMethods].Add (commonAttributes);
}
}
}
Expand Up @@ -16,6 +16,12 @@ class Arm64LlvmIrGenerator : LlvmIrGenerator
public override int PointerSize => 8;
protected override string Triple => "aarch64-unknown-linux-android"; // NDK appends API level, we don't need that

static readonly LlvmFunctionAttributeSet commonAttributes = new LlvmFunctionAttributeSet {
new FramePointerFunctionAttribute ("non-leaf"),
new TargetCpuFunctionAttribute ("generic"),
new TargetFeaturesFunctionAttribute ("+neon,+outline-atomics"),
};

public Arm64LlvmIrGenerator (AndroidTargetArch arch, StreamWriter output, string fileName)
: base (arch, output, fileName)
{}
Expand All @@ -29,5 +35,13 @@ protected override void AddModuleFlagsMetadata (List<LlvmIrMetadataItem> flagsFi
flagsFields.Add (MetadataManager.AddNumbered (LlvmIrModuleMergeBehavior.Error, "sign-return-address-all", 0));
flagsFields.Add (MetadataManager.AddNumbered (LlvmIrModuleMergeBehavior.Error, "sign-return-address-with-bkey", 0));
}

protected override void InitFunctionAttributes ()
{
base.InitFunctionAttributes ();

FunctionAttributes[FunctionAttributesXamarinAppInit].Add (commonAttributes);
FunctionAttributes[FunctionAttributesJniMethods].Add (commonAttributes);
}
}
}

0 comments on commit 903ba37

Please sign in to comment.