Merge pull request #701 from Sergio0694/dev/hlsl-rewriter-cancellation

Check for cancellation during HLSL rewriting
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-04 12:07:29 +01:00
コミット ea9cc0809a
6ファイル変更
+71 -16

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

+10 -2
ファイルの表示
@@ -284,13 +284,20 @@ partial class D2DPixelShaderDescriptorGenerator
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
token.ThrowIfCancellationRequested();
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
diagnostics,
token);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator);
token.ThrowIfCancellationRequested();
string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString();
needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute;
@@ -368,6 +375,7 @@ partial class D2DPixelShaderDescriptorGenerator
constructors,
constantDefinitions,
diagnostics,
token,
isShaderEntryPoint);
// Rewrite the method syntax tree
+20 -1
ファイルの表示
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -33,16 +34,19 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <param name="discoveredTypes">The set of discovered custom types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="token">The <see cref="System.Threading.CancellationToken"/> value for the current operation.</param>
protected HlslSourceRewriter(
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
CancellationToken token)
{
SemanticModel = semanticModel;
DiscoveredTypes = discoveredTypes;
ConstantDefinitions = constantDefinitions;
Diagnostics = diagnostics;
CancellationToken = token;
}
/// <summary>
@@ -65,6 +69,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// </summary>
protected ImmutableArrayBuilder<DiagnosticInfo> Diagnostics { get; }
/// <summary>
/// Gets the <see cref="System.Threading.CancellationToken"/> value for the current operation.
/// </summary>
protected CancellationToken CancellationToken { get; }
/// <inheritdoc/>
public sealed override SyntaxNode VisitCastExpression(CastExpressionSyntax node)
{
@@ -118,6 +127,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <returns>The rewritten <see cref="SyntaxNode"/> for the object creation expression.</returns>
private SyntaxNode VisitObjectCreationExpression(BaseObjectCreationExpressionSyntax node, BaseObjectCreationExpressionSyntax updatedNode, TypeSyntax targetType)
{
CancellationToken.ThrowIfCancellationRequested();
ITypeSymbol? typeSymbol = SemanticModel.For(node).GetTypeInfo(node).Type;
// Handle the edge case of the type being null (shouldn't really happen)
@@ -192,6 +203,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <inheritdoc/>
public sealed override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
LiteralExpressionSyntax updatedNode = (LiteralExpressionSyntax)base.VisitLiteralExpression(node)!;
if (updatedNode.IsKind(SyntaxKind.DefaultLiteralExpression))
@@ -250,6 +263,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <inheritdoc/>
public sealed override SyntaxNode? VisitElementAccessExpression(ElementAccessExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
ElementAccessExpressionSyntax updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!;
if (SemanticModel.For(node).GetOperation(node) is IPropertyReferenceOperation operation)
@@ -313,6 +328,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <inheritdoc/>
public sealed override SyntaxNode? VisitAssignmentExpression(AssignmentExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
AssignmentExpressionSyntax updatedNode = (AssignmentExpressionSyntax)base.VisitAssignmentExpression(node)!;
if (SemanticModel.For(node).GetOperation(node) is ICompoundAssignmentOperation { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method })
@@ -340,6 +357,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
/// <inheritdoc/>
public sealed override SyntaxNode? VisitBinaryExpression(BinaryExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
BinaryExpressionSyntax updatedNode = (BinaryExpressionSyntax)base.VisitBinaryExpression(node)!;
// Process binary operations to see if the target operator method is an intrinsic
+23 -9
ファイルの表示
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -77,6 +78,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
/// <param name="constructors">The collection of discovered constructors for custom struct types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="token">The <see cref="System.Threading.CancellationToken"/> value for the current operation.</param>
/// <param name="isEntryPoint">Whether or not the current instance is processing a shader entry point.</param>
public ShaderSourceRewriter(
INamedTypeSymbol shaderType,
@@ -87,8 +89,9 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
IDictionary<IMethodSymbol, (MethodDeclarationSyntax, MethodDeclarationSyntax)> constructors,
IDictionary<IFieldSymbol, string> constantDefinitions,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
CancellationToken token,
bool isEntryPoint)
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics)
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token)
{
this.shaderType = shaderType;
this.staticMethods = staticMethods;
@@ -109,6 +112,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
/// <param name="constructors">The collection of discovered constructors for custom struct types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="token">The <see cref="System.Threading.CancellationToken"/> value for the current operation.</param>
private ShaderSourceRewriter(
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -116,8 +120,9 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
IDictionary<IMethodSymbol, (MethodDeclarationSyntax, MethodDeclarationSyntax)> constructors,
IDictionary<IFieldSymbol, string> constantDefinitions,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics)
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
CancellationToken token)
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token)
{
this.staticMethods = staticMethods;
this.instanceMethods = instanceMethods;
@@ -308,6 +313,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
/// <inheritdoc/>
public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
// If the current identifier matches the one for the current method, it means the local function
// statement is the one being inspected, and it's nto coming from a local function in a method
// being parsed. That is, this was a blobal method that was annotated in source.
@@ -369,6 +376,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
/// <inheritdoc/>
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
MemberAccessExpressionSyntax updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!;
if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) &&
@@ -477,6 +486,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
/// <inheritdoc/>
public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
InvocationExpressionSyntax updatedNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!;
if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation { TargetMethod: IMethodSymbol method } operation)
@@ -520,7 +531,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
if (!this.staticMethods.ContainsKey(method))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
if (!method.TryGetSyntaxNode(CancellationToken, out MethodDeclarationSyntax? methodNode))
{
Diagnostics.Add(InvalidMethodOrConstructorCall, node, method);
@@ -534,7 +545,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
this.instanceMethods,
this.constructors,
ConstantDefinitions,
Diagnostics);
Diagnostics,
CancellationToken);
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia();
@@ -574,7 +586,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
// Same as for static methods, ensure the method is available in source, and process it if so
if (!this.instanceMethods.ContainsKey(method))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
if (!method.TryGetSyntaxNode(CancellationToken, out MethodDeclarationSyntax? methodNode))
{
Diagnostics.Add(InvalidMethodOrConstructorCall, node, method);
@@ -588,7 +600,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
this.instanceMethods,
this.constructors,
ConstantDefinitions,
Diagnostics);
Diagnostics,
CancellationToken);
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia();
@@ -660,7 +673,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
// (even if IsImplicitlyDeclared is false) and a primary constructor. This is deliberately not supported, as there
// are very subtle ways in which captures can interact with fields and methods, and we cannot guarantee to track and
// rewrite all of them correctly to exactly preserve the same semantics. So we just block such cases entirely as well.
if (constructor.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not ConstructorDeclarationSyntax constructorNode)
if (!constructor.TryGetSyntaxNode(CancellationToken, out ConstructorDeclarationSyntax? constructorNode))
{
Diagnostics.Add(InvalidMethodOrConstructorCall, node, constructor);
@@ -682,7 +695,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
this.instanceMethods,
this.constructors,
ConstantDefinitions,
Diagnostics);
Diagnostics,
CancellationToken);
ConstructorDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(constructorNode)!.WithoutTrivia();
+7 -1
ファイルの表示
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -23,11 +24,14 @@ namespace ComputeSharp.SourceGeneration.SyntaxRewriters;
/// <param name="discoveredTypes">The set of discovered custom types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="token">The <see cref="System.Threading.CancellationToken"/> value for the current operation.</param>
internal sealed partial class StaticFieldRewriter(
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics) : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, diagnostics)
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
CancellationToken token)
: HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token)
{
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public ExpressionSyntax? Visit(VariableDeclaratorSyntax? node)
@@ -43,6 +47,8 @@ internal sealed partial class StaticFieldRewriter(
/// <inheritdoc/>
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
CancellationToken.ThrowIfCancellationRequested();
MemberAccessExpressionSyntax updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!;
if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) &&
+10 -2
ファイルの表示
@@ -298,13 +298,20 @@ partial class ComputeShaderDescriptorGenerator
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
token.ThrowIfCancellationRequested();
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
diagnostics,
token);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator);
token.ThrowIfCancellationRequested();
string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString();
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
}
@@ -427,6 +434,7 @@ partial class ComputeShaderDescriptorGenerator
constructors,
constantDefinitions,
diagnostics,
token,
isShaderEntryPoint);
// Rewrite the method syntax tree
@@ -239,7 +239,7 @@ public sealed class InvalidComputeContextCopyAnalyzer : DiagnosticAnalyzer
return;
}
context.ReportDiagnostic(Diagnostic.Create(InvalidComputeContextCopy, symbol.DeclaringSyntaxReferences[0].GetSyntax().GetLocation()));
context.ReportDiagnostic(Diagnostic.Create(InvalidComputeContextCopy, symbol.DeclaringSyntaxReferences[0].GetSyntax(context.CancellationToken).GetLocation()));
}
}, SymbolKind.Field);
});