From be614c5af5e2204609918f21b0c59e66ade2df98 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 6 Dec 2021 13:33:29 +0100 Subject: [PATCH] Enable caching for DispatchId info --- .../IShaderGenerator.Incremental.cs | 12 ++-- .../Models/DispatchIdInfo.cs | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs index 92484930..9afca133 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs @@ -59,14 +59,15 @@ public sealed partial class IShaderGenerator2 : IIncrementalGenerator .Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol), item.Type!)); // Get the hierarchy info and delegate field names for each shader type - IncrementalValuesProvider<(HierarchyInfo Hierarchy, ImmutableArray Delegates)> hierarchyInfoAndDelegateFieldNames = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchIdInfo Info)> hierarchyAndDispatchIdInfo = shaderDeclarations - .Select(static (item, token) => (item.Hierarchy, GetDelegateFieldNames(item.Symbol))); + .Select(static (item, token) => (item.Hierarchy, new DispatchIdInfo(GetDelegateFieldNames(item.Symbol)))) + .WithComparers(HierarchyInfo.Comparer.Default, DispatchIdInfo.Comparer.Default); // Generate the GetDispatchId() methods - context.RegisterSourceOutput(hierarchyInfoAndDelegateFieldNames.Combine(canUseSkipLocalsInit), static (context, item) => + context.RegisterSourceOutput(hierarchyAndDispatchIdInfo.Combine(canUseSkipLocalsInit), static (context, item) => { - MethodDeclarationSyntax getDispatchIdMethod = CreateGetDispatchIdMethod(item.Left.Delegates); + MethodDeclarationSyntax getDispatchIdMethod = CreateGetDispatchIdMethod(item.Left.Info.Delegates); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, getDispatchIdMethod, item.Right); context.AddSource($"{item.Left.Hierarchy.FilenameHint}.GetDispatchId", SourceText.From(compilationUnit.ToFullString(), Encoding.UTF8)); @@ -99,7 +100,8 @@ public sealed partial class IShaderGenerator2 : IIncrementalGenerator // Get a filtered sequence to enable caching IncrementalValuesProvider dispatchDataInfo = dispatchDataInfoWithErrors - .Select(static (item, token) => item.Value); + .Select(static (item, token) => item.Value) + .WithComparer(DispatchDataInfo.Comparer.Default); // Generate the LoadDispatchData() methods context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) => diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs new file mode 100644 index 00000000..772a9516 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs @@ -0,0 +1,55 @@ +using ComputeSharp.SourceGenerators.Extensions; +using System; +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model representing gathered info on a shader dispatch id. +/// +/// The list of delegate field names for the shader. +internal sealed record DispatchIdInfo(ImmutableArray Delegates) +{ + /// + /// An implementation for . + /// + public sealed class Comparer : IEqualityComparer + { + /// + /// The singleton instance. + /// + public static Comparer Default { get; } = new(); + + /// + public bool Equals(DispatchIdInfo? x, DispatchIdInfo? y) + { + if (x is null && y is null) + { + return true; + } + + if (x is null || y is null) + { + return false; + } + + if (ReferenceEquals(x, y)) + { + return true; + } + + return x.Delegates.SequenceEqual(y.Delegates); + } + + /// + public int GetHashCode(DispatchIdInfo obj) + { + HashCode hashCode = default; + + hashCode.AddRange(obj.Delegates); + + return hashCode.ToHashCode(); + } + } +}