From 7a92525b71cf840e29f7defc167f0d96b4876190 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 28 Aug 2022 16:39:16 +0200 Subject: [PATCH] Implement operator intrinsics support for binary expressions --- .../SyntaxRewriters/HlslSourceRewriter.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index e487032c..ba23e8d8 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -291,6 +291,31 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter return updatedNode; } + /// + public override SyntaxNode? VisitBinaryExpression(BinaryExpressionSyntax node) + { + var updatedNode = (BinaryExpressionSyntax)base.VisitBinaryExpression(node)!; + + // Process binary operations to see if the target operator method is an intrinsic + if (SemanticModel.For(node).GetOperation(node) is IBinaryOperation { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) + { + // If the operator is indeed an HLSL overload, replace the expression with an invocation. + // That is, do the following transformation: + // + // x * y => (x, y) + if (HlslKnownOperators.TryGetMappedName(method.GetFullMetadataName(), method.Parameters.Select(static p => p.Type.Name), out string? mapped)) + { + return + InvocationExpression(IdentifierName(mapped!)) + .AddArgumentListArguments( + Argument(updatedNode.Left), + Argument(updatedNode.Right)); + } + } + + return updatedNode; + } + /// public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) {