From 4fe3c857be49a3d90dddd1f961564a0242a6f41e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Feb 2021 20:19:58 +0100 Subject: [PATCH] Added errors for invalid group shared fields --- samples/ComputeSharp.Sample.Shared/Program.cs | 5 ++- .../AnalyzerReleases.Unshipped.md | 4 ++- .../Diagnostics/DiagnosticDescriptors.cs | 32 +++++++++++++++++ .../IComputeShaderSourceGenerator.cs | 34 ++++++++++++++++--- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index ee01f2a1..a4e38ca1 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -21,7 +21,7 @@ namespace ComputeSharp.Sample using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader - Gpu.Default.For(100, new MainKernel(gpuBuffer)); + Gpu.Default.For(100, new MainKernel()); // Print the initial matrix PrintMatrix(array, width, height, "BEFORE"); @@ -41,6 +41,9 @@ namespace ComputeSharp.Sample { public readonly ReadWriteBuffer buffer; + [GroupShared] + public static string[] Foo; + /// public void Execute() { diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index 2b28783f..bb77ae76 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -5,4 +5,6 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- -CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file +CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0002 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0003 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 81a35cae..e54e2496 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -22,5 +22,37 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A type representing a compute shader contains a field of a type that is not supported in HLSL.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid group shared field type. + /// + /// Format: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2}". + /// + /// + public static DiagnosticDescriptor InvalidGroupSharedFieldType { get; } = new( + id: "CMPS0002", + title: "Invalid group shared field type", + messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2} (it must be an array)", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A group shared field must be of an array type.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid group shared field element type. + /// + /// Format: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid element type {2}". + /// + /// + public static DiagnosticDescriptor InvalidGroupSharedFieldElementType { get; } = new( + id: "CMPS0003", + title: "Invalid group shared field type", + messageFormat: "The compute shader of type {0} contains a group shared field \"{1}\" of an invalid type {2} (it must be a primitive or unmanaged type)", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A group shared field element must be of a primitive or unmanaged type.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index 1a799b82..aba966b1 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -51,7 +51,7 @@ namespace ComputeSharp.SourceGenerators // Explore the syntax tree and extract the processed info var processedMembers = GetProcessedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray(); - var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); + var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray(); var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray(); @@ -135,19 +135,45 @@ namespace ComputeSharp.SourceGenerators /// /// Gets a sequence of captured members and their mapped names. /// + /// The current generator context in use. /// The input instance to process. /// The collection of currently discovered types. /// A sequence of captured members in . [Pure] - private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) + private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers( + GeneratorExecutionContext context, + INamedTypeSymbol structDeclarationSymbol, + ICollection types) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { if (!fieldSymbol.IsStatic) continue; - AttributeData attribute = fieldSymbol.GetAttributes().First(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); + AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); + + if (attribute is null) continue; + + if (fieldSymbol.Type is not IArrayTypeSymbol typeSymbol) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidGroupSharedFieldType, + fieldSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type)); + + continue; + } + + if (typeSymbol.ElementType.IsUnmanagedType) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.InvalidGroupSharedFieldElementType, + fieldSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type)); + + continue; + } + int? bufferSize = (int?)attribute.ConstructorArguments.FirstOrDefault().Value; - IArrayTypeSymbol typeSymbol = (IArrayTypeSymbol)fieldSymbol.Type; string typeName = HlslKnownTypes.GetMappedElementName(typeSymbol);