From f61cee8fdf3bf68df8b8fe42a45422a58373b49c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 20:13:27 +0200 Subject: [PATCH] Optimize generated Memory for input types --- ...nerator.CreateInputTypesProperty.Syntax.cs | 86 +++++++++++++------ 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs index f6ee5550..e7cd8e02 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs @@ -36,9 +36,16 @@ partial class ID2D1ShaderGenerator } else { - memoryExpression = MemberAccessExpression( + // Create a ReadOnlyMemory instance from the memory manager: + // + // InputTypesMemoryManager.Instance.Memory + memoryExpression = + MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("InputTypesMemoryManager"), + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("InputTypesMemoryManager"), + IdentifierName("Instance")), IdentifierName("Memory")); additionalTypes = new TypeDeclarationSyntax[] { GetMemoryManagerDeclaration(inputTypes) }; @@ -46,7 +53,7 @@ partial class ID2D1ShaderGenerator // This code produces a method declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => ; + // readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => ; return PropertyDeclaration( GenericName(Identifier("global::System.ReadOnlyMemory")) @@ -96,34 +103,21 @@ partial class ID2D1ShaderGenerator using ImmutableArrayBuilder memberDeclarations = ImmutableArrayBuilder.Rent(); - // Declare the singleton property to get the memory instance: + // Declare the singleton property to get the memory manager: // - // /// The singleton instance for the memory manager. - // public static new readonly global::System.ReadOnlyMemory Memory = new InputTypesMemoryManager().CreateMemory(); + // /// The singleton instance to use. + // public static readonly InputTypesMemoryManager Instance = new(); memberDeclarations.Add( FieldDeclaration( - VariableDeclaration( - GenericName(Identifier("global::System.ReadOnlyMemory")) - .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType"))) + VariableDeclaration(IdentifierName("InputTypesMemoryManager")) .AddVariables( - VariableDeclarator(Identifier("Memory")) - .WithInitializer( - EqualsValueClause( - InvocationExpression( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - ObjectCreationExpression(IdentifierName("InputTypesMemoryManager")).WithArgumentList(ArgumentList()), - IdentifierName("CreateMemory"))) - .AddArgumentListArguments(Argument( - LiteralExpression( - SyntaxKind.NumericLiteralExpression, - Literal(inputTypes.Length)))))))) + VariableDeclarator(Identifier("Instance")) + .WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))) .AddModifiers( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword), - Token(SyntaxKind.NewKeyword), Token(SyntaxKind.ReadOnlyKeyword)) - .WithLeadingTrivia(Comment("""/// The singleton instance for the memory manager."""))); + .WithLeadingTrivia(Comment("""/// The singleton instance to use."""))); using (ImmutableArrayBuilder inputTypeExpressions = ImmutableArrayBuilder.Rent()) { @@ -162,7 +156,7 @@ partial class ID2D1ShaderGenerator // /// // public override unsafe global::System.Span GetSpan // { - // return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), ); + // return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), Data.Length); // } memberDeclarations.Add( MethodDeclaration( @@ -189,7 +183,49 @@ partial class ID2D1ShaderGenerator IdentifierName("GetReference"))) .AddArgumentListArguments(Argument(IdentifierName("Data")))) .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))), - Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes.Length)))))) + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("Data"), + IdentifierName("Length")))))) + .WithLeadingTrivia(Comment("/// "))); + + // Override the Memory property: + // + // /// + // public override global::System.Memory Memory + // { + // [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + // get => CreateMemory(Data.Length); + // } + memberDeclarations.Add( + PropertyDeclaration( + GenericName(Identifier("global::System.Memory")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), + Identifier("Memory")) + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)) + .AddAccessorListAccessors( + AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) + .AddAttributeLists( + AttributeList( + SingletonSeparatedList( + Attribute(IdentifierName("global::System.Runtime.CompilerServices.MethodImpl")) + .AddArgumentListArguments( + AttributeArgument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.MethodImplOptions"), + IdentifierName("AggressiveInlining"))))))) + .WithExpressionBody( + ArrowExpressionClause( + InvocationExpression(IdentifierName("CreateMemory")) + .AddArgumentListArguments( + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("Data"), + IdentifierName("Length")))))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken))) .WithLeadingTrivia(Comment("/// "))); // Add the Pin(int elementIndex) method: