Skip to content

Commit d20fdf1

Browse files
Call the jit shutdown logic on crossgen2 shutdown (#56187)
- Allows the jit shutdown logic to execute reliably on both Windows and Unix
1 parent 058a531 commit d20fdf1

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,38 @@ private struct PgoInstrumentationResults
7474
[DllImport(JitLibrary)]
7575
private extern static IntPtr jitStartup(IntPtr host);
7676

77-
[DllImport(JitLibrary)]
78-
private extern static IntPtr getJit();
77+
private static class JitPointerAccessor
78+
{
79+
[DllImport(JitLibrary)]
80+
private extern static IntPtr getJit();
81+
82+
public static IntPtr Get()
83+
{
84+
if (s_jit != IntPtr.Zero)
85+
{
86+
return s_jit;
87+
}
88+
89+
lock(typeof(JitPointerAccessor))
90+
{
91+
s_jit = getJit();
92+
return s_jit;
93+
}
94+
}
95+
96+
[DllImport(JitSupportLibrary)]
97+
private extern static CorJitResult JitProcessShutdownWork(IntPtr jit);
98+
99+
public static void ShutdownJit()
100+
{
101+
if (s_jit != IntPtr.Zero)
102+
{
103+
JitProcessShutdownWork(s_jit);
104+
}
105+
}
106+
107+
private static IntPtr s_jit;
108+
}
79109

80110
[DllImport(JitLibrary)]
81111
private extern static IntPtr getLikelyClass(PgoInstrumentationSchema* schema, uint countSchemaItems, byte*pInstrumentationData, int ilOffset, out uint pLikelihood, out uint pNumberOfClasses);
@@ -129,9 +159,14 @@ public static void Startup()
129159
jitStartup(GetJitHost(JitConfigProvider.Instance.UnmanagedInstance));
130160
}
131161

162+
public static void ShutdownJit()
163+
{
164+
JitPointerAccessor.ShutdownJit();
165+
}
166+
132167
public CorInfoImpl()
133168
{
134-
_jit = getJit();
169+
_jit = JitPointerAccessor.Get();
135170
if (_jit == IntPtr.Zero)
136171
{
137172
throw new IOException("Failed to initialize JIT");

src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public ReadyToRunCodegenCompilationBuilder(
6565
((ReadyToRunCompilerContext)context).SetCompilationGroup(group);
6666
}
6767

68+
// Shutdown the Jit if it has been loaded. This must only be called once per process
69+
public static void ShutdownJit()
70+
{
71+
CorInfoImpl.ShutdownJit();
72+
}
73+
6874
public override CompilationBuilder UseBackendOptions(IEnumerable<string> options)
6975
{
7076
var builder = new ArrayBuilder<KeyValuePair<string, string>>();

src/coreclr/tools/aot/crossgen2/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,14 @@ private static int Main(string[] args)
993993
#if DEBUG
994994
try
995995
{
996-
return new Program().Run(args);
996+
try
997+
{
998+
return new Program().Run(args);
999+
}
1000+
finally
1001+
{
1002+
ReadyToRunCodegenCompilationBuilder.ShutdownJit();
1003+
}
9971004
}
9981005
catch (CodeGenerationFailedException ex) when (DumpReproArguments(ex))
9991006
{
@@ -1002,7 +1009,14 @@ private static int Main(string[] args)
10021009
#else
10031010
try
10041011
{
1005-
return new Program().Run(args);
1012+
try
1013+
{
1014+
return new Program().Run(args);
1015+
}
1016+
finally
1017+
{
1018+
ReadyToRunCodegenCompilationBuilder.ShutdownJit();
1019+
}
10061020
}
10071021
catch (Exception e)
10081022
{
@@ -1011,6 +1025,7 @@ private static int Main(string[] args)
10111025
return 1;
10121026
}
10131027
#endif
1028+
10141029
}
10151030
}
10161031
}

src/coreclr/tools/aot/jitinterface/jitwrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ DLL_EXPORT int JitCompileMethod(
4343
return 1;
4444
}
4545

46+
DLL_EXPORT void JitProcessShutdownWork(ICorJitCompiler * pJit)
47+
{
48+
return pJit->ProcessShutdownWork(nullptr);
49+
}
50+
4651
DLL_EXPORT unsigned GetMaxIntrinsicSIMDVectorLength(
4752
ICorJitCompiler * pJit,
4853
CORJIT_FLAGS * flags)

0 commit comments

Comments
 (0)