Enable diagnostics from bytecode transformation
このコミットが含まれているのは:
1 親
2209c9eb41
コミット
2e160c5caa
4ファイル変更
+100
-27
検索条件に一致するファイルはありません
@@ -94,20 +94,21 @@ public sealed partial class IShaderGenerator
|
||||
/// </summary>
|
||||
/// <param name="threadIds">The input <see cref="ThreadIdsInfo"/> instance to process.</param>
|
||||
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
|
||||
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
|
||||
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
|
||||
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
|
||||
[Pure]
|
||||
public static unsafe ImmutableArray<byte> GetBytecode(
|
||||
ThreadIdsInfo threadIds,
|
||||
string hlslSource,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
out DiagnosticInfo? diagnostic)
|
||||
{
|
||||
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// No embedded shader was requested
|
||||
if (threadIds.IsDefault)
|
||||
{
|
||||
diagnostic = null;
|
||||
|
||||
goto End;
|
||||
}
|
||||
|
||||
@@ -131,30 +132,18 @@ public sealed partial class IShaderGenerator
|
||||
byte[] array = new ReadOnlySpan<byte>(buffer, length).ToArray();
|
||||
|
||||
bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
|
||||
diagnostic = null;
|
||||
}
|
||||
catch (Win32Exception)
|
||||
catch (Win32Exception e)
|
||||
{
|
||||
// TODO
|
||||
//builder.Add(
|
||||
// EmbeddedBytecodeFailedWithWin32Exception,
|
||||
// structDeclarationSymbol,
|
||||
// structDeclarationSymbol,
|
||||
// e.HResult,
|
||||
// e.Message);
|
||||
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message);
|
||||
}
|
||||
catch (HlslCompilationException)
|
||||
catch (HlslCompilationException e)
|
||||
{
|
||||
// TODO
|
||||
//builder.Add(
|
||||
// EmbeddedBytecodeFailedWithHlslCompilationException,
|
||||
// structDeclarationSymbol,
|
||||
// structDeclarationSymbol,
|
||||
// e.Message);
|
||||
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithHlslCompilationException, e.Message);
|
||||
}
|
||||
|
||||
End:
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
return bytecode;
|
||||
}
|
||||
|
||||
|
||||
@@ -193,11 +193,11 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<string>.Default, ThreadIdsInfo.Comparer.Default);
|
||||
|
||||
// Compile the requested shader bytecodes
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<EmbeddedBytecodeInfo> BytecodeInfo)> embeddedBytecodeWithErrors =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
|
||||
shaderBytecodeInfo
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
ImmutableArray<byte> bytecode = TryGetBytecode.GetBytecode(item.ThreadIds, item.Hlsl, out ImmutableArray<Diagnostic> diagnostics);
|
||||
ImmutableArray<byte> bytecode = TryGetBytecode.GetBytecode(item.ThreadIds, item.Hlsl, out DiagnosticInfo? diagnostic);
|
||||
|
||||
EmbeddedBytecodeInfo bytecodeInfo = new(
|
||||
item.ThreadIds.X,
|
||||
@@ -205,16 +205,33 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
item.ThreadIds.Z,
|
||||
bytecode);
|
||||
|
||||
return (item.Hierarchy, new Result<EmbeddedBytecodeInfo>(bytecodeInfo, diagnostics));
|
||||
return (item.Hierarchy, bytecodeInfo, diagnostic);
|
||||
});
|
||||
|
||||
// Gather the diagnostics
|
||||
IncrementalValuesProvider<ImmutableArray<Diagnostic>> embeddedBytecodeDiagnostics =
|
||||
embeddedBytecodeWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy.MetadataName, item.Diagnostic))
|
||||
.Where(static item => item.Diagnostic is not null)
|
||||
.Combine(context.CompilationProvider)
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
INamedTypeSymbol? typeSymbol = item.Right.GetTypeByMetadataName(item.Left.MetadataName)!;
|
||||
Diagnostic diagnostic = Diagnostic.Create(
|
||||
item.Left.Diagnostic!.Descriptor,
|
||||
typeSymbol.Locations.FirstOrDefault(),
|
||||
new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray());
|
||||
|
||||
return ImmutableArray.Create(diagnostic);
|
||||
});
|
||||
|
||||
// Output the diagnostics
|
||||
context.ReportDiagnostics(embeddedBytecodeWithErrors.Select(static (item, token) => item.BytecodeInfo.Errors));
|
||||
context.ReportDiagnostics(embeddedBytecodeDiagnostics);
|
||||
|
||||
// Get the filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
|
||||
embeddedBytecodeWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo.Value))
|
||||
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
|
||||
|
||||
// Generate the TryGetBytecode() methods
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model for a serializeable diagnostic provider.
|
||||
/// </summary>
|
||||
/// <param name="Descriptor">The wrapped <see cref="DiagnosticDescriptor"/> instance.</param>
|
||||
/// <param name="Args">The diagnostic args.</param>
|
||||
internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params object[] Args)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DiagnosticInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : IEqualityComparer<DiagnosticInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <see cref="Comparer"/> instance.
|
||||
/// </summary>
|
||||
public static Comparer Default { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(DiagnosticInfo? x, DiagnosticInfo? y)
|
||||
{
|
||||
if (x is null && y is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return
|
||||
x.Descriptor.Equals(y.Descriptor) &&
|
||||
x.Args.SequenceEqual(y.Args);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(DiagnosticInfo obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.Add(obj.Descriptor);
|
||||
|
||||
foreach (object arg in obj.Args)
|
||||
{
|
||||
hashCode.Add(arg);
|
||||
}
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,10 @@ namespace ComputeSharp.SourceGenerators.Models;
|
||||
/// A model describing the hierarchy info for a specific type.
|
||||
/// </summary>
|
||||
/// <param name="FilenameHint">The filename hint for the current type.</param>
|
||||
/// <param name="MetadataName">The metadata name for the current type.</param>
|
||||
/// <param name="Namespace">Gets the namespace for the current type.</param>
|
||||
/// <param name="Names">Gets the sequence of type definitions containing the current type.</param>
|
||||
internal sealed record HierarchyInfo(string FilenameHint, string Namespace, ImmutableArray<string> Names)
|
||||
internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray<string> Names)
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HierarchyInfo"/> instance from a given <see cref="INamedTypeSymbol"/>.
|
||||
@@ -35,6 +36,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
|
||||
|
||||
return new(
|
||||
typeSymbol.GetGeneratedFileName(),
|
||||
typeSymbol.MetadataName,
|
||||
typeSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)),
|
||||
names.ToImmutable());
|
||||
}
|
||||
@@ -69,6 +71,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
|
||||
|
||||
return
|
||||
x.FilenameHint == y.FilenameHint &&
|
||||
x.MetadataName == y.MetadataName &&
|
||||
x.Namespace == y.Namespace &&
|
||||
x.Names.SequenceEqual(y.Names);
|
||||
}
|
||||
@@ -76,7 +79,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(HierarchyInfo obj)
|
||||
{
|
||||
return HashCode.Combine(obj.Namespace, obj.Names, obj.Names[0]);
|
||||
return HashCode.Combine(obj.Namespace, obj.MetadataName, obj.Names, obj.Names[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする