Skip to content

Commit 39ce2f9

Browse files
authored
Bring in changes from PR #8478 (#8727)
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.
1 parent 8213348 commit 39ce2f9

13 files changed

+678
-190
lines changed

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfig.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ sealed class ApplicationConfig
4242
public uint bundled_assembly_name_width;
4343
public uint number_of_assembly_store_files;
4444
public uint number_of_dso_cache_entries;
45+
46+
[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
4547
public uint android_runtime_jnienv_class_token;
48+
49+
[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
4650
public uint jnienv_initialize_method_token;
51+
52+
[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
4753
public uint jnienv_registerjninatives_method_token;
4854
public uint jni_remapping_replacement_type_count;
4955
public uint jni_remapping_replacement_method_index_entry_count;
56+
57+
[NativeAssembler (NumberFormat = LLVMIR.LlvmIrVariableNumberFormat.Hexadecimal)]
5058
public uint mono_components_mask;
5159
public string android_package_name = String.Empty;
5260
}

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override string GetComment (object data, string fieldName)
2929
{
3030
var dso_entry = EnsureType<DSOCacheEntry> (data);
3131
if (String.Compare ("hash", fieldName, StringComparison.Ordinal) == 0) {
32-
return $" hash 0x{dso_entry.hash:x}, from name: {dso_entry.HashedName}";
32+
return $" from name: {dso_entry.HashedName}";
3333
}
3434

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

52-
[NativeAssembler (UsesDataProvider = true)]
52+
[NativeAssembler (UsesDataProvider = true, NumberFormat = LlvmIrVariableNumberFormat.Hexadecimal)]
5353
public ulong hash;
5454
public bool ignore;
5555

@@ -93,11 +93,11 @@ sealed class AssemblyStoreSingleAssemblyRuntimeData
9393
// src/monodroid/jni/xamarin-app.hh AssemblyStoreRuntimeData structure
9494
sealed class AssemblyStoreRuntimeData
9595
{
96-
[NativePointer]
96+
[NativePointer (IsNull = true)]
9797
public byte data_start;
9898
public uint assembly_count;
9999

100-
[NativePointer]
100+
[NativePointer (IsNull = true)]
101101
public AssemblyStoreAssemblyDescriptor assemblies;
102102
}
103103

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

302-
entry.hash = GetXxHash (entry.HashedName, is64Bit);
302+
entry.hash = MonoAndroidHelper.GetXxHash (entry.HashedName, is64Bit);
303303
}
304304

305305
cache.Sort ((StructureInstance<DSOCacheEntry> a, StructureInstance<DSOCacheEntry> b) => a.Instance.hash.CompareTo (b.Instance.hash));

src/Xamarin.Android.Build.Tasks/Utilities/AssemblyCompression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static CompressionResult Compress (AssemblyData data, string outputDirect
7878
}
7979

8080
destBytes = bytePool.Rent (LZ4Codec.MaximumOutputSize (sourceBytes.Length));
81-
int encodedLength = LZ4Codec.Encode (sourceBytes, 0, checked((int)fi.Length), destBytes, 0, destBytes.Length, LZ4Level.L09_HC);
81+
int encodedLength = LZ4Codec.Encode (sourceBytes, 0, checked((int)fi.Length), destBytes, 0, destBytes.Length, LZ4Level.L12_MAX);
8282
if (encodedLength < 0)
8383
return CompressionResult.EncodingFailed;
8484

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrComposer.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.IO.Hashing;
4-
using System.Text;
53

64
using Microsoft.Build.Utilities;
75

@@ -41,18 +39,13 @@ public void Generate (LlvmIrModule module, AndroidTargetArch arch, StreamWriter
4139
LlvmIrGenerator generator = LlvmIrGenerator.Create (arch, fileName);
4240
generator.Generate (output, module);
4341
output.Flush ();
44-
}
4542

46-
public static ulong GetXxHash (string str, bool is64Bit)
47-
{
48-
byte[] stringBytes = Encoding.UTF8.GetBytes (str);
49-
if (is64Bit) {
50-
return XxHash64.HashToUInt64 (stringBytes);
51-
}
52-
53-
return (ulong)XxHash32.HashToUInt32 (stringBytes);
43+
CleanupAfterGeneration (arch);
5444
}
5545

46+
protected virtual void CleanupAfterGeneration (AndroidTargetArch arch)
47+
{}
48+
5649
protected LlvmIrGlobalVariable EnsureGlobalVariable (LlvmIrVariable variable)
5750
{
5851
var gv = variable as LlvmIrGlobalVariable;

0 commit comments

Comments
 (0)