Sergio0694/ComputeSharp

NullReferenceException in HlslSourceRewriter.VisitObjectCreationExpression

matthew-a-thomas opened this issue · 1 comments

Description

Generator 'ComputeShaderDescriptorGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.'
System.NullReferenceException: Object reference not set to an instance of an object.
    at ComputeSharp.SourceGeneration.SyntaxRewriters.HlslSourceRewriter.VisitObjectCreationExpression(BaseObjectCreationExpressionSyntax node, BaseObjectCreationExpressionSyntax updatedNode, TypeSyntax targetType) in /_/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs:line 128
    at ComputeSharp.SourceGeneration.SyntaxRewriters.HlslSourceRewriter.VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) in /_/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs:line 102
    at Microsoft.CodeAnalysis.CSharp.Syntax.ObjectCreationExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs:line 3635
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 41
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitAssignmentExpression(AssignmentExpressionSyntax node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Main.Generated.cs:line 1567
    at ComputeSharp.SourceGeneration.SyntaxRewriters.HlslSourceRewriter.VisitAssignmentExpression(AssignmentExpressionSyntax node) in /_/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs:line 281
    at Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs:line 1807
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 41
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitExpressionStatement(ExpressionStatementSyntax node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Main.Generated.cs:line 1819
    at Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs:line 6843
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 41
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitListElement[TNode](TNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 168
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitList[TNode](SyntaxList`1 list) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 145
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitBlock(BlockSyntax node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Main.Generated.cs:line 1792
    at Microsoft.CodeAnalysis.CSharp.Syntax.BlockSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs:line 6243
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 41
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitMethodDeclaration(MethodDeclarationSyntax node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Main.Generated.cs:line 2011
    at ComputeSharp.SourceGeneration.SyntaxRewriters.ShaderSourceRewriter.VisitMethodDeclaration(MethodDeclarationSyntax node) in /_/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs:line 245
    at Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs:line 12069
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 41
    at ComputeSharp.SourceGeneration.SyntaxRewriters.ShaderSourceRewriter.Visit(MethodDeclarationSyntax node) in /_/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs:line 134
    at ComputeSharp.SourceGenerators.ComputeShaderDescriptorGenerator.HlslSource.GetProcessedMethods(ImmutableArrayBuilder`1 diagnostics, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, ICollection`1 discoveredTypes, IDictionary`2 staticMethods, IDictionary`2 instanceMethods, IDictionary`2 constantDefinitions, Boolean isComputeShader) in /_/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs:line 379
    at ComputeSharp.SourceGenerators.ComputeShaderDescriptorGenerator.HlslSource.GetInfo(ImmutableArrayBuilder`1 diagnostics, Compilation compilation, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, Int32 threadsX, Int32 threadsY, Int32 threadsZ, Boolean& isImplicitTextureUsed, Boolean& isSamplerUsed, String& hlslSource) in /_/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs:line 74
    at ComputeSharp.SourceGenerators.ComputeShaderDescriptorGenerator.c. b__2_1(GeneratorAttributeSyntaxContext context, CancellationToken token) in /_/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs:line 37
    at Microsoft.CodeAnalysis.SyntaxValueProvider.c__DisplayClass11_0`1. b__0(ValueTuple`2 tuple, CancellationToken cancellationToken) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\Core\Portable\SourceGeneration\Nodes\SyntaxValueProvider_ForAttributeWithMetadataName.cs:line 120
    at Microsoft.CodeAnalysis.UserFunctionExtensions.c__DisplayClass0_0`2. b__0(TInput input, CancellationToken token) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\Core\Portable\SourceGeneration\UserFunction.cs:line 66

Reproduction Steps

public struct StructType
{
    public float Field;
}

[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
public readonly partial struct Simulation(ReadWriteBuffer<StructType> buffer) : IComputeShader
{
    public void Execute()
    {
        buffer[0] = new StructType { Field = 0 };
    }
}

ParticleSim.zip

(Sorry it's not super minimal, but hopefully it's reproduceable for you)

Expected Behavior

No exception.

Actual Behavior

Observed above exception.

System info

ComputeSharp 3.0.0-preview1

DxDiag.txt

Thank you for reporting this! Got a minimal repro, added it to your original message. Struct constructors are not supported yet (see #481), and neither are object initializers like in this case. That said, at the very least it should not fail to build.


Exception is here:

if (updatedNode.ArgumentList!.Arguments.Count == 0)

Because ArgumentList is null. Why did I even suppress this in the first place...

image