Merge pull request #532 from Sergio0694/dev/trim-computesharp-configuration
Add ILLink file for ComputeSharp configuration
このコミットが含まれているのは:
5ファイル変更
+160
-15
検索条件に一致するファイルはありません
@@ -34,6 +34,17 @@
|
||||
<IlcOptimizationPreference>Speed</IlcOptimizationPreference>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--
|
||||
Manually set all configuration switches to trim unused features. These are usually set automatically by the
|
||||
.targets file included in ComputeSharp, but since this project is directly referencing the project and not
|
||||
going through NuGet, it can't rely on that, so it's just copying those three directives here.
|
||||
-->
|
||||
<ItemGroup>
|
||||
<RuntimeHostConfigurationOption Include="COMPUTESHARP_ENABLE_DEBUG_OUTPUT" Value="false" Trim="true" />
|
||||
<RuntimeHostConfigurationOption Include="COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA" Value="false" Trim="true" />
|
||||
<RuntimeHostConfigurationOption Include="COMPUTESHARP_DISABLE_GPU_TIMEOUT" Value="false" Trim="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- If requested, also reference the UPX compression package -->
|
||||
<ItemGroup Condition="'$(PUBLISH_AOT_COMPRESSED)' == 'true'">
|
||||
<PackageReference Include="PublishAotCompressed" Version="1.0.0" />
|
||||
|
||||
@@ -28,6 +28,11 @@
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Include the ILLink file on the .NET 6 target (to properly trim configuration switches in publish builds) -->
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
|
||||
<EmbeddedResource Include="Properties\ILLink.Substitutions.xml" LogicalName="ILLink.Substitutions.xml" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- T4 template generation service (the .tt/.g.cs files are resolved in the .targets file) -->
|
||||
<ItemGroup>
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
|
||||
@@ -57,4 +57,26 @@
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
<!-- Automatically set the configuration switches -->
|
||||
<ItemGroup>
|
||||
|
||||
<!-- COMPUTESHARP_ENABLE_DEBUG_OUTPUT switch -->
|
||||
<RuntimeHostConfigurationOption Condition="'$(ComputeSharpEnableDebugOutput)' != ''"
|
||||
Include="COMPUTESHARP_ENABLE_DEBUG_OUTPUT"
|
||||
Value="$(ComputeSharpEnableDebugOutput)"
|
||||
Trim="true" />
|
||||
|
||||
<!-- COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA switch -->
|
||||
<RuntimeHostConfigurationOption Condition="'$(ComputeSharpEnableDeviceRemovedExtendedData)' != ''"
|
||||
Include="COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"
|
||||
Value="$(ComputeSharpEnableDeviceRemovedExtendedData)"
|
||||
Trim="true" />
|
||||
|
||||
<!-- COMPUTESHARP_DISABLE_GPU_TIMEOUT switch -->
|
||||
<RuntimeHostConfigurationOption Condition="'$(ComputeSharpDisableGpuTimeout)' != ''"
|
||||
Include="COMPUTESHARP_DISABLE_GPU_TIMEOUT"
|
||||
Value="$(ComputeSharpDisableGpuTimeout)"
|
||||
Trim="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -2,59 +2,148 @@ using System;
|
||||
#if DEBUG
|
||||
using System.Diagnostics;
|
||||
#endif
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// A container for all shared <see cref="AppContext"/> configuration switches for ComputeSharp.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This type uses a very specific setup for configuration switches to ensure ILLink can work the best.
|
||||
/// This mirrors the architecture of feature switches in the runtime as well, and it's needed so that
|
||||
/// no static constructor is generated for the type.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For more info, see <see href="https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md#adding-new-feature-switch"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal static class Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// The configuration property name for <see cref="IsDebugOutputEnabled"/>.
|
||||
/// </summary>
|
||||
private const string EnableDebugOutput = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT";
|
||||
private const string IsDebugOutputEnabledPropertyName = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT";
|
||||
|
||||
/// <summary>
|
||||
/// The configuration property name for <see cref="IsDeviceRemovedExtendedDataEnabled"/>.
|
||||
/// </summary>
|
||||
private const string EnableDeviceRemovedExtendedDataInfo = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA";
|
||||
private const string IsDeviceRemovedExtendedDataEnabledPropertyName = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA";
|
||||
|
||||
/// <summary>
|
||||
/// The configuration property name for <see cref="IsGpuTimeoutDisabled"/>.
|
||||
/// </summary>
|
||||
private const string DisableGpuTimeout = "COMPUTESHARP_DISABLE_GPU_TIMEOUT";
|
||||
private const string IsGpuTimeoutDisabledPropertyName = "COMPUTESHARP_DISABLE_GPU_TIMEOUT";
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not the debug output is enabled.
|
||||
/// The backing field for <see cref="IsDebugOutputEnabled"/>.
|
||||
/// </summary>
|
||||
public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput);
|
||||
private static int isDebugOutputEnabledConfigurationValue;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not the debug output is enabled.
|
||||
/// The backing field for <see cref="IsDeviceRemovedExtendedDataEnabled"/>.
|
||||
/// </summary>
|
||||
public static readonly bool IsDeviceRemovedExtendedDataEnabled = GetConfigurationValue(EnableDeviceRemovedExtendedDataInfo);
|
||||
private static int isDeviceRemovedExtendedDataEnabledConfigurationValue;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not the GPU timeout is disabled.
|
||||
/// The backing field for <see cref="IsGpuTimeoutDisabled"/>.
|
||||
/// </summary>
|
||||
public static readonly bool IsGpuTimeoutDisabled = GetConfigurationValue(DisableGpuTimeout);
|
||||
private static int isGpuTimeoutDisabledConfigurationValue;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the debug output is enabled (defaults to <see langword="false"/>).
|
||||
/// </summary>
|
||||
public static bool IsDebugOutputEnabled
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => GetConfigurationValue(IsDebugOutputEnabledPropertyName, ref isDebugOutputEnabledConfigurationValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the debug output is enabled (defaults to <see langword="false"/>).
|
||||
/// </summary>
|
||||
public static bool IsDeviceRemovedExtendedDataEnabled
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => GetConfigurationValue(IsDeviceRemovedExtendedDataEnabledPropertyName, ref isDeviceRemovedExtendedDataEnabledConfigurationValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the GPU timeout is disabled (defaults to <see langword="false"/>).
|
||||
/// </summary>
|
||||
public static bool IsGpuTimeoutDisabled
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => GetConfigurationValue(IsGpuTimeoutDisabledPropertyName, ref isGpuTimeoutDisabledConfigurationValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a configuration value for a specified property.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The property name to retrieve the value for.</param>
|
||||
/// <param name="cachedResult">The cached result for the target configuration value.</param>
|
||||
/// <returns>The value of the specified configuration setting.</returns>
|
||||
private static bool GetConfigurationValue(string propertyName)
|
||||
private static bool GetConfigurationValue(string propertyName, ref int cachedResult)
|
||||
{
|
||||
#if DEBUG
|
||||
if (Debugger.IsAttached && propertyName != DisableGpuTimeout)
|
||||
// The cached switch value has 3 states:
|
||||
// 0: unknown.
|
||||
// 1: true
|
||||
// -1: false
|
||||
//
|
||||
// This method doesn't need to worry about concurrent accesses to the cached result,
|
||||
// as even if the configuration value is retrieved twice, that'll always be the same.
|
||||
if (cachedResult < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cachedResult > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (AppContext.TryGetSwitch(propertyName, out bool isEnabled))
|
||||
// Get the configuration switch value, or its default
|
||||
if (!AppContext.TryGetSwitch(propertyName, out bool isEnabled))
|
||||
{
|
||||
return isEnabled;
|
||||
isEnabled = GetDefaultConfigurationValue(propertyName);
|
||||
}
|
||||
|
||||
// Update the cached result
|
||||
cachedResult = isEnabled ? 1 : -1;
|
||||
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default configuration value for a given feature switch.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The property name to retrieve the value for.</param>
|
||||
/// <returns>The default value for the target <paramref name="propertyName"/>.</returns>
|
||||
private static bool GetDefaultConfigurationValue(string propertyName)
|
||||
{
|
||||
// Debug output (always enabled in DEBUG if there is an attached debugger)
|
||||
if (propertyName == IsDebugOutputEnabledPropertyName)
|
||||
{
|
||||
#if DEBUG
|
||||
return Debugger.IsAttached;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Device removed extended data (same as for the debug output)
|
||||
if (propertyName == IsDeviceRemovedExtendedDataEnabledPropertyName)
|
||||
{
|
||||
#if DEBUG
|
||||
return Debugger.IsAttached;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Disable GPU timeout (always false by default)
|
||||
if (propertyName == IsGpuTimeoutDisabledPropertyName)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<linker>
|
||||
<assembly fullname="ComputeSharp">
|
||||
<type fullname="Configuration">
|
||||
|
||||
<!-- COMPUTESHARP_ENABLE_DEBUG_OUTPUT switch -->
|
||||
<method signature="System.Boolean get_IsDebugOutputEnabled()" body="stub" value="false" feature="COMPUTESHARP_ENABLE_DEBUG_OUTPUT" featurevalue="false"/>
|
||||
<method signature="System.Boolean get_IsDebugOutputEnabled()" body="stub" value="true" feature="COMPUTESHARP_ENABLE_DEBUG_OUTPUT" featurevalue="true"/>
|
||||
|
||||
<!-- COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA switch -->
|
||||
<method signature="System.Boolean get_IsDeviceRemovedExtendedDataEnabled()" body="stub" value="false" feature="COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA" featurevalue="false"/>
|
||||
<method signature="System.Boolean get_IsDeviceRemovedExtendedDataEnabled()" body="stub" value="true" feature="COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA" featurevalue="true"/>
|
||||
|
||||
<!-- COMPUTESHARP_DISABLE_GPU_TIMEOUT switch -->
|
||||
<method signature="System.Boolean get_IsGpuTimeoutDisabled()" body="stub" value="false" feature="COMPUTESHARP_DISABLE_GPU_TIMEOUT" featurevalue="false"/>
|
||||
<method signature="System.Boolean get_IsGpuTimeoutDisabled()" body="stub" value="true" feature="COMPUTESHARP_DISABLE_GPU_TIMEOUT" featurevalue="true"/>
|
||||
</type>
|
||||
</assembly>
|
||||
</linker>
|
||||
新しいイシューから参照
ユーザーをブロックする