From 7411c1fe0db40ad16a191d54248db050f4a86a72 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 24 May 2022 22:10:15 +0200 Subject: [PATCH] Add diagnostics for [D2DCompileOptions] at assembly level --- .../Diagnostics/DiagnosticDescriptors.cs | 6 +- ...haderGenerator.CreateLoadBytecodeMethod.cs | 70 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 6 ++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 0e8167b3..8f46d380 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -622,16 +622,16 @@ internal static class DiagnosticDescriptors /// /// Gets a for the PackMatrixColumnMajor option being used. /// - /// Format: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute". + /// Format: "The D2D1 shader of type (or assembly) {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute". /// /// public static readonly DiagnosticDescriptor InvalidPackMatrixColumnMajorOption = new DiagnosticDescriptor( id: "CMPSD2D0044", title: "Invalid PackMatrixColumnMajor compile option", - messageFormat: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute", + messageFormat: "The D2D1 shader of type (or assembly) {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "A D2D1 shader generated with ComputeSharp.D2D1 cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.", + description: "A D2D1 shader generated with ComputeSharp.D2D1 (or an assembly) cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 08445119..82bfaea4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -11,6 +11,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -79,6 +80,75 @@ partial class ID2D1ShaderGenerator return null; } + /// + /// Gets an instance producing -s for invalid assembly-level [D2D1CompileOptions] attributes. + /// + /// The input instance to use to produce the diagnostics. + /// The diagnostic for the attribute, if invalid. + public static IncrementalValuesProvider GetAssemblyLevelCompileOptionsDiagnostics(SyntaxValueProvider syntaxProvider) + { + // In order to emit diagnostics for [D2D1CompileOptions] attributes at the assembly level, the following is needed: + // - The type symbol for the attribute, to be able to check it's actually [D2D1CompileOptions]. + // - The syntax node representing the attribute targeting the assembly, to get a location. + // - The input D2D1CompileOptions value, which can be retrieved as a constant argument to the attribute. + // + // The first step to do this is to find all AttributeListSyntax values and filter those targeting the assembly. + static bool IsAssemblyTargetingAttributeNode(SyntaxNode node, CancellationToken token) + { + return node is AttributeListSyntax attributeList && attributeList.Target?.Identifier.IsKind(SyntaxKind.AssemblyKeyword) == true; + } + + // Helper that detects invalid [D2D1CompileOptions] uses and produces a diagnostic + static Diagnostic? TryGetDiagnosticForAssemblyLevelCompileOptions(GeneratorSyntaxContext context, CancellationToken token) + { + Location? location = null; + D2D1CompileOptions? options = null; + + // The input is an AttributeListSyntax object, so first traverse all its containing attributes + foreach (AttributeSyntax attributeSyntax in ((AttributeListSyntax)context.Node).Attributes) + { + // Match the symbol (retrieved from the constructor symbol) to check it's a [D2D1CompileOptions] attribute + if (context.SemanticModel.GetSymbolInfo(attributeSyntax, token).Symbol is IMethodSymbol attributeConstructorSymbol && + attributeConstructorSymbol.ContainingType.HasFullyQualifiedName("ComputeSharp.D2D1.D2DCompileOptionsAttribute")) + { + // Get the value from the expression (get the argument expression and the constant value from there) + if (attributeSyntax.ArgumentList?.Arguments.FirstOrDefault() is AttributeArgumentSyntax argumentSyntax && + context.SemanticModel.GetConstantValue(argumentSyntax.Expression, token) is { HasValue: true, Value: int rawOptions }) + { + options = (D2D1CompileOptions)rawOptions; + } + + // Also get the location from the attribute syntax node + location = attributeSyntax.GetLocation(); + + break; + } + } + + // Check that some options were actually found, and that they were incorrect. + // The attribute can't be repeated, so checking for multiple values isn't needed. + if (options is not null && + (options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + IAssemblySymbol assemblySymbol = context.SemanticModel.Compilation.Assembly; + + // Emit the diagnostic targeting the assembly (instead of a shader type) + return Diagnostic.Create( + InvalidPackMatrixColumnMajorOption, + location, + assemblySymbol); + } + + return null; + } + + // Create the incremental collection and only retrieve non null items + return + syntaxProvider + .CreateSyntaxProvider(IsAssemblyTargetingAttributeNode, TryGetDiagnosticForAssemblyLevelCompileOptions) + .Where(static diagnostic => diagnostic is not null)!; + } + /// /// Extracts the metadata definition for the current shader. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 9005fb51..1ea0d17d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -116,6 +116,12 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics)); + // Get all errors for invalid [D2D1CompileOptions] attributes added to the assembly level + IncrementalValuesProvider d2DCompileOptionsAtAssemblyLevelErrors = LoadBytecode.GetAssemblyLevelCompileOptionsDiagnostics(context.SyntaxProvider); + + // Output the diagnostics + context.ReportDiagnostics(d2DCompileOptionsAtAssemblyLevelErrors.Select(static (item, token) => item)); + // Filter all items to enable caching at a coarse level, and remove diagnostics IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors