diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslOperatorsSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslOperatorsSyntaxProcessor.cs index 699d7e09..f1f77a51 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslOperatorsSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslOperatorsSyntaxProcessor.cs @@ -33,8 +33,21 @@ internal static class HlslOperatorsSyntaxProcessor CancellationToken token, [NotNullWhen(true)] out ExpressionSyntax? rewrittenNode) { - // Process binary operations to see if the target operator method is an intrinsic - if (semanticModel.GetOperation(originalNode, token) is not IBinaryOperation operation) + // Make sure we can correctly retrieve an operation first + if (semanticModel.GetOperation(originalNode, token) is not IOperation operation) + { + rewrittenNode = null; + + return false; + } + + // Next, try to get the operation info + if (!TryGetBinaryOperationInfo( + operation, + out BinaryOperatorKind operationKind, + out IOperation? leftOperation, + out IOperation? rightOperation, + out IMethodSymbol? operatorMethod)) { rewrittenNode = null; @@ -42,7 +55,7 @@ internal static class HlslOperatorsSyntaxProcessor } // Pre-filter candidates based on the operation type, to minimize allocations - if (!HlslKnownOperators.IsCandidateOperator(operation.OperatorKind)) + if (!HlslKnownOperators.IsCandidateOperator(operationKind)) { rewrittenNode = null; @@ -55,9 +68,9 @@ internal static class HlslOperatorsSyntaxProcessor // First, try to handle unsigned right shift operators, which are a special case. // This is because they require explicit rewriting, and not just a change in method name. // We need to do this after pre-filtering, as this is also supported on 'int' and 'uint'. - if (operation.OperatorKind == BinaryOperatorKind.UnsignedRightShift) + if (operationKind == BinaryOperatorKind.UnsignedRightShift) { - string typeName = operation.LeftOperand.Type!.GetFullyQualifiedMetadataName(); + string typeName = leftOperation.Type!.GetFullyQualifiedMetadataName(); // If the left operand is already unsigned, there's no rewriting to do other // than just replacing '>>>' with '>>', since the former doesn't exist in HLSL. @@ -65,6 +78,8 @@ internal static class HlslOperatorsSyntaxProcessor { rewrittenNode = BinaryExpression(SyntaxKind.RightShiftExpression, leftNode, rightNode); + WrapRewrittenNodeIfNeeded(updatedNode, operation, ref rewrittenNode); + return true; } @@ -85,6 +100,8 @@ internal static class HlslOperatorsSyntaxProcessor CastExpression(IdentifierName(unsignedTypeName), leftNode), rightNode))); + WrapRewrittenNodeIfNeeded(updatedNode, operation, ref rewrittenNode); + return true; } @@ -95,7 +112,7 @@ internal static class HlslOperatorsSyntaxProcessor } // Also pre-filter just operators that are defined in ComputeSharp primitives - if (operation is not { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) + if (operatorMethod is not { ContainingType.ContainingNamespace.Name: "ComputeSharp" }) { rewrittenNode = null; @@ -106,7 +123,10 @@ internal static class HlslOperatorsSyntaxProcessor // That is, do the following transformation: // // x * y => (x, y) - if (HlslKnownOperators.TryGetMappedName(method.GetFullyQualifiedMetadataName(), method.Parameters.Select(static p => p.Type.Name), out string? mapped)) + if (HlslKnownOperators.TryGetMappedName( + operatorMethod.GetFullyQualifiedMetadataName(), + operatorMethod.Parameters.Select(static p => p.Type.Name), + out string? mapped)) { rewrittenNode = InvocationExpression(IdentifierName(mapped!)) @@ -114,6 +134,8 @@ internal static class HlslOperatorsSyntaxProcessor Argument(leftNode), Argument(rightNode)); + WrapRewrittenNodeIfNeeded(updatedNode, operation, ref rewrittenNode); + return true; } @@ -122,6 +144,48 @@ internal static class HlslOperatorsSyntaxProcessor return false; } + /// + /// Tries to get the info for a given binary operation. + /// + /// The input operation to extract info from. + /// The resulting kind for . + /// The resulting left child operation, or the target. + /// The resulting right child operation. + /// The target method being invoked, if any. + /// Whether the input operation is supported and info could be retrieved. + private static bool TryGetBinaryOperationInfo( + IOperation operation, + out BinaryOperatorKind operationKind, + [NotNullWhen(true)] out IOperation? leftOrTargetOperation, + [NotNullWhen(true)] out IOperation? rightOperation, + out IMethodSymbol? targetMethod) + { + switch (operation) + { + case IBinaryOperation binaryOperation: + operationKind = binaryOperation.OperatorKind; + leftOrTargetOperation = binaryOperation.LeftOperand; + rightOperation = binaryOperation.RightOperand; + targetMethod = binaryOperation.OperatorMethod; + + return true; + case ICompoundAssignmentOperation assignmentOperation: + operationKind = assignmentOperation.OperatorKind; + leftOrTargetOperation = assignmentOperation.Target; + rightOperation = assignmentOperation.Value; + targetMethod = assignmentOperation.OperatorMethod; + + return true; + default: + operationKind = BinaryOperatorKind.None; + leftOrTargetOperation = null; + rightOperation = null; + targetMethod = null; + + return false; + } + } + /// /// Extracts the binary children from an expression node. /// @@ -148,4 +212,31 @@ internal static class HlslOperatorsSyntaxProcessor throw new NotSupportedException("Invalid input expression node."); } } + + /// + /// Wraps a rewritten node if needed, depending on the operation type. + /// + /// The updated node to propagate and potentially rewrite. + /// The input operation being processed. + /// The rewritten node, to wrap if needed. + /// Throw if the input operation isn't valid for the input expression node. + private static void WrapRewrittenNodeIfNeeded( + ExpressionSyntax updatedNode, + IOperation operation, + ref ExpressionSyntax rewrittenNode) + { + switch (operation) + { + case IBinaryOperation: + break; + case ICompoundAssignmentOperation when updatedNode is AssignmentExpressionSyntax assignmentNode: + rewrittenNode = AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + assignmentNode.Left, + rewrittenNode); + break; + default: + throw new NotSupportedException("Invalid input operation type."); + } + } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 0d242e7d..98e79d17 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; @@ -348,23 +347,14 @@ internal abstract partial class HlslSourceRewriter( AssignmentExpressionSyntax updatedNode = (AssignmentExpressionSyntax)base.VisitAssignmentExpression(node)!; - if (SemanticModel.For(node).GetOperation(node, CancellationToken) is ICompoundAssignmentOperation { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) + if (HlslOperatorsSyntaxProcessor.TryProcessCustomOperator( + originalNode: node, + updatedNode: updatedNode, + semanticModel: SemanticModel.For(node), + token: CancellationToken, + rewrittenNode: out ExpressionSyntax? rewrittenNode)) { - // If the compound assignment is using an HLSL operator, replace the expression with an invocation and assignment. - // That is, do the following transformation: - // - // x *= y => x = (x, y) - if (HlslKnownOperators.TryGetMappedName(method.GetFullyQualifiedMetadataName(), method.Parameters.Select(static p => p.Type.Name), out string? mapped)) - { - return - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - updatedNode.Left, - InvocationExpression(IdentifierName(mapped!)) - .AddArgumentListArguments( - Argument(updatedNode.Left), - Argument(updatedNode.Right))); - } + return rewrittenNode; } return updatedNode;