diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.cs
index 2fd047af..1042650a 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.cs
@@ -1,9 +1,5 @@
-using System;
using ComputeSharp.SourceGeneration.Extensions;
-using ComputeSharp.SourceGeneration.Helpers;
-using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
-using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
@@ -18,14 +14,12 @@ partial class ComputeShaderDescriptorGenerator
///
/// Gets the thread ids values for a given shader type, if available.
///
- /// The collection of produced instances.
/// The input instance to process.
/// The thread ids value for the X axis.
/// The thread ids value for the Y axis.
/// The thread ids value for the Z axis.
/// Whether compilation should be attempted for the current info.
public static void GetInfo(
- ImmutableArrayBuilder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
out int threadsX,
out int threadsY,
@@ -35,8 +29,6 @@ partial class ComputeShaderDescriptorGenerator
// Try to get the attribute that controls shader precompilation (this is always required)
if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.ThreadGroupSizeAttribute", out AttributeData? attribute))
{
- diagnostics.Add(MissingThreadGroupSizeAttribute, structDeclarationSymbol, structDeclarationSymbol);
-
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
@@ -44,11 +36,9 @@ partial class ComputeShaderDescriptorGenerator
}
// Check for a dispatch axis argument first
- if (attribute.ConstructorArguments.Length == 1)
+ if (attribute.ConstructorArguments is [{ Value: var defaultSize }])
{
- int defaultSize = (int)attribute.ConstructorArguments[0].Value!;
-
- (threadsX, threadsY, threadsZ) = (DefaultThreadGroupSizes)defaultSize switch
+ (threadsX, threadsY, threadsZ) = (DefaultThreadGroupSizes?)(defaultSize as int?) switch
{
DefaultThreadGroupSizes.X => (64, 1, 1),
DefaultThreadGroupSizes.Y => (1, 64, 1),
@@ -60,31 +50,15 @@ partial class ComputeShaderDescriptorGenerator
_ => (0, 0, 0)
};
- // Validate the dispatch axis argument
- if ((threadsX, threadsY, threadsZ) is (0, 0, 0))
- {
- diagnostics.Add(InvalidThreadGroupSizeAttributeDefaultThreadGroupSizes, structDeclarationSymbol, structDeclarationSymbol);
-
- threadsX = threadsY = threadsZ = 0;
- isCompilationEnabled = false;
-
- return;
- }
-
- isCompilationEnabled = true;
+ // Only enable compilation if we have valid thread group size values
+ isCompilationEnabled = (threadsX, threadsY, threadsZ) is not (0, 0, 0);
}
- else if (
- attribute.ConstructorArguments.Length != 3 ||
- attribute.ConstructorArguments[0].Value is not int explicitThreadsX ||
- attribute.ConstructorArguments[1].Value is not int explicitThreadsY ||
- attribute.ConstructorArguments[2].Value is not int explicitThreadsZ ||
- explicitThreadsX is < 1 or > 1024 ||
- explicitThreadsY is < 1 or > 1024 ||
- explicitThreadsZ is < 1 or > 64)
+ else if (attribute.ConstructorArguments is not [{ Value: int explicitThreadsX }, { Value: int explicitThreadsY }, { Value: int explicitThreadsZ }] ||
+ explicitThreadsX is < 1 or > 1024 ||
+ explicitThreadsY is < 1 or > 1024 ||
+ explicitThreadsZ is < 1 or > 64)
{
- // Failed to validate the thread number arguments
- diagnostics.Add(InvalidThreadGroupSizeAttributeValues, structDeclarationSymbol, structDeclarationSymbol);
-
+ // Also disable compilation if we have no valid explicit sizes
threadsX = threadsY = threadsZ = 0;
isCompilationEnabled = false;
@@ -92,6 +66,7 @@ partial class ComputeShaderDescriptorGenerator
}
else
{
+ // Enable compilation and track the explicit thread group sizes
threadsX = explicitThreadsX;
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
index 10b60cfe..d16b9733 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
@@ -73,7 +73,6 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
// Thread group size info
NumThreads.GetInfo(
- diagnostics,
typeSymbol,
out int threadsX,
out int threadsY,
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidThreadGroupSizeAttributeUseAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidThreadGroupSizeAttributeUseAnalyzer.cs
new file mode 100644
index 00000000..1f7fb94b
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidThreadGroupSizeAttributeUseAnalyzer.cs
@@ -0,0 +1,102 @@
+using System.Collections.Immutable;
+using System.Linq;
+using ComputeSharp.SourceGeneration.Extensions;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.Diagnostics;
+using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.SourceGenerators;
+
+///
+/// A diagnostic analyzer that generates diagnostics for invalid uses of [ThreadGroupSize].
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidThreadGroupSizeAttributeUseAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(
+ MissingThreadGroupSizeAttribute,
+ InvalidThreadGroupSizeAttributeDefaultThreadGroupSizes,
+ InvalidThreadGroupSizeAttributeValues);
+
+ ///
+ public override void Initialize(AnalysisContext context)
+ {
+ context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
+ context.EnableConcurrentExecution();
+
+ context.RegisterCompilationStartAction(static context =>
+ {
+ // Get the IComputeShader, IComputeShader and [ThreadGroupSize] symbols
+ if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol ||
+ context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol ||
+ context.Compilation.GetTypeByMetadataName("ComputeSharp.ThreadGroupSizeAttribute") is not { } threadGroupSizeAttributeSymbol)
+ {
+ return;
+ }
+
+ context.RegisterSymbolAction(context =>
+ {
+ // Only struct types are possible targets
+ if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
+ {
+ return;
+ }
+
+ // If the type is not a compute shader type, immediately bail out
+ if (!MissingComputeShaderDescriptorOnComputeShaderAnalyzer.IsComputeShaderType(typeSymbol, computeShaderSymbol, pixelShaderSymbol))
+ {
+ return;
+ }
+
+ // Warn if the shader type is not using [ThreadGroupSize]
+ if (!typeSymbol.TryGetAttributeWithType(threadGroupSizeAttributeSymbol, out AttributeData? attributeData))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ MissingThreadGroupSizeAttribute,
+ typeSymbol.Locations.First(),
+ typeSymbol));
+
+ return;
+ }
+
+ // If there is a single argument, validate that it is valid
+ if (attributeData.ConstructorArguments is [{ Value: var defaultSize }])
+ {
+ int? rawDefaultSize = defaultSize as int?;
+
+ if ((DefaultThreadGroupSizes?)rawDefaultSize is not
+ (DefaultThreadGroupSizes.X or
+ DefaultThreadGroupSizes.Y or
+ DefaultThreadGroupSizes.Z or
+ DefaultThreadGroupSizes.XY or
+ DefaultThreadGroupSizes.XZ or
+ DefaultThreadGroupSizes.YZ or
+ DefaultThreadGroupSizes.XYZ))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidThreadGroupSizeAttributeDefaultThreadGroupSizes,
+ attributeData.GetLocation(),
+ typeSymbol));
+ }
+
+ return;
+ }
+
+ // If there are three arguments, validate that they are also valid thread group sizes
+ if (attributeData.ConstructorArguments is not [{ Value: int threadsX }, { Value: int threadsY }, { Value: int threadsZ }] ||
+ threadsX is < 1 or > 1024 ||
+ threadsY is < 1 or > 1024 ||
+ threadsZ is < 1 or > 64)
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidThreadGroupSizeAttributeValues,
+ attributeData.GetLocation(),
+ typeSymbol));
+
+ return;
+ }
+ }, SymbolKind.NamedType);
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 4cd5a7da..5126f67f 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -592,13 +592,13 @@ partial class DiagnosticDescriptors
///
/// Gets a for invalid thread group sizes.
///
- /// Format: "The shader of type {0} is annotated with invalid [ThreadGroupSize] values".
+ /// Format: "The [ThreadGroupSize] attribute on shader type {0} is using invalid thread group size values".
///
///
public static readonly DiagnosticDescriptor InvalidThreadGroupSizeAttributeValues = new(
id: "CMPS0044",
title: "Invalid values for [ThreadGroupSize] attribute",
- messageFormat: "The shader of type {0} is annotated with invalid [ThreadGroupSize] values",
+ messageFormat: "The [ThreadGroupSize] attribute on shader type {0} is using invalid thread group size values",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
@@ -656,13 +656,13 @@ partial class DiagnosticDescriptors
///
/// Gets a for shaders shader with an invalid DefaultThreadGroupSizes value.
///
- /// Format: "The shader of type {0} is using an invalid DefaultThreadGroupSizes value in its [ThreadGroupSize] attribute".
+ /// Format: "The [ThreadGroupSize] attribute on shader type {0} is using an invalid DefaultThreadGroupSizes value".
///
///
public static readonly DiagnosticDescriptor InvalidThreadGroupSizeAttributeDefaultThreadGroupSizes = new(
id: "CMPS0048",
title: "Invalid DefaultThreadGroupSizes value for [ThreadGroupSize] use",
- messageFormat: "The shader of type {0} is using an invalid DefaultThreadGroupSizes value in its [ThreadGroupSize] attribute",
+ messageFormat: "The [ThreadGroupSize] attribute on shader type {0} is using an invalid DefaultThreadGroupSizes value",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,