From 5973921567f7d481fee3479b815e9d62c059d090 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Dec 2023 00:47:00 +0100 Subject: [PATCH] Handle identifier names for all static shader fields --- .../SyntaxRewriters/HlslSourceRewriter.cs | 2 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 96 ++++++++++++------- .../ComputeSharp.Tests/ShaderRewriterTests.cs | 12 ++- 3 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 8f43912a..6c6bb244 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -390,7 +390,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter } /// - public sealed override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) + public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) { IdentifierNameSyntax updatedNode = (IdentifierNameSyntax)base.VisitIdentifierName(node)!; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index d4d9584f..e71d9f25 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Threading; @@ -333,6 +334,26 @@ internal sealed partial class ShaderSourceRewriter( } } + /// + public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) + { + // Intercept identifier names to catch whether we are accessing a static field from its containing type. + // This needs special handling, as it would not go through the usual member access path. If we are doing + // this, then we rewrite it with the shared logic for all static fields, otherwise we return the normal + // visit. Before getting semantic information, we can pre-filter to exclude invocation expressions and + // member access expressions. This is safe, because those two parent nodes would already be handled. + // We also only process fields from external types, as those in shader types don't need handling here. + // This is because they're lowered to the same identifier name, without fully qualified names to rewrite. + if (node.Parent is not (InvocationExpressionSyntax or MemberAccessExpressionSyntax) && + SemanticModel.For(node).GetOperation(node) is IFieldReferenceOperation { Field.IsStatic: true } operation && + !SymbolEqualityComparer.Default.Equals(operation.Field.ContainingType, this.shaderType)) + { + return VisitExternalStaticFieldAccess(null, operation.Field) ?? base.VisitIdentifierName(node); + } + + return base.VisitIdentifierName(node); + } + /// public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { @@ -371,8 +392,6 @@ internal sealed partial class ShaderSourceRewriter( // Handle static fields, which can be either in the shader type itself, or in external types if (fieldOperation.Field.IsStatic) { - (string Name, string, string?) fieldInfo; - // Special case static fields in the shader type itself. Those are already discovered separately, when crawling // all members of the shader type. But, if this path was hit, it means that they are accessed via a member access // expression, which means they need to be rewritten to match the simple name they will have in the shader. This @@ -383,37 +402,8 @@ internal sealed partial class ShaderSourceRewriter( return IdentifierName(mappedFieldName ?? fieldOperation.Field.Name); } - else if (!StaticFieldDefinitions.TryGetValue(fieldOperation.Field, out fieldInfo)) - { - // Execute the same logic as for shader static fields, to process them and extract the relevant info. - // In this case, we use the fully qualified name of the field as identifier, not just the field name. - if (HlslDefinitionsSyntaxProcessor.TryGetStaticField( - this.shaderType, - fieldOperation.Field, - SemanticModel, - DiscoveredTypes, - ConstantDefinitions, - StaticFieldDefinitions, - Diagnostics, - CancellationToken, - out _, - out string? typeDeclaration, - out string? assignmentExpression, - out _)) - { - fieldInfo.Name = fieldOperation.Field.GetFullyQualifiedMetadataName().ToHlslIdentifierName(); - StaticFieldDefinitions.Add(fieldOperation.Field, (fieldInfo.Name, typeDeclaration, assignmentExpression)); - } - else - { - // We failed to process the field for whatever reason. Just stop rewriting it and return - // the current updated node. We'll have some diagnostic for this case emitted previously. - return updatedNode; - } - } - - return IdentifierName(fieldInfo.Name); + return VisitExternalStaticFieldAccess(updatedNode, fieldOperation.Field); } } @@ -770,6 +760,48 @@ internal sealed partial class ShaderSourceRewriter( return base.VisitUserDefinedObjectCreationExpression(node, updatedNode, targetType); } + /// + /// Visits a static field access in an external type and rewrites it as needed. + /// + /// The current instance for the static field. + /// The instance for . + /// The rewritten static field expression. + [return: NotNullIfNotNull(nameof(node))] + private SyntaxNode? VisitExternalStaticFieldAccess(SyntaxNode? node, IFieldSymbol fieldSymbol) + { + if (!StaticFieldDefinitions.TryGetValue(fieldSymbol, out (string Name, string, string?) fieldInfo)) + { + // Execute the same logic as for shader static fields, to process them and extract the relevant info. + // In this case, we use the fully qualified name of the field as identifier, not just the field name. + if (HlslDefinitionsSyntaxProcessor.TryGetStaticField( + this.shaderType, + fieldSymbol, + SemanticModel, + DiscoveredTypes, + ConstantDefinitions, + StaticFieldDefinitions, + Diagnostics, + CancellationToken, + out _, + out string? typeDeclaration, + out string? assignmentExpression, + out _)) + { + fieldInfo.Name = fieldSymbol.GetFullyQualifiedMetadataName().ToHlslIdentifierName(); + + StaticFieldDefinitions.Add(fieldSymbol, (fieldInfo.Name, typeDeclaration, assignmentExpression)); + } + else + { + // We failed to process the field for whatever reason. Just stop rewriting it and return + // the current updated node. We'll have some diagnostic for this case emitted previously. + return node; + } + } + + return IdentifierName(fieldInfo.Name); + } + /// /// Rewrites a sampled texture access. /// diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index 5290fabf..1a36df60 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -5,7 +5,7 @@ using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable IDE0007, IDE0008, IDE0009 +#pragma warning disable IDE0002, IDE0007, IDE0008, IDE0009 namespace ComputeSharp.Tests; @@ -509,6 +509,7 @@ public partial class ShaderRewriterTests Assert.AreEqual(666, results[11]); Assert.AreEqual(3.14f, results[12]); Assert.AreEqual(3.14f, results[13]); + Assert.AreEqual(6.28f, results[14]); } [AutoConstructor] @@ -551,6 +552,9 @@ public partial class ShaderRewriterTests // Access shader fields via a member access buffer[12] = MixedStaticFieldsInExternalTypesShader.PI; buffer[13] = ExternalType.ReadPI(); + + // Access an external field as an identifier name + buffer[14] = ExternalType.ReadPI2(); } } @@ -565,6 +569,12 @@ public partial class ShaderRewriterTests { return MixedStaticFieldsInExternalTypesShader.PI; } + + // See https://github.com/Sergio0694/ComputeSharp/issues/298 + public static float ReadPI2() + { + return PI2; + } } // See https://github.com/Sergio0694/ComputeSharp/issues/278