diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs index c8f03de0..c760404a 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; +using System.Reflection; +using ComputeSharp.Core.Intrinsics.Attributes; namespace ComputeSharp.SourceGenerators.Mappings { @@ -10,19 +12,51 @@ namespace ComputeSharp.SourceGenerators.Mappings internal static class HlslKnownKeywords { /// - /// The mapping of known reserved HLSL keywords. + /// The mapping of known HLSL keywords. /// - private static readonly IReadOnlyCollection KnownKeywords = new HashSet(new[] + private static readonly IReadOnlyCollection KnownKeywords = BuildKnownKeywordsMap(); + + /// + /// Builds the mapping of all known HLSL keywords. + /// + [Pure] + private static IReadOnlyCollection BuildKnownKeywordsMap() { - "asm", "asm_fragment", "cbuffer", "centroid", "column_major", - "compile", "discard", "dword", "export", "fxgroup", "groupshared", - "half", "inline", "inout", "line", "lineadj", "linear", "matrix", - "nointerpolation", "noperspective", "NULL", "packoffset", "pass", - "pixelfragment", "point", "precise", "register", "row_major", "sample", - "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer", - "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm", - "unsigned", "vector", "vertexfragment", "zero" - }); + // HLSL keywords + HashSet knownKeywords = new(new[] + { + "asm", "asm_fragment", "cbuffer", "centroid", "column_major", + "compile", "discard", "dword", "export", "fxgroup", "groupshared", + "half", "inline", "inout", "line", "lineadj", "linear", "matrix", + "nointerpolation", "noperspective", "NULL", "packoffset", "pass", + "pixelfragment", "point", "precise", "register", "row_major", "sample", + "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer", + "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm", + "unsigned", "vector", "vertexfragment", "zero" + }); + + // Dispatch type names + knownKeywords.Add(nameof(ThreadIds)); + knownKeywords.Add(nameof(GroupIds)); + knownKeywords.Add(nameof(GroupSize)); + knownKeywords.Add(nameof(WarpIds)); + + // HLSL intrinsics method names + foreach (var method in + from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static) + group method by method.Name + into groups + select (Name: groups.Key, MethodInfo: groups.First())) + { + string hlslName = method.MethodInfo.GetCustomAttribute() != null + ? method.Name + : method.Name.ToLowerInvariant(); + + knownKeywords.Add(hlslName); + } + + return knownKeywords; + } /// /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs new file mode 100644 index 00000000..3cf860e9 --- /dev/null +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -0,0 +1,31 @@ +using ComputeSharp.Interop; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("ShaderCompiler")] + public partial class ShaderCompilerTests + { + [TestMethod] + public void ReservedKeywords() + { + ReflectionServices.GetShaderInfo(out _); + } + + [AutoConstructor] + public readonly partial struct SoftmaxActivation : IComputeShader + { + public readonly ReadWriteBuffer row_major; + public readonly float dword; + + public void Execute() + { + float exp = Hlsl.Exp(dword * row_major[ThreadIds.X]); + float log = Hlsl.Log(1 + exp); + + row_major[ThreadIds.X] = log / dword; + } + } + } +}