Call the jit shutdown logic on crossgen2 shutdown (#56187)
authorDavid Wrighton <davidwr@microsoft.com>
Fri, 23 Jul 2021 00:28:07 +0000 (17:28 -0700)
committerGitHub <noreply@github.com>
Fri, 23 Jul 2021 00:28:07 +0000 (17:28 -0700)
- Allows the jit shutdown logic to execute reliably on both Windows and Unix

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs
src/coreclr/tools/aot/crossgen2/Program.cs
src/coreclr/tools/aot/jitinterface/jitwrapper.cpp

index 9898ff8..ff268b7 100644 (file)
@@ -74,8 +74,38 @@ namespace Internal.JitInterface
         [DllImport(JitLibrary)]
         private extern static IntPtr jitStartup(IntPtr host);
 
-        [DllImport(JitLibrary)]
-        private extern static IntPtr getJit();
+        private static class JitPointerAccessor
+        {
+            [DllImport(JitLibrary)]
+            private extern static IntPtr getJit();
+
+            public static IntPtr Get()
+            {
+                if (s_jit != IntPtr.Zero)
+                {
+                    return s_jit;
+                }
+
+                lock(typeof(JitPointerAccessor))
+                {
+                    s_jit = getJit();
+                    return s_jit;
+                }
+            }
+
+            [DllImport(JitSupportLibrary)]
+            private extern static CorJitResult JitProcessShutdownWork(IntPtr jit);
+
+            public static void ShutdownJit()
+            {
+                if (s_jit != IntPtr.Zero)
+                {
+                    JitProcessShutdownWork(s_jit);
+                }
+            }
+
+            private static IntPtr s_jit;
+        }
 
         [DllImport(JitLibrary)]
         private extern static IntPtr getLikelyClass(PgoInstrumentationSchema* schema, uint countSchemaItems, byte*pInstrumentationData, int ilOffset, out uint pLikelihood, out uint pNumberOfClasses);
@@ -129,9 +159,14 @@ namespace Internal.JitInterface
             jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance));
         }
 
+        public static void ShutdownJit()
+        {
+            JitPointerAccessor.ShutdownJit();
+        }
+
         public CorInfoImpl()
         {
-            _jit = getJit();
+            _jit = JitPointerAccessor.Get();
             if (_jit == IntPtr.Zero)
             {
                 throw new IOException("Failed to initialize JIT");
index c1207a4..4b5d4a7 100644 (file)
@@ -65,6 +65,12 @@ namespace ILCompiler
             ((ReadyToRunCompilerContext)context).SetCompilationGroup(group);
         }
 
+        // Shutdown the Jit if it has been loaded. This must only be called once per process
+        public static void ShutdownJit()
+        {
+            CorInfoImpl.ShutdownJit();
+        }
+
         public override CompilationBuilder UseBackendOptions(IEnumerable<string> options)
         {
             var builder = new ArrayBuilder<KeyValuePair<string, string>>();
index 22e7b32..cc8e910 100644 (file)
@@ -993,7 +993,14 @@ namespace ILCompiler
 #if DEBUG
             try
             {
-                return new Program().Run(args);
+                try
+                {
+                    return new Program().Run(args);
+                }
+                finally
+                {
+                    ReadyToRunCodegenCompilationBuilder.ShutdownJit();
+                }
             }
             catch (CodeGenerationFailedException ex) when (DumpReproArguments(ex))
             {
@@ -1002,7 +1009,14 @@ namespace ILCompiler
 #else
             try
             {
-                return new Program().Run(args);
+                try
+                {
+                    return new Program().Run(args);
+                }
+                finally
+                {
+                    ReadyToRunCodegenCompilationBuilder.ShutdownJit();
+                }
             }
             catch (Exception e)
             {
@@ -1011,6 +1025,7 @@ namespace ILCompiler
                 return 1;
             }
 #endif
+
         }
     }
 }
index b76dabf..5f611cc 100644 (file)
@@ -43,6 +43,11 @@ DLL_EXPORT int JitCompileMethod(
     return 1;
 }
 
+DLL_EXPORT void JitProcessShutdownWork(ICorJitCompiler * pJit)
+{
+    return pJit->ProcessShutdownWork(nullptr);
+}
+
 DLL_EXPORT unsigned GetMaxIntrinsicSIMDVectorLength(
     ICorJitCompiler * pJit,
     CORJIT_FLAGS * flags)