Improve HLSL build error diagnostics for DX12 package

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:33 +02:00
1 親 14fa96998f
コミット e1d823a833
5ファイル変更
+94 -40

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

+1 -36
ファイルの表示
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;
@@ -134,41 +133,7 @@ partial class ID2D1ShaderGenerator
// Add square brackets around error headers
message = Regex.Replace(message, @"((?:error|warning) \w+):", static m => $"[{m.Groups[1].Value}]:");
char[] buffer = ArrayPool<char>.Shared.Rent(message.Length);
int index = 0;
char lastCharacter = '\0';
foreach (char newCharacter in message)
{
char pendingCharacter = newCharacter;
// Ignore '\r' characters
if (pendingCharacter is '\r')
{
continue;
}
// Convert newlines to spaces (this avoids VS droppping multiline diagnostics)
if (pendingCharacter is '\n')
{
pendingCharacter = ' ';
}
// Drop duplicate spaces
if (pendingCharacter is ' ' &&
lastCharacter is ' ')
{
continue;
}
buffer[index++] = lastCharacter = pendingCharacter;
}
string text = buffer.AsSpan(0, index).ToString();
ArrayPool<char>.Shared.Return(buffer);
return text;
return message.NormalizeToSingleLine();
}
}
}
+1 -1
ファイルの表示
@@ -226,7 +226,7 @@ internal static unsafe partial class D2D1ShaderCompiler
// The error message will be in a format like this:
// "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Shader@0x0000019AD1B4BA70(22,32-35): error X3004: undeclared identifier 'this'"
// This regex tries to match the unnecessary header and remove it, if present. This doesn't need to be bulletproof, and this regex should match all cases anyway.
message = Regex.Replace(message, @"[A-Z]:\\[^:]+: (\w+ \w+:)", static m => m.Groups[1].Value).Trim();
message = Regex.Replace(message, @"^[A-Z]:\\[^:]+: (\w+ \w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline).Trim();
// Add a trailing '.' if not present
if (message is { Length: > 0 } &&
+17
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Exceptions;
using TerraFX.Interop.DirectX;
@@ -151,6 +152,22 @@ internal sealed unsafe partial class ShaderCompiler
string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer());
// The error message will be in a format like this:
// "hlsl.hlsl:11:20: error: redefinition of 'float1' as different kind of symbol
// static const float float1 = asfloat(0xFFC00000);
// ^
// note: previous definition is here"
// These regex-s try to match the unnecessary headers and remove them, if present.
// This doesn't need to be bulletproof, and these regex-s should match all cases anyway.
message = Regex.Replace(message, @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline);
// Add a trailing '.' if not present
if (message is { Length: > 0 } &&
message[message.Length - 1] != '.')
{
message += '.';
}
throw new DxcCompilationException(message);
}
}
+53 -1
ファイルの表示
@@ -1,4 +1,7 @@
namespace ComputeSharp.SourceGeneration.Extensions;
using System;
using System.Buffers;
namespace ComputeSharp.SourceGeneration.Extensions;
/// <summary>
/// Extension methods for <see cref="string"/> types.
@@ -14,4 +17,53 @@ internal static class StringExtensions
{
return text.Replace('.', '_').Replace('+', '_');
}
/// <summary>
/// Normalizes a given <see cref="string"/> instance to make it single line and with no repeated spaces.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to normalize.</param>
/// <returns>The normalized version of <paramref name="text"/>.</returns>
public static string NormalizeToSingleLine(this string text)
{
if (text is "")
{
return text;
}
char[] buffer = ArrayPool<char>.Shared.Rent(text.Length);
int index = 0;
char lastCharacter = '\0';
foreach (char newCharacter in text)
{
char pendingCharacter = newCharacter;
// Ignore '\r' characters
if (pendingCharacter is '\r')
{
continue;
}
// Convert newlines to spaces (this avoids VS droppping multiline diagnostics)
if (pendingCharacter is '\n')
{
pendingCharacter = ' ';
}
// Drop duplicate spaces
if (pendingCharacter is ' ' &&
lastCharacter is ' ')
{
continue;
}
buffer[index++] = lastCharacter = pendingCharacter;
}
text = buffer.AsSpan(0, index).ToString();
ArrayPool<char>.Shared.Return(buffer);
return text;
}
}
+22 -2
ファイルの表示
@@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using ComputeSharp.Exceptions;
using ComputeSharp.Shaders.Translation;
@@ -187,17 +188,36 @@ partial class IShaderGenerator
}
catch (Win32Exception e)
{
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message);
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message));
}
catch (DxcCompilationException e)
{
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message);
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message));
}
End:
return bytecode;
}
/// <summary>
/// Fixes up an exception message to improve the way it's displayed in VS.
/// </summary>
/// <param name="message">The input exception message.</param>
/// <returns>The updated exception message.</returns>
private static string FixupExceptionMessage(string message)
{
// Add square brackets around error headers
message = Regex.Replace(message, @"^(error|warning):", static m => $"[{m.Groups[1].Value}]:", RegexOptions.Multiline);
// Remove lines with notes
message = Regex.Replace(message, @"^note:.+", string.Empty, RegexOptions.Multiline);
// Remove syntax error indicators
message = Regex.Replace(message, @"^ +\^", string.Empty, RegexOptions.Multiline);
return message.NormalizeToSingleLine();
}
/// <summary>
/// Extracts and loads the <c>dxcompiler.dll</c> and <c>dxil.dll</c> libraries.
/// </summary>