Merge pull request #601 from Sergio0694/dev/computesharp-generator-tweaks
Always assume [SkipLocalsInit] is available in DX12 generator
このコミットが含まれているのは:
6ファイル変更
+86
-35
検索条件に一致するファイルはありません
@@ -1,4 +1,4 @@
|
||||
; Shipped analyzer releases
|
||||
; Shipped analyzer releases
|
||||
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
|
||||
|
||||
## Release 1.0
|
||||
@@ -58,3 +58,4 @@ CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
|
||||
CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0051 | ComputeSharp | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Immutable;
|
||||
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 an error if the <c>AllowUnsafeBlocks</c> compilation option is not set.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class MissingAllowUnsafeBlocksCompilationOptionAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(MissingAllowUnsafeBlocksOption);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
context.RegisterCompilationAction(static context =>
|
||||
{
|
||||
// Check whether unsafe blocks are available, and emit an error if they are not
|
||||
if (!context.Compilation.IsAllowUnsafeBlocksEnabled())
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(MissingAllowUnsafeBlocksOption, location: null));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -729,4 +729,21 @@ partial class DiagnosticDescriptors
|
||||
isEnabledByDefault: true,
|
||||
description: "The ComputeContext type cannot be copied (and values should rather be passed via ref readonly or in instead) and cannot be used as a field of value types (as it could be indirectly copied).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>AllowUnsafeBlocks</c> option is not set.
|
||||
/// <para>
|
||||
/// Format: <c>"Unsafe blocks must be enabled for the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor MissingAllowUnsafeBlocksOption = new DiagnosticDescriptor(
|
||||
id: "CMPS0052",
|
||||
title: "Missing 'AllowUnsafeBlocks' compilation option",
|
||||
messageFormat: "Unsafe blocks must be enabled for the source generators to emit valid code (add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your .csproj/.props file)",
|
||||
category: "ComputeSharp.Shaders",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "Unsafe blocks must be enabled for the source generators to emit valid code (the <AllowUnsafeBlocks>true</AllowUnsafeBlocks> option must be set in the .csproj/.props file).",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp",
|
||||
customTags: WellKnownDiagnosticTags.CompilationEnd);
|
||||
}
|
||||
@@ -60,12 +60,12 @@ partial class IShaderGenerator
|
||||
/// </summary>
|
||||
/// <param name="hierarchyInfo">The <see cref="HierarchyInfo"/> instance for the current type.</param>
|
||||
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> item to insert.</param>
|
||||
/// <param name="canUseSkipLocalsInit">Whether <c>[SkipLocalsInit]</c> can be used.</param>
|
||||
/// <param name="addSkipLocalsInitAttribute">Whether <c>[SkipLocalsInit]</c> should be used.</param>
|
||||
/// <returns>A <see cref="CompilationUnitSyntax"/> object wrapping <paramref name="methodDeclaration"/>.</returns>
|
||||
private static CompilationUnitSyntax GetCompilationUnitFromMethod(
|
||||
HierarchyInfo hierarchyInfo,
|
||||
MethodDeclarationSyntax methodDeclaration,
|
||||
bool canUseSkipLocalsInit)
|
||||
bool addSkipLocalsInitAttribute)
|
||||
{
|
||||
// Method attributes
|
||||
List<AttributeListSyntax> attributes = new()
|
||||
@@ -86,8 +86,8 @@ partial class IShaderGenerator
|
||||
Literal("This method is not intended to be used directly by user code"))))))
|
||||
};
|
||||
|
||||
// Add [SkipLocalsInit] if the target project allows it
|
||||
if (canUseSkipLocalsInit)
|
||||
// Add [SkipLocalsInit] if needed
|
||||
if (addSkipLocalsInitAttribute)
|
||||
{
|
||||
attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit")))));
|
||||
}
|
||||
|
||||
@@ -22,13 +22,6 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
// Check whether [SkipLocalsInit] can be used
|
||||
IncrementalValueProvider<bool> canUseSkipLocalsInit =
|
||||
context.CompilationProvider
|
||||
.Select(static (compilation, _) =>
|
||||
compilation.Options is CSharpCompilationOptions { AllowUnsafe: true } &&
|
||||
compilation.HasAccessibleTypeWithMetadataName("System.Runtime.CompilerServices.SkipLocalsInitAttribute"));
|
||||
|
||||
// Check whether or not dynamic shaders are supported
|
||||
IncrementalValueProvider<bool> supportsDynamicShaders =
|
||||
context.CompilationProvider
|
||||
@@ -41,6 +34,12 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
static (node, _) => node.IsTypeDeclarationWithOrPotentiallyWithBaseTypes<StructDeclarationSyntax>(),
|
||||
static (context, token) =>
|
||||
{
|
||||
// The source generator requires unsafe blocks to be enabled (eg. for pointers, [SkipLocalsInit], etc.)
|
||||
if (!context.SemanticModel.Compilation.IsAllowUnsafeBlocksEnabled())
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
StructDeclarationSyntax typeDeclaration = (StructDeclarationSyntax)context.Node;
|
||||
|
||||
// If the type symbol doesn't have at least one interface, it can't possibly be a shader type
|
||||
@@ -128,59 +127,56 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
context.RegisterSourceOutput(dispatchIdInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax getDispatchIdMethod = GetDispatchId.GetSyntax(item.DispatchId.Delegates);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getDispatchIdMethod, canUseSkipLocalsInit: false);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getDispatchIdMethod, addSkipLocalsInitAttribute: false);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetDispatchId)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Get the LoadDispatchData() info (hierarchy and dispatch data info)
|
||||
IncrementalValuesProvider<((HierarchyInfo Hierarchy, DispatchDataInfo DispatchData) Info, bool CanUseSkipLocalsInit)> dispatchDataInfo =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo DispatchData)> dispatchDataInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, _) => (item.Hierarchy, item.DispatchData))
|
||||
.Combine(canUseSkipLocalsInit);
|
||||
.Select(static (item, _) => (item.Hierarchy, item.DispatchData));
|
||||
|
||||
// Generate the LoadDispatchData() methods
|
||||
context.RegisterSourceOutput(dispatchDataInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax loadDispatchDataMethod = LoadDispatchData.GetSyntax(
|
||||
item.Info.DispatchData.Type,
|
||||
item.Info.DispatchData.FieldInfos,
|
||||
item.Info.DispatchData.ResourceCount,
|
||||
item.Info.DispatchData.Root32BitConstantCount);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, loadDispatchDataMethod, item.CanUseSkipLocalsInit);
|
||||
item.DispatchData.Type,
|
||||
item.DispatchData.FieldInfos,
|
||||
item.DispatchData.ResourceCount,
|
||||
item.DispatchData.Root32BitConstantCount);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadDispatchDataMethod, addSkipLocalsInitAttribute: true);
|
||||
|
||||
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Get the BuildHlslSource info (hierarchy, HLSL source and parsing options)
|
||||
IncrementalValuesProvider<(((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Left, bool CanUseSkipLocalsInit) Left, bool SupportsDynamicShaders)> hlslSourceInfo =
|
||||
IncrementalValuesProvider<((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Left, bool SupportsDynamicShaders)> hlslSourceInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource))
|
||||
.Combine(canUseSkipLocalsInit)
|
||||
.Combine(supportsDynamicShaders);
|
||||
|
||||
// Generate the BuildHlslSource() methods
|
||||
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.Left.HlslShaderSource, item.SupportsDynamicShaders, item.Left.Left.Hierarchy.Hierarchy.Length);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Left.Hierarchy, buildHlslStringMethod, item.Left.CanUseSkipLocalsInit);
|
||||
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.HlslShaderSource, item.SupportsDynamicShaders, item.Left.Hierarchy.Hierarchy.Length);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, addSkipLocalsInitAttribute: true);
|
||||
|
||||
context.AddSource($"{item.Left.Left.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
context.AddSource($"{item.Left.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Get the LoadDispatchMetadata() info (hierarchy and dispatch metadata info)
|
||||
IncrementalValuesProvider<((HierarchyInfo Hierarchy, DispatchMetadataInfo DispatchMetadata) Info, bool CanUseSkipLocalsInit)> dispatchMetadataInfo =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchMetadataInfo DispatchMetadata)> dispatchMetadataInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, _) => (item.Hierarchy, item.DispatchMetadata))
|
||||
.Combine(canUseSkipLocalsInit);
|
||||
.Select(static (item, _) => (item.Hierarchy, item.DispatchMetadata));
|
||||
|
||||
// Generate the LoadDispatchMetadata() methods
|
||||
context.RegisterSourceOutput(dispatchMetadataInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax buildHlslStringMethod = LoadDispatchMetadata.GetSyntax(item.Info.DispatchMetadata);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, buildHlslStringMethod, item.CanUseSkipLocalsInit);
|
||||
MethodDeclarationSyntax buildHlslStringMethod = LoadDispatchMetadata.GetSyntax(item.DispatchMetadata);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, addSkipLocalsInitAttribute: true);
|
||||
|
||||
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchMetadata)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchMetadata)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Transform the raw HLSL source to compile (this step aggregates the HLSL source to ensure compilation is only done for actual HLSL changes)
|
||||
@@ -232,7 +228,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
context.RegisterSourceOutput(embeddedBytecodeInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax tryGetBytecodeMethod = LoadBytecode.GetSyntax(item.Info.EmbeddedBytecode, item.SupportsDynamicShaders, out Func<SyntaxNode, SourceText> fixup);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, tryGetBytecodeMethod, canUseSkipLocalsInit: false);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, tryGetBytecodeMethod, addSkipLocalsInitAttribute: false);
|
||||
SourceText text = fixup(compilationUnit);
|
||||
|
||||
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text);
|
||||
|
||||
@@ -1804,7 +1804,11 @@ public class DiagnosticsTests
|
||||
let reference = MetadataReference.CreateFromFile(assembly.Location)
|
||||
select reference;
|
||||
|
||||
CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
|
||||
CSharpCompilation compilation = CSharpCompilation.Create(
|
||||
"original",
|
||||
new SyntaxTree[] { syntaxTree },
|
||||
references,
|
||||
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
|
||||
|
||||
IIncrementalGenerator generator = new TGenerator();
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする