-
-
Notifications
You must be signed in to change notification settings - Fork 948
/
Generator.cs
282 lines (254 loc) · 15.5 KB
/
Generator.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BenchmarkDotNet.ConsoleArguments;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Portability.Cpu;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
using BenchmarkDotNet.Toolchains.DotNetCli;
namespace BenchmarkDotNet.Toolchains.NativeAot
{
/// <summary>
/// generates new csproj file for self-contained NativeAOT app
/// based on https://github.com/dotnet/corert/blob/7f902d4d8b1c3280e60f5e06c71951a60da173fb/Documentation/how-to-build-and-run-ilcompiler-in-console-shell-prompt.md#compiling-source-to-native-code-using-the-ilcompiler-you-built
/// and https://github.com/dotnet/corert/tree/7f902d4d8b1c3280e60f5e06c71951a60da173fb/samples/HelloWorld#add-corert-to-your-project
/// </summary>
public class Generator : CsProjGenerator
{
internal const string NativeAotNuGetFeed = "nativeAotNuGetFeed";
internal const string GeneratedRdXmlFileName = "bdn_generated.rd.xml";
internal Generator(string ilCompilerVersion,
string runtimeFrameworkVersion, string targetFrameworkMoniker, string cliPath,
string runtimeIdentifier, IReadOnlyDictionary<string, string> feeds, bool useNuGetClearTag,
bool useTempFolderForRestore, string packagesRestorePath,
bool rootAllApplicationAssemblies, bool ilcGenerateCompleteTypeMetadata, bool ilcGenerateStackTraceData,
string ilcOptimizationPreference, string ilcInstructionSet)
: base(targetFrameworkMoniker, cliPath, GetPackagesDirectoryPath(useTempFolderForRestore, packagesRestorePath), runtimeFrameworkVersion)
{
this.ilCompilerVersion = ilCompilerVersion;
this.runtimeIdentifier = runtimeIdentifier;
this.Feeds = feeds;
this.useNuGetClearTag = useNuGetClearTag;
this.useTempFolderForRestore = useTempFolderForRestore;
this.rootAllApplicationAssemblies = rootAllApplicationAssemblies;
this.ilcGenerateCompleteTypeMetadata = ilcGenerateCompleteTypeMetadata;
this.ilcGenerateStackTraceData = ilcGenerateStackTraceData;
this.ilcOptimizationPreference = ilcOptimizationPreference;
this.ilcInstructionSet = ilcInstructionSet;
}
internal readonly IReadOnlyDictionary<string, string> Feeds;
private readonly string ilCompilerVersion;
private readonly string runtimeIdentifier;
private readonly bool useNuGetClearTag;
private readonly bool useTempFolderForRestore;
private readonly bool rootAllApplicationAssemblies;
private readonly bool ilcGenerateCompleteTypeMetadata;
private readonly bool ilcGenerateStackTraceData;
private readonly string ilcOptimizationPreference;
private readonly string ilcInstructionSet;
protected override string GetExecutableExtension() => RuntimeInformation.ExecutableExtension;
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
=> useTempFolderForRestore
? Path.Combine(Path.GetTempPath(), programName) // store everything in temp to avoid collisions with IDE
: base.GetBuildArtifactsDirectoryPath(buildPartition, programName);
protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration)
=> Path.Combine(buildArtifactsDirectoryPath, "bin", configuration, TargetFrameworkMoniker, runtimeIdentifier, "publish");
protected override void GenerateBuildScript(BuildPartition buildPartition, ArtifactsPaths artifactsPaths)
{
string extraArguments = NativeAotToolchain.GetExtraArguments(runtimeIdentifier);
var content = new StringBuilder(300)
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetRestoreCommand(artifactsPaths, buildPartition, extraArguments)}")
.AppendLine($"call {CliPath ?? "dotnet"} {DotNetCliCommand.GetPublishCommand(artifactsPaths, buildPartition, extraArguments)}")
.ToString();
File.WriteAllText(artifactsPaths.BuildScriptFilePath, content);
}
// we always want to have a new directory for NuGet packages restore
// to avoid this https://github.com/dotnet/coreclr/blob/master/Documentation/workflow/UsingDotNetCli.md#update-coreclr-using-runtime-nuget-package
// some of the packages are going to contain source code, so they can not be in the subfolder of current solution
// otherwise they would be compiled too (new .csproj include all .cs files from subfolders by default
private static string GetPackagesDirectoryPath(bool useTempFolderForRestore, string packagesRestorePath)
=> packagesRestorePath ?? (useTempFolderForRestore ? Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) : null);
protected override string[] GetArtifactsToCleanup(ArtifactsPaths artifactsPaths)
=> useTempFolderForRestore
? base.GetArtifactsToCleanup(artifactsPaths).Concat(new[] { artifactsPaths.PackagesDirectoryName }).ToArray()
: base.GetArtifactsToCleanup(artifactsPaths);
protected override void GenerateNuGetConfig(ArtifactsPaths artifactsPaths)
{
if (!Feeds.Any())
return;
string content =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
{(useNuGetClearTag ? "<clear/>" : string.Empty)}
{string.Join(Environment.NewLine + " ", Feeds.Select(feed => $"<add key=\"{feed.Key}\" value=\"{feed.Value}\" />"))}
</packageSources>
</configuration>";
File.WriteAllText(artifactsPaths.NuGetConfigPath, content);
}
protected override void GenerateProject(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger)
{
File.WriteAllText(artifactsPaths.ProjectFilePath, GenerateProjectForNuGetBuild(buildPartition, artifactsPaths, logger));
GenerateReflectionFile(artifactsPaths);
}
private string GenerateProjectForNuGetBuild(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger) => $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<ImportDirectoryBuildProps>false</ImportDirectoryBuildProps>
<ImportDirectoryBuildTargets>false</ImportDirectoryBuildTargets>
<OutputType>Exe</OutputType>
<TargetFramework>{TargetFrameworkMoniker}</TargetFramework>
<RuntimeIdentifier>{runtimeIdentifier}</RuntimeIdentifier>
<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>
<AssemblyName>{artifactsPaths.ProgramName}</AssemblyName>
<AssemblyTitle>{artifactsPaths.ProgramName}</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>{buildPartition.Platform.ToConfig()}</PlatformTarget>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugSymbols>false</DebugSymbols>
<UseSharedCompilation>false</UseSharedCompilation>
<Deterministic>true</Deterministic>
<RunAnalyzers>false</RunAnalyzers>
<PublishAot Condition="" '$(TargetFramework)' != 'net6.0' "">true</PublishAot>
<IlcOptimizationPreference>{ilcOptimizationPreference}</IlcOptimizationPreference>
{GetTrimmingSettings()}
<IlcGenerateCompleteTypeMetadata>{ilcGenerateCompleteTypeMetadata}</IlcGenerateCompleteTypeMetadata>
<IlcGenerateStackTraceData>{ilcGenerateStackTraceData}</IlcGenerateStackTraceData>
<EnsureNETCoreAppRuntime>false</EnsureNETCoreAppRuntime> <!-- workaround for 'This runtime may not be supported by.NET Core.' error -->
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles> <!-- workaround for 'Found multiple publish output files with the same relative path.' error -->
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
{GetInstructionSetSettings(buildPartition)}
</PropertyGroup>
{GetRuntimeSettings(buildPartition.RepresentativeBenchmarkCase.Job.Environment.Gc, buildPartition.Resolver)}
<ItemGroup>
<Compile Include=""{Path.GetFileName(artifactsPaths.ProgramCodePath)}"" Exclude=""bin\**;obj\**;**\*.xproj;packages\**"" />
</ItemGroup>
<ItemGroup>
{GetILCompilerPackageReference()}
<ProjectReference Include=""{GetProjectFilePath(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger).FullName}"" />
</ItemGroup>
<ItemGroup>
{string.Join(Environment.NewLine, GetRdXmlFiles(buildPartition.RepresentativeBenchmarkCase.Descriptor.Type, logger).Select(file => $"<RdXmlFile Include=\"{file}\" />"))}
</ItemGroup>
</Project>";
private string GetILCompilerPackageReference()
=> string.IsNullOrEmpty(ilCompilerVersion) ? "" : $@"<PackageReference Include=""Microsoft.DotNet.ILCompiler"" Version=""{ilCompilerVersion}"" />";
private string GetTrimmingSettings()
=> rootAllApplicationAssemblies
// Use the defaults
? ""
// TrimMode is set in explicit way as for older versions it might have different default value
: "<TrimMode>link</TrimMode><TrimmerDefaultAction>link</TrimmerDefaultAction>";
private string GetInstructionSetSettings(BuildPartition buildPartition)
{
string instructionSet = ilcInstructionSet ?? GetCurrentInstructionSet(buildPartition.Platform);
return !string.IsNullOrEmpty(instructionSet) ? $"<IlcInstructionSet>{instructionSet}</IlcInstructionSet>" : "";
}
public IEnumerable<string> GetRdXmlFiles(Type benchmarkTarget, ILogger logger)
{
yield return GeneratedRdXmlFileName;
var projectFile = GetProjectFilePath(benchmarkTarget, logger);
var projectFileFolder = projectFile.DirectoryName;
var rdXml = Path.Combine(projectFileFolder, "rd.xml");
if (File.Exists(rdXml))
{
yield return rdXml;
}
foreach (var item in Directory.GetFiles(projectFileFolder, "*.rd.xml"))
{
yield return item;
}
}
/// <summary>
/// mandatory to make it possible to call GC.GetAllocatedBytesForCurrentThread() using reflection (not part of .NET Standard)
/// </summary>
private void GenerateReflectionFile(ArtifactsPaths artifactsPaths)
{
const string content = @"
<Directives>
<Application>
<Assembly Name=""System.Runtime"">
<Type Name=""System.GC"" Dynamic=""Required All"" />
</Assembly>
<Assembly Name=""System.Threading.ThreadPool"">
<Type Name=""System.Threading.ThreadPool"" Dynamic=""Required All"" />
</Assembly>
<Assembly Name=""System.Threading"">
<Type Name=""System.Threading.Monitor"" Dynamic=""Required All"" />
</Assembly>
</Application>
</Directives>
";
string directoryName = Path.GetDirectoryName(artifactsPaths.ProjectFilePath);
if (directoryName != null)
File.WriteAllText(Path.Combine(directoryName, GeneratedRdXmlFileName), content);
else
throw new InvalidOperationException($"Can't get directory of projectFilePath ('{artifactsPaths.ProjectFilePath}')");
}
private string GetCurrentInstructionSet(Platform platform)
=> string.Join(",", GetCurrentProcessInstructionSets(platform));
// based on https://github.com/dotnet/runtime/blob/ce61c09a5f6fc71d8f717d3fc4562f42171869a0/src/coreclr/tools/Common/JitInterface/CorInfoInstructionSet.cs#L727
private IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
{
if (!ConfigParser.TryParse(TargetFrameworkMoniker, out RuntimeMoniker runtimeMoniker))
{
throw new NotSupportedException($"Invalid TFM: '{TargetFrameworkMoniker}'");
}
if (platform == RuntimeInformation.GetCurrentPlatform() // "native" does not support cross-compilation (so does BDN for now)
&& runtimeMoniker >= RuntimeMoniker.NativeAot80)
{
yield return "native"; // added in .NET 8 https://github.com/dotnet/runtime/pull/87865
yield break;
}
switch (platform)
{
case Platform.X86:
case Platform.X64:
if (HardwareIntrinsics.IsX86BaseSupported) yield return "base";
if (HardwareIntrinsics.IsX86SseSupported) yield return "sse";
if (HardwareIntrinsics.IsX86Sse2Supported) yield return "sse2";
if (HardwareIntrinsics.IsX86Sse3Supported) yield return "sse3";
if (HardwareIntrinsics.IsX86Ssse3Supported) yield return "ssse3";
if (HardwareIntrinsics.IsX86Sse41Supported) yield return "sse4.1";
if (HardwareIntrinsics.IsX86Sse42Supported) yield return "sse4.2";
if (HardwareIntrinsics.IsX86AvxSupported) yield return "avx";
if (HardwareIntrinsics.IsX86Avx2Supported) yield return "avx2";
if (HardwareIntrinsics.IsX86Avx512FSupported) yield return "avx-512f";
if (HardwareIntrinsics.IsX86Avx512BWSupported) yield return "avx-512bw";
if (HardwareIntrinsics.IsX86Avx512CDSupported) yield return "avx-512cd";
if (HardwareIntrinsics.IsX86Avx512DQSupported) yield return "avx-512dq";
if (HardwareIntrinsics.IsX86Avx512VbmiSupported) yield return "avx-512vbmi";
if (HardwareIntrinsics.IsX86AesSupported) yield return "aes";
if (HardwareIntrinsics.IsX86Bmi1Supported) yield return "bmi";
if (HardwareIntrinsics.IsX86Bmi2Supported) yield return "bmi2";
if (HardwareIntrinsics.IsX86FmaSupported) yield return "fma";
if (HardwareIntrinsics.IsX86LzcntSupported) yield return "lzcnt";
if (HardwareIntrinsics.IsX86PclmulqdqSupported) yield return "pclmul";
if (HardwareIntrinsics.IsX86PopcntSupported) yield return "popcnt";
if (HardwareIntrinsics.IsX86AvxVnniSupported) yield return "avxvnni";
if (HardwareIntrinsics.IsX86SerializeSupported && runtimeMoniker > RuntimeMoniker.NativeAot70) yield return "serialize"; // https://github.com/dotnet/BenchmarkDotNet/issues/2463#issuecomment-1809625008
break;
case Platform.Arm64:
if (HardwareIntrinsics.IsArmBaseSupported) yield return "base";
if (HardwareIntrinsics.IsArmAdvSimdSupported) yield return "neon";
if (HardwareIntrinsics.IsArmAesSupported) yield return "aes";
if (HardwareIntrinsics.IsArmCrc32Supported) yield return "crc";
if (HardwareIntrinsics.IsArmDpSupported) yield return "dotprod";
if (HardwareIntrinsics.IsArmRdmSupported) yield return "rdma";
if (HardwareIntrinsics.IsArmSha1Supported) yield return "sha1";
if (HardwareIntrinsics.IsArmSha256Supported) yield return "sha2";
// todo: handle "lse"
break;
default:
yield break;
}
}
}
}