Move operator logic to 'HlslOperatorsSyntaxProcessor'
このコミットが含まれているのは:
1 親
c7070d73a0
コミット
ca46b9a726
3ファイル変更
+160
-66
検索条件に一致するファイルはありません
@@ -32,6 +32,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\TypeAliases.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxProcessors\HlslBytecodeSyntaxProcessor.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxProcessors\ConstantBufferSyntaxProcessor.Generation.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxProcessors\HlslOperatorsSyntaxProcessor.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxProcessors\HlslSourceSyntaxProcessor.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxProcessors\HlslDefinitionsSyntaxProcessor.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)SyntaxRewriters\HlslSourceRewriter.Tracking.cs" />
|
||||
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Mappings;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.SyntaxProcessors;
|
||||
|
||||
/// <summary>
|
||||
/// A processor responsible for processing special HLSL operators.
|
||||
/// </summary>
|
||||
internal static class HlslOperatorsSyntaxProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to process a custom operator from an input expression node.
|
||||
/// </summary>
|
||||
/// <param name="originalNode">The original syntax node being processed.</param>
|
||||
/// <param name="updatedNode">The updated node to propagate and potentially rewrite.</param>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance to use.</param>
|
||||
/// <param name="token">The cancellation token for the operation.</param>
|
||||
/// <param name="rewrittenNode">The rewritten node, if successfully produced.</param>
|
||||
/// <returns>Whether a rewritten node was produced.</returns>
|
||||
public static bool TryProcessCustomOperator(
|
||||
ExpressionSyntax originalNode,
|
||||
ExpressionSyntax updatedNode,
|
||||
SemanticModel semanticModel,
|
||||
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)
|
||||
{
|
||||
rewrittenNode = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pre-filter candidates based on the operation type, to minimize allocations
|
||||
if (!HlslKnownOperators.IsCandidateOperator(operation.OperatorKind))
|
||||
{
|
||||
rewrittenNode = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the binary nodes, in case we need them for rewriting
|
||||
GetBinaryNodes(updatedNode, out ExpressionSyntax leftNode, out ExpressionSyntax rightNode);
|
||||
|
||||
// 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))
|
||||
{
|
||||
rewrittenNode = BinaryExpression(SyntaxKind.RightShiftExpression, leftNode, rightNode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
rewrittenNode =
|
||||
CastExpression(
|
||||
IdentifierName(typeNameOfLeftOperand),
|
||||
ParenthesizedExpression(
|
||||
BinaryExpression(
|
||||
SyntaxKind.RightShiftExpression,
|
||||
CastExpression(IdentifierName(unsignedTypeName), leftNode),
|
||||
rightNode)));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// All other cases are unsupported, so just return the original tree
|
||||
rewrittenNode = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Also pre-filter just operators that are defined in ComputeSharp primitives
|
||||
if (operation is not { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method })
|
||||
{
|
||||
rewrittenNode = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the operator is indeed an HLSL overload, replace the expression with an invocation.
|
||||
// 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))
|
||||
{
|
||||
rewrittenNode =
|
||||
InvocationExpression(IdentifierName(mapped!))
|
||||
.AddArgumentListArguments(
|
||||
Argument(leftNode),
|
||||
Argument(rightNode));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
rewrittenNode = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the binary children from an expression node.
|
||||
/// </summary>
|
||||
/// <param name="node">The input expression node.</param>
|
||||
/// <param name="left">The resulting left child node.</param>
|
||||
/// <param name="right">The resulting right child node.</param>
|
||||
/// <exception cref="NotSupportedException">Thrown if <paramref name="node"/> is of an unsupported type.</exception>
|
||||
private static void GetBinaryNodes(
|
||||
ExpressionSyntax node,
|
||||
out ExpressionSyntax left,
|
||||
out ExpressionSyntax right)
|
||||
{
|
||||
switch (node)
|
||||
{
|
||||
case BinaryExpressionSyntax binaryExpression:
|
||||
left = binaryExpression.Left;
|
||||
right = binaryExpression.Right;
|
||||
break;
|
||||
case AssignmentExpressionSyntax assignmentExpression:
|
||||
left = assignmentExpression.Left;
|
||||
right = assignmentExpression.Right;
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException("Invalid input expression node.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Mappings;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
using ComputeSharp.SourceGeneration.SyntaxProcessors;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
@@ -376,73 +377,14 @@ 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 not IBinaryOperation operation)
|
||||
if (HlslOperatorsSyntaxProcessor.TryProcessCustomOperator(
|
||||
originalNode: node,
|
||||
updatedNode: updatedNode,
|
||||
semanticModel: SemanticModel.For(node),
|
||||
token: CancellationToken,
|
||||
rewrittenNode: out ExpressionSyntax? rewrittenNode))
|
||||
{
|
||||
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 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 => <INTRINSIC>(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 rewrittenNode;
|
||||
}
|
||||
|
||||
return updatedNode;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする