From c7070d73a04e537512d9be8310ccc6576a07013c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Jun 2024 19:07:01 -0700 Subject: [PATCH] Handle '>>>' operators in HLSL rewriters --- .../Mappings/HlslKnownOperators.cs | 12 +++ .../Mappings/HlslKnownTypes.cs | 32 ++++++++ .../SyntaxRewriters/HlslSourceRewriter.cs | 74 ++++++++++++++++--- 3 files changed, 107 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs index 3403bac6..b800f567 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using ComputeSharp.Core.Intrinsics.Attributes; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Operations; namespace ComputeSharp.SourceGeneration.Mappings; @@ -47,6 +49,16 @@ internal static partial class HlslKnownOperators return knownOperators; } + /// + /// Checks whether a given operator kind represents a candidate special intrinsic that can handle. + /// + /// The input operator kind to check. + /// Whether is a candidate operator for a special intrinsic to rewrite. + public static bool IsCandidateOperator(BinaryOperatorKind operatorKind) + { + return operatorKind is BinaryOperatorKind.Multiply or BinaryOperatorKind.UnsignedRightShift; + } + /// /// Tries to get the mapped HLSL-compatible method name for the input operator name. /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs index 4d62654f..0dad51aa 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs @@ -237,6 +237,38 @@ internal static partial class HlslKnownTypes return false; } + /// + /// Checks whether a given type name represents a well known signed integer type. + /// + /// The type name to check. + /// Whether represents a well known signed integer type. + public static bool IsKnownSignedIntegerType(string typeName) + { + return typeName is + "System.Int32" or + "ComputeSharp.Int2" or "ComputeSharp.Int3" or "ComputeSharp.Int4" or + "ComputeSharp.Int1x1" or "ComputeSharp.Int1x2" or "ComputeSharp.Int1x3" or "ComputeSharp.Int1x4" or + "ComputeSharp.Int2x1" or "ComputeSharp.Int2x2" or "ComputeSharp.Int2x3" or "ComputeSharp.Int2x4" or + "ComputeSharp.Int3x1" or "ComputeSharp.Int3x2" or "ComputeSharp.Int3x3" or "ComputeSharp.Int3x4" or + "ComputeSharp.Int4x1" or "ComputeSharp.Int4x2" or "ComputeSharp.Int4x3" or "ComputeSharp.Int4x4"; + } + + /// + /// Checks whether a given type name represents a well known unsigned integer type. + /// + /// The type name to check. + /// Whether represents a well known known integer type. + public static bool IsKnownUnsignedIntegerType(string typeName) + { + return typeName is + "System.UInt32" or + "ComputeSharp.UInt2" or "ComputeSharp.UInt3" or "ComputeSharp.UInt4" or + "ComputeSharp.UInt1x1" or "ComputeSharp.UInt1x2" or "ComputeSharp.UInt1x3" or "ComputeSharp.UInt1x4" or + "ComputeSharp.UInt2x1" or "ComputeSharp.UInt2x2" or "ComputeSharp.UInt2x3" or "ComputeSharp.UInt2x4" or + "ComputeSharp.UInt3x1" or "ComputeSharp.UInt3x2" or "ComputeSharp.UInt3x3" or "ComputeSharp.UInt3x4" or + "ComputeSharp.UInt4x1" or "ComputeSharp.UInt4x2" or "ComputeSharp.UInt4x3" or "ComputeSharp.UInt4x4"; + } + /// /// Gets the mapped HLSL-compatible type name for the input type symbol. /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 84734904..dfac4ddd 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -377,20 +377,72 @@ internal abstract partial class HlslSourceRewriter( BinaryExpressionSyntax updatedNode = (BinaryExpressionSyntax)base.VisitBinaryExpression(node)!; // Process binary operations to see if the target operator method is an intrinsic - if (SemanticModel.For(node).GetOperation(node, CancellationToken) is IBinaryOperation { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) + if (SemanticModel.For(node).GetOperation(node, CancellationToken) is not IBinaryOperation operation) { - // 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.GetFullyQualifiedMetadataName(), method.Parameters.Select(static p => p.Type.Name), out string? mapped)) + return updatedNode; + } + + // Pre-filter candidates based on the operation type, to minimize allocations + if (!HlslKnownOperators.IsCandidateOperator(operation.OperatorKind)) + { + return updatedNode; + } + + // 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) + { + string typeName = operation.LeftOperand.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. + if (HlslKnownTypes.IsKnownUnsignedIntegerType(typeName)) { - return - InvocationExpression(IdentifierName(mapped!)) - .AddArgumentListArguments( - Argument(updatedNode.Left), - Argument(updatedNode.Right)); + return BinaryExpression(SyntaxKind.RightShiftExpression, updatedNode.Left, updatedNode.Right); } + + // Rewrite the syntax tree as follows: + // + // x >>> y => (TYPE_X)((UNSIGNED_TYPE_X)x >> y) + if (HlslKnownTypes.IsKnownSignedIntegerType(typeName)) + { + string typeNameOfLeftOperand = HlslKnownTypes.GetMappedName(typeName); + string unsignedTypeName = 'u' + typeNameOfLeftOperand; + + return + CastExpression( + IdentifierName(typeNameOfLeftOperand), + ParenthesizedExpression( + BinaryExpression( + SyntaxKind.RightShiftExpression, + CastExpression( + IdentifierName(unsignedTypeName), + updatedNode.Left), + updatedNode.Right))); + } + + // All other cases are unsupported, so just return the original tree + return updatedNode; + } + + // Also pre-filter just operators that are defined in ComputeSharp primitives + if (operation is not { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) + { + return updatedNode; + } + + // 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.GetFullyQualifiedMetadataName(), method.Parameters.Select(static p => p.Type.Name), out string? mapped)) + { + return + InvocationExpression(IdentifierName(mapped!)) + .AddArgumentListArguments( + Argument(updatedNode.Left), + Argument(updatedNode.Right)); } return updatedNode;