Add InvalidThreadGroupSizeAttributeUseAnalyzer
このコミットが含まれているのは:
1 親
022242fcf9
コミット
b912ab1cc1
4ファイル変更
+116
-40
検索条件に一致するファイルはありません
@@ -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
|
||||
/// <summary>
|
||||
/// Gets the thread ids values for a given shader type, if available.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <param name="threadsX">The thread ids value for the X axis.</param>
|
||||
/// <param name="threadsY">The thread ids value for the Y axis.</param>
|
||||
/// <param name="threadsZ">The thread ids value for the Z axis.</param>
|
||||
/// <param name="isCompilationEnabled">Whether compilation should be attempted for the current info.</param>
|
||||
public static void GetInfo(
|
||||
ImmutableArrayBuilder<DiagnosticInfo> 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;
|
||||
|
||||
@@ -73,7 +73,6 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
|
||||
|
||||
// Thread group size info
|
||||
NumThreads.GetInfo(
|
||||
diagnostics,
|
||||
typeSymbol,
|
||||
out int threadsX,
|
||||
out int threadsY,
|
||||
|
||||
+102
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// A diagnostic analyzer that generates diagnostics for invalid uses of [ThreadGroupSize].
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidThreadGroupSizeAttributeUseAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||
MissingThreadGroupSizeAttribute,
|
||||
InvalidThreadGroupSizeAttributeDefaultThreadGroupSizes,
|
||||
InvalidThreadGroupSizeAttributeValues);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationStartAction(static context =>
|
||||
{
|
||||
// Get the IComputeShader, IComputeShader<TPixel> 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -592,13 +592,13 @@ partial class DiagnosticDescriptors
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for invalid thread group sizes.
|
||||
/// <para>
|
||||
/// Format: <c>"The shader of type {0} is annotated with invalid [ThreadGroupSize] values"</c>.
|
||||
/// Format: <c>"The [ThreadGroupSize] attribute on shader type {0} is using invalid thread group size values"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for shaders shader with an invalid DefaultThreadGroupSizes value.
|
||||
/// <para>
|
||||
/// Format: <c>"The shader of type {0} is using an invalid DefaultThreadGroupSizes value in its [ThreadGroupSize] attribute"</c>.
|
||||
/// Format: <c>"The [ThreadGroupSize] attribute on shader type {0} is using an invalid DefaultThreadGroupSizes value"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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,
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする