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