Skip to content

Commit

Permalink
Bring in changes from PR #8478 (#8727)
Browse files Browse the repository at this point in the history
Context: #8478

Changes in this commit were originally made in #8478 but aren't
strictly tied to that PR.  To make reviews of #8478 easier, a portion
of changes were extracted and placed here.

Modify the LLVM IR generator to output more readable and compact
code:

  * Use hexadecimal integer notation wherever it makes sense (e.g.
     hashes, managed token IDs)
  * Use boolean literals `true` and `false` instead of previously used
     `1` and `0`, respectively
  * Improve formatting of arrays
  * Introduce arrays which can composed from various "sections",
     each with its own comment and with data provided during the
     generation process, instead of during the preparation phase.

Additional changes:

Extract portions of `MonoAndroidHelper.cs` and place them in a separate
file, `MonoAndroidHelper.Basic.cs` .   Code placed in there provides conversion
between RID, and various forms of Android ABI names as well as `xxHash` functions
used throughout the repository, as well as methods to handle paths of ZIP archive
entries in a uniform way.  #8478 uses these extracted methods in tests, in standalone
utilities in addition to build tasks.

Use maximum LZ4 compression level when compressing assemblies to be placed in
the application APK/AAB archives.
  • Loading branch information
grendello authored Feb 21, 2024
1 parent 8213348 commit 39ce2f9
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ sealed class ApplicationConfig
public uint bundled_assembly_name_width;
public uint number_of_assembly_store_files;
public uint number_of_dso_cache_entries;

[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
public uint android_runtime_jnienv_class_token;

[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
public uint jnienv_initialize_method_token;

[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
public uint jnienv_registerjninatives_method_token;
public uint jni_remapping_replacement_type_count;
public uint jni_remapping_replacement_method_index_entry_count;

[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
public uint mono_components_mask;
public string android_package_name = String.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string GetComment (object data, string fieldName)
{
var dso_entry = EnsureType<DSOCacheEntry> (data);
if (String.Compare ("hash", fieldName, StringComparison.Ordinal) == 0) {
return $" hash 0x{dso_entry.hash:x}, from name: {dso_entry.HashedName}";
return $" from name: {dso_entry.HashedName}";
}

if (String.Compare ("name", fieldName, StringComparison.Ordinal) == 0) {
Expand All @@ -49,7 +49,7 @@ sealed class DSOCacheEntry
[NativeAssembler (Ignore = true)]
public string HashedName;

[NativeAssembler (UsesDataProvider = true)]
[NativeAssembler (UsesDataProvider = true, NumberFormat = LlvmIrVariableNumberFormat.Hexadecimal)]
public ulong hash;
public bool ignore;

Expand Down Expand Up @@ -93,11 +93,11 @@ sealed class AssemblyStoreSingleAssemblyRuntimeData
// src/monodroid/jni/xamarin-app.hh AssemblyStoreRuntimeData structure
sealed class AssemblyStoreRuntimeData
{
[NativePointer]
[NativePointer (IsNull = true)]
public byte data_start;
public uint assembly_count;

[NativePointer]
[NativePointer (IsNull = true)]
public AssemblyStoreAssemblyDescriptor assemblies;
}

Expand Down Expand Up @@ -299,7 +299,7 @@ void HashAndSortDSOCache (LlvmIrVariable variable, LlvmIrModuleTarget target, ob
throw new InvalidOperationException ($"Internal error: DSO cache entry has unexpected type {instance.Obj.GetType ()}");
}

entry.hash = GetXxHash (entry.HashedName, is64Bit);
entry.hash = MonoAndroidHelper.GetXxHash (entry.HashedName, is64Bit);
}

cache.Sort ((StructureInstance<DSOCacheEntry> a, StructureInstance<DSOCacheEntry> b) => a.Instance.hash.CompareTo (b.Instance.hash));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static CompressionResult Compress (AssemblyData data, string outputDirect
}

destBytes = bytePool.Rent (LZ4Codec.MaximumOutputSize (sourceBytes.Length));
int encodedLength = LZ4Codec.Encode (sourceBytes, 0, checked((int)fi.Length), destBytes, 0, destBytes.Length, LZ4Level.L09_HC);
int encodedLength = LZ4Codec.Encode (sourceBytes, 0, checked((int)fi.Length), destBytes, 0, destBytes.Length, LZ4Level.L12_MAX);
if (encodedLength < 0)
return CompressionResult.EncodingFailed;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.IO.Hashing;
using System.Text;

using Microsoft.Build.Utilities;

Expand Down Expand Up @@ -41,18 +39,13 @@ public void Generate (LlvmIrModule module, AndroidTargetArch arch, StreamWriter
LlvmIrGenerator generator = LlvmIrGenerator.Create (arch, fileName);
generator.Generate (output, module);
output.Flush ();
}

public static ulong GetXxHash (string str, bool is64Bit)
{
byte[] stringBytes = Encoding.UTF8.GetBytes (str);
if (is64Bit) {
return XxHash64.HashToUInt64 (stringBytes);
}

return (ulong)XxHash32.HashToUInt32 (stringBytes);
CleanupAfterGeneration (arch);
}

protected virtual void CleanupAfterGeneration (AndroidTargetArch arch)
{}

protected LlvmIrGlobalVariable EnsureGlobalVariable (LlvmIrVariable variable)
{
var gv = variable as LlvmIrGlobalVariable;
Expand Down
Loading

0 comments on commit 39ce2f9

Please sign in to comment.