Add crossgen2 option to specify JIT library path (dotnet/coreclr#26967)
authorAnton Lapounov <antonl@microsoft.com>
Tue, 8 Oct 2019 01:11:39 +0000 (18:11 -0700)
committerGitHub <noreply@github.com>
Tue, 8 Oct 2019 01:11:39 +0000 (18:11 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/7d4e358140ebd55531b584a244a8d8767ddc7cea

src/coreclr/src/tools/crossgen2/Common/Compiler/CompilationBuilder.cs
src/coreclr/src/tools/crossgen2/Common/JitInterface/CorInfoImpl.cs
src/coreclr/src/tools/crossgen2/Common/JitInterface/JitConfigProvider.cs
src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilationBuilder.cs
src/coreclr/src/tools/crossgen2/crossgen2/Program.cs

index e115ecbd47f15032786d05896edfd984bc17c8a9..7fd7ad1769ff511b31a7c1cc5d6d34ca17d1b68c 100644 (file)
@@ -72,6 +72,8 @@ namespace ILCompiler
 
         protected abstract ILProvider GetILProvider();
 
+        public abstract CompilationBuilder UseJitPath(string jitPath);
+
         protected DependencyAnalyzerBase<NodeFactory> CreateDependencyGraph(NodeFactory factory, IComparer<DependencyNodeCore<NodeFactory>> comparer = null)
         {
             return _dependencyTrackingLevel.CreateDependencyGraph(factory, comparer);
index 68db43ca65a5e7ae10ae1e513e66316791196080..7cc4572b259d0f8f3fb24c317d775175c8ab088c 100644 (file)
@@ -42,6 +42,8 @@ namespace Internal.JitInterface
             ARM = 0x01c4,
         }
 
+        private const string JitLibrary = "clrjitilc";
+
 #if SUPPORT_JIT
         private const string JitSupportLibrary = "*";
 #else
@@ -57,13 +59,13 @@ namespace Internal.JitInterface
 
         private static bool s_jitRegistered = RegisterJITModule();
 
-        [DllImport("clrjitilc")]
+        [DllImport(JitLibrary)]
         private extern static IntPtr PAL_RegisterModule([MarshalAs(UnmanagedType.LPUTF8Str)] string moduleName);
 
-        [DllImport("clrjitilc", CallingConvention=CallingConvention.StdCall)] // stdcall in CoreCLR!
+        [DllImport(JitLibrary, CallingConvention=CallingConvention.StdCall)] // stdcall in CoreCLR!
         private extern static IntPtr jitStartup(IntPtr host);
 
-        [DllImport("clrjitilc", CallingConvention=CallingConvention.StdCall)]
+        [DllImport(JitLibrary, CallingConvention=CallingConvention.StdCall)]
         private extern static IntPtr getJit();
 
         [DllImport(JitSupportLibrary)]
@@ -126,6 +128,16 @@ namespace Internal.JitInterface
             }
         }
 
+        private IntPtr JitLibraryResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
+        {
+            IntPtr libHandle = IntPtr.Zero;
+            if (libraryName == JitLibrary)
+            {
+                libHandle = NativeLibrary.Load(_jitConfig.JitPath, assembly, searchPath);
+            }
+            return libHandle;
+        }
+
         public CorInfoImpl(JitConfigProvider jitConfig)
         {
             //
@@ -133,7 +145,14 @@ namespace Internal.JitInterface
             //
             _jitConfig = jitConfig;
             if (!s_jitRegistered)
+            {
                 throw new IOException("Failed to register JIT");
+            }
+
+            if (_jitConfig.JitPath != null)
+            {
+                NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, JitLibraryResolver);
+            }
 
             jitStartup(GetJitHost(_jitConfig.UnmanagedInstance));
 
index f965c8a1e47aeb361e9e63e49af491209c2d5717..0e73be8bf6a738d220e9e8c826ad04852c9c168c 100644 (file)
@@ -21,19 +21,21 @@ namespace Internal.JitInterface
             get;
         }
 
+        public string JitPath
+        {
+            get;
+        }
+
         public IEnumerable<CorJitFlag> Flags => _jitFlags;
 
         /// <summary>
         /// Creates a new instance of <see cref="JitConfigProvider"/>.
         /// </summary>
-        /// <param name="parameters">Name-value pairs separated by an equals sign.</param>
-        public JitConfigProvider(IEnumerable<CorJitFlag> jitFlags, IEnumerable<KeyValuePair<string, string>> parameters)
+        /// <param name="jitFlags">A collection of JIT compiler flags.</param>
+        /// <param name="parameters">A collection of parameter name/value pairs.</param>
+        /// <param name="jitPath">A path to the JIT library to be used (may be null).</param>
+        public JitConfigProvider(IEnumerable<CorJitFlag> jitFlags, IEnumerable<KeyValuePair<string, string>> parameters, string jitPath = null)
         {
-            foreach (var param in parameters)
-            {
-                _config[param.Key] = param.Value;
-            }
-
             ArrayBuilder<CorJitFlag> jitFlagBuilder = new ArrayBuilder<CorJitFlag>();
             foreach (CorJitFlag jitFlag in jitFlags)
             {
@@ -41,6 +43,12 @@ namespace Internal.JitInterface
             }
             _jitFlags = jitFlagBuilder.ToArray();
 
+            foreach (var param in parameters)
+            {
+                _config[param.Key] = param.Value;
+            }
+
+            JitPath = jitPath;
             UnmanagedInstance = CreateUnmanagedInstance();
         }
 
index 5c6b48319f2ca6eb7cc95d96cde50895f8191b40..0bb9b015ac7a1e60838514afd1d8bc83d1966543 100644 (file)
@@ -22,6 +22,7 @@ namespace ILCompiler
         private readonly EcmaModule _inputModule;
         private readonly bool _ibcTuning;
         private readonly bool _resilient;
+        private string _jitPath;
 
         // These need to provide reasonable defaults so that the user can optionally skip
         // calling the Use/Configure methods and still get something reasonable back.
@@ -31,9 +32,9 @@ namespace ILCompiler
         public ReadyToRunCodegenCompilationBuilder(CompilerTypeSystemContext context, CompilationModuleGroup group, string inputFilePath, bool ibcTuning, bool resilient)
             : base(context, group, new CoreRTNameMangler())
         {
+            _inputFilePath = inputFilePath;
             _ibcTuning = ibcTuning;
             _resilient = resilient;
-            _inputFilePath = inputFilePath;
 
             _inputModule = context.GetModuleFromPath(_inputFilePath);
 
@@ -77,6 +78,12 @@ namespace ILCompiler
             return _ilProvider;
         }
 
+        public override CompilationBuilder UseJitPath(string jitPath)
+        {
+            _jitPath = jitPath;
+            return this;
+        }
+
         public override ICompilation ToCompilation()
         {
             ModuleTokenResolver moduleTokenResolver = new ModuleTokenResolver(_compilationGroup, _context);
@@ -136,7 +143,7 @@ namespace ILCompiler
                 corJitFlags.Add(CorJitFlag.CORJIT_FLAG_BBINSTR);
 
             corJitFlags.Add(CorJitFlag.CORJIT_FLAG_FEATURE_SIMD);
-            var jitConfig = new JitConfigProvider(corJitFlags, _ryujitOptions);
+            var jitConfig = new JitConfigProvider(corJitFlags, _ryujitOptions, _jitPath);
 
             return new ReadyToRunCodegenCompilation(
                 graph,
index 0cbdd5c06597435391f3da734d4c35d837758242..1be1379f56d568cfc2724c160a24b1e17f4320f2 100644 (file)
@@ -35,6 +35,7 @@ namespace ILCompiler
         private string _targetArchitectureStr;
         private TargetOS _targetOS;
         private string _targetOSStr;
+        private string _jitPath;
         private OptimizationMode _optimizationMode;
         private string _systemModuleName = DefaultSystemModule;
         private bool _tuning;
@@ -140,6 +141,7 @@ namespace ILCompiler
 
                 syntax.DefineOption("targetarch", ref _targetArchitectureStr, "Target architecture for cross compilation");
                 syntax.DefineOption("targetos", ref _targetOSStr, "Target OS for cross compilation");
+                syntax.DefineOption("jitpath", ref _jitPath, "Path to JIT compiler library");
 
                 syntax.DefineOption("singlemethodtypename", ref _singleMethodTypeName, "Single method compilation: name of the owning type");
                 syntax.DefineOption("singlemethodname", ref _singleMethodName, "Single method compilation: name of the method");
@@ -385,12 +387,12 @@ namespace ILCompiler
 
                     ILProvider ilProvider = new ReadyToRunILProvider();
 
-
                     DependencyTrackingLevel trackingLevel = _dgmlLogFileName == null ?
                         DependencyTrackingLevel.None : (_generateFullDgmlLog ? DependencyTrackingLevel.All : DependencyTrackingLevel.First);
 
                     builder
                         .UseILProvider(ilProvider)
+                        .UseJitPath(_jitPath)
                         .UseBackendOptions(_codegenOptions)
                         .UseLogger(logger)
                         .UseDependencyTracking(trackingLevel)