Share operators rewriting with assignment expressions

このコミットが含まれているのは:
Sergio Pedri committed 2024-06-24 19:29:18 -07:00
1 親 ca46b9a726
コミット 60eed206d6
2ファイル変更
+105 -24

検索条件に一致するファイルはありません

+98 -7
ファイルの表示
@@ -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 => <INTRINSIC>(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;
}
/// <summary>
/// Tries to get the info for a given binary operation.
/// </summary>
/// <param name="operation">The input operation to extract info from.</param>
/// <param name="operationKind">The resulting kind for <paramref name="operation"/>.</param>
/// <param name="leftOrTargetOperation">The resulting left child operation, or the target.</param>
/// <param name="rightOperation">The resulting right child operation.</param>
/// <param name="targetMethod">The target method being invoked, if any.</param>
/// <returns>Whether the input operation is supported and info could be retrieved.</returns>
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;
}
}
/// <summary>
/// Extracts the binary children from an expression node.
/// </summary>
@@ -148,4 +212,31 @@ internal static class HlslOperatorsSyntaxProcessor
throw new NotSupportedException("Invalid input expression node.");
}
}
/// <summary>
/// Wraps a rewritten node if needed, depending on the operation type.
/// </summary>
/// <param name="updatedNode">The updated node to propagate and potentially rewrite.</param>
/// <param name="operation">The input operation being processed.</param>
/// <param name="rewrittenNode">The rewritten node, to wrap if needed.</param>
/// <exception cref="NotSupportedException">Throw if the input operation isn't valid for the input expression node.</exception>
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.");
}
}
}
+7 -17
ファイルの表示
@@ -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 = <INTRINSIC>(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;