Added diagnostics for non constant matrix swizzling indices

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-02 16:14:12 +01:00
1 親 e200378d1c
コミット e930c969cb
6ファイル変更
+123 -10

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

+2 -1
ファイルの表示
@@ -45,4 +45,5 @@ CMPS0037 | ComputeSharp.SourceGenerators.AutoConstructorGenerator | Warning | Au
CMPS0038 | ComputeSharp.SourceGenerators.IComputeShaderDataLoaderGenerator | Warning | IComputeShaderDataLoaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0039 | ComputeSharp.SourceGenerators.IComputeShaderHashCodeGenerator | Warning | IComputeShaderHashCodeGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0040 | ComputeSharp.SourceGenerators.IComputeShaderSourceGenerator | Warning | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+6
ファイルの表示
@@ -35,30 +35,36 @@
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool2.g.cs" Link="Core\Primitives\Bool\Bool2.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool3.g.cs" Link="Core\Primitives\Bool\Bool3.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool4.g.cs" Link="Core\Primitives\Bool\Bool4.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\BoolMxN.g.cs" Link="Core\Primitives\Bool\BoolMxN.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double2.cs" Link="Core\Primitives\Double\Double2.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double3.cs" Link="Core\Primitives\Double\Double3.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double4.cs" Link="Core\Primitives\Double\Double4.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double2.g.cs" Link="Core\Primitives\Double\Double2.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double3.g.cs" Link="Core\Primitives\Double\Double3.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double4.g.cs" Link="Core\Primitives\Double\Double4.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Double\DoubleMxN.g.cs" Link="Core\Primitives\Double\DoubleMxN.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float2.cs" Link="Core\Primitives\Float\Float2.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float3.cs" Link="Core\Primitives\Float\Float3.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float4.cs" Link="Core\Primitives\Float\Float4.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float2.g.cs" Link="Core\Primitives\Float\Float2.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float3.g.cs" Link="Core\Primitives\Float\Float3.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float4.g.cs" Link="Core\Primitives\Float\Float4.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Float\FloatMxN.g.cs" Link="Core\Primitives\Float\FloatMxN.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int2.cs" Link="Core\Primitives\Int\Int2.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int3.cs" Link="Core\Primitives\Int\Int3.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int4.cs" Link="Core\Primitives\Int\Int4.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int2.g.cs" Link="Core\Primitives\Int\Int2.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int3.g.cs" Link="Core\Primitives\Int\Int3.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int4.g.cs" Link="Core\Primitives\Int\Int4.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\Int\IntMxN.g.cs" Link="Core\Primitives\Int\IntMxN.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt2.cs" Link="Core\Primitives\UInt\UInt2.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt3.cs" Link="Core\Primitives\UInt\UInt3.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt4.cs" Link="Core\Primitives\UInt\UInt4.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt2.g.cs" Link="Core\Primitives\UInt\UInt2.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt3.g.cs" Link="Core\Primitives\UInt\UInt3.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt4.g.cs" Link="Core\Primitives\UInt\UInt4.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UIntMxN.g.cs" Link="Core\Primitives\UInt\UIntMxN.g.cs" />
<Compile Include="..\ComputeSharp\Core\Primitives\MatrixIndex.cs" Link="Core\Primitives\MatrixIndex.cs" />
</ItemGroup>
<ItemGroup>
+13
ファイルの表示
@@ -587,5 +587,18 @@ namespace ComputeSharp.SourceGenerators.Diagnostics
isEnabledByDefault: true,
description: $"The {nameof(ShaderMethodSourceGenerator)} generator encountered an error while processing a method, and using it in a shader might result in a runtime crash. Please report this issue at https://github.com/Sergio0694/ComputeSharp/issues.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an incorrect matrix swizzling property argument.
/// </summary>
public static readonly DiagnosticDescriptor NonConstantMatrixSwizzledIndex = new(
id: "CMPS0042",
title: "Non constant matrix swizzled property argument",
messageFormat: "The arguments in a swizzled indexer for a matrix type must be compile-time constants",
category: typeof(ShaderMethodSourceGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The arguments in a swizzled indexer for a matrix type must be compile-time constants.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
}
+11
ファイルの表示
@@ -94,6 +94,17 @@ namespace ComputeSharp.SourceGenerators.Extensions
return $"{declaringTypeName}.{symbol.Name}";
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IFieldSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IFieldSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IFieldSymbol symbol)
{
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
}
/// <summary>
/// Gets a valid filename for a target symbol and generator type.
/// </summary>
+67 -5
ファイルの表示
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text.RegularExpressions;
namespace ComputeSharp.SourceGenerators.Mappings
{
@@ -13,9 +15,9 @@ namespace ComputeSharp.SourceGenerators.Mappings
internal static class HlslKnownMembers
{
/// <summary>
/// The mapping of supported known indexers to HLSL vector type names.
/// The mapping of supported known indexers to HLSL resource type names.
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownIndexers = new Dictionary<string, string>
private static readonly IReadOnlyDictionary<string, string> KnownResourceIndexers = new Dictionary<string, string>
{
[$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
@@ -236,6 +238,66 @@ namespace ComputeSharp.SourceGenerators.Mappings
return knownMembers;
}
/// <summary>
/// The mapping of supported known members to HLSL names.
/// </summary>
private static readonly IReadOnlyCollection<string> KnownMatrixIndexers = BuildKnownMatrixIndexers();
/// <summary>
/// Builds the mapping of swizzled matrix indexer properties.
/// </summary>
[Pure]
private static IReadOnlyCollection<string> BuildKnownMatrixIndexers()
{
return (
from type in Assembly.GetExecutingAssembly().ExportedTypes
where Regex.IsMatch(type.FullName, @"ComputeSharp\.(Bool|Double|Float|Int|UInt)[1-4]x[1-4]")
from property in type.GetProperties()
let indices = property.GetIndexParameters()
where indices.Length > 0 &&
indices.All(static index => index.ParameterType == typeof(MatrixIndex))
let metadataName = $"{type.FullName}.this[{string.Join(", ", indices.Select(index => index.ParameterType))}]"
select metadataName).ToImmutableHashSet();
}
/// <summary>
/// The mapping of supported known members to HLSL names.
/// </summary>
private static readonly IReadOnlyCollection<string> KnownMatrixIndices = BuildKnownMatrixIndices();
/// <summary>
/// Builds the mapping of swizzled matrix indices.
/// </summary>
[Pure]
private static IReadOnlyCollection<string> BuildKnownMatrixIndices()
{
return (
from name in Enum.GetNames(typeof(MatrixIndex))
select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet();
}
/// <summary>
/// Checks whether or not a given property fullname matches a matrix swizzled indexer.
/// </summary>
/// <param name="name">The input fully qualified member name.</param>
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled indexer.</returns>
[Pure]
public static bool IsKnownMatrixIndexer(string name)
{
return KnownMatrixIndexers.Contains(name);
}
/// <summary>
/// Checks whether or not a given property fullname matches a matrix swizzled index.
/// </summary>
/// <param name="name">The input fully qualified member name.</param>
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled index.</returns>
[Pure]
public static bool IsKnownMatrixIndex(string name)
{
return KnownMatrixIndices.Contains(name);
}
/// <summary>
/// Tries to get the mapped HLSL-compatible member name for the input member name.
/// </summary>
@@ -249,15 +311,15 @@ namespace ComputeSharp.SourceGenerators.Mappings
}
/// <summary>
/// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name.
/// Tries to get the mapped HLSL-compatible indexer resource type name for the input indexer name.
/// </summary>
/// <param name="name">The input fully qualified indexer name.</param>
/// <param name="mapped">The mapped type name, if one is found.</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
[Pure]
public static bool TryGetMappedIndexerTypeName(string name, out string? mapped)
public static bool TryGetMappedResourceIndexerTypeName(string name, out string? mapped)
{
return KnownIndexers.TryGetValue(name, out mapped);
return KnownResourceIndexers.TryGetValue(name, out mapped);
}
/// <summary>
+24 -4
ファイルの表示
@@ -557,12 +557,32 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!;
if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation &&
HlslKnownMembers.TryGetMappedIndexerTypeName(operation.Property.GetFullMetadataName(), out string? mapping))
if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation)
{
var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments));
string propertyName = operation.Property.GetFullMetadataName();
return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index))));
// Rewrite texture resource indices taking vectors into individual indices as per HLSL spec.
// For instance: texture[ThreadIds.XY] will be rewritten as texture[ThreadIds.X, ThreadIds.Y].
if (HlslKnownMembers.TryGetMappedResourceIndexerTypeName(propertyName, out string? mapping))
{
var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments));
return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index))));
}
// If the current property is a swizzled matrix indexer, ensure all the arguments are constants, and rewrite
// the property access to the corresponding HLSL syntax. For instance, m[M11, M12] will become m._m00_m01.
if (HlslKnownMembers.IsKnownMatrixIndexer(propertyName))
{
foreach (ArgumentSyntax argument in node.ArgumentList.Arguments)
{
if (this.semanticModel.GetOperation(argument.Expression) is not IFieldReferenceOperation fieldReference ||
!HlslKnownMembers.IsKnownMatrixIndex(fieldReference.Field.GetFullMetadataName()))
{
this.context.ReportDiagnostic(NonConstantMatrixSwizzledIndex, argument);
}
}
}
}
return updatedNode;