From 8c55dec945afae0c1dae404938e0ea7262925f30 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 3 Jun 2020 01:43:12 +0200 Subject: [PATCH 01/26] Crossgen2 ARM64 runs --- eng/pipelines/coreclr/crossgen2.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eng/pipelines/coreclr/crossgen2.yml b/eng/pipelines/coreclr/crossgen2.yml index e20ebb4152b999..dd462e679f16c6 100644 --- a/eng/pipelines/coreclr/crossgen2.yml +++ b/eng/pipelines/coreclr/crossgen2.yml @@ -22,6 +22,7 @@ jobs: buildConfig: checked platforms: - Linux_x64 + - Linux_arm64 - OSX_x64 - Windows_NT_x64 - CoreClrTestBuildHost # Either OSX_x64 or Linux_x64 @@ -45,8 +46,10 @@ jobs: buildConfig: checked platforms: - Linux_x64 + - Linux_arm64 - OSX_x64 - Windows_NT_x64 + - Windows_NT_arm64 jobParameters: testGroup: innerloop readyToRun: true From b11436df9c24aa8be28e00384e70b47dfe94d8d2 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 3 Jun 2020 11:05:32 +0200 Subject: [PATCH 02/26] Enable framework crossgenning on ARM64 --- src/coreclr/build-test.cmd | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index ceeca835dc7b6c..3beecc5fde3abb 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -557,15 +557,11 @@ if defined __DoCrossgen ( if defined __DoCrossgen2 ( set __CrossgenArg="/p:Crossgen2=true" - if "%__BuildArch%" == "x64" ( - echo %__MsgPrefix%Running crossgen2 on framework assemblies in CORE_ROOT: %CORE_ROOT% - call :PrecompileFX - if ERRORLEVEL 1 ( - echo %__ErrMsgPrefix%%__MsgPrefix%Error: crossgen2 precompilation of framework assemblies failed - exit /b 1 - ) - ) else ( - echo "%__MsgPrefix%Crossgen2 only supported on x64, for now" + echo %__MsgPrefix%Running crossgen2 on framework assemblies in CORE_ROOT: %CORE_ROOT% + call :PrecompileFX + if ERRORLEVEL 1 ( + echo %__ErrMsgPrefix%%__MsgPrefix%Error: crossgen2 precompilation of framework assemblies failed + exit /b 1 ) ) From 532f771b9670df96a487e8068b34c44cde032153 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 3 Jun 2020 11:13:15 +0200 Subject: [PATCH 03/26] Fix cross-compilation native bin folder for arm/arm64 --- .../src/tools/crossgen2/crossgen2/crossgen2.csproj | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index b3c21b883c576c..b550d55cb1370d 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -50,14 +50,20 @@ .dll + + + x64\ + x86\ + + - - Date: Wed, 3 Jun 2020 12:19:19 +0200 Subject: [PATCH 04/26] Revert incorrect attempt at path fix --- .../src/tools/crossgen2/crossgen2/crossgen2.csproj | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index b550d55cb1370d..b3c21b883c576c 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -50,20 +50,14 @@ .dll - - - x64\ - x86\ - - - - Date: Wed, 3 Jun 2020 16:23:40 +0200 Subject: [PATCH 05/26] Add jitinterface to crosscomponents.cmake; crossgen2 cross-package --- src/coreclr/crosscomponents.cmake | 1 + .../src/tools/crossgen2/crossgen2/crossgen2.csproj | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/coreclr/crosscomponents.cmake b/src/coreclr/crosscomponents.cmake index f8ea417471ca6e..5bb0b5bef5c738 100644 --- a/src/coreclr/crosscomponents.cmake +++ b/src/coreclr/crosscomponents.cmake @@ -8,6 +8,7 @@ if (CLR_CMAKE_HOST_OS STREQUAL CLR_CMAKE_TARGET_OS) set (CLR_CROSS_COMPONENTS_LIST crossgen clrjit + jitinterface ) endif() diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index b3c21b883c576c..b550d55cb1370d 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -50,14 +50,20 @@ .dll + + + x64\ + x86\ + + - - Date: Thu, 4 Jun 2020 01:50:24 +0200 Subject: [PATCH 06/26] Add missing targetarch option to Crossgen2 framework compilation --- src/coreclr/build-test.cmd | 2 +- src/coreclr/build-test.sh | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index 3beecc5fde3abb..d06e1e2e74bb2b 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -706,7 +706,7 @@ if defined __DoCrossgen ( echo !__CrossgenCmd! !__CrossgenCmd! ) else ( - set __CrossgenCmd=!__CrossgenExe! -r:"!CORE_ROOT!\System.*.dll" -r:"!CORE_ROOT!\Microsoft.*.dll" -r:"!CORE_ROOT!\mscorlib.dll" -r:"!CORE_ROOT!\netstandard.dll" -O --inputbubble --out:!__CrossgenOutputFile! !AssemblyPath! + set __CrossgenCmd=!__CrossgenExe! -r:"!CORE_ROOT!\System.*.dll" -r:"!CORE_ROOT!\Microsoft.*.dll" -r:"!CORE_ROOT!\mscorlib.dll" -r:"!CORE_ROOT!\netstandard.dll" -O --inputbubble --out:!__CrossgenOutputFile! !AssemblyPath! --targetarch %__BuildArch% echo !__CrossgenCmd! call !__CrossgenCmd! ) diff --git a/src/coreclr/build-test.sh b/src/coreclr/build-test.sh index 9ad2fde6224152..e907f586ba34c2 100755 --- a/src/coreclr/build-test.sh +++ b/src/coreclr/build-test.sh @@ -185,6 +185,7 @@ precompile_coreroot_fx() compositeCommandLine+=" --composite" compositeCommandLine+=" -O" compositeCommandLine+=" --out:$outputDir/framework-r2r.dll" + compositeCommandLine+=" --targetarch ${__BuildArch}" declare -a failedAssemblies filesToPrecompile=$(find -L "$overlayDir" -maxdepth 1 -iname Microsoft.\*.dll -o -iname System.\*.dll -o -iname netstandard.dll -o -iname mscorlib.dll -type f) @@ -206,7 +207,7 @@ precompile_coreroot_fx() fi if [[ "$__DoCrossgen2" != 0 ]]; then - commandLine="${__DotNetCli} $overlayDir/crossgen2/crossgen2.dll $crossgen2References -O --inputbubble --out $outputDir/$(basename $filename) $filename" + commandLine="${__DotNetCli} $overlayDir/crossgen2/crossgen2.dll $crossgen2References -O --inputbubble --out $outputDir/$(basename $filename) $filename --targetarch ${__BuildArch}" fi echo Precompiling "$filename" From eb86271520c0481fd9c32843d83b49b13aba15e4 Mon Sep 17 00:00:00 2001 From: Tomas Date: Thu, 4 Jun 2020 16:22:32 +0200 Subject: [PATCH 07/26] Initial support for Crossgen2 multitargeting This change introduces initial provisions for dynamically loading native Crossgen2 components (jitinterface, clrjit) based on the hosting / targeting OS and architecture. In general, jitinterface depends on the hosting OS and architecture, clrjit depends on both host and target characteristics. As of this commit, the host environment is represented by the first subfolder level beneath the "crossgen2" executable; its currently supported name combinations are host-win-x64, host-win-arm64, host-lnx-x64, host-lnx-arm64. Targeting environment is represented by the second-level subfolder under the hosting folder; its currently supported name combinations are target-win-x64, target-win-arm64, target-lnx-x64, target-lnx-arm64. Thanks Tomas --- src/coreclr/build-test.cmd | 1 + .../tools/Common/JitInterface/CorInfoImpl.cs | 131 ++++++++++++++++-- .../Common/JitInterface/JitConfigProvider.cs | 2 - .../JitInterface/CorInfoImpl.ReadyToRun.cs | 2 +- .../crossgen2/crossgen2/crossgen2.csproj | 57 +++++--- 5 files changed, 166 insertions(+), 27 deletions(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index d06e1e2e74bb2b..b77610fb2e0663 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -642,6 +642,7 @@ if defined __CompositeBuildMode ( mkdir !__CompositeOutputDir! echo --composite>>!__CompositeResponseFile! echo -O>>!__CompositeResponseFile! + echo --targetarch:%__BuildArch%>>!__CompositeResponseFile! echo --out^:%__CompositeOutputDir%\framework-r2r.dll>>!__CompositeResponseFile! ) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index fef6e6e5eb5ed7..d7d1091b5397c9 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -58,16 +58,129 @@ private enum ImageFileMachine private IntPtr _unmanagedCallbacks; // array of pointers to JIT-EE interface callbacks private Object _keepAlive; // Keeps delegates for the callbacks alive + private IntPtr _jitLibrary; + private ExceptionDispatchInfo _lastException; - [DllImport(JitLibrary, CallingConvention = CallingConvention.StdCall)] // stdcall in CoreCLR! - private extern static IntPtr jitStartup(IntPtr host); + private delegate IntPtr JitStartupDelegate(IntPtr host); + private delegate IntPtr GetJitDelegate(); + private delegate IntPtr GetJitHostDelegate(IntPtr configProvider); - [DllImport(JitLibrary, CallingConvention = CallingConvention.StdCall)] - private extern static IntPtr getJit(); + private void InitializeJitLibrary(TargetOS targetOS, TargetArchitecture targetArchitecture) + { + if (_jitLibrary == IntPtr.Zero) + { + string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); + string jitTargetPath = Path.Combine(crossgenPath, GetHostSpec(), GetTargetSpec(targetOS, targetArchitecture)); + string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + _jitLibrary = NativeLibrary.Load(jitLibraryName); + } + } - [DllImport(JitSupportLibrary)] - private extern static IntPtr GetJitHost(IntPtr configProvider); + private IntPtr jitStartup(IntPtr host) + { + IntPtr jitStartupExport = NativeLibrary.GetExport(_jitLibrary, "jitStartup"); + JitStartupDelegate jitStartupDelegate = Marshal.GetDelegateForFunctionPointer(jitStartupExport); + return jitStartupDelegate(host); + } + + private IntPtr getJit() + { + IntPtr getJitExport = NativeLibrary.GetExport(_jitLibrary, "getJit"); + GetJitDelegate getJitDelegate = Marshal.GetDelegateForFunctionPointer(getJitExport); + return getJitDelegate(); + } + + private IntPtr GetJitHost(IntPtr configProvider) + { + string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); + string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); + string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); + IntPtr libraryHandle = NativeLibrary.Load(jitHostLibraryName); + IntPtr getJitHostExport = NativeLibrary.GetExport(libraryHandle, "GetJitHost"); + GetJitHostDelegate getJitHostDelegate = Marshal.GetDelegateForFunctionPointer(getJitHostExport); + return getJitHostDelegate(configProvider); + } + private static string GetLibraryPrefix() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "lib"; + } + + private static string GetLibraryExtension() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return ".dll"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return ".so"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return ".dylib"; + } + else + { + throw new NotImplementedException("GetLibraryExtension"); + } + } + + private static string GetHostSpec() + { + string hostOsComponent; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + hostOsComponent = "win"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + hostOsComponent = "lnx"; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + hostOsComponent = "osx"; + } + else + { + throw new NotImplementedException("GetJitHost"); + } + + string hostArchComponent; + switch (RuntimeInformation.OSArchitecture) + { + case Architecture.X64: + hostArchComponent = "x64"; + break; + + case Architecture.Arm64: + hostArchComponent = "arm64"; + break; + + default: + throw new NotImplementedException(RuntimeInformation.OSArchitecture.ToString()); + } + + return "host-" + hostOsComponent + '-' + hostArchComponent; + } + + private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture targetArchitecture) + { + string targetOSComponent = targetOS switch + { + TargetOS.Windows => "win", + TargetOS.Linux => "lnx", + TargetOS.OSX => "osx", + _ => throw new NotImplementedException(targetOS.ToString()) + }; + string targetArchComponent = targetArchitecture switch + { + TargetArchitecture.X64 => "x64", + TargetArchitecture.ARM64 => "arm64", + _ => throw new NotImplementedException(targetArchitecture.ToString()) + }; + return "target-" + targetOSComponent + '-' + targetArchComponent; + } // // Per-method initialization and state @@ -110,13 +223,15 @@ private IntPtr AllocException(Exception ex) [DllImport(JitSupportLibrary)] private extern static char* GetExceptionMessage(IntPtr obj); - public static void Startup() + public void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) { + InitializeJitLibrary(targetOS, targetArchitecture); jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } - public CorInfoImpl() + public CorInfoImpl(TargetOS targetOS, TargetArchitecture targetArchitecture) { + Startup(targetOS, targetArchitecture); _jit = getJit(); if (_jit == IntPtr.Zero) { diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index 78fbd064bf7252..b2afb3f1bdc45e 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -54,8 +54,6 @@ public static void Initialize(IEnumerable jitFlags, IEnumerable - - lib - .so - .dylib - - - - .dll - - - - x64\ - x86\ + win + lnx + osx + host-$(HostOSComponent)-$(TargetArchitecture) + + + x64 + host-$(HostOSComponent)-$(CrossHostArch) + + win + lnx + osx + target-$(TargetOSComponent)-$(TargetArchitecture) + + lib + + .dll + .so + .dylib - + + + + + + + + + + + From f2b0ad76614919052616945d33d5aceed3db2837 Mon Sep 17 00:00:00 2001 From: Tomas Date: Thu, 4 Jun 2020 17:36:11 +0200 Subject: [PATCH 08/26] Fix one overlooked NativeLibraryPrefix --- src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index 559e7dfceeee34..31d23ded14c12e 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -77,7 +77,7 @@ - Date: Thu, 4 Jun 2020 22:11:27 +0200 Subject: [PATCH 09/26] Fix race condition in clrjit loading --- eng/pipelines/coreclr/crossgen2.yml | 2 +- .../tools/Common/JitInterface/CorInfoImpl.cs | 122 +++++++++--------- 2 files changed, 64 insertions(+), 60 deletions(-) diff --git a/eng/pipelines/coreclr/crossgen2.yml b/eng/pipelines/coreclr/crossgen2.yml index dd462e679f16c6..da4efbab7249bd 100644 --- a/eng/pipelines/coreclr/crossgen2.yml +++ b/eng/pipelines/coreclr/crossgen2.yml @@ -25,7 +25,7 @@ jobs: - Linux_arm64 - OSX_x64 - Windows_NT_x64 - - CoreClrTestBuildHost # Either OSX_x64 or Linux_x64 + - Windows_NT_arm64 jobParameters: testGroup: innerloop diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index d7d1091b5397c9..3d0942f23923f6 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -58,49 +58,72 @@ private enum ImageFileMachine private IntPtr _unmanagedCallbacks; // array of pointers to JIT-EE interface callbacks private Object _keepAlive; // Keeps delegates for the callbacks alive - private IntPtr _jitLibrary; - - private ExceptionDispatchInfo _lastException; - private delegate IntPtr JitStartupDelegate(IntPtr host); private delegate IntPtr GetJitDelegate(); + private delegate IntPtr GetJitHostDelegate(IntPtr configProvider); + private delegate CorJitResult JitCompileMethodDelegate(out IntPtr exception, + IntPtr jit, IntPtr thisHandle, IntPtr callbacks, + ref CORINFO_METHOD_INFO info, uint flags, out IntPtr nativeEntry, out uint codeSize); + private delegate IntPtr AllocExceptionDelegate([MarshalAs(UnmanagedType.LPWStr)]string message, int messageLength); + private delegate void FreeExceptionDelegate(IntPtr obj); + private delegate char* GetExceptionMessageDelegate(IntPtr obj); - private void InitializeJitLibrary(TargetOS targetOS, TargetArchitecture targetArchitecture) - { - if (_jitLibrary == IntPtr.Zero) - { - string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitTargetPath = Path.Combine(crossgenPath, GetHostSpec(), GetTargetSpec(targetOS, targetArchitecture)); - string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); - _jitLibrary = NativeLibrary.Load(jitLibraryName); - } - } - private IntPtr jitStartup(IntPtr host) - { - IntPtr jitStartupExport = NativeLibrary.GetExport(_jitLibrary, "jitStartup"); - JitStartupDelegate jitStartupDelegate = Marshal.GetDelegateForFunctionPointer(jitStartupExport); - return jitStartupDelegate(host); - } + private static object s_jitLibraryLock = new object(); + private static IntPtr s_jitLibrary; + private static TargetOS s_jitLibraryTargetOS; + private static TargetArchitecture s_jitLibraryTargetArchitecture; + private static IntPtr s_jitHostLibrary; - private IntPtr getJit() - { - IntPtr getJitExport = NativeLibrary.GetExport(_jitLibrary, "getJit"); - GetJitDelegate getJitDelegate = Marshal.GetDelegateForFunctionPointer(getJitExport); - return getJitDelegate(); - } + private static JitStartupDelegate s_jitStartup; + private static GetJitDelegate s_getJit; + + private static GetJitHostDelegate s_getJitHost; + private static JitCompileMethodDelegate s_jitCompileMethod; + private static AllocExceptionDelegate s_allocException; + private static FreeExceptionDelegate s_freeException; + private static GetExceptionMessageDelegate s_getExceptionMessage; + + private ExceptionDispatchInfo _lastException; - private IntPtr GetJitHost(IntPtr configProvider) + private static void InitializeJitLibrary(TargetOS targetOS, TargetArchitecture targetArchitecture) { - string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); - string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); - IntPtr libraryHandle = NativeLibrary.Load(jitHostLibraryName); - IntPtr getJitHostExport = NativeLibrary.GetExport(libraryHandle, "GetJitHost"); - GetJitHostDelegate getJitHostDelegate = Marshal.GetDelegateForFunctionPointer(getJitHostExport); - return getJitHostDelegate(configProvider); + if (s_jitLibrary == IntPtr.Zero) + { + lock (s_jitLibraryLock) + { + if (s_jitLibrary == IntPtr.Zero) + { + string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); + string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); + string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); + + s_jitHostLibrary = NativeLibrary.Load(jitHostLibraryName); + s_getJitHost = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetJitHost")); + s_jitCompileMethod = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "JitCompileMethod")); + s_allocException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "AllocException")); + s_freeException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "FreeException")); + s_getExceptionMessage = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetExceptionMessage")); + + string jitTargetPath = Path.Combine(jitHostPath, GetTargetSpec(targetOS, targetArchitecture)); + string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + + s_jitLibrary = NativeLibrary.Load(jitLibraryName); + s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); + s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); + + s_jitLibraryTargetOS = targetOS; + s_jitLibraryTargetArchitecture = targetArchitecture; + s_jitStartup(s_getJitHost(JitConfigProvider.Instance.UnmanagedInstance)); + } + // Currently we don't support loading two different versions of clrjit from the same Crossgen2 process + Debug.Assert(targetOS == s_jitLibraryTargetOS); + Debug.Assert(targetArchitecture == s_jitLibraryTargetArchitecture); + } + } } + private static string GetLibraryPrefix() { return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "lib"; @@ -192,23 +215,12 @@ private static CorInfoImpl GetThis(IntPtr thisHandle) return _this; } - [DllImport(JitSupportLibrary)] - private extern static CorJitResult JitCompileMethod(out IntPtr exception, - IntPtr jit, IntPtr thisHandle, IntPtr callbacks, - ref CORINFO_METHOD_INFO info, uint flags, out IntPtr nativeEntry, out uint codeSize); - - [DllImport(JitSupportLibrary)] - private extern static uint GetMaxIntrinsicSIMDVectorLength(IntPtr jit, CORJIT_FLAGS* flags); - - [DllImport(JitSupportLibrary)] - private extern static IntPtr AllocException([MarshalAs(UnmanagedType.LPWStr)]string message, int messageLength); - private IntPtr AllocException(Exception ex) { _lastException = ExceptionDispatchInfo.Capture(ex); string exString = ex.ToString(); - IntPtr nativeException = AllocException(exString, exString.Length); + IntPtr nativeException = s_allocException(exString, exString.Length); if (_nativeExceptions == null) { _nativeExceptions = new List(); @@ -217,22 +229,14 @@ private IntPtr AllocException(Exception ex) return nativeException; } - [DllImport(JitSupportLibrary)] - private extern static void FreeException(IntPtr obj); - - [DllImport(JitSupportLibrary)] - private extern static char* GetExceptionMessage(IntPtr obj); - public void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) { - InitializeJitLibrary(targetOS, targetArchitecture); - jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } public CorInfoImpl(TargetOS targetOS, TargetArchitecture targetArchitecture) { - Startup(targetOS, targetArchitecture); - _jit = getJit(); + InitializeJitLibrary(targetOS, targetArchitecture); + _jit = s_getJit(); if (_jit == IntPtr.Zero) { throw new IOException("Failed to initialize JIT"); @@ -277,7 +281,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method IntPtr exception; IntPtr nativeEntry; uint codeSize; - var result = JitCompileMethod(out exception, + var result = s_jitCompileMethod(out exception, _jit, (IntPtr)Unsafe.AsPointer(ref _this), _unmanagedCallbacks, ref methodInfo, (uint)CorJitFlag.CORJIT_FLAG_CALL_GETJITFLAGS, out nativeEntry, out codeSize); if (exception != IntPtr.Zero) @@ -311,7 +315,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method } // This is a failure we don't know much about. - char* szMessage = GetExceptionMessage(exception); + char* szMessage = s_getExceptionMessage(exception); string message = szMessage != null ? new string(szMessage) : "JIT Exception"; throw new Exception(message); } @@ -470,7 +474,7 @@ private void CompileMethodCleanup() if (_nativeExceptions != null) { foreach (IntPtr ex in _nativeExceptions) - FreeException(ex); + s_freeException(ex); _nativeExceptions = null; } From 28493a17914f9f25a59471af1e80ef917681a01d Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 5 Jun 2020 00:54:48 +0200 Subject: [PATCH 10/26] Fix target architecture and callin convention --- src/coreclr/build-test.cmd | 1 + src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs | 3 +++ src/coreclr/tests/src/CLRTest.CrossGen.targets | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index b77610fb2e0663..1e40584f0a3835 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -640,6 +640,7 @@ set __CompositeResponseFile=%__CompositeOutputDir%\framework-r2r.dll.rsp if defined __CompositeBuildMode ( mkdir !__CompositeOutputDir! + del /Y !__CompositeResponseFile! echo --composite>>!__CompositeResponseFile! echo -O>>!__CompositeResponseFile! echo --targetarch:%__BuildArch%>>!__CompositeResponseFile! diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index 3d0942f23923f6..a9bbe0b2e68e5a 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -58,7 +58,10 @@ private enum ImageFileMachine private IntPtr _unmanagedCallbacks; // array of pointers to JIT-EE interface callbacks private Object _keepAlive; // Keeps delegates for the callbacks alive + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate IntPtr JitStartupDelegate(IntPtr host); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate IntPtr GetJitDelegate(); private delegate IntPtr GetJitHostDelegate(IntPtr configProvider); diff --git a/src/coreclr/tests/src/CLRTest.CrossGen.targets b/src/coreclr/tests/src/CLRTest.CrossGen.targets index 0dcc732202f752..f9c3d9b7084d7d 100644 --- a/src/coreclr/tests/src/CLRTest.CrossGen.targets +++ b/src/coreclr/tests/src/CLRTest.CrossGen.targets @@ -106,7 +106,7 @@ if [ ! -z ${RunCrossGen2+x} ]%3B then echo -r:$CORE_ROOT/System.*.dll>>$__ResponseFile echo -r:$CORE_ROOT/Microsoft.*.dll>>$__ResponseFile echo -r:$CORE_ROOT/mscorlib.dll>>$__ResponseFile - echo --targetarch=x64>>$__ResponseFile + echo --targetarch=$TargetArchitecture>>$__ResponseFile echo -O>>$__ResponseFile echo "Response file: $__ResponseFile" @@ -204,7 +204,7 @@ if defined RunCrossGen2 ( ) echo -o:!__OutputFile!>>!__ResponseFile! - echo --targetarch:x64>>!__ResponseFile! + echo --targetarch:!TargetArchitecture!>>!__ResponseFile! echo -O>>!__ResponseFile! echo -r:!CORE_ROOT!\System.*.dll>>!__ResponseFile! echo -r:!CORE_ROOT!\Microsoft.*.dll>>!__ResponseFile! From 45aefc210159b70fc09cff47397a44b5fb01f66c Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 5 Jun 2020 02:24:27 +0200 Subject: [PATCH 11/26] Fix bug in targetarch specification in CLRTest.Crossgen.targets --- src/coreclr/tests/src/CLRTest.CrossGen.targets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tests/src/CLRTest.CrossGen.targets b/src/coreclr/tests/src/CLRTest.CrossGen.targets index f9c3d9b7084d7d..e5b1da870e4f20 100644 --- a/src/coreclr/tests/src/CLRTest.CrossGen.targets +++ b/src/coreclr/tests/src/CLRTest.CrossGen.targets @@ -106,7 +106,7 @@ if [ ! -z ${RunCrossGen2+x} ]%3B then echo -r:$CORE_ROOT/System.*.dll>>$__ResponseFile echo -r:$CORE_ROOT/Microsoft.*.dll>>$__ResponseFile echo -r:$CORE_ROOT/mscorlib.dll>>$__ResponseFile - echo --targetarch=$TargetArchitecture>>$__ResponseFile + echo --targetarch:$(TargetArchitecture)>>$__ResponseFile echo -O>>$__ResponseFile echo "Response file: $__ResponseFile" @@ -204,7 +204,7 @@ if defined RunCrossGen2 ( ) echo -o:!__OutputFile!>>!__ResponseFile! - echo --targetarch:!TargetArchitecture!>>!__ResponseFile! + echo --targetarch:$(TargetArchitecture)>>!__ResponseFile! echo -O>>!__ResponseFile! echo -r:!CORE_ROOT!\System.*.dll>>!__ResponseFile! echo -r:!CORE_ROOT!\Microsoft.*.dll>>!__ResponseFile! From 1529908bad0ab6df8a73a4c01cb71b43ec1c3004 Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 5 Jun 2020 02:27:30 +0200 Subject: [PATCH 12/26] Fix typo in 'del' command syntax --- src/coreclr/build-test.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index 1e40584f0a3835..1ebf8ca0c3ab16 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -640,7 +640,7 @@ set __CompositeResponseFile=%__CompositeOutputDir%\framework-r2r.dll.rsp if defined __CompositeBuildMode ( mkdir !__CompositeOutputDir! - del /Y !__CompositeResponseFile! + del /Q !__CompositeResponseFile! echo --composite>>!__CompositeResponseFile! echo -O>>!__CompositeResponseFile! echo --targetarch:%__BuildArch%>>!__CompositeResponseFile! From eec60a94db4eb52e0f83dca55dc724c4c07b345e Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 5 Jun 2020 16:00:41 +0200 Subject: [PATCH 13/26] Initialize JIT library and interface on the main thread --- .../tools/Common/JitInterface/CorInfoImpl.cs | 68 +++++++------------ .../Compiler/ReadyToRunCodegenCompilation.cs | 2 + .../JitInterface/CorInfoImpl.ReadyToRun.cs | 2 +- 3 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index a9bbe0b2e68e5a..4e8db4b60663a6 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -72,11 +72,7 @@ private delegate CorJitResult JitCompileMethodDelegate(out IntPtr exception, private delegate void FreeExceptionDelegate(IntPtr obj); private delegate char* GetExceptionMessageDelegate(IntPtr obj); - - private static object s_jitLibraryLock = new object(); private static IntPtr s_jitLibrary; - private static TargetOS s_jitLibraryTargetOS; - private static TargetArchitecture s_jitLibraryTargetArchitecture; private static IntPtr s_jitHostLibrary; private static JitStartupDelegate s_jitStartup; @@ -90,41 +86,30 @@ private delegate CorJitResult JitCompileMethodDelegate(out IntPtr exception, private ExceptionDispatchInfo _lastException; - private static void InitializeJitLibrary(TargetOS targetOS, TargetArchitecture targetArchitecture) + public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) { - if (s_jitLibrary == IntPtr.Zero) - { - lock (s_jitLibraryLock) - { - if (s_jitLibrary == IntPtr.Zero) - { - string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); - string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); - - s_jitHostLibrary = NativeLibrary.Load(jitHostLibraryName); - s_getJitHost = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetJitHost")); - s_jitCompileMethod = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "JitCompileMethod")); - s_allocException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "AllocException")); - s_freeException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "FreeException")); - s_getExceptionMessage = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetExceptionMessage")); - - string jitTargetPath = Path.Combine(jitHostPath, GetTargetSpec(targetOS, targetArchitecture)); - string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); - - s_jitLibrary = NativeLibrary.Load(jitLibraryName); - s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); - s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); - - s_jitLibraryTargetOS = targetOS; - s_jitLibraryTargetArchitecture = targetArchitecture; - s_jitStartup(s_getJitHost(JitConfigProvider.Instance.UnmanagedInstance)); - } - // Currently we don't support loading two different versions of clrjit from the same Crossgen2 process - Debug.Assert(targetOS == s_jitLibraryTargetOS); - Debug.Assert(targetArchitecture == s_jitLibraryTargetArchitecture); - } - } + Debug.Assert(s_jitLibrary == IntPtr.Zero); + Debug.Assert(s_jitHostLibrary == IntPtr.Zero); + + string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); + string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); + string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); + + s_jitHostLibrary = NativeLibrary.Load(jitHostLibraryName); + s_getJitHost = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetJitHost")); + s_jitCompileMethod = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "JitCompileMethod")); + s_allocException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "AllocException")); + s_freeException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "FreeException")); + s_getExceptionMessage = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetExceptionMessage")); + + string jitTargetPath = Path.Combine(jitHostPath, GetTargetSpec(targetOS, targetArchitecture)); + string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + + s_jitLibrary = NativeLibrary.Load(jitLibraryName); + s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); + s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); + + s_jitStartup(s_getJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } private static string GetLibraryPrefix() @@ -232,13 +217,8 @@ private IntPtr AllocException(Exception ex) return nativeException; } - public void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) - { - } - - public CorInfoImpl(TargetOS targetOS, TargetArchitecture targetArchitecture) + public CorInfoImpl() { - InitializeJitLibrary(targetOS, targetArchitecture); _jit = s_getJit(); if (_jit == IntPtr.Zero) { diff --git a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs index bd08ba25809dbf..059f245bcfe954 100644 --- a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs +++ b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs @@ -263,6 +263,8 @@ internal ReadyToRunCodegenCompilation( _inputFiles = inputFiles; CompilationModuleGroup = (ReadyToRunCompilationModuleGroupBase)nodeFactory.CompilationModuleGroup; + CorInfoImpl.Startup(nodeFactory.Target.OperatingSystem, nodeFactory.Target.Architecture); + // Generate baseline support specification for InstructionSetSupport. This will prevent usage of the generated // code if the runtime environment doesn't support the specified instruction set string instructionSetSupportString = ReadyToRunInstructionSetSupportSignature.ToInstructionSetSupportString(instructionSetSupport); diff --git a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs index 8b498949414db4..08f83f537c2bbd 100644 --- a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs +++ b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs @@ -144,7 +144,7 @@ unsafe partial class CorInfoImpl private static readonly UnboxingMethodDescFactory s_unboxingThunkFactory = new UnboxingMethodDescFactory(); public CorInfoImpl(ReadyToRunCodegenCompilation compilation) - : this(compilation.NodeFactory.Target.OperatingSystem, compilation.NodeFactory.Target.Architecture) + : this() { _compilation = compilation; } From 8cb5fc0e380018cdd35f2b01cb780d5b6ed1cb44 Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 5 Jun 2020 19:38:44 +0200 Subject: [PATCH 14/26] Block out the Dev_27924 test on CG2 ARM64 with issue #34316 --- src/coreclr/tests/issues.targets | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/coreclr/tests/issues.targets b/src/coreclr/tests/issues.targets index 31ac86f26a9c58..5854d4b30f7d72 100644 --- a/src/coreclr/tests/issues.targets +++ b/src/coreclr/tests/issues.targets @@ -939,6 +939,13 @@ + + + + https://github.com/dotnet/runtime/issues/34316 + + + From 216fe03480a27ea2818436dcf9f91e0c2a5ea47d Mon Sep 17 00:00:00 2001 From: Tomas Date: Sat, 6 Jun 2020 01:13:02 +0200 Subject: [PATCH 15/26] Address Anton's PR feedback --- eng/pipelines/coreclr/crossgen2.yml | 1 + src/coreclr/tests/issues.targets | 32 +++++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/eng/pipelines/coreclr/crossgen2.yml b/eng/pipelines/coreclr/crossgen2.yml index da4efbab7249bd..6ecdb931150a26 100644 --- a/eng/pipelines/coreclr/crossgen2.yml +++ b/eng/pipelines/coreclr/crossgen2.yml @@ -26,6 +26,7 @@ jobs: - OSX_x64 - Windows_NT_x64 - Windows_NT_arm64 + - CoreClrTestBuildHost # Either OSX_x64 or Linux_x64 jobParameters: testGroup: innerloop diff --git a/src/coreclr/tests/issues.targets b/src/coreclr/tests/issues.targets index 5854d4b30f7d72..63de74d32247fc 100644 --- a/src/coreclr/tests/issues.targets +++ b/src/coreclr/tests/issues.targets @@ -913,39 +913,35 @@ https://github.com/dotnet/runtime/issues/32725 + + https://github.com/dotnet/runtime/issues/7597 + + + Not compatible with crossgen2 + https://github.com/dotnet/runtime/issues/615 + + https://github.com/dotnet/runtime/issues/34316 + + + https://github.com/dotnet/runtime/issues/35724 + https://github.com/dotnet/runtime/issues/32725 - - https://github.com/dotnet/runtime/issues/32728 - - - Not compatible with crossgen2 - Not compatible with crossgen2 - - https://github.com/dotnet/runtime/issues/7597 - https://github.com/dotnet/runtime/issues/34905 - - https://github.com/dotnet/runtime/issues/35724 + + https://github.com/dotnet/runtime/issues/32728 - - - - https://github.com/dotnet/runtime/issues/34316 - - - From 5de653bfaaaf86ceb3b8f0f2ced39c23b4079812 Mon Sep 17 00:00:00 2001 From: Tomas Date: Tue, 9 Jun 2020 11:30:15 +0200 Subject: [PATCH 16/26] Revert dynamic loading of jitinterface per PR feedback --- src/coreclr/build-test.cmd | 21 ++- src/coreclr/build-test.sh | 5 +- .../tools/Common/JitInterface/CorInfoImpl.cs | 144 ++++++------------ .../Common/JitInterface/JitConfigProvider.cs | 9 +- .../Compiler/ReadyToRunCodegenCompilation.cs | 2 - .../ReadyToRunCodegenCompilationBuilder.cs | 3 +- .../crossgen2/crossgen2/crossgen2.csproj | 65 ++++---- 7 files changed, 109 insertions(+), 140 deletions(-) diff --git a/src/coreclr/build-test.cmd b/src/coreclr/build-test.cmd index 1ebf8ca0c3ab16..a3e16dcffac262 100644 --- a/src/coreclr/build-test.cmd +++ b/src/coreclr/build-test.cmd @@ -638,6 +638,13 @@ set __FailedAssemblies= set __CompositeOutputDir=%CORE_ROOT%\composite.out set __CompositeResponseFile=%__CompositeOutputDir%\framework-r2r.dll.rsp +set __CrossgenDir=%__BinDir% +if /i "%__BuildArch%" == "arm" (set __CrossgenDir=!__CrossgenDir!\x86) +if /i "%__BuildArch%" == "arm64" (set __CrossgenDir=!__CrossgenDir!\x64) + +set __CrossgenExe="%__CrossgenDir%\crossgen.exe" +set __Crossgen2Dll="%__RepoRootDir%\dotnet.cmd" "%__CrossgenDir%\crossgen2\crossgen2.dll" + if defined __CompositeBuildMode ( mkdir !__CompositeOutputDir! del /Q !__CompositeResponseFile! @@ -667,8 +674,7 @@ if defined __CompositeBuildMode ( ) if defined __CompositeBuildMode ( - set __CompositeCommandLine="%__RepoRootDir%\dotnet.cmd" - set __CompositeCommandLine=!__CompositeCommandLine! "%CORE_ROOT%\crossgen2\crossgen2.dll" + set __CompositeCommandLine=%__Crossgen2Dll% set __CompositeCommandLine=!__CompositeCommandLine! "@%__CompositeResponseFile%" echo Building composite R2R framework^: !__CompositeCommandLine! call !__CompositeCommandLine! @@ -689,15 +695,6 @@ REM Compile the managed assemblies in Core_ROOT before running the tests set AssemblyPath=%1 set AssemblyName=%2 -set __CrossgenExe="%__BinDir%\crossgen.exe" -if /i "%__BuildArch%" == "arm" ( set __CrossgenExe="%__BinDir%\x86\crossgen.exe" ) -if /i "%__BuildArch%" == "arm64" ( set __CrossgenExe="%__BinDir%\x64\crossgen.exe" ) -set __CrossgenExe=%__CrossgenExe% - -if defined __DoCrossgen2 ( - set __CrossgenExe="%__RepoRootDir%\dotnet.cmd" "%CORE_ROOT%\crossgen2\crossgen2.dll" -) - REM Intentionally avoid using the .dll extension to prevent REM subsequent compilations from picking it up as a reference set __CrossgenOutputFile="%CORE_ROOT%\temp.ni._dll" @@ -708,7 +705,7 @@ if defined __DoCrossgen ( echo !__CrossgenCmd! !__CrossgenCmd! ) else ( - set __CrossgenCmd=!__CrossgenExe! -r:"!CORE_ROOT!\System.*.dll" -r:"!CORE_ROOT!\Microsoft.*.dll" -r:"!CORE_ROOT!\mscorlib.dll" -r:"!CORE_ROOT!\netstandard.dll" -O --inputbubble --out:!__CrossgenOutputFile! !AssemblyPath! --targetarch %__BuildArch% + set __CrossgenCmd=!__Crossgen2Dll! -r:"!CORE_ROOT!\System.*.dll" -r:"!CORE_ROOT!\Microsoft.*.dll" -r:"!CORE_ROOT!\mscorlib.dll" -r:"!CORE_ROOT!\netstandard.dll" -O --inputbubble --out:!__CrossgenOutputFile! !AssemblyPath! --targetarch %__BuildArch% echo !__CrossgenCmd! call !__CrossgenCmd! ) diff --git a/src/coreclr/build-test.sh b/src/coreclr/build-test.sh index e907f586ba34c2..fb4fe83f43ff50 100755 --- a/src/coreclr/build-test.sh +++ b/src/coreclr/build-test.sh @@ -181,7 +181,7 @@ precompile_coreroot_fx() local totalPrecompiled=0 local failedToPrecompile=0 local compositeCommandLine="${__DotNetCli}" - compositeCommandLine+=" $__BinDir/crossgen2/crossgen2.dll" + compositeCommandLine+=" $__Crossgen2Dll" compositeCommandLine+=" --composite" compositeCommandLine+=" -O" compositeCommandLine+=" --out:$outputDir/framework-r2r.dll" @@ -207,7 +207,7 @@ precompile_coreroot_fx() fi if [[ "$__DoCrossgen2" != 0 ]]; then - commandLine="${__DotNetCli} $overlayDir/crossgen2/crossgen2.dll $crossgen2References -O --inputbubble --out $outputDir/$(basename $filename) $filename --targetarch ${__BuildArch}" + commandLine="${__DotNetCli} $__Crossgen2Dll $crossgen2References -O --inputbubble --out $outputDir/$(basename $filename) $filename --targetarch ${__BuildArch}" fi echo Precompiling "$filename" @@ -697,6 +697,7 @@ if [[ "$__CrossBuild" == 1 ]]; then fi __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__TargetOS.$BuildArch.$__BuildType.log" __CrossgenExe="$__CrossComponentBinDir/crossgen" +__Crossgen2Dll="$__CrossComponentBinDir/crossgen2/crossgen2.dll" # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to it. # This is needed by CLI to function. diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index 4e8db4b60663a6..ed2616bcf6dc52 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -64,52 +64,70 @@ private enum ImageFileMachine [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate IntPtr GetJitDelegate(); - private delegate IntPtr GetJitHostDelegate(IntPtr configProvider); - private delegate CorJitResult JitCompileMethodDelegate(out IntPtr exception, - IntPtr jit, IntPtr thisHandle, IntPtr callbacks, - ref CORINFO_METHOD_INFO info, uint flags, out IntPtr nativeEntry, out uint codeSize); - private delegate IntPtr AllocExceptionDelegate([MarshalAs(UnmanagedType.LPWStr)]string message, int messageLength); - private delegate void FreeExceptionDelegate(IntPtr obj); - private delegate char* GetExceptionMessageDelegate(IntPtr obj); - private static IntPtr s_jitLibrary; - private static IntPtr s_jitHostLibrary; private static JitStartupDelegate s_jitStartup; private static GetJitDelegate s_getJit; + private ExceptionDispatchInfo _lastException; - private static GetJitHostDelegate s_getJitHost; - private static JitCompileMethodDelegate s_jitCompileMethod; - private static AllocExceptionDelegate s_allocException; - private static FreeExceptionDelegate s_freeException; - private static GetExceptionMessageDelegate s_getExceptionMessage; + [DllImport(JitSupportLibrary)] + private extern static IntPtr GetJitHost(IntPtr configProvider); + + // + // Per-method initialization and state + // + private static CorInfoImpl GetThis(IntPtr thisHandle) + { + CorInfoImpl _this = Unsafe.Read((void*)thisHandle); + Debug.Assert(_this is CorInfoImpl); + return _this; + } + + [DllImport(JitSupportLibrary)] + private extern static CorJitResult JitCompileMethod(out IntPtr exception, + IntPtr jit, IntPtr thisHandle, IntPtr callbacks, + ref CORINFO_METHOD_INFO info, uint flags, out IntPtr nativeEntry, out uint codeSize); + + [DllImport(JitSupportLibrary)] + private extern static uint GetMaxIntrinsicSIMDVectorLength(IntPtr jit, CORJIT_FLAGS* flags); + + [DllImport(JitSupportLibrary)] + private extern static IntPtr AllocException([MarshalAs(UnmanagedType.LPWStr)] string message, int messageLength); + + private IntPtr AllocException(Exception ex) + { + _lastException = ExceptionDispatchInfo.Capture(ex); + + string exString = ex.ToString(); + IntPtr nativeException = AllocException(exString, exString.Length); + if (_nativeExceptions == null) + { + _nativeExceptions = new List(); + } + _nativeExceptions.Add(nativeException); + return nativeException; + } + + [DllImport(JitSupportLibrary)] + private extern static void FreeException(IntPtr obj); + + [DllImport(JitSupportLibrary)] + private extern static char* GetExceptionMessage(IntPtr obj); - private ExceptionDispatchInfo _lastException; public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) { Debug.Assert(s_jitLibrary == IntPtr.Zero); - Debug.Assert(s_jitHostLibrary == IntPtr.Zero); string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitHostPath = Path.Combine(crossgenPath, GetHostSpec()); - string jitHostLibraryName = Path.Combine(jitHostPath, GetLibraryPrefix() + JitSupportLibrary + GetLibraryExtension()); - - s_jitHostLibrary = NativeLibrary.Load(jitHostLibraryName); - s_getJitHost = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetJitHost")); - s_jitCompileMethod = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "JitCompileMethod")); - s_allocException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "AllocException")); - s_freeException = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "FreeException")); - s_getExceptionMessage = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitHostLibrary, "GetExceptionMessage")); - - string jitTargetPath = Path.Combine(jitHostPath, GetTargetSpec(targetOS, targetArchitecture)); + string jitTargetPath = Path.Combine(crossgenPath, GetTargetSpec(targetOS, targetArchitecture)); string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); s_jitLibrary = NativeLibrary.Load(jitLibraryName); s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); - s_jitStartup(s_getJitHost(JitConfigProvider.Instance.UnmanagedInstance)); + s_jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } private static string GetLibraryPrefix() @@ -137,44 +155,6 @@ private static string GetLibraryExtension() } } - private static string GetHostSpec() - { - string hostOsComponent; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - hostOsComponent = "win"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - hostOsComponent = "lnx"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - hostOsComponent = "osx"; - } - else - { - throw new NotImplementedException("GetJitHost"); - } - - string hostArchComponent; - switch (RuntimeInformation.OSArchitecture) - { - case Architecture.X64: - hostArchComponent = "x64"; - break; - - case Architecture.Arm64: - hostArchComponent = "arm64"; - break; - - default: - throw new NotImplementedException(RuntimeInformation.OSArchitecture.ToString()); - } - - return "host-" + hostOsComponent + '-' + hostArchComponent; - } - private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture targetArchitecture) { string targetOSComponent = targetOS switch @@ -190,31 +170,7 @@ private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture target TargetArchitecture.ARM64 => "arm64", _ => throw new NotImplementedException(targetArchitecture.ToString()) }; - return "target-" + targetOSComponent + '-' + targetArchComponent; - } - - // - // Per-method initialization and state - // - private static CorInfoImpl GetThis(IntPtr thisHandle) - { - CorInfoImpl _this = Unsafe.Read((void*)thisHandle); - Debug.Assert(_this is CorInfoImpl); - return _this; - } - - private IntPtr AllocException(Exception ex) - { - _lastException = ExceptionDispatchInfo.Capture(ex); - - string exString = ex.ToString(); - IntPtr nativeException = s_allocException(exString, exString.Length); - if (_nativeExceptions == null) - { - _nativeExceptions = new List(); - } - _nativeExceptions.Add(nativeException); - return nativeException; + return targetOSComponent + '-' + targetArchComponent; } public CorInfoImpl() @@ -264,7 +220,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method IntPtr exception; IntPtr nativeEntry; uint codeSize; - var result = s_jitCompileMethod(out exception, + var result = JitCompileMethod(out exception, _jit, (IntPtr)Unsafe.AsPointer(ref _this), _unmanagedCallbacks, ref methodInfo, (uint)CorJitFlag.CORJIT_FLAG_CALL_GETJITFLAGS, out nativeEntry, out codeSize); if (exception != IntPtr.Zero) @@ -298,7 +254,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method } // This is a failure we don't know much about. - char* szMessage = s_getExceptionMessage(exception); + char* szMessage = GetExceptionMessage(exception); string message = szMessage != null ? new string(szMessage) : "JIT Exception"; throw new Exception(message); } @@ -457,7 +413,7 @@ private void CompileMethodCleanup() if (_nativeExceptions != null) { foreach (IntPtr ex in _nativeExceptions) - s_freeException(ex); + FreeException(ex); _nativeExceptions = null; } diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index b2afb3f1bdc45e..81fdd2e70a54f5 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices; using System.Threading; using ILCompiler; +using Internal.TypeSystem; using NumberStyles = System.Globalization.NumberStyles; namespace Internal.JitInterface @@ -29,7 +30,12 @@ public static JitConfigProvider Instance private Dictionary _config = new Dictionary(StringComparer.OrdinalIgnoreCase); private object _keepAlive; // Keeps callback delegates alive - public static void Initialize(IEnumerable jitFlags, IEnumerable> parameters, string jitPath = null) + public static void Initialize( + TargetOS targetOS, + TargetArchitecture targetArchitecture, + IEnumerable jitFlags, + IEnumerable> parameters, + string jitPath = null) { var config = new JitConfigProvider(jitFlags, parameters); @@ -54,6 +60,7 @@ public static void Initialize(IEnumerable jitFlags, IEnumerable - win - lnx - osx - host-$(HostOSComponent)-$(TargetArchitecture) - x64 - host-$(HostOSComponent)-$(CrossHostArch) win lnx osx - target-$(TargetOSComponent)-$(TargetArchitecture) + $(TargetOSComponent)-$(TargetArchitecture) lib - - .dll - .so - .dylib + + .dll + .so + .dylib + + $(LibraryNamePrefix)jitinterface$(LibraryNameExtension) - - - + - - + + $(BinDir)\$(CrossHostArch)\crossgen2 + + + + + + + + + + + + + - - - From 0819cdc7fd63af5c034d6fffd9821c3dadcf061d Mon Sep 17 00:00:00 2001 From: Tomas Date: Tue, 9 Jun 2020 22:50:56 +0200 Subject: [PATCH 17/26] Fix Crossgen2 file list in the installer to match the new structure --- .../src/tools/crossgen2/crossgen2/crossgen2.csproj | 2 +- .../pkg/Microsoft.NETCore.App.Crossgen2.pkgproj | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index f02662c8a026c0..fbd4fda24f1a4a 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -68,7 +68,7 @@ diff --git a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj index 7eacc16697c6eb..5c5bfe3c23469c 100644 --- a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj +++ b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj @@ -42,13 +42,22 @@ + + win + lnx + osx + $(TargetOSComponent)-$(TargetArchitecture) + + + + - + From 7c1111717ca0596ed1da33277d7dcbaf66ca3c53 Mon Sep 17 00:00:00 2001 From: Tomas Date: Tue, 9 Jun 2020 23:41:35 +0200 Subject: [PATCH 18/26] One more fix for the new Crossgen2 package structure --- .../netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj index 5c5bfe3c23469c..cacdc82d24c75a 100644 --- a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj +++ b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj @@ -57,8 +57,8 @@ - + From dbdab94db1dc54b97a554c3c0ab1b9704803f35d Mon Sep 17 00:00:00 2001 From: Tomas Date: Tue, 9 Jun 2020 23:54:03 +0200 Subject: [PATCH 19/26] Rename OS target component from 'lnx' to 'linux' per JanK's feedback --- src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs | 2 +- src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj | 2 +- .../netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index ed2616bcf6dc52..adf0a1fabc4755 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -160,7 +160,7 @@ private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture target string targetOSComponent = targetOS switch { TargetOS.Windows => "win", - TargetOS.Linux => "lnx", + TargetOS.Linux => "linux", TargetOS.OSX => "osx", _ => throw new NotImplementedException(targetOS.ToString()) }; diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index fbd4fda24f1a4a..01e00f63d331d2 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -45,7 +45,7 @@ x64 win - lnx + linux osx $(TargetOSComponent)-$(TargetArchitecture) diff --git a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj index cacdc82d24c75a..037154d596a23b 100644 --- a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj +++ b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj @@ -44,7 +44,7 @@ win - lnx + linux osx $(TargetOSComponent)-$(TargetArchitecture) From ffdfb88593281810de5f5ea1dab07c1041a02d0c Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 00:12:32 +0200 Subject: [PATCH 20/26] Add explicit handling of jitPath to Startup; remove SetDllImportResolver --- .../tools/Common/JitInterface/CorInfoImpl.cs | 16 ++++++++++++---- .../Common/JitInterface/JitConfigProvider.cs | 18 +----------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index adf0a1fabc4755..360b66074f2bec 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -115,13 +115,21 @@ private IntPtr AllocException(Exception ex) private extern static char* GetExceptionMessage(IntPtr obj); - public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture) + public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture, string jitPathOverride) { Debug.Assert(s_jitLibrary == IntPtr.Zero); - string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitTargetPath = Path.Combine(crossgenPath, GetTargetSpec(targetOS, targetArchitecture)); - string jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + string jitLibraryName; + if (!string.IsNullOrEmpty(jitPathOverride)) + { + jitLibraryName = jitPathOverride; + } + else + { + string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); + string jitTargetPath = Path.Combine(crossgenPath, GetTargetSpec(targetOS, targetArchitecture)); + jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + } s_jitLibrary = NativeLibrary.Load(jitLibraryName); s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index 81fdd2e70a54f5..03318787cc4ae9 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -44,23 +44,7 @@ public static void Initialize( if (Interlocked.CompareExchange(ref s_instance, config, null) != null) throw new InvalidOperationException(); -#if READYTORUN - if (jitPath != null) - { - NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, (libName, assembly, searchPath) => - { - IntPtr libHandle = IntPtr.Zero; - if (libName == CorInfoImpl.JitLibrary) - { - libHandle = NativeLibrary.Load(jitPath, assembly, searchPath); - } - return libHandle; - }); - } -#else - Debug.Assert(jitPath == null); -#endif - CorInfoImpl.Startup(targetOS, targetArchitecture); + CorInfoImpl.Startup(targetOS, targetArchitecture, jitPath); } public IntPtr UnmanagedInstance From 6ca1613bf0901ef5225ed10eb12bd1bab8376aae Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 00:35:31 +0200 Subject: [PATCH 21/26] Remove clrjit subfolder, suffix its name with target OS / arch --- .../tools/Common/JitInterface/CorInfoImpl.cs | 19 +++++-------------- .../crossgen2/crossgen2/crossgen2.csproj | 8 +++----- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index 360b66074f2bec..098e714ff35930 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -45,7 +45,7 @@ private enum ImageFileMachine ARM64 = 0xaa64, } - internal const string JitLibrary = "clrjitilc"; + internal const string JitLibrary = "clrjit"; #if SUPPORT_JIT private const string JitSupportLibrary = "*"; @@ -119,19 +119,16 @@ public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitect { Debug.Assert(s_jitLibrary == IntPtr.Zero); - string jitLibraryName; if (!string.IsNullOrEmpty(jitPathOverride)) { - jitLibraryName = jitPathOverride; + s_jitLibrary = NativeLibrary.Load(jitPathOverride); } else { - string crossgenPath = Path.GetDirectoryName(typeof(CorInfoImpl).Assembly.Location); - string jitTargetPath = Path.Combine(crossgenPath, GetTargetSpec(targetOS, targetArchitecture)); - jitLibraryName = Path.Combine(jitTargetPath, GetLibraryPrefix() + JitLibrary + GetLibraryExtension()); + string jitLibraryName = JitLibrary + '-' + GetTargetSpec(targetOS, targetArchitecture); + s_jitLibrary = NativeLibrary.Load(jitLibraryName, typeof(CorInfoImpl).Assembly, searchPath: null); } - s_jitLibrary = NativeLibrary.Load(jitLibraryName); s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); @@ -165,13 +162,7 @@ private static string GetLibraryExtension() private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture targetArchitecture) { - string targetOSComponent = targetOS switch - { - TargetOS.Windows => "win", - TargetOS.Linux => "linux", - TargetOS.OSX => "osx", - _ => throw new NotImplementedException(targetOS.ToString()) - }; + string targetOSComponent = (targetOS == TargetOS.Windows ? "win" : "unix"); string targetArchComponent = targetArchitecture switch { TargetArchitecture.X64 => "x64", diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index 01e00f63d331d2..efcfd9c70e951a 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -44,9 +44,8 @@ x64 + unix win - linux - osx $(TargetOSComponent)-$(TargetArchitecture) lib @@ -68,7 +67,7 @@ @@ -95,10 +94,9 @@ UseHardLinksIfPossible="true" /> - From 34b6ecc02cb0ac1df5bfb34205a27605990678d4 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 00:41:21 +0200 Subject: [PATCH 22/26] Addressing additional bits of JanK's PR feedback Remove obsoleted methods GetLibraryPrefix and GetLibraryExtension; Put installer pkgproj in sync with the updated clrjit naming; Move _lastException to the other instance fields in CorInfoImpl; Add TargetSpec mapping for all four CPU architectures. --- .../tools/Common/JitInterface/CorInfoImpl.cs | 30 +++---------------- .../Microsoft.NETCore.App.Crossgen2.pkgproj | 5 ++-- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index 098e714ff35930..c51bcfe3f60726 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -58,6 +58,8 @@ private enum ImageFileMachine private IntPtr _unmanagedCallbacks; // array of pointers to JIT-EE interface callbacks private Object _keepAlive; // Keeps delegates for the callbacks alive + private ExceptionDispatchInfo _lastException; + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate IntPtr JitStartupDelegate(IntPtr host); @@ -68,7 +70,6 @@ private enum ImageFileMachine private static JitStartupDelegate s_jitStartup; private static GetJitDelegate s_getJit; - private ExceptionDispatchInfo _lastException; [DllImport(JitSupportLibrary)] private extern static IntPtr GetJitHost(IntPtr configProvider); @@ -135,37 +136,14 @@ public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitect s_jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } - private static string GetLibraryPrefix() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "lib"; - } - - private static string GetLibraryExtension() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return ".dll"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return ".so"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - return ".dylib"; - } - else - { - throw new NotImplementedException("GetLibraryExtension"); - } - } - private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture targetArchitecture) { string targetOSComponent = (targetOS == TargetOS.Windows ? "win" : "unix"); string targetArchComponent = targetArchitecture switch { + TargetArchitecture.X86 => "x86", TargetArchitecture.X64 => "x64", + TargetArchitecture.ARM => "arm", TargetArchitecture.ARM64 => "arm64", _ => throw new NotImplementedException(targetArchitecture.ToString()) }; diff --git a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj index 037154d596a23b..2946f91bfded8c 100644 --- a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj +++ b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj @@ -43,9 +43,8 @@ + unix win - linux - osx $(TargetOSComponent)-$(TargetArchitecture) @@ -58,7 +57,7 @@ - + From 69444629f06cf7c5122ef2d2ef8218eb3695cccf Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 01:15:01 +0200 Subject: [PATCH 23/26] Addressing additional JanK's feedback - Use TargetDetails to pass the target OS / arch info; - Remove obsoleted Error clause from the installer pkg script; - Deleted unneeded using clauses from R2RCGCompilationBuilder. --- .../src/tools/Common/JitInterface/CorInfoImpl.cs | 12 ++++++------ .../tools/Common/JitInterface/JitConfigProvider.cs | 5 ++--- .../Compiler/ReadyToRunCodegenCompilationBuilder.cs | 4 +--- .../pkg/Microsoft.NETCore.App.Crossgen2.pkgproj | 2 -- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index c51bcfe3f60726..e23e676a512dcf 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -116,7 +116,7 @@ private IntPtr AllocException(Exception ex) private extern static char* GetExceptionMessage(IntPtr obj); - public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitecture, string jitPathOverride) + public static void Startup(TargetDetails target, string jitPathOverride) { Debug.Assert(s_jitLibrary == IntPtr.Zero); @@ -126,7 +126,7 @@ public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitect } else { - string jitLibraryName = JitLibrary + '-' + GetTargetSpec(targetOS, targetArchitecture); + string jitLibraryName = JitLibrary + '-' + GetTargetSpec(target); s_jitLibrary = NativeLibrary.Load(jitLibraryName, typeof(CorInfoImpl).Assembly, searchPath: null); } @@ -136,16 +136,16 @@ public static void Startup(TargetOS targetOS, TargetArchitecture targetArchitect s_jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } - private static string GetTargetSpec(TargetOS targetOS, TargetArchitecture targetArchitecture) + private static string GetTargetSpec(TargetDetails target) { - string targetOSComponent = (targetOS == TargetOS.Windows ? "win" : "unix"); - string targetArchComponent = targetArchitecture switch + string targetOSComponent = (target.OperatingSystem == TargetOS.Windows ? "win" : "unix"); + string targetArchComponent = target.Architecture switch { TargetArchitecture.X86 => "x86", TargetArchitecture.X64 => "x64", TargetArchitecture.ARM => "arm", TargetArchitecture.ARM64 => "arm64", - _ => throw new NotImplementedException(targetArchitecture.ToString()) + _ => throw new NotImplementedException(target.Architecture.ToString()) }; return targetOSComponent + '-' + targetArchComponent; } diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index 03318787cc4ae9..124b7196a53fec 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -31,8 +31,7 @@ public static JitConfigProvider Instance private object _keepAlive; // Keeps callback delegates alive public static void Initialize( - TargetOS targetOS, - TargetArchitecture targetArchitecture, + TargetDetails target, IEnumerable jitFlags, IEnumerable> parameters, string jitPath = null) @@ -44,7 +43,7 @@ public static void Initialize( if (Interlocked.CompareExchange(ref s_instance, config, null) != null) throw new InvalidOperationException(); - CorInfoImpl.Startup(targetOS, targetArchitecture, jitPath); + CorInfoImpl.Startup(target, jitPath); } public IntPtr UnmanagedInstance diff --git a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs index 2c6f64b56b039d..e6adde44dcfb25 100644 --- a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs +++ b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs @@ -9,12 +9,10 @@ using ILCompiler.DependencyAnalysis; using ILCompiler.DependencyAnalysis.ReadyToRun; using ILCompiler.DependencyAnalysisFramework; -using ILCompiler.PEWriter; using ILCompiler.Win32Resources; using Internal.IL; using Internal.JitInterface; using Internal.ReadyToRunConstants; -using Internal.TypeSystem; using Internal.TypeSystem.Ecma; namespace ILCompiler @@ -193,7 +191,7 @@ public override ICompilation ToCompilation() if (_ibcTuning) corJitFlags.Add(CorJitFlag.CORJIT_FLAG_BBINSTR); - JitConfigProvider.Initialize(_context.Target.OperatingSystem, _context.Target.Architecture, corJitFlags, _ryujitOptions, _jitPath); + JitConfigProvider.Initialize(_context.Target, corJitFlags, _ryujitOptions, _jitPath); return new ReadyToRunCodegenCompilation( graph, diff --git a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj index 2946f91bfded8c..a6df67996ac404 100644 --- a/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj +++ b/src/installer/pkg/projects/netcoreapp/pkg/Microsoft.NETCore.App.Crossgen2.pkgproj @@ -48,8 +48,6 @@ $(TargetOSComponent)-$(TargetArchitecture) - - From 8e2fe047b1a41599b7885c0a7f64762b85d7ae31 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 06:28:17 +0200 Subject: [PATCH 24/26] Fix clrjit path in Signing.props --- eng/Signing.props | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eng/Signing.props b/eng/Signing.props index d2bd5b84e50198..81179505a9e350 100644 --- a/eng/Signing.props +++ b/eng/Signing.props @@ -44,9 +44,11 @@ - + + + From 9e2e87c64872121375bc3fcfd9ce8fa79bad437c Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 13:48:29 +0200 Subject: [PATCH 25/26] Put back SetDllImportResolver per Anton's and Michal's feedback --- .../tools/Common/JitInterface/CorInfoImpl.cs | 53 ++++--------------- .../Common/JitInterface/JitConfigProvider.cs | 43 ++++++++++++++- .../crossgen2/crossgen2/crossgen2.csproj | 10 ++-- src/coreclr/tests/issues.targets | 3 ++ 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs index e23e676a512dcf..fef6e6e5eb5ed7 100644 --- a/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs @@ -45,7 +45,7 @@ private enum ImageFileMachine ARM64 = 0xaa64, } - internal const string JitLibrary = "clrjit"; + internal const string JitLibrary = "clrjitilc"; #if SUPPORT_JIT private const string JitSupportLibrary = "*"; @@ -60,16 +60,11 @@ private enum ImageFileMachine private ExceptionDispatchInfo _lastException; - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate IntPtr JitStartupDelegate(IntPtr host); + [DllImport(JitLibrary, CallingConvention = CallingConvention.StdCall)] // stdcall in CoreCLR! + private extern static IntPtr jitStartup(IntPtr host); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate IntPtr GetJitDelegate(); - - private static IntPtr s_jitLibrary; - - private static JitStartupDelegate s_jitStartup; - private static GetJitDelegate s_getJit; + [DllImport(JitLibrary, CallingConvention = CallingConvention.StdCall)] + private extern static IntPtr getJit(); [DllImport(JitSupportLibrary)] private extern static IntPtr GetJitHost(IntPtr configProvider); @@ -93,7 +88,7 @@ private extern static CorJitResult JitCompileMethod(out IntPtr exception, private extern static uint GetMaxIntrinsicSIMDVectorLength(IntPtr jit, CORJIT_FLAGS* flags); [DllImport(JitSupportLibrary)] - private extern static IntPtr AllocException([MarshalAs(UnmanagedType.LPWStr)] string message, int messageLength); + private extern static IntPtr AllocException([MarshalAs(UnmanagedType.LPWStr)]string message, int messageLength); private IntPtr AllocException(Exception ex) { @@ -115,44 +110,14 @@ private IntPtr AllocException(Exception ex) [DllImport(JitSupportLibrary)] private extern static char* GetExceptionMessage(IntPtr obj); - - public static void Startup(TargetDetails target, string jitPathOverride) - { - Debug.Assert(s_jitLibrary == IntPtr.Zero); - - if (!string.IsNullOrEmpty(jitPathOverride)) - { - s_jitLibrary = NativeLibrary.Load(jitPathOverride); - } - else - { - string jitLibraryName = JitLibrary + '-' + GetTargetSpec(target); - s_jitLibrary = NativeLibrary.Load(jitLibraryName, typeof(CorInfoImpl).Assembly, searchPath: null); - } - - s_jitStartup = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "jitStartup")); - s_getJit = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(s_jitLibrary, "getJit")); - - s_jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); - } - - private static string GetTargetSpec(TargetDetails target) + public static void Startup() { - string targetOSComponent = (target.OperatingSystem == TargetOS.Windows ? "win" : "unix"); - string targetArchComponent = target.Architecture switch - { - TargetArchitecture.X86 => "x86", - TargetArchitecture.X64 => "x64", - TargetArchitecture.ARM => "arm", - TargetArchitecture.ARM64 => "arm64", - _ => throw new NotImplementedException(target.Architecture.ToString()) - }; - return targetOSComponent + '-' + targetArchComponent; + jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance)); } public CorInfoImpl() { - _jit = s_getJit(); + _jit = getJit(); if (_jit == IntPtr.Zero) { throw new IOException("Failed to initialize JIT"); diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index 124b7196a53fec..55b17cffb7dfc1 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -17,6 +17,8 @@ public sealed class JitConfigProvider { // Jit configuration is static because RyuJIT doesn't support multiple hosts within the same process. private static JitConfigProvider s_instance; + private static IntPtr s_jitLibrary; + public static JitConfigProvider Instance { get @@ -43,7 +45,32 @@ public static void Initialize( if (Interlocked.CompareExchange(ref s_instance, config, null) != null) throw new InvalidOperationException(); - CorInfoImpl.Startup(target, jitPath); +#if READYTORUN + NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, (libName, assembly, searchPath) => + { + IntPtr libHandle = IntPtr.Zero; + if (libName == CorInfoImpl.JitLibrary) + { + if (s_jitLibrary == IntPtr.Zero) + { + if (!string.IsNullOrEmpty(jitPath)) + { + s_jitLibrary = NativeLibrary.Load(jitPath); + } + else + { + s_jitLibrary = NativeLibrary.Load("clrjit-" + GetTargetSpec(target), assembly, searchPath); + } + } + libHandle = s_jitLibrary; + } + return libHandle; + }); +#else + Debug.Assert(jitPath == null); +#endif + + CorInfoImpl.Startup(); } public IntPtr UnmanagedInstance @@ -110,6 +137,20 @@ public string GetStringConfigValue(string name) return String.Empty; } + private static string GetTargetSpec(TargetDetails target) + { + string targetOSComponent = (target.OperatingSystem == TargetOS.Windows ? "win" : "unix"); + string targetArchComponent = target.Architecture switch + { + TargetArchitecture.X86 => "x86", + TargetArchitecture.X64 => "x64", + TargetArchitecture.ARM => "arm", + TargetArchitecture.ARM64 => "arm64", + _ => throw new NotImplementedException(target.Architecture.ToString()) + }; + return targetOSComponent + '-' + targetArchComponent; + } + #region Unmanaged instance private unsafe IntPtr CreateUnmanagedInstance() diff --git a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj index efcfd9c70e951a..05051a5cd6e62e 100644 --- a/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj +++ b/src/coreclr/src/tools/crossgen2/crossgen2/crossgen2.csproj @@ -78,7 +78,9 @@ - + + @@ -88,12 +90,6 @@ UseHardLinksIfPossible="true" /> - - https://github.com/dotnet/runtime/issues/615 + + https://github.com/dotnet/runtime/issues/37579 + https://github.com/dotnet/runtime/issues/34316 From 58239a590350c8e34d1f1ab0990f2e7b71a38a13 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Jun 2020 14:54:02 +0200 Subject: [PATCH 26/26] Remove caching of clrjit handle per Michal's PR feedback --- .../Common/JitInterface/JitConfigProvider.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs index 55b17cffb7dfc1..13044997af0041 100644 --- a/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs @@ -17,8 +17,6 @@ public sealed class JitConfigProvider { // Jit configuration is static because RyuJIT doesn't support multiple hosts within the same process. private static JitConfigProvider s_instance; - private static IntPtr s_jitLibrary; - public static JitConfigProvider Instance { get @@ -51,18 +49,14 @@ public static void Initialize( IntPtr libHandle = IntPtr.Zero; if (libName == CorInfoImpl.JitLibrary) { - if (s_jitLibrary == IntPtr.Zero) + if (!string.IsNullOrEmpty(jitPath)) + { + libHandle = NativeLibrary.Load(jitPath); + } + else { - if (!string.IsNullOrEmpty(jitPath)) - { - s_jitLibrary = NativeLibrary.Load(jitPath); - } - else - { - s_jitLibrary = NativeLibrary.Load("clrjit-" + GetTargetSpec(target), assembly, searchPath); - } + libHandle = NativeLibrary.Load("clrjit-" + GetTargetSpec(target), assembly, searchPath); } - libHandle = s_jitLibrary; } return libHandle; });