Merge pull request #717 from Sergio0694/dev/fix-rewriting-recursion

Handle recursive members in HLSL rewriting
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-14 16:43:18 +01:00
コミット d6ed7dd696
2ファイル変更
+80 -5

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

+14 -5
ファイルの表示
@@ -547,6 +547,11 @@ internal sealed partial class ShaderSourceRewriter(
return updatedNode;
}
// Add a marker to avoid endless recursion in case we have any loops in the graph.
// This will be updated right after rewriting this method completes. This temporary
// value is fine, as the actual rewritten method is not used while rewriting HLSL.
this.staticMethods.Add(method, null!);
ShaderSourceRewriter shaderSourceRewriter = new(
this.shaderType,
SemanticModel,
@@ -561,7 +566,7 @@ internal sealed partial class ShaderSourceRewriter(
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
this.staticMethods[method] = processedMethod.WithIdentifier(Identifier(methodIdentifier));
}
return updatedNode.WithExpression(IdentifierName(methodIdentifier));
@@ -602,11 +607,13 @@ internal sealed partial class ShaderSourceRewriter(
return updatedNode;
}
this.instanceMethods[method] = null!;
ShaderSourceRewriter shaderSourceRewriter = new(
this.shaderType,
SemanticModel,
DiscoveredTypes,
this.instanceMethods,
this.staticMethods,
this.instanceMethods,
this.constructors,
ConstantDefinitions,
@@ -616,7 +623,7 @@ internal sealed partial class ShaderSourceRewriter(
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia();
this.instanceMethods.Add(method, processedMethod);
this.instanceMethods[method] = processedMethod;
}
return updatedNode;
@@ -691,11 +698,13 @@ internal sealed partial class ShaderSourceRewriter(
Diagnostics.Add(InvalidBaseConstructorDeclaration, node, constructor);
}
this.constructors[constructor] = default!;
ShaderSourceRewriter shaderSourceRewriter = new(
this.shaderType,
SemanticModel,
DiscoveredTypes,
this.instanceMethods,
this.staticMethods,
this.instanceMethods,
this.constructors,
ConstantDefinitions,
@@ -757,7 +766,7 @@ internal sealed partial class ShaderSourceRewriter(
.WithParameterList(processedMethod.ParameterList)
.WithBody(processedMethod.Body);
this.constructors.Add(constructor, (stubNode, ctorNode));
this.constructors[constructor] = (stubNode, ctorNode);
}
// Rewrite the expression to invoke the rewritten constructor:
+66
ファイルの表示
@@ -1573,6 +1573,72 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(source, "CMPS0047", "CMPS0062");
}
[TestMethod]
public void RecursiveMembers()
{
const string source = """
using ComputeSharp;
internal static class ClassWithRecursiveMembers
{
public static int A(int x)
{
return B(x);
}
public static int B(int x)
{
if (x <= 0)
{
return 0;
}
return A(x - 1);
}
}
public struct StructTypeWithRecursiveMembers
{
public int x;
public int Fib()
{
if (x <= 0)
{
return x;
}
x--;
return Fib();
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly struct ShaderWithRecursiveMembers : IComputeShader
{
public readonly ReadWriteBuffer<int> results;
public void Execute()
{
results[0] = ClassWithRecursiveMembers.A(42);
StructTypeWithRecursiveMembers instance;
instance.x = 42;
results[1] = instance.Fib();
}
}
""";
// This should be correctly handled by the HLSL rewriter, and not cause a StackOverflowException.
// The HLSL compilation is expected to fail because recursion is not actually supported:
//
// "error: recursive functions not allowed"
VerifyGeneratedDiagnostics<ComputeShaderDescriptorGenerator>(source, "CMPS0046");
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>