Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw21 aot #1452

Draft
wants to merge 22 commits into
base: unity-main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ artifacts
/builds/
/tmp/
external/buildscripts/build.gen.*
.idea
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
url = git://github.com/mono/illinker-test-assets.git
[submodule "external/llvm-project"]
path = external/llvm-project
url = git://github.com/dotnet/llvm-project.git
url = git://github.com/ecuzzillo/llvm-project.git
branch = release/6.x
[submodule "external/bdwgc"]
path = external/bdwgc
Expand Down
18 changes: 18 additions & 0 deletions .yamato/AOT ClassLibs Windows x64.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: AOT ClassLibs Windows x64

agent:
type: Unity::VM
image: platform-foundation/windows-vs2019-il2cpp-bokken:stable
flavor: b1.xlarge

dependencies:
- .yamato/Build Classlibs OSX.yml
- .yamato/Build Windows x64.yml

commands:
- cd external\buildscripts\bcl_aot && ..\bee.exe

artifacts:
win64:
paths:
- incomingbuilds\aot-classlibs-win64\**
1 change: 1 addition & 0 deletions .yamato/Collate Builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- .yamato/Build Classlibs OSX.yml
- .yamato/Build Runtime OSX.yml
- .yamato/Build Windows x64.yml
- .yamato/AOT ClassLibs Windows x64.yml
- .yamato/Build Windows x86.yml

commands:
Expand Down
61 changes: 61 additions & 0 deletions external/buildscripts/bcl_aot/Build.bee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bee.Core;
using Bee.Core.Stevedore;
using Bee.NativeProgramSupport;
using Bee.Stevedore.Program;
using Bee.Tools;
using NiceIO;

class Build
{
static void Main()
{
NPath root = "../../..";
NPath monoDir = $@"{root}/incomingbuilds\win64\monodistribution\bin-x64";

NPath unityJitRelative = @"monodistribution\lib\mono\unityjit-win32";
NPath classLibDir = $@"{root}/incomingbuilds\classlibs\{unityJitRelative}";
NPath outputDir = $@"{root}/incomingbuilds\aot-classlibs-win64\{unityJitRelative}";

// TODO: this is the version yamato has right now. Do stevedore here.
NPath msvcRoot = @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30037";
NPath winSdkLib = @"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64";

var clangExecutableArtifact = new StevedoreArtifact(new RepoName("testing"), new ArtifactId("llvm-clang-win64/12.0.0_73aaea91fe24775a257cf553f0a6db3e8845890cc8c6e35eecba97ec45fc93b6.7z"));

var paths = new NPath[]
{
monoDir,
$@"{msvcRoot}\bin\Hostx64\x64",
clangExecutableArtifact.Path,
};

var _envVar = new Dictionary<string, string>
{
{"PATH", paths.Select(p => p.ResolveWithFileSystem().ToString(SlashMode.Native)).Append(Environment.GetEnvironmentVariable("PATH")).SeparateWith(";")},
{"MONO_PATH", classLibDir.ToString(SlashMode.Native)}
};

var libPaths = new[] {$@"{msvcRoot}\lib\x64", winSdkLib};
var libpathstr = libPaths.Select(p => $"/LIBPATH:\\\"{p.ResolveWithFileSystem().ToString(SlashMode.Native)}\\\"").SeparateWithSpace();

var classLibs = classLibDir.Files("*.dll");
foreach (var classLib in classLibs)
{
var nativeTarget = classLib.ChangeExtension($".{classLib.Extension}.dll");
NPath pdb = $"{nativeTarget}.pdb";
var outputFile = outputDir.Combine(nativeTarget.FileName);

Backend.Current.AddAction("MonoAot",
new[] {nativeTarget, pdb},
classLibs.Append(clangExecutableArtifact.Path).ToArray(),
monoDir.Combine("mono-bdwgc.exe").InQuotes(),
new[] {$"--aot=keep-temps,ld-flags=\"{libpathstr}\"", "--llvm", classLib.InQuotesResolved()},
environmentVariables: _envVar);
Backend.Current.SetupCopyFile(outputFile, nativeTarget);
Backend.Current.SetupCopyFile(outputDir.Combine(pdb.FileName), pdb);
}
}
}
24 changes: 24 additions & 0 deletions external/buildscripts/build_win_no_cygwin.pl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@
my $archNameForBuild = $arch32 ? 'Win32' : 'x64';
my $configDirName = $debug ? "Debug" : "Release";

if ($arch32)
{

} else {
print(">>> copying llvm binaries...\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/opt.exe", "$monoprefix/bin/opt.exe") or die ("failed copying opt.exe\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/llvm-as.exe", "$monoprefix/bin/llvm-as.exe") or die ("failed copying llvm-as.exe\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/llvm-dis.exe", "$monoprefix/bin/llvm-dis.exe") or die ("failed copying llvm-dis.exe\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/llvm-mc.exe", "$monoprefix/bin/llvm-mc.exe") or die ("failed copying llvm-mc.exe\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/llc.exe", "$monoprefix/bin/llc.exe") or die ("failed copying llc.exe\n");
}

print(">>> copying mono binaries...\n");

copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/mono-bdwgc.exe", "$monoprefix/bin/mono-bdwgc.exe") or die ("failed copying mono-bdwgc.exe\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/mono-2.0-bdwgc.dll", "$monoprefix/bin/mono-2.0-bdwgc.dll") or die ("failed copying mono-2.0-bdwgc.dll\n");
copy("$monoroot/msvc/build/boehm/$archNameForBuild/bin/$configDirName/mono-2.0-bdwgc.pdb", "$monoprefix/bin/mono-2.0-bdwgc.pdb") or die ("failed copying mono-2.0-bdwgc.pdb\n");
Expand Down Expand Up @@ -221,6 +235,16 @@
copy("$monoprefix/bin/MonoPosixHelper.dll", "$distDirArchBin/.") or die ("failed copying MonoPosixHelper.dll\n");
copy("$monoprefix/bin/MonoPosixHelper.pdb", "$distDirArchBin/.") or die ("failed copying MonoPosixHelper.pdb\n");

if ($arch32)
{
} else {
print(">>> copying llvm binaries to monodistribution...\n");
copy("$monoprefix/bin/opt.exe", "$distDirArchBin/.") or die ("failed copying opt.exe\n");
copy("$monoprefix/bin/llvm-as.exe", "$distDirArchBin/.") or die ("failed copying llvm-as.exe\n");
copy("$monoprefix/bin/llvm-dis.exe", "$distDirArchBin/.") or die ("failed copying llvm-dis.exe\n");
copy("$monoprefix/bin/llvm-mc.exe", "$distDirArchBin/.") or die ("failed copying llvm-mc.exe\n");
copy("$monoprefix/bin/llc.exe", "$distDirArchBin/.") or die ("failed copying llc.exe\n");
}

# Output version information
print(">>> Creating version file : $versionsOutputFile\n");
Expand Down
6 changes: 6 additions & 0 deletions mono/metadata/appdomain.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,9 @@ mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetupHa
data->domain = MONO_HANDLE_RAW (ad);
mono_gc_wbarrier_generic_nostore_internal (&data->domain);
data->friendly_name = g_strdup (friendly_name);
// hack to setup AOT domain earlier than Unity can with embedding API.
if (!strcmp(data->friendly_name, "Unity Child Domain"))
mono_aot_domain_set (data);

MONO_PROFILER_RAISE (domain_name, (data, data->friendly_name));

Expand Down Expand Up @@ -1604,6 +1607,7 @@ mono_domain_assembly_postload_search (MonoAssemblyLoadContext *alc, MonoAssembly
return assembly;
}

extern MonoDomainAssemblyLoadedFunc domain_image_loaded;
/*
* LOCKING: assumes assemblies_lock in the domain is already locked.
*/
Expand Down Expand Up @@ -1631,6 +1635,8 @@ add_assemblies_to_domain (MonoDomain *domain, MonoAssembly *ass, GHashTable *ht)
g_hash_table_add (ht, ass);
domain->domain_assemblies = g_slist_append (domain->domain_assemblies, ass);
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "Assembly %s[%p] added to domain %s, ref_count=%d", ass->aname.name, ass, domain->friendly_name, ass->ref_count);
if (domain_image_loaded)
domain_image_loaded (domain, ass);
}

#ifndef ENABLE_NETCORE
Expand Down
15 changes: 15 additions & 0 deletions mono/metadata/appdomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ typedef void (*MonoDomainFunc) (MonoDomain *domain, void* user_data);
typedef void (*MonoJitInfoFunc)(MonoDomain *domain, MonoMethod* method, MonoJitInfo* jinfo, void* user_data);
typedef void (*MonoUnityExceptionFunc) (MonoObject* exc);
typedef void (*MonoDomainAssemblyFunc) (MonoAssembly *assembly, void* user_data);
typedef void (*MonoDomainAssemblyLoadedFunc) (MonoDomain* domain, MonoAssembly* assembly);
typedef void (*MonoImageAOTModuleDestroyFunc) (MonoDomain* domain, MonoImage* image);
typedef void (*MonoAOTResetFunc) ();

MONO_API MonoDomain*
mono_init (const char *filename);
Expand Down Expand Up @@ -64,6 +67,9 @@ mono_runtime_is_shutting_down (void);
MONO_API const char*
mono_check_corlib_version (void);

void
mono_domain_install_aot_callbacks (MonoDomainAssemblyLoadedFunc image_loaded, MonoAOTResetFunc aot_reset, MonoImageAOTModuleDestroyFunc image_aot_module_destroy);

MONO_API MonoDomain *
mono_domain_create (void);

Expand All @@ -73,6 +79,15 @@ mono_domain_create_appdomain (char *friendly_name, char *configuration_file);
MONO_API MONO_RT_EXTERNAL_ONLY void
mono_domain_set_config (MonoDomain *domain, const char *base_dir, const char *config_file_name);

MONO_API MonoDomain*
mono_aot_domain_get (void);

MONO_API void
mono_aot_domain_set (MonoDomain* domain);

MONO_API void
mono_aot_domain_init_root_domain_set (mono_bool init_root_domain);

MONO_API MonoDomain *
mono_domain_get (void);

Expand Down
3 changes: 2 additions & 1 deletion mono/metadata/boehm-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,8 @@ mono_gc_is_moving (void)
gboolean
mono_gc_needs_write_barriers(void)
{
return GC_is_incremental_mode ();
/* FIXME: Jon can we detect we are doing AOT compile and force this? */
/*return GC_is_incremental_mode ();*/ return TRUE;
}

gboolean
Expand Down
2 changes: 2 additions & 0 deletions mono/metadata/domain-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ struct _MonoDomain {
MonoJitInfoTable *
volatile aot_modules;
GSList *jit_info_free_queue;
/* maps MonoImage to MonoAotModule */
GHashTable* aot_module_hash;
/* Used when loading assemblies */
gchar **search_path;
gchar *private_bin_path;
Expand Down
44 changes: 44 additions & 0 deletions mono/metadata/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ gboolean mono_dont_free_domains;
static MonoCoopMutex appdomains_mutex;

static MonoDomain *mono_root_domain = NULL;
static MonoDomain* mono_aot_domain = NULL;
static gboolean mono_aot_domain_init_root_domain = TRUE;

/* some statistics */
static int max_domain_code_size = 0;
Expand Down Expand Up @@ -452,6 +454,8 @@ mono_domain_create (void)
domain->memory_manager = (MonoMemoryManager *)mono_mem_manager_create_singleton (NULL, domain, TRUE);
#endif

domain->aot_module_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);

domain->lock_free_mp = lock_free_mempool_new ();
domain->env = mono_g_hash_table_new_type_internal ((GHashFunc)mono_string_hash_internal, (GCompareFunc)mono_string_equal_internal, MONO_HASH_KEY_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, domain, "Domain Environment Variable Table");
domain->domain_assemblies = NULL;
Expand Down Expand Up @@ -572,6 +576,11 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
domain = mono_domain_create ();
mono_root_domain = domain;

if (mono_aot_domain_init_root_domain)
{
mono_aot_domain = mono_root_domain;
}

SET_APPDOMAIN (domain);

#if defined(ENABLE_EXPERIMENT_null)
Expand Down Expand Up @@ -958,6 +967,24 @@ mono_get_root_domain (void)
return mono_root_domain;
}

MonoDomain*
mono_aot_domain_get (void)
{
return mono_aot_domain;
}

void
mono_aot_domain_set (MonoDomain* domain)
{
mono_aot_domain = domain;
}

void
mono_aot_domain_init_root_domain_set (gboolean init_root_domain)
{
mono_aot_domain_init_root_domain = init_root_domain;
}

/**
* mono_domain_get:
*
Expand Down Expand Up @@ -1141,6 +1168,18 @@ mono_domain_assembly_open_internal (MonoDomain *domain, MonoAssemblyLoadContext
return ass;
}

MonoDomainAssemblyLoadedFunc domain_image_loaded = NULL;
static MonoAOTResetFunc domain_unload_aot_reset = NULL;
static MonoImageAOTModuleDestroyFunc domain_unload_image_aot_module_destroy = NULL;

void
mono_domain_install_aot_callbacks (MonoDomainAssemblyLoadedFunc image_loaded, MonoAOTResetFunc aot_reset, MonoImageAOTModuleDestroyFunc image_aot_module_destroy)
{
domain_image_loaded = image_loaded;
domain_unload_aot_reset = aot_reset;
domain_unload_image_aot_module_destroy = image_aot_module_destroy;
}

/**
* mono_domain_free:
* \param domain the domain to release
Expand Down Expand Up @@ -1223,6 +1262,8 @@ mono_domain_free (MonoDomain *domain, gboolean force)
if (!ass->image || image_is_dynamic (ass->image))
continue;
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "Unloading domain %s[%p], assembly %s[%p], ref_count=%d", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
if (domain_unload_image_aot_module_destroy)
domain_unload_image_aot_module_destroy (domain, ass->image);
if (!mono_assembly_close_except_image_pools (ass))
tmp->data = NULL;
}
Expand All @@ -1235,6 +1276,9 @@ mono_domain_free (MonoDomain *domain, gboolean force)
g_slist_free (domain->domain_assemblies);
domain->domain_assemblies = NULL;

if (domain_unload_aot_reset)
domain_unload_aot_reset ();

/*
* Send this after the assemblies have been unloaded and the domain is still in a
* usable state.
Expand Down
6 changes: 3 additions & 3 deletions mono/metadata/jit-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ mono_jit_info_table_find_internal (MonoDomain *domain, gpointer addr, gboolean t
return ji;

/* Maybe its an AOT module */
if (try_aot && mono_get_root_domain () && mono_get_root_domain ()->aot_modules) {
table = (MonoJitInfoTable *)mono_get_hazardous_pointer ((gpointer volatile*)&mono_get_root_domain ()->aot_modules, hp, JIT_INFO_TABLE_HAZARD_INDEX);
if (try_aot && mono_aot_domain_get () && mono_aot_domain_get ()->aot_modules) {
table = (MonoJitInfoTable *)mono_get_hazardous_pointer ((gpointer volatile*)&mono_aot_domain_get ()->aot_modules, hp, JIT_INFO_TABLE_HAZARD_INDEX);
module_ji = jit_info_table_find (table, hp, (gint8*)addr);
if (module_ji)
ji = jit_info_find_in_aot_func (domain, module_ji->d.image, addr);
Expand Down Expand Up @@ -809,7 +809,7 @@ void
mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
{
MonoJitInfo *ji;
MonoDomain *domain = mono_get_root_domain ();
MonoDomain *domain = mono_aot_domain_get ();

g_assert (domain);
mono_domain_lock (domain);
Expand Down
2 changes: 1 addition & 1 deletion mono/metadata/metadata-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ struct _MonoImage {
MonoImage **files;
guint32 file_count;

MonoAotModule *aot_module;
//MonoAotModule *aot_module;

guint8 aotid[16];

Expand Down
Loading