-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathMonoAOTCompiler.cs
437 lines (371 loc) · 14.8 KB
/
MonoAOTCompiler.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// 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.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Path to AOT cross-compiler binary (mono-aot-cross)
/// </summary>
[Required]
public string CompilerBinaryPath { get; set; } = ""!;
/// <summary>
/// Assemblies to be AOTd. They need to be in a self-contained directory.
///
/// Metadata:
/// - AotArguments: semicolon-separated list of options that will be passed to --aot=
/// - ProcessArguments: semicolon-separated list of options that will be passed to the AOT compiler itself
/// </summary>
[Required]
public ITaskItem[] Assemblies { get; set; } = Array.Empty<ITaskItem>();
/// <summary>
/// Assemblies which were AOT compiled.
///
/// Successful AOT compilation will set the following metadata on the items:
/// - AssemblerFile (when using OutputType=AsmOnly)
/// - ObjectFile (when using OutputType=Normal)
/// - AotDataFile
/// - LlvmObjectFile (if using LLVM)
/// - LlvmBitcodeFile (if using LLVM-only)
/// </summary>
[Output]
public ITaskItem[]? CompiledAssemblies { get; set; }
/// <summary>
/// Disable parallel AOT compilation
/// </summary>
public bool DisableParallelAot { get; set; }
/// <summary>
/// Use LLVM for AOT compilation.
/// The cross-compiler must be built with LLVM support
/// </summary>
public bool UseLLVM { get; set; }
/// <summary>
/// Use a separate .aotdata file for the AOT data.
/// Defaults to true.
/// </summary>
public bool UseAotDataFile { get; set; } = true;
/// <summary>
/// File to use for profile-guided optimization.
/// </summary>
public string? AotProfilePath { get; set; }
/// <summary>
/// List of profilers to use.
/// </summary>
public string[]? Profilers { get; set; }
/// <summary>
/// Generate a file containing mono_aot_register_module() calls for each AOT module
/// Defaults to false.
/// </summary>
public string? AotModulesTablePath { get; set; }
/// <summary>
/// Source code language of the AOT modules table. Supports "C" or "ObjC".
/// Defaults to "C".
/// </summary>
public string? AotModulesTableLanguage { get; set; } = nameof(MonoAotModulesTableLanguage.C);
/// <summary>
/// Choose between 'Normal', 'Full', 'LLVMOnly'.
/// LLVMOnly means to use only LLVM for FullAOT, AOT result will be a LLVM Bitcode file (the cross-compiler must be built with LLVM support)
/// </summary>
public string Mode { get; set; } = nameof(MonoAotMode.Normal);
/// <summary>
/// Choose between 'Normal', 'AsmOnly'
/// AsmOnly means the AOT compiler will produce .s assembly code instead of an .o object file.
/// </summary>
public string OutputType { get; set; } = nameof(MonoAotOutputType.Normal);
/// <summary>
/// Path to the directory where LLVM binaries (opt and llc) are found.
/// It's required if UseLLVM is set
/// </summary>
public string? LLVMPath { get; set; }
/// <summary>
/// Path to the directory where msym artifacts are stored.
/// </summary>
public string? MsymPath { get; set; }
[Output]
public string[]? FileWrites { get; private set; }
private List<string> _fileWrites = new();
private ConcurrentBag<ITaskItem> compiledAssemblies = new ConcurrentBag<ITaskItem>();
private MonoAotMode parsedAotMode;
private MonoAotOutputType parsedOutputType;
private MonoAotModulesTableLanguage parsedAotModulesTableLanguage;
public override bool Execute()
{
Utils.Logger = Log;
if (string.IsNullOrEmpty(CompilerBinaryPath))
{
throw new ArgumentException($"'{nameof(CompilerBinaryPath)}' is required.", nameof(CompilerBinaryPath));
}
if (!File.Exists(CompilerBinaryPath))
{
throw new ArgumentException($"'{CompilerBinaryPath}' doesn't exist.", nameof(CompilerBinaryPath));
}
if (Assemblies.Length == 0)
{
throw new ArgumentException($"'{nameof(Assemblies)}' is required.", nameof(Assemblies));
}
if (!string.IsNullOrEmpty(AotProfilePath) && !File.Exists(AotProfilePath))
{
Log.LogError($"'{AotProfilePath}' doesn't exist.", nameof(AotProfilePath));
return false;
}
if (UseLLVM)
{
if (string.IsNullOrEmpty(LLVMPath))
// prevent using some random llc/opt from PATH (installed with clang)
throw new ArgumentException($"'{nameof(LLVMPath)}' is required when '{nameof(UseLLVM)}' is true.", nameof(LLVMPath));
if (!Directory.Exists(LLVMPath))
{
Log.LogError($"Could not find LLVMPath=${LLVMPath}");
return false;
}
}
if (!Enum.TryParse(Mode, true, out parsedAotMode))
{
Log.LogError($"Unknown Mode value: {Mode}. '{nameof(Mode)}' must be one of: {string.Join(',', Enum.GetNames(typeof(MonoAotMode)))}");
return false;
}
switch (OutputType)
{
case "Normal": parsedOutputType = MonoAotOutputType.Normal; break;
case "AsmOnly": parsedOutputType = MonoAotOutputType.AsmOnly; break;
default:
throw new ArgumentException($"'{nameof(OutputType)}' must be one of: '{nameof(MonoAotOutputType.Normal)}', '{nameof(MonoAotOutputType.AsmOnly)}'. Received: '{OutputType}'.", nameof(OutputType));
}
if (parsedAotMode == MonoAotMode.LLVMOnly && !UseLLVM)
{
throw new ArgumentException($"'{nameof(UseLLVM)}' must be true when '{nameof(Mode)}' is {nameof(MonoAotMode.LLVMOnly)}.", nameof(UseLLVM));
}
switch (AotModulesTableLanguage)
{
case "C": parsedAotModulesTableLanguage = MonoAotModulesTableLanguage.C; break;
case "ObjC": parsedAotModulesTableLanguage = MonoAotModulesTableLanguage.ObjC; break;
default:
throw new ArgumentException($"'{nameof(AotModulesTableLanguage)}' must be one of: '{nameof(MonoAotModulesTableLanguage.C)}', '{nameof(MonoAotModulesTableLanguage.ObjC)}'. Received: '{AotModulesTableLanguage}'.", nameof(AotModulesTableLanguage));
}
if (!string.IsNullOrEmpty(AotModulesTablePath))
{
GenerateAotModulesTable(Assemblies, Profilers);
}
if (DisableParallelAot)
{
foreach (var assemblyItem in Assemblies)
{
if (!PrecompileLibrary(assemblyItem))
return !Log.HasLoggedErrors;
}
}
else
{
Parallel.ForEach(Assemblies,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
assemblyItem => PrecompileLibrary (assemblyItem));
}
CompiledAssemblies = compiledAssemblies.ToArray();
FileWrites = _fileWrites.ToArray();
return !Log.HasLoggedErrors;
}
private bool PrecompileLibrary(ITaskItem assemblyItem)
{
string assembly = assemblyItem.ItemSpec;
string directory = Path.GetDirectoryName(assembly)!;
var aotAssembly = new TaskItem(assembly);
var aotArgs = new List<string>();
var processArgs = new List<string>();
var a = assemblyItem.GetMetadata("AotArguments");
if (a != null)
{
aotArgs.AddRange(a.Split(";", StringSplitOptions.RemoveEmptyEntries));
}
var p = assemblyItem.GetMetadata("ProcessArguments");
if (p != null)
{
processArgs.AddRange(p.Split(";", StringSplitOptions.RemoveEmptyEntries));
}
Utils.LogInfo($"[AOT] {assembly}");
processArgs.Add("--debug");
// add LLVM options
if (UseLLVM)
{
processArgs.Add("--llvm");
aotArgs.Add($"nodebug"); // can't use debug symbols with LLVM
aotArgs.Add($"llvm-path={LLVMPath}");
}
else
{
processArgs.Add("--nollvm");
}
// compute output mode and file names
if (parsedAotMode == MonoAotMode.LLVMOnly || parsedAotMode == MonoAotMode.AotInterp)
{
aotArgs.Add("llvmonly");
string llvmBitcodeFile = Path.ChangeExtension(assembly, ".dll.bc");
aotAssembly.SetMetadata("LlvmBitcodeFile", llvmBitcodeFile);
if (parsedAotMode == MonoAotMode.AotInterp)
{
aotArgs.Add("interp");
}
if (parsedOutputType == MonoAotOutputType.AsmOnly)
{
aotArgs.Add("asmonly");
aotArgs.Add($"llvm-outfile={llvmBitcodeFile}");
}
else
{
aotArgs.Add($"outfile={llvmBitcodeFile}");
}
}
else
{
if (parsedAotMode == MonoAotMode.Full)
{
aotArgs.Add("full");
}
if (parsedOutputType == MonoAotOutputType.AsmOnly)
{
aotArgs.Add("asmonly");
string assemblerFile = Path.ChangeExtension(assembly, ".dll.s");
aotArgs.Add($"outfile={assemblerFile}");
aotAssembly.SetMetadata("AssemblerFile", assemblerFile);
}
else
{
string objectFile = Path.ChangeExtension(assembly, ".dll.o");
aotArgs.Add($"outfile={objectFile}");
aotAssembly.SetMetadata("ObjectFile", objectFile);
}
if (UseLLVM)
{
string llvmObjectFile = Path.ChangeExtension(assembly, ".dll-llvm.o");
aotArgs.Add($"llvm-outfile={llvmObjectFile}");
aotAssembly.SetMetadata("LlvmObjectFile", llvmObjectFile);
}
}
// pass msym-dir if specified
if (MsymPath != null)
{
aotArgs.Add($"msym-dir={MsymPath}");
}
if (UseAotDataFile)
{
string aotDataFile = Path.ChangeExtension(assembly, ".aotdata");
aotArgs.Add($"data-outfile={aotDataFile}");
aotAssembly.SetMetadata("AotDataFile", aotDataFile);
}
if (!string.IsNullOrEmpty(AotProfilePath))
{
aotArgs.Add($"profile={AotProfilePath},profile-only");
}
// we need to quote the entire --aot arguments here to make sure it is parsed
// on Windows as one argument. Otherwise it will be split up into multiple
// values, which wont work.
processArgs.Add($"\"--aot={string.Join(",", aotArgs)}\"");
processArgs.Add(assembly);
var envVariables = new Dictionary<string, string>
{
{"MONO_PATH", directory},
{"MONO_ENV_OPTIONS", string.Empty} // we do not want options to be provided out of band to the cross compilers
};
try
{
// run the AOT compiler
Utils.RunProcess(CompilerBinaryPath, string.Join(" ", processArgs), envVariables, directory, silent: false, outputMessageImportance: MessageImportance.Low);
}
catch (Exception ex)
{
Log.LogMessage(MessageImportance.Low, ex.ToString());
Log.LogError($"Precompiling failed for {assembly}: {ex.Message}");
return false;
}
compiledAssemblies.Add(aotAssembly);
return true;
}
private void GenerateAotModulesTable(ITaskItem[] assemblies, string[]? profilers)
{
var symbols = new List<string>();
foreach (var asm in assemblies)
{
var name = Path.GetFileNameWithoutExtension(asm.ItemSpec).Replace ('.', '_').Replace ('-', '_');
symbols.Add($"mono_aot_module_{name}_info");
}
Directory.CreateDirectory(Path.GetDirectoryName(AotModulesTablePath!)!);
using (var writer = File.CreateText(AotModulesTablePath!))
{
_fileWrites.Add(AotModulesTablePath!);
if (parsedAotModulesTableLanguage == MonoAotModulesTableLanguage.C)
{
foreach (var symbol in symbols)
{
writer.WriteLine($"extern void *{symbol};");
}
writer.WriteLine("static void register_aot_modules ()");
writer.WriteLine("{");
foreach (var symbol in symbols)
{
writer.WriteLine($"\tmono_aot_register_module ({symbol});");
}
writer.WriteLine("}");
foreach (var profiler in profilers ?? Enumerable.Empty<string>())
{
writer.WriteLine($"void mono_profiler_init_{profiler} (const char *desc);");
writer.WriteLine("EMSCRIPTEN_KEEPALIVE void mono_wasm_load_profiler_" + profiler + " (const char *desc) { mono_profiler_init_" + profiler + " (desc); }");
}
if (parsedAotMode == MonoAotMode.LLVMOnly)
{
writer.WriteLine("#define EE_MODE_LLVMONLY 1");
}
if (parsedAotMode == MonoAotMode.AotInterp)
{
writer.WriteLine("#define EE_MODE_LLVMONLY_INTERP 1");
}
}
else if (parsedAotModulesTableLanguage == MonoAotModulesTableLanguage.ObjC)
{
writer.WriteLine("#include <mono/jit/jit.h>");
writer.WriteLine("#include <TargetConditionals.h>");
writer.WriteLine("");
writer.WriteLine("#if TARGET_OS_IPHONE && (!TARGET_IPHONE_SIMULATOR || FORCE_AOT)");
foreach (var symbol in symbols)
{
writer.WriteLine($"extern void *{symbol};");
}
writer.WriteLine("void register_aot_modules (void)");
writer.WriteLine("{");
foreach (var symbol in symbols)
{
writer.WriteLine($"\tmono_aot_register_module ({symbol});");
}
writer.WriteLine("}");
writer.WriteLine("#endif");
}
else
{
throw new NotSupportedException();
}
Log.LogMessage(MessageImportance.Low, $"Generated {AotModulesTablePath}");
}
}
}
public enum MonoAotMode
{
Normal,
Full,
LLVMOnly,
AotInterp
}
public enum MonoAotOutputType
{
Normal,
AsmOnly,
}
public enum MonoAotModulesTableLanguage
{
C,
ObjC
}