Add diagnostics to TryGetBytecode generation

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-06 17:43:44 +01:00
1 親 9d0b4326bd
コミット 6a7e256979
3ファイル変更
+118 -37

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

+4
ファイルの表示
@@ -52,3 +52,7 @@ CMPS0044 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentatio
CMPS0045 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0046 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0047 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0049 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0050 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0051 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+64
ファイルの表示
@@ -630,4 +630,68 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A shader type cannot implement more than one shader interface.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a dynamic shader annotated as an embedded bytecode.
/// <para>
/// Format: <c>"The shader of type {0} cannot be precompiled, as it is dynamic (it captures delegate fields)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor EmbeddedBytecodeWithDynamicShader = new(
id: "CMPS0048",
title: "Embedded bytecode for dynamic shader",
messageFormat: $"The shader of type {{0}} cannot be precompiled, as it is dynamic (it captures delegate fields)",
category: typeof(IShaderGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A shader type cannot be precompiled if it captures one or more delegate values.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader with invalid thread ids values.
/// <para>
/// Format: <c>"The shader of type {0} is annotated with invalid thread ids values ({1}, {2}, {3})"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidEmbeddedBytecodeThreadIds = new(
id: "CMPS0049",
title: "Invalid thread ids for shader with embedded bytecode",
messageFormat: $"The shader of type {{0}} is annotated with invalid thread ids values ({{1}}, {{2}}, {{3}})",
category: typeof(IShaderGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The thread ids values for a shader marked as embedded bytecode have to be in the valid range.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader failed due to a Win32 exception.
/// <para>
/// Format: <c>"The shader of type {{0}} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: "{2}")"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithWin32Exception = new(
id: "CMPS0050",
title: "Embedded bytecode compilation failed due to Win32 exception",
messageFormat: $"The shader of type {{0}} failed to compile due to a Win32 exception (HRESULT: {{1:X8}}, Message: \"{{2}}\")",
category: typeof(IShaderGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The embedded bytecode for a shader failed to be compiled due to a Win32 exception.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader failed due to an HLSL compilation exception.
/// <para>
/// Format: <c>"The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: "{1}")"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithHlslCompilationException = new(
id: "CMPS0051",
title: "Embedded bytecode compilation failed due to an HLSL compiler error",
messageFormat: $"The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: \"{{1}}\")",
category: typeof(IShaderGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The embedded bytecode for a shader failed to be compiled due to an HLSL compiler error.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+50 -37
ファイルの表示
@@ -8,11 +8,13 @@ using System.Runtime.InteropServices;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Shaders.Translation;
using ComputeSharp.SourceGenerators.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
#pragma warning disable CS0618
@@ -52,51 +54,40 @@ public sealed partial class IShaderGenerator
Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
Parameter(Identifier("bytecode")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("global::System.ReadOnlySpan<byte>")))
.WithBody(GetShaderBytecodeBody(structDeclarationSymbol, isDynamicShader, hlslSource, out bytecodeLiterals));
.WithBody(GetShaderBytecodeBody(context, structDeclarationSymbol, isDynamicShader, hlslSource, out bytecodeLiterals));
}
/// <summary>
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="isDynamicShader">Indicates whether or not the shader is dynamic (ie. captures delegates).</param>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <param name="bytecodeLiterals">The resulting bytecode literals to insert into the final source code.</param>
/// <returns>The <see cref="BlockSyntax"/> instance to hash the input shader.</returns>
[Pure]
private static unsafe BlockSyntax GetShaderBytecodeBody(INamedTypeSymbol structDeclarationSymbol, bool isDynamicShader, string hlslSource, out string? bytecodeLiterals)
private static unsafe BlockSyntax GetShaderBytecodeBody(
GeneratorExecutionContext context,
INamedTypeSymbol structDeclarationSymbol,
bool isDynamicShader,
string hlslSource,
out string? bytecodeLiterals)
{
AttributeData? attribute = structDeclarationSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(EmbeddedBytecodeAttribute) });
// No embedded shader was requested
if (attribute is null)
{
bytecodeLiterals = null;
// This code produces a method declaration as follows:
//
// bytecode = default;
//
// return false;
return
Block(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("bytecode"),
LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))),
ReturnStatement(
LiteralExpression(
SyntaxKind.FalseLiteralExpression)));
goto Failure;
}
// If the current shader is dynamic, emit a diagnostic error
if (isDynamicShader)
{
bytecodeLiterals = null;
context.ReportDiagnostic(EmbeddedBytecodeWithDynamicShader, structDeclarationSymbol, structDeclarationSymbol);
// TODO
return Block();
goto Failure;
}
// Validate the thread number arguments
@@ -107,16 +98,16 @@ public sealed partial class IShaderGenerator
threadsY is < 1 or > 1024 ||
threadsZ is < 1 or > 64)
{
bytecodeLiterals = null;
context.ReportDiagnostic(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
// TODO
return Block();
goto Failure;
}
LoadNativeDxcLibraries();
try
{
// Try to load dxcompiler.dll and dxil.dll
LoadNativeDxcLibraries();
// Replace the actual thread num values
hlslSource = hlslSource.Replace("<THREADSX>", threadsX.ToString()).Replace("<THREADSY>", threadsY.ToString()).Replace("<THREADSZ>", threadsZ.ToString());
@@ -184,20 +175,42 @@ public sealed partial class IShaderGenerator
LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))),
ReturnStatement(LiteralExpression(SyntaxKind.FalseLiteralExpression)));
}
catch (Win32Exception)
catch (Win32Exception e)
{
bytecodeLiterals = null;
// TODO
return Block();
context.ReportDiagnostic(
EmbeddedBytecodeFailedWithWin32Exception,
structDeclarationSymbol,
structDeclarationSymbol,
e.HResult,
e.Message);
}
catch (HlslCompilationException)
catch (HlslCompilationException e)
{
bytecodeLiterals = null;
// TODO
return Block();
context.ReportDiagnostic(
EmbeddedBytecodeFailedWithHlslCompilationException,
structDeclarationSymbol,
structDeclarationSymbol,
e.Message);
}
Failure:
bytecodeLiterals = null;
// This code produces a method declaration as follows:
//
// bytecode = default;
//
// return false;
return
Block(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("bytecode"),
LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))),
ReturnStatement(
LiteralExpression(
SyntaxKind.FalseLiteralExpression)));
}
/// <summary>