From 49d0ec2a5b65ba4a681a4e78b6d2d14e1cd13602 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Oct 2023 23:08:09 +0200 Subject: [PATCH] Unify output node for D3D12 source generator --- .../Extensions/DiagnosticsExtensions.cs | 10 --- .../Models/DiagnosticInfo.cs | 6 +- .../IShaderGenerator.Helpers.cs | 45 ------------ .../IShaderGenerator.cs | 69 +++---------------- 4 files changed, 16 insertions(+), 114 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs index 033be458..f5aadb9b 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs @@ -41,16 +41,6 @@ internal static class DiagnosticsExtensions diagnostics.Add(DiagnosticInfo.Create(descriptor, node, args)); } - /// - /// Registers an output node into an to output diagnostics. - /// - /// The input instance. - /// The input sequence of diagnostics. - public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider diagnostics) - { - context.RegisterSourceOutput(diagnostics, static (context, diagnostic) => context.ReportDiagnostic(diagnostic.ToDiagnostic())); - } - /// /// Registers an output node into an to output diagnostics. /// diff --git a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs index e7009ff6..de3ae0ed 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs @@ -13,7 +13,11 @@ namespace ComputeSharp.SourceGeneration.Models; /// The tree to use as location for the diagnostic, if available. /// The span to use as location for the diagnostic. /// The diagnostic arguments. -internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, SyntaxTree? SyntaxTree, TextSpan TextSpan, EquatableArray Arguments) +internal sealed record DiagnosticInfo( + DiagnosticDescriptor Descriptor, + SyntaxTree? SyntaxTree, + TextSpan TextSpan, + EquatableArray Arguments) { /// /// Creates a new instance with the state from this model. diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 6a29ab66..f12485d9 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -1,9 +1,4 @@ -using System.Collections.Generic; -using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.SourceGenerators; @@ -43,44 +38,4 @@ partial class IShaderGenerator return false; } - - /// - /// Creates a instance wrapping the given method. - /// - /// The instance for the current type. - /// The item to insert. - /// Whether [SkipLocalsInit] should be used. - /// A object wrapping . - private static CompilationUnitSyntax GetCompilationUnitFromMethod( - HierarchyInfo hierarchyInfo, - MethodDeclarationSyntax methodDeclaration, - bool addSkipLocalsInitAttribute) - { - // Method attributes - List attributes = new() - { - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(IShaderGenerator).FullName))), - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(IShaderGenerator).Assembly.GetName().Version.ToString())))))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))), - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.ComponentModel.EditorBrowsable")).AddArgumentListArguments( - AttributeArgument(ParseExpression("global::System.ComponentModel.EditorBrowsableState.Never"))))), - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.Obsolete")).AddArgumentListArguments( - AttributeArgument(LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal("This method is not intended to be used directly by user code")))))) - }; - - // Add [SkipLocalsInit] if needed - if (addSkipLocalsInitAttribute) - { - attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit"))))); - } - - return hierarchyInfo.GetSyntax(methodDeclaration.AddAttributeLists(attributes.ToArray())); - } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index c189e5d0..7acef9d5 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -26,7 +26,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { // Discover all shader types and extract all the necessary info from each of them - IncrementalValuesProvider shaderInfoWithErrors = + IncrementalValuesProvider shaderInfo = context.SyntaxProvider .CreateSyntaxProvider( static (node, _) => node.IsTypeDeclarationWithOrPotentiallyWithBaseTypes(), @@ -117,68 +117,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator }) .Where(static item => item is not null)!; - // Output the diagnostics, if any - context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostcs)); + // Split the diagnostics, and drop them from the output provider (see more notes in the D2D1 generator) + IncrementalValuesProvider> diagnosticInfo = shaderInfo.Select(static (item, _) => item.Diagnostcs); + IncrementalValuesProvider outputInfo = shaderInfo.Select(static (item, _) => item with { Diagnostcs = default }); - // Generate the LoadDispatchData() methods - context.RegisterSourceOutput(shaderInfoWithErrors, static (context, item) => + // Output the diagnostics, if any + context.ReportDiagnostics(diagnosticInfo); + + // Generate the source files, if any + context.RegisterSourceOutput(outputInfo, static (context, item) => { using ImmutableArrayBuilder> declaredMembers = new(); declaredMembers.Add(LoadDispatchData.WriteSyntax); - - using IndentedTextWriter writer = new(); - - item.Hierarchy.WriteSyntax( - state: item, - writer: writer, - baseTypes: ReadOnlySpan.Empty, - memberCallbacks: declaredMembers.WrittenSpan); - - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", writer.ToString()); - }); - - // Generate the BuildHlslSource() methods - context.RegisterSourceOutput(shaderInfoWithErrors, static (context, item) => - { - using ImmutableArrayBuilder> declaredMembers = new(); - - declaredMembers.Add(BuildHlslSource.WriteSyntax); - - using IndentedTextWriter writer = new(); - - item.Hierarchy.WriteSyntax( - state: item, - writer: writer, - baseTypes: ReadOnlySpan.Empty, - memberCallbacks: declaredMembers.WrittenSpan); - - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", writer.ToString()); - }); - - // Generate the LoadDispatchMetadata() methods - context.RegisterSourceOutput(shaderInfoWithErrors, static (context, item) => - { - using ImmutableArrayBuilder> declaredMembers = new(); - declaredMembers.Add(LoadDispatchMetadata.WriteSyntax); - - using IndentedTextWriter writer = new(); - - item.Hierarchy.WriteSyntax( - state: item, - writer: writer, - baseTypes: ReadOnlySpan.Empty, - memberCallbacks: declaredMembers.WrittenSpan); - - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchMetadata)}.g.cs", writer.ToString()); - }); - - // Generate the TryGetBytecode() methods - context.RegisterSourceOutput(shaderInfoWithErrors, static (context, item) => - { - using ImmutableArrayBuilder> declaredMembers = new(); - + declaredMembers.Add(BuildHlslSource.WriteSyntax); declaredMembers.Add(LoadBytecode.WriteSyntax); using IndentedTextWriter writer = new(); @@ -189,7 +142,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator baseTypes: ReadOnlySpan.Empty, memberCallbacks: declaredMembers.WrittenSpan); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", writer.ToString()); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.g.cs", writer.ToString()); }); } } \ No newline at end of file