Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/MLCompute/MLHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace MLCompute {
public static class MLCActivationTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCActivationTypeDebugDescription (MLCActivationType activationType);

public static string? GetDebugDescription (this MLCActivationType self)
Expand All @@ -25,6 +27,8 @@ public static class MLCActivationTypeExtensions {
public static class MLCArithmeticOperationExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCArithmeticOperationDebugDescription (MLCArithmeticOperation operation);

public static string? GetDebugDescription (this MLCArithmeticOperation self)
Expand All @@ -40,6 +44,8 @@ public static class MLCArithmeticOperationExtensions {
public static class MLCPaddingPolicyExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCPaddingPolicyDebugDescription (MLCPaddingPolicy paddingPolicy);

public static string? GetDebugDescription (this MLCPaddingPolicy self)
Expand All @@ -55,6 +61,8 @@ public static class MLCPaddingPolicyExtensions {
public static class MLCLossTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCLossTypeDebugDescription (MLCLossType lossType);

public static string? GetDebugDescription (this MLCLossType self)
Expand All @@ -70,6 +78,8 @@ public static class MLCLossTypeExtensions {
public static class MLCReductionTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCReductionTypeDebugDescription (MLCReductionType reductionType);

public static string? GetDebugDescription (this MLCReductionType self)
Expand All @@ -85,6 +95,8 @@ public static class MLCReductionTypeExtensions {
public static class MLCPaddingTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCPaddingTypeDebugDescription (MLCPaddingType paddingType);

public static string? GetDebugDescription (this MLCPaddingType self)
Expand All @@ -100,6 +112,8 @@ public static class MLCPaddingTypeExtensions {
public static class MLCConvolutionTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCConvolutionTypeDebugDescription (MLCConvolutionType convolutionType);

public static string? GetDebugDescription (this MLCConvolutionType self)
Expand All @@ -115,6 +129,8 @@ public static class MLCConvolutionTypeExtensions {
public static class MLCPoolingTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCPoolingTypeDebugDescription (MLCPoolingType poolingType);

public static string? GetDebugDescription (this MLCPoolingType self)
Expand All @@ -130,6 +146,8 @@ public static class MLCPoolingTypeExtensions {
public static class MLCSoftmaxOperationExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCSoftmaxOperationDebugDescription (MLCSoftmaxOperation operation);

public static string? GetDebugDescription (this MLCSoftmaxOperation self)
Expand All @@ -145,6 +163,8 @@ public static class MLCSoftmaxOperationExtensions {
public static class MLCSampleModeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCSampleModeDebugDescription (MLCSampleMode mode);

public static string? GetDebugDescription (this MLCSampleMode self)
Expand All @@ -160,6 +180,8 @@ public static class MLCSampleModeExtensions {
public static class MLCLstmResultModeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCLSTMResultModeDebugDescription (MLCLstmResultMode mode);

public static string? GetDebugDescription (this MLCLstmResultMode self)
Expand All @@ -175,6 +197,8 @@ public static class MLCLstmResultModeExtensions {
public static class MLCComparisonOperationExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCComparisonOperationDebugDescription (MLCComparisonOperation operation);

public static string? GetDebugDescription (this MLCComparisonOperation self)
Expand All @@ -190,6 +214,8 @@ public static class MLCComparisonOperationExtensions {
public static class MLCGradientClippingTypeExtensions {

[DllImport (Constants.MLComputeLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern /* NSString */ IntPtr MLCGradientClippingTypeDebugDescription (MLCGradientClippingType gradientClippingType);

public static string? GetDebugDescription (this MLCGradientClippingType self)
Expand Down
6 changes: 6 additions & 0 deletions src/Metal/MTLEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,8 @@ public enum MTLIOCompressionStatus : long {
[Mac (13, 0), iOS (16, 0), MacCatalyst (16, 0), TV (16, 0)]
[Native]
[ErrorDomain ("MTLIOErrorDomain")]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
public enum MTLIOError : long {
UrlInvalid = 1,
Internal = 2,
Expand Down Expand Up @@ -2466,6 +2468,8 @@ public enum MTL4BlendState : long {
[Mac (26, 0), iOS (26, 0), MacCatalyst (26, 0), TV (26, 0)]
[Native]
[ErrorDomain ("MTL4CommandQueueErrorDomain")]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
public enum MTL4CommandQueueError : long {
None = 0,
Timeout = 1,
Expand Down Expand Up @@ -2599,6 +2603,8 @@ public enum MTLDeviceError : long {
[Mac (26, 0), iOS (26, 0), MacCatalyst (26, 0), TV (26, 0)]
[Native]
[ErrorDomain ("MTLTensorDomain")]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
public enum MTLTensorError : long {
None = 0,
InternalError = 1,
Expand Down
8 changes: 8 additions & 0 deletions src/Metal/MTLIOCompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MTLIOCompressionContext : DisposableObject {
MTLIOCompressionContext (NativeHandle handle, bool owns) : base (handle, owns) { }

[DllImport (Constants.MetalLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern unsafe void MTLIOCompressionContextAppendData (IntPtr context, void* data, nuint size);

unsafe void AppendData (void* data, nuint size)
Expand Down Expand Up @@ -47,6 +49,8 @@ public void AppendData (NSData data)
}

[DllImport (Constants.MetalLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
// [return: NullAllowed]
static extern IntPtr MTLIOCreateCompressionContext (IntPtr path, long type, long chunkSize);

Expand Down Expand Up @@ -74,6 +78,8 @@ protected override void Dispose (bool disposing)
}

[DllImport (Constants.MetalLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern long MTLIOFlushAndDestroyCompressionContext (IntPtr context);

public MTLIOCompressionStatus FlushAndDestroy ()
Expand All @@ -86,6 +92,8 @@ public MTLIOCompressionStatus FlushAndDestroy ()
}

[DllImport (Constants.MetalLibrary)]
[UnsupportedSimulator ("ios")]
[UnsupportedSimulator ("tvos")]
static extern nint MTLIOCompressionContextDefaultChunkSize ();

public static nint DefaultChunkSize => MTLIOCompressionContextDefaultChunkSize ();
Expand Down
45 changes: 45 additions & 0 deletions src/ObjCRuntime/SupportedSimulatorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

namespace ObjCRuntime {
/// <summary>Indicates that an API is supported for a specified platform. If a version is specified, the API is available starting in the specified OS version. Multiple attributes can be applied to indicate support on multiple operating systems.</summary>
/// <remarks>
/// <para>Contrary to standard availability attributes (such as <see cref="SupportedOSPlatformAttribute" /> attributes), the presence of this attribute for some platforms does not imply any meaning for other platforms.</para>
/// <para>If there are no <see cref="SupportedSimulatorAttribute" /> or <see cref="UnsupportedSimulatorAttribute" /> attributes on an API, the API is assumed to be available in the simulator.</para>
/// <para>This attribute will be trimmed away if the app is trimmed.</para>
/// </remarks>
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Constructor | AttributeTargets.Field, AllowMultiple = true)]
public sealed class SupportedSimulatorAttribute : Attribute {
/// <summary>Initializes a new <see cref="SupportedSimulatorAttribute" /> attribute.</summary>
/// <param name="platformName">The platform where this API is supported in the simulator.
/// Format: platform name (e.g., "ios", "tvos") optionally followed by a version number (e.g., "ios17.0").</param>
public SupportedSimulatorAttribute (string platformName)
{
PlatformName = platformName;
}

/// <summary>The name of the platform.</summary>
public string PlatformName { get; init; }
}

/// <summary>Indicates that an API is not supported in the simulator for the specified platform. Multiple attributes can be applied to indicate lack of support on multiple platforms.</summary>
/// <remarks>
/// <para>Contrary to standard availability attributes (such as <see cref="SupportedOSPlatformAttribute" /> attributes), the presence of this attribute for some platforms does not imply any meaning for other platforms.</para>
/// <para>If there are no <see cref="SupportedSimulatorAttribute" /> or <see cref="UnsupportedSimulatorAttribute" /> attributes on an API, the API is assumed to be available in the simulator.</para>
/// <para>This attribute will be trimmed away if the app is trimmed.</para>
/// </remarks>
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Constructor | AttributeTargets.Field, AllowMultiple = true)]
public sealed class UnsupportedSimulatorAttribute : Attribute {
/// <summary>Initializes a new <see cref="UnsupportedSimulatorAttribute" /> attribute.</summary>
/// <param name="platformName">The platform where this API is not supported in the simulator.</param>
public UnsupportedSimulatorAttribute (string platformName)
{
PlatformName = platformName;
}

/// <summary>The name of the platform.</summary>
public string PlatformName { get; init; }
}
}
12 changes: 12 additions & 0 deletions src/TrimAttributes.LinkDescription.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@
</attribute>
</type>
</assembly>

<!-- Attributes that are allowed to be in any assembly -->
<assembly fullname="*">
<!-- Simulator availability attributes: these are documented to be trimmed away, so just do it unconditionally. -->
<type fullname="ObjCRuntime.SupportedSimulatorAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
<type fullname="ObjCRuntime.UnsupportedSimulatorAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
</assembly>

<!-- Suppress the IL2026 warning from System.StartupHookProvider.ProcessStartupHooks when we're the ones enabling StartupHookSupport -->
<assembly fullname="System.Private.CoreLib" feature="ObjCRuntime.SuppressStartupHookTrimWarning" featurevalue="true">
<type fullname="System.StartupHookProvider">
Expand Down
4 changes: 4 additions & 0 deletions src/bgen/AttributeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public AttributeManager (TypeCache typeCache)
return typeof (ObjCRuntime.RequiresSuperAttribute);
case "ObjCRuntime.ObjectiveCFrameworkAttribute":
return typeof (ObjCRuntime.ObjectiveCFrameworkAttribute);
case "ObjCRuntime.SupportedSimulatorAttribute":
return typeof (ObjCRuntime.SupportedSimulatorAttribute);
case "ObjCRuntime.UnsupportedSimulatorAttribute":
return typeof (ObjCRuntime.UnsupportedSimulatorAttribute);
case "UnavailableAttribute":
return typeof (UnavailableAttribute);
case "OptionalImplementationAttribute":
Expand Down
1 change: 1 addition & 0 deletions src/bgen/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ void GenerateEnum (Type type)
}
}
// the *Extensions has the same version requirement as the enum itself
PrintSimulatorAvailabilityAttributes (type);
PrintPlatformAttributes (type);
PrintExperimentalAttribute (type);
print_generated_code ();
Expand Down
60 changes: 59 additions & 1 deletion src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5547,7 +5547,7 @@ public void PrintBindAsAttribute (ICustomAttributeProvider? mi, StringBuilder? s
// Not adding the experimental attribute is bad (it would mean that an API
// we meant to be experimental ended up being released as stable), so it's
// opt-out instead of opt-in.
public void PrintAttributes (ICustomAttributeProvider? mi, bool platform = false, bool preserve = false, bool advice = false, bool notImplemented = false, bool bindAs = false, bool requiresSuper = false, Type? inlinedType = null, bool experimental = true, bool obsolete = false, bool objectiveCFramework = false)
public void PrintAttributes (ICustomAttributeProvider? mi, bool platform = false, bool preserve = false, bool advice = false, bool notImplemented = false, bool bindAs = false, bool requiresSuper = false, Type? inlinedType = null, bool experimental = true, bool obsolete = false, bool objectiveCFramework = false, bool simulatorAvailability = true)
{
if (platform)
PrintPlatformAttributes (mi as MemberInfo, inlinedType);
Expand All @@ -5567,6 +5567,8 @@ public void PrintAttributes (ICustomAttributeProvider? mi, bool platform = false
PrintObsoleteAttributes (mi);
if (objectiveCFramework)
PrintObjectiveCFrameworkAttribute (mi);
if (simulatorAvailability)
PrintSimulatorAvailabilityAttributes (mi);
}

public void PrintExperimentalAttribute (ICustomAttributeProvider? mi)
Expand All @@ -5585,6 +5587,62 @@ public void PrintObjectiveCFrameworkAttribute (ICustomAttributeProvider? mi)
print ($"[ObjectiveCFramework (\"{attrib.Framework}\")]");
}

public void PrintSimulatorAvailabilityAttributes (ICustomAttributeProvider? provider)
{
if (!TryGetSimulatorAvailabilityPlatformName (out var platformName))
return;

PrintSupportedSimulatorAttribute (provider, platformName);
PrintUnsupportedSimulatorAttribute (provider, platformName);
}

bool TryGetSimulatorAvailabilityPlatformName ([NotNullWhen (true)] out string? platformName)
Comment thread
rolfbjarne marked this conversation as resolved.
{
platformName = null;

switch (CurrentPlatform) {
Comment thread
rolfbjarne marked this conversation as resolved.
case PlatformName.MacCatalyst:
case PlatformName.MacOSX:
return false;
case PlatformName.iOS:
platformName = "ios";
return true;
case PlatformName.TvOS:
platformName = "tvos";
return true;
default:
throw new BindingException (1047, CurrentPlatform);
}
}

void PrintSupportedSimulatorAttribute (ICustomAttributeProvider? provider, string platformName)
{
var attribs = AttributeManager.GetCustomAttributes<SupportedSimulatorAttribute> (provider);
if (attribs?.Any () != true)
return;

// Only print the attribute for the current platform, we don't care about other platforms.
foreach (var attrib in attribs) {
if (!attrib.PlatformName.StartsWith (platformName, StringComparison.OrdinalIgnoreCase))
continue;
print ($"[SupportedSimulator (\"{attrib.PlatformName}\")]");
}
}

void PrintUnsupportedSimulatorAttribute (ICustomAttributeProvider? provider, string platformName)
{
var attribs = AttributeManager.GetCustomAttributes<UnsupportedSimulatorAttribute> (provider);
if (attribs?.Any () != true)
return;

// Only print the attribute for the current platform, we don't care about other platforms.
foreach (var attrib in attribs) {
if (!attrib.PlatformName.StartsWith (platformName, StringComparison.OrdinalIgnoreCase))
continue;
print ($"[UnsupportedSimulator (\"{attrib.PlatformName}\")]");
}
}

bool WriteDocumentation (MemberInfo info, Func<XmlNode, XmlNode>? transformNode = null)
{
return DocumentationManager.WriteDocumentation (sw!, indent, info, transformNode);
Expand Down
1 change: 1 addition & 0 deletions src/bgen/bgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\NativeAttribute.cs" />
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\NativeNameAttribute.cs" />
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\ObjectiveCFrameworkAttribute.cs" />
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\SupportedSimulatorAttribute.cs" />
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\Registrar.core.cs" />
<Compile Include="$(RepositoryPath)\src\ObjCRuntime\RequiresSuperAttribute.cs" />
<Compile Include="$(RepositoryPath)\tools\common\Execution.cs">
Expand Down
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,7 @@ SHARED_CORE_SOURCES = \
ObjCRuntime/Registrar.core.cs \
ObjCRuntime/RequiresSuperAttribute.cs \
ObjCRuntime/Selector.cs \
ObjCRuntime/SupportedSimulatorAttribute.cs \
ObjCRuntime/SystemVersion.cs \
ObjCRuntime/ThrowHelper.cs \
Simd/MathHelper.cs \
Expand Down
Loading
Loading