From 2c6eb178d5f8c9634fa2bbb90a658234619b4883 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Dec 2023 03:08:21 +0100 Subject: [PATCH] Add [RequiresDoublePrecisionSupport] diagnostics --- .../Diagnostics/DiagnosticDescriptors.cs | 10 ++--- .../AnalyzerReleases.Shipped.md | 2 + ...eShaderDescriptorGenerator.HlslBytecode.cs | 38 +++++++++++++++++++ .../ComputeShaderDescriptorGenerator.cs | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 32 ++++++++++++++++ 5 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 9d9f0d36..23ce21bf 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1194,21 +1194,21 @@ partial class DiagnosticDescriptors /// /// Gets a for a shader missing [D2DRequiresDoublePrecisionSupport]. /// - /// Format: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in into that functionality)". + /// Format: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in to that functionality)". /// /// public static readonly DiagnosticDescriptor MissingD2DRequiresDoublePrecisionSupportAttribute = new( id: "CMPSD2D0080", title: "Missing [D2DRequiresDoublePrecisionSupport] attribute", - messageFormat: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in into that functionality)", + messageFormat: "The shader {0} requires double precision support, but it does not have the [D2DRequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in to that functionality)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "Shaders performing double precision operations must be annotated with [D2DRequiresDoublePrecisionSupport] to explicitly opt-in into that functionality.", + description: "Shaders performing double precision operations must be annotated with [D2DRequiresDoublePrecisionSupport] to explicitly opt-in to that functionality.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// - /// Gets a for a shader is unnecessarily using [UnnecessaryD2DRequiresDoublePrecisionSupportAttribute]. + /// Gets a for a shader is unnecessarily using [D2DRequiresDoublePrecisionSupport]. /// /// Format: "The shader {0} does not require double precision support, but it has the [D2DRequiresDoublePrecisionSupport] attribute on it (using the attribute is not needed if the shader is not performing any double precision operations)". /// @@ -1224,7 +1224,7 @@ partial class DiagnosticDescriptors helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// - /// Gets a for a shader is using [UnnecessaryD2DRequiresDoublePrecisionSupportAttribute] incorrectly. + /// Gets a for a shader is using [D2DRequiresDoublePrecisionSupport] incorrectly. /// /// Format: "The shader {0} has the [D2DRequiresDoublePrecisionSupport] attribute on it, but it is not precompiled, so validation for use of double precision operations cannot be performed". /// diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 0dbb22ff..b211839f 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -69,3 +69,5 @@ CMPS0060 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser CMPS0061 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0062 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0063 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0064 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0065 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index e78440aa..ab84b2fe 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -139,5 +139,43 @@ partial class ComputeShaderDescriptorGenerator diagnostics.Add(diagnostic); } } + + /// + /// Gets the diagnostics for when double precision support is configured incorrectly. + /// + /// The input instance to process. + /// The source instance. + /// The collection of produced instances. + public static void GetDoublePrecisionSupportDiagnostics( + INamedTypeSymbol structDeclarationSymbol, + HlslBytecodeInfo info, + ImmutableArrayBuilder diagnostics) + { + // If we have no compiled HLSL bytecode, there is nothing more to do + if (info is not HlslBytecodeInfo.Success success) + { + return; + } + + bool hasD2DRequiresDoublePrecisionSupportAttribute = structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName( + "ComputeSharp.RequiresDoublePrecisionSupportAttribute", + out AttributeData? attributeData); + + // Check the two cases where diagnostics are necessary (same as the D2D generator) + if (!hasD2DRequiresDoublePrecisionSupportAttribute && success.RequiresDoublePrecisionSupport) + { + diagnostics.Add(DiagnosticInfo.Create( + MissingRequiresDoublePrecisionSupportAttribute, + structDeclarationSymbol, + structDeclarationSymbol)); + } + else if (hasD2DRequiresDoublePrecisionSupportAttribute && !success.RequiresDoublePrecisionSupport) + { + diagnostics.Add(DiagnosticInfo.Create( + UnnecessaryRequiresDoublePrecisionSupportAttribute, + attributeData!.GetLocation(), + structDeclarationSymbol)); + } + } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index baa69f59..c218dd6e 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -122,6 +122,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener token.ThrowIfCancellationRequested(); HlslBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecode.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 99aab81a..ce9a2f6b 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -901,4 +901,36 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "Methods from the Math and MathF types cannot be used in a shader, and equivalent APIs from the Hlsl type should be used instead.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader missing [RequiresDoublePrecisionSupport]. + /// + /// Format: "The shader {0} requires double precision support, but it does not have the [RequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in to that functionality)". + /// + /// + public static readonly DiagnosticDescriptor MissingRequiresDoublePrecisionSupportAttribute = new( + id: "CMPS0064", + title: "Missing [RequiresDoublePrecisionSupport] attribute", + messageFormat: "The shader {0} requires double precision support, but it does not have the [RequiresDoublePrecisionSupport] attribute on it (adding the attribute is necessary to explicitly opt-in to that functionality)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Shaders performing double precision operations must be annotated with [RequiresDoublePrecisionSupport] to explicitly opt-in to that functionality.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader is unnecessarily using [RequiresDoublePrecisionSupportAttribute]. + /// + /// Format: "The shader {0} does not require double precision support, but it has the [RequiresDoublePrecisionSupport] attribute on it (using the attribute is not needed if the shader is not performing any double precision operations)". + /// + /// + public static readonly DiagnosticDescriptor UnnecessaryRequiresDoublePrecisionSupportAttribute = new( + id: "CMPS0065", + title: "Unnecessary [RequiresDoublePrecisionSupport] attribute", + messageFormat: "The shader {0} does not require double precision support, but it has the [RequiresDoublePrecisionSupport] attribute on it (using the attribute is not needed if the shader is not performing any double precision operations)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shaders not performing any double precision operations should not be annotated with [RequiresDoublePrecisionSupport], as the attribute is not needed in that case.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file