-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathCrossgen2RootCommand.cs
382 lines (352 loc) · 18.7 KB
/
Crossgen2RootCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Parsing;
using System.IO;
using System.Runtime.InteropServices;
using Internal.TypeSystem;
namespace ILCompiler
{
internal class Crossgen2RootCommand : RootCommand
{
public Argument<Dictionary<string, string>> InputFilePaths { get; } =
new("input-file-path", result => Helpers.BuildPathDictionary(result.Tokens, true), false, "Input file(s)") { Arity = ArgumentArity.OneOrMore };
public Option<Dictionary<string, string>> UnrootedInputFilePaths { get; } =
new(new[] { "--unrooted-input-file-paths", "-u" }, result => Helpers.BuildPathDictionary(result.Tokens, true), true, SR.UnrootedInputFilesToCompile);
public Option<Dictionary<string, string>> ReferenceFilePaths { get; } =
new(new[] { "--reference", "-r" }, result => Helpers.BuildPathDictionary(result.Tokens, false), true, SR.ReferenceFiles);
public Option<string> InstructionSet { get; } =
new(new[] { "--instruction-set" }, SR.InstructionSets);
public Option<string[]> MibcFilePaths { get; } =
new(new[] { "--mibc", "-m" }, Array.Empty<string>, SR.MibcFiles);
public Option<string> OutputFilePath { get; } =
new(new[] { "--out", "-o" }, SR.OutputFilePath);
public Option<string> CompositeRootPath { get; } =
new(new[] { "--compositerootpath", "--crp" }, SR.CompositeRootPath);
public Option<bool> Optimize { get; } =
new(new[] { "--optimize", "-O" }, SR.EnableOptimizationsOption);
public Option<bool> OptimizeDisabled { get; } =
new(new[] { "--optimize-disabled", "--Od" }, SR.DisableOptimizationsOption);
public Option<bool> OptimizeSpace { get; } =
new(new[] { "--optimize-space", "--Os" }, SR.OptimizeSpaceOption);
public Option<bool> OptimizeTime { get; } =
new(new[] { "--optimize-time", "--Ot" }, SR.OptimizeSpeedOption);
public Option<bool> InputBubble { get; } =
new(new[] { "--inputbubble" }, SR.InputBubbleOption);
public Option<Dictionary<string, string>> InputBubbleReferenceFilePaths { get; } =
new(new[] { "--inputbubbleref" }, result => Helpers.BuildPathDictionary(result.Tokens, false), true, SR.InputBubbleReferenceFiles);
public Option<bool> Composite { get; } =
new(new[] { "--composite" }, SR.CompositeBuildMode);
public Option<string> CompositeKeyFile { get; } =
new(new[] { "--compositekeyfile" }, SR.CompositeKeyFile);
public Option<bool> CompileNoMethods { get; } =
new(new[] { "--compile-no-methods" }, SR.CompileNoMethodsOption);
public Option<bool> OutNearInput { get; } =
new(new[] { "--out-near-input" }, SR.OutNearInputOption);
public Option<bool> SingleFileCompilation { get; } =
new(new[] { "--single-file-compilation" }, SR.SingleFileCompilationOption);
public Option<bool> Partial { get; } =
new(new[] { "--partial" }, SR.PartialImageOption);
public Option<bool> CompileBubbleGenerics { get; } =
new(new[] { "--compilebubblegenerics" }, SR.BubbleGenericsOption);
public Option<bool> EmbedPgoData { get; } =
new(new[] { "--embed-pgo-data" }, SR.EmbedPgoDataOption);
public Option<string> DgmlLogFileName { get; } =
new(new[] { "--dgmllog" }, SR.SaveDependencyLogOption);
public Option<bool> GenerateFullDgmlLog { get; } =
new(new[] { "--fulllog" }, SR.SaveDetailedLogOption);
public Option<bool> IsVerbose { get; } =
new(new[] { "--verbose" }, SR.VerboseLoggingOption);
public Option<string> SystemModuleName { get; } =
new(new[] { "--systemmodule" }, () => Helpers.DefaultSystemModule, SR.SystemModuleOverrideOption);
public Option<bool> WaitForDebugger { get; } =
new(new[] { "--waitfordebugger" }, SR.WaitForDebuggerOption);
public Option<string[]> CodegenOptions { get; } =
new(new[] { "--codegenopt" }, Array.Empty<string>, SR.CodeGenOptions);
public Option<bool> SupportIbc { get; } =
new(new[] { "--support-ibc" }, SR.SupportIbc);
public Option<bool> Resilient { get; } =
new(new[] { "--resilient" }, SR.ResilientOption);
public Option<string> ImageBase { get; } =
new(new[] { "--imagebase" }, SR.ImageBase);
public Option<TargetArchitecture> TargetArchitecture { get; } =
new(new[] { "--targetarch" }, result =>
{
string firstToken = result.Tokens.Count > 0 ? result.Tokens[0].Value : null;
if (firstToken != null && firstToken.Equals("armel", StringComparison.OrdinalIgnoreCase))
{
IsArmel = true;
return Internal.TypeSystem.TargetArchitecture.ARM;
}
return Helpers.GetTargetArchitecture(firstToken);
}, true, SR.TargetArchOption) { Arity = ArgumentArity.OneOrMore };
public Option<TargetOS> TargetOS { get; } =
new(new[] { "--targetos" }, result => Helpers.GetTargetOS(result.Tokens.Count > 0 ? result.Tokens[0].Value : null), true, SR.TargetOSOption);
public Option<string> JitPath { get; } =
new(new[] { "--jitpath" }, SR.JitPathOption);
public Option<bool> PrintReproInstructions { get; } =
new(new[] { "--print-repro-instructions" }, SR.PrintReproInstructionsOption);
public Option<string> SingleMethodTypeName { get; } =
new(new[] { "--singlemethodtypename" }, SR.SingleMethodTypeName);
public Option<string> SingleMethodName { get; } =
new(new[] { "--singlemethodname" }, SR.SingleMethodMethodName);
public Option<int> SingleMethodIndex { get; } =
new(new[] { "--singlemethodindex" }, SR.SingleMethodIndex);
public Option<string[]> SingleMethodGenericArgs { get; } =
new(new[] { "--singlemethodgenericarg" }, SR.SingleMethodGenericArgs);
public Option<int> Parallelism { get; } =
new(new[] { "--parallelism" }, result =>
{
if (result.Tokens.Count > 0)
return int.Parse(result.Tokens[0].Value);
// Limit parallelism to 24 wide at most by default, more parallelism is unlikely to improve compilation speed
// as many portions of the process are single threaded, and is known to use excessive memory.
var parallelism = Math.Min(24, Environment.ProcessorCount);
// On 32bit platforms restrict it more, as virtual address space is quite limited
if (!Environment.Is64BitProcess)
parallelism = Math.Min(4, parallelism);
return parallelism;
}, true, SR.ParalellismOption);
public Option<int> CustomPESectionAlignment { get; } =
new(new[] { "--custom-pe-section-alignment" }, SR.CustomPESectionAlignmentOption);
public Option<bool> Map { get; } =
new(new[] { "--map" }, SR.MapFileOption);
public Option<bool> MapCsv { get; } =
new(new[] { "--mapcsv" }, SR.MapCsvFileOption);
public Option<bool> Pdb { get; } =
new(new[] { "--pdb" }, SR.PdbFileOption);
public Option<string> PdbPath { get; } =
new(new[] { "--pdb-path" }, SR.PdbFilePathOption);
public Option<bool> PerfMap { get; } =
new(new[] { "--perfmap" }, SR.PerfMapFileOption);
public Option<string> PerfMapPath { get; } =
new(new[] { "--perfmap-path" }, SR.PerfMapFilePathOption);
public Option<int> PerfMapFormatVersion { get; } =
new(new[] { "--perfmap-format-version" }, () => 0, SR.PerfMapFormatVersionOption);
public Option<string[]> CrossModuleInlining { get; } =
new(new[] { "--opt-cross-module" }, SR.CrossModuleInlining);
public Option<bool> AsyncMethodOptimization { get; } =
new(new[] { "--opt-async-methods" }, SR.AsyncModuleOptimization);
public Option<string> NonLocalGenericsModule { get; } =
new(new[] { "--non-local-generics-module" }, () => string.Empty, SR.NonLocalGenericsModule);
public Option<ReadyToRunMethodLayoutAlgorithm> MethodLayout { get; } =
new(new[] { "--method-layout" }, result =>
{
if (result.Tokens.Count == 0 )
return ReadyToRunMethodLayoutAlgorithm.DefaultSort;
return result.Tokens[0].Value.ToLowerInvariant() switch
{
"defaultsort" => ReadyToRunMethodLayoutAlgorithm.DefaultSort,
"exclusiveweight" => ReadyToRunMethodLayoutAlgorithm.ExclusiveWeight,
"hotcold" => ReadyToRunMethodLayoutAlgorithm.HotCold,
"hotwarmcold" => ReadyToRunMethodLayoutAlgorithm.HotWarmCold,
"callfrequency" => ReadyToRunMethodLayoutAlgorithm.CallFrequency,
"pettishansen" => ReadyToRunMethodLayoutAlgorithm.PettisHansen,
"random" => ReadyToRunMethodLayoutAlgorithm.Random,
_ => throw new CommandLineException(SR.InvalidMethodLayout)
};
}, true, SR.MethodLayoutOption);
public Option<ReadyToRunFileLayoutAlgorithm> FileLayout { get; } =
new(new[] { "--file-layout" }, result =>
{
if (result.Tokens.Count == 0 )
return ReadyToRunFileLayoutAlgorithm.DefaultSort;
return result.Tokens[0].Value.ToLowerInvariant() switch
{
"defaultsort" => ReadyToRunFileLayoutAlgorithm.DefaultSort,
"methodorder" => ReadyToRunFileLayoutAlgorithm.MethodOrder,
_ => throw new CommandLineException(SR.InvalidFileLayout)
};
}, true, SR.FileLayoutOption);
public Option<bool> VerifyTypeAndFieldLayout { get; } =
new(new[] { "--verify-type-and-field-layout" }, SR.VerifyTypeAndFieldLayoutOption);
public Option<string> CallChainProfileFile { get; } =
new(new[] { "--callchain-profile" }, SR.CallChainProfileFile);
public Option<string> MakeReproPath { get; } =
new(new[] { "--make-repro-path" }, "Path where to place a repro package");
public Option<bool> HotColdSplitting { get; } =
new(new[] { "--hot-cold-splitting" }, SR.HotColdSplittingOption);
public Option<bool> SynthesizeRandomMibc { get; } =
new(new[] { "--synthesize-random-mibc" });
public bool CompositeOrInputBubble { get; private set; }
public OptimizationMode OptimizationMode { get; private set; }
public ParseResult Result { get; private set; }
public static bool IsArmel { get; private set; }
public Crossgen2RootCommand(string[] args) : base(SR.Crossgen2BannerText)
{
AddArgument(InputFilePaths);
AddOption(UnrootedInputFilePaths);
AddOption(ReferenceFilePaths);
AddOption(InstructionSet);
AddOption(MibcFilePaths);
AddOption(OutputFilePath);
AddOption(CompositeRootPath);
AddOption(Optimize);
AddOption(OptimizeDisabled);
AddOption(OptimizeSpace);
AddOption(OptimizeTime);
AddOption(InputBubble);
AddOption(InputBubbleReferenceFilePaths);
AddOption(Composite);
AddOption(CompositeKeyFile);
AddOption(CompileNoMethods);
AddOption(OutNearInput);
AddOption(SingleFileCompilation);
AddOption(Partial);
AddOption(CompileBubbleGenerics);
AddOption(EmbedPgoData);
AddOption(DgmlLogFileName);
AddOption(GenerateFullDgmlLog);
AddOption(IsVerbose);
AddOption(SystemModuleName);
AddOption(WaitForDebugger);
AddOption(CodegenOptions);
AddOption(SupportIbc);
AddOption(Resilient);
AddOption(ImageBase);
AddOption(TargetArchitecture);
AddOption(TargetOS);
AddOption(JitPath);
AddOption(PrintReproInstructions);
AddOption(SingleMethodTypeName);
AddOption(SingleMethodName);
AddOption(SingleMethodIndex);
AddOption(SingleMethodGenericArgs);
AddOption(Parallelism);
AddOption(CustomPESectionAlignment);
AddOption(Map);
AddOption(MapCsv);
AddOption(Pdb);
AddOption(PdbPath);
AddOption(PerfMap);
AddOption(PerfMapPath);
AddOption(PerfMapFormatVersion);
AddOption(CrossModuleInlining);
AddOption(AsyncMethodOptimization);
AddOption(NonLocalGenericsModule);
AddOption(MethodLayout);
AddOption(FileLayout);
AddOption(VerifyTypeAndFieldLayout);
AddOption(CallChainProfileFile);
AddOption(MakeReproPath);
AddOption(HotColdSplitting);
AddOption(SynthesizeRandomMibc);
this.SetHandler(context =>
{
Result = context.ParseResult;
CompositeOrInputBubble = context.ParseResult.GetValue(Composite) | context.ParseResult.GetValue(InputBubble);
if (context.ParseResult.GetValue(OptimizeSpace))
{
OptimizationMode = OptimizationMode.PreferSize;
}
else if (context.ParseResult.GetValue(OptimizeTime))
{
OptimizationMode = OptimizationMode.PreferSpeed;
}
else if (context.ParseResult.GetValue(Optimize))
{
OptimizationMode = OptimizationMode.Blended;
}
else
{
OptimizationMode = OptimizationMode.None;
}
try
{
int alignment = context.ParseResult.GetValue(CustomPESectionAlignment);
if (alignment != 0)
{
// Must be a power of two and >= 4096
if (alignment < 4096 || (alignment & (alignment - 1)) != 0)
throw new CommandLineException(SR.InvalidCustomPESectionAlignment);
}
string makeReproPath = context.ParseResult.GetValue(MakeReproPath);
if (makeReproPath != null)
{
// Create a repro package in the specified path
// This package will have the set of input files needed for compilation
// + the original command line arguments
// + a rsp file that should work to directly run out of the zip file
Helpers.MakeReproPackage(makeReproPath, context.ParseResult.GetValue(OutputFilePath), args,
context.ParseResult, new[] { "r", "reference", "u", "unrooted-input-file-paths", "m", "mibc", "inputbubbleref" });
}
context.ExitCode = new Program(this).Run();
}
#if DEBUG
catch (CodeGenerationFailedException ex) when (DumpReproArguments(ex))
{
throw new NotSupportedException(); // Unreachable
}
#else
catch (Exception e)
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Error: " + e.Message);
Console.Error.WriteLine(e.ToString());
Console.ResetColor();
context.ExitCode = 1;
}
#endif
});
}
public static IEnumerable<Action<HelpContext>> GetExtendedHelp(HelpContext _)
{
foreach (Action<HelpContext> sectionDelegate in HelpBuilder.Default.GetLayout())
yield return sectionDelegate;
yield return _ =>
{
Console.WriteLine(SR.OptionPassingHelp);
Console.WriteLine();
Console.WriteLine(SR.DashDashHelp);
Console.WriteLine();
string[] ValidArchitectures = new string[] {"arm", "armel", "arm64", "x86", "x64"};
string[] ValidOS = new string[] {"windows", "linux", "osx"};
Console.WriteLine(String.Format(SR.SwitchWithDefaultHelp, "--targetos", String.Join("', '", ValidOS), Helpers.GetTargetOS(null).ToString().ToLowerInvariant()));
Console.WriteLine();
Console.WriteLine(String.Format(SR.SwitchWithDefaultHelp, "--targetarch", String.Join("', '", ValidArchitectures), Helpers.GetTargetArchitecture(null).ToString().ToLowerInvariant()));
Console.WriteLine();
Console.WriteLine(SR.InstructionSetHelp);
foreach (string arch in ValidArchitectures)
{
Console.Write(arch);
Console.Write(": ");
TargetArchitecture targetArch = Helpers.GetTargetArchitecture(arch);
bool first = true;
foreach (var instructionSet in Internal.JitInterface.InstructionSetFlags.ArchitectureToValidInstructionSets(targetArch))
{
// Only instruction sets with are specifiable should be printed to the help text
if (instructionSet.Specifiable)
{
if (first)
{
first = false;
}
else
{
Console.Write(", ");
}
Console.Write(instructionSet.Name);
}
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine(SR.CpuFamilies);
Console.WriteLine(string.Join(", ", Internal.JitInterface.InstructionSetFlags.AllCpuNames));
};
}
#if DEBUG
private static bool DumpReproArguments(CodeGenerationFailedException ex)
{
Console.WriteLine(SR.DumpReproInstructions);
MethodDesc failingMethod = ex.Method;
Console.WriteLine(Program.CreateReproArgumentString(failingMethod));
return false;
}
#endif
}
}