Handle '>>>' operators in HLSL rewriters
このコミットが含まれているのは:
1 親
5ded1ceb59
コミット
c7070d73a0
3ファイル変更
+107
-11
検索条件に一致するファイルはありません
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given operator kind represents a candidate special intrinsic that can handle.
|
||||
/// </summary>
|
||||
/// <param name="operatorKind">The input operator kind to check.</param>
|
||||
/// <returns>Whether <paramref name="operatorKind"/> is a candidate operator for a special intrinsic to rewrite.</returns>
|
||||
public static bool IsCandidateOperator(BinaryOperatorKind operatorKind)
|
||||
{
|
||||
return operatorKind is BinaryOperatorKind.Multiply or BinaryOperatorKind.UnsignedRightShift;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible method name for the input operator name.
|
||||
/// </summary>
|
||||
|
||||
@@ -237,6 +237,38 @@ internal static partial class HlslKnownTypes
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given type name represents a well known signed integer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The type name to check.</param>
|
||||
/// <returns>Whether <paramref name="typeName"/> represents a well known signed integer type.</returns>
|
||||
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";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given type name represents a well known unsigned integer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The type name to check.</param>
|
||||
/// <returns>Whether <paramref name="typeName"/> represents a well known known integer type.</returns>
|
||||
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";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type symbol.
|
||||
/// </summary>
|
||||
|
||||
@@ -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 => <INTRINSIC>(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 => <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 updatedNode;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする