From a2b7fbd841556a4fbad89989b8479ac06c312e58 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 24 Jun 2023 12:41:26 +0200 Subject: [PATCH 1/5] Add ILLink file for ComputeSharp configuration --- src/ComputeSharp/ComputeSharp.csproj | 5 ++++ src/ComputeSharp/Properties/Configuration.cs | 6 ++-- .../Properties/ILLink.Substitutions.xml | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp/Properties/ILLink.Substitutions.xml diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 272a33ff..0ad1927a 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -28,6 +28,11 @@ + + + + + diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs index e5e96e46..ce7ee04a 100644 --- a/src/ComputeSharp/Properties/Configuration.cs +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -24,17 +24,17 @@ internal static class Configuration private const string DisableGpuTimeout = "COMPUTESHARP_DISABLE_GPU_TIMEOUT"; /// - /// Indicates whether or not the debug output is enabled. + /// Indicates whether or not the debug output is enabled (defaults to ). /// public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput); /// - /// Indicates whether or not the debug output is enabled. + /// Indicates whether or not the debug output is enabled (defaults to ). /// public static readonly bool IsDeviceRemovedExtendedDataEnabled = GetConfigurationValue(EnableDeviceRemovedExtendedDataInfo); /// - /// Indicates whether or not the GPU timeout is disabled. + /// Indicates whether or not the GPU timeout is disabled (defaults to ). /// public static readonly bool IsGpuTimeoutDisabled = GetConfigurationValue(DisableGpuTimeout); diff --git a/src/ComputeSharp/Properties/ILLink.Substitutions.xml b/src/ComputeSharp/Properties/ILLink.Substitutions.xml new file mode 100644 index 00000000..73ac251b --- /dev/null +++ b/src/ComputeSharp/Properties/ILLink.Substitutions.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 1a75418aa6dc6851f8e5febf72f216899e59cef4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 25 Jun 2023 11:35:28 +0200 Subject: [PATCH 2/5] Switch configuration switches to static properties This is needed to ensure ILLink can trim them as expected --- src/ComputeSharp/Properties/Configuration.cs | 48 +++++++++++++++---- .../Properties/ILLink.Substitutions.xml | 12 ++--- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs index ce7ee04a..b049068f 100644 --- a/src/ComputeSharp/Properties/Configuration.cs +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -2,6 +2,7 @@ using System; #if DEBUG using System.Diagnostics; #endif +using System.Runtime.CompilerServices; /// /// A container for all shared configuration switches for ComputeSharp. @@ -11,32 +12,59 @@ internal static class Configuration /// /// The configuration property name for . /// - private const string EnableDebugOutput = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; + private const string IsDebugOutputEnabledPropertyName = "COMPUTESHARP_ENABLE_DEBUG_OUTPUT"; /// /// The configuration property name for . /// - private const string EnableDeviceRemovedExtendedDataInfo = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"; + private const string IsDeviceRemovedExtendedDataEnabledPropertyName = "COMPUTESHARP_ENABLE_DEVICE_REMOVED_EXTENDED_DATA"; /// /// The configuration property name for . /// - private const string DisableGpuTimeout = "COMPUTESHARP_DISABLE_GPU_TIMEOUT"; + private const string IsGpuTimeoutDisabledPropertyName = "COMPUTESHARP_DISABLE_GPU_TIMEOUT"; /// - /// Indicates whether or not the debug output is enabled (defaults to ). + /// The backing field for . /// - public static readonly bool IsDebugOutputEnabled = GetConfigurationValue(EnableDebugOutput); + private static readonly bool IsDebugOutputEnabledConfigurationValue = GetConfigurationValue(IsDebugOutputEnabledPropertyName); /// - /// Indicates whether or not the debug output is enabled (defaults to ). + /// The backing field for . /// - public static readonly bool IsDeviceRemovedExtendedDataEnabled = GetConfigurationValue(EnableDeviceRemovedExtendedDataInfo); + private static readonly bool IsDeviceRemovedExtendedDataEnabledConfigurationValue = GetConfigurationValue(IsDeviceRemovedExtendedDataEnabledPropertyName); /// - /// Indicates whether or not the GPU timeout is disabled (defaults to ). + /// The backing field for . /// - public static readonly bool IsGpuTimeoutDisabled = GetConfigurationValue(DisableGpuTimeout); + private static readonly bool IsGpuTimeoutDisabledConfigurationValue = GetConfigurationValue(IsGpuTimeoutDisabledPropertyName); + + /// + /// Gets a value indicating whether or not the debug output is enabled (defaults to ). + /// + public static bool IsDebugOutputEnabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => IsDebugOutputEnabledConfigurationValue; + } + + /// + /// Gets a value indicating whether or not the debug output is enabled (defaults to ). + /// + public static bool IsDeviceRemovedExtendedDataEnabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => IsDeviceRemovedExtendedDataEnabledConfigurationValue; + } + + /// + /// Gets a value indicating whether or not the GPU timeout is disabled (defaults to ). + /// + public static bool IsGpuTimeoutDisabled + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => IsGpuTimeoutDisabledConfigurationValue; + } /// /// Gets a configuration value for a specified property. @@ -46,7 +74,7 @@ internal static class Configuration private static bool GetConfigurationValue(string propertyName) { #if DEBUG - if (Debugger.IsAttached && propertyName != DisableGpuTimeout) + if (Debugger.IsAttached && propertyName != IsGpuTimeoutDisabledPropertyName) { return true; } diff --git a/src/ComputeSharp/Properties/ILLink.Substitutions.xml b/src/ComputeSharp/Properties/ILLink.Substitutions.xml index 73ac251b..a57a24bc 100644 --- a/src/ComputeSharp/Properties/ILLink.Substitutions.xml +++ b/src/ComputeSharp/Properties/ILLink.Substitutions.xml @@ -3,26 +3,26 @@ - + - + - + - + - + - + \ No newline at end of file From 25d6cffb0fa4b0381c54607da1afd5a497d5eabe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 25 Jun 2023 11:59:14 +0200 Subject: [PATCH 3/5] Remove static constructor from Configuration class This uses the same setup as the built-in runtime switches --- src/ComputeSharp/Properties/Configuration.cs | 85 ++++++++++++++++--- .../Properties/ILLink.Substitutions.xml | 30 +++---- 2 files changed, 83 insertions(+), 32 deletions(-) diff --git a/src/ComputeSharp/Properties/Configuration.cs b/src/ComputeSharp/Properties/Configuration.cs index b049068f..4ed1e4b7 100644 --- a/src/ComputeSharp/Properties/Configuration.cs +++ b/src/ComputeSharp/Properties/Configuration.cs @@ -7,6 +7,16 @@ using System.Runtime.CompilerServices; /// /// A container for all shared configuration switches for ComputeSharp. /// +/// +/// +/// 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. +/// +/// +/// For more info, see . +/// +/// internal static class Configuration { /// @@ -27,17 +37,17 @@ internal static class Configuration /// /// The backing field for . /// - private static readonly bool IsDebugOutputEnabledConfigurationValue = GetConfigurationValue(IsDebugOutputEnabledPropertyName); + private static int isDebugOutputEnabledConfigurationValue; /// /// The backing field for . /// - private static readonly bool IsDeviceRemovedExtendedDataEnabledConfigurationValue = GetConfigurationValue(IsDeviceRemovedExtendedDataEnabledPropertyName); + private static int isDeviceRemovedExtendedDataEnabledConfigurationValue; /// /// The backing field for . /// - private static readonly bool IsGpuTimeoutDisabledConfigurationValue = GetConfigurationValue(IsGpuTimeoutDisabledPropertyName); + private static int isGpuTimeoutDisabledConfigurationValue; /// /// Gets a value indicating whether or not the debug output is enabled (defaults to ). @@ -45,7 +55,7 @@ internal static class Configuration public static bool IsDebugOutputEnabled { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => IsDebugOutputEnabledConfigurationValue; + get => GetConfigurationValue(IsDebugOutputEnabledPropertyName, ref isDebugOutputEnabledConfigurationValue); } /// @@ -54,7 +64,7 @@ internal static class Configuration public static bool IsDeviceRemovedExtendedDataEnabled { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => IsDeviceRemovedExtendedDataEnabledConfigurationValue; + get => GetConfigurationValue(IsDeviceRemovedExtendedDataEnabledPropertyName, ref isDeviceRemovedExtendedDataEnabledConfigurationValue); } /// @@ -63,26 +73,77 @@ internal static class Configuration public static bool IsGpuTimeoutDisabled { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => IsGpuTimeoutDisabledConfigurationValue; + get => GetConfigurationValue(IsGpuTimeoutDisabledPropertyName, ref isGpuTimeoutDisabledConfigurationValue); } /// /// Gets a configuration value for a specified property. /// /// The property name to retrieve the value for. + /// The cached result for the target configuration value. /// The value of the specified configuration setting. - private static bool GetConfigurationValue(string propertyName) + private static bool GetConfigurationValue(string propertyName, ref int cachedResult) { -#if DEBUG - if (Debugger.IsAttached && propertyName != IsGpuTimeoutDisabledPropertyName) + // 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; + } + + /// + /// Gets the default configuration value for a given feature switch. + /// + /// The property name to retrieve the value for. + /// The default value for the target . + 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; diff --git a/src/ComputeSharp/Properties/ILLink.Substitutions.xml b/src/ComputeSharp/Properties/ILLink.Substitutions.xml index a57a24bc..3885337c 100644 --- a/src/ComputeSharp/Properties/ILLink.Substitutions.xml +++ b/src/ComputeSharp/Properties/ILLink.Substitutions.xml @@ -1,28 +1,18 @@ + - - - - - - - + + + - - - - - - - + + + - - - - - - + + + \ No newline at end of file From cc11b73f5f49d82ddd979dff5aef332b54ebdcc9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 25 Jun 2023 12:11:15 +0200 Subject: [PATCH 4/5] Set configuration switches in bundled .targets file --- src/ComputeSharp/ComputeSharp.targets | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ComputeSharp/ComputeSharp.targets b/src/ComputeSharp/ComputeSharp.targets index 72cc9d66..991faa81 100644 --- a/src/ComputeSharp/ComputeSharp.targets +++ b/src/ComputeSharp/ComputeSharp.targets @@ -57,4 +57,26 @@ + + + + + + + + + + + + + From 33f01c2423c68801d5d530bbe5cb4fc2ce448f9e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 25 Jun 2023 12:11:32 +0200 Subject: [PATCH 5/5] Manually set configuration switches in DX12 CLI sample --- .../ComputeSharp.SwapChain.Cli.csproj | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj index 6e94f279..5be6880a 100644 --- a/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.Cli/ComputeSharp.SwapChain.Cli.csproj @@ -34,6 +34,17 @@ Speed + + + + + + +