Enabled constants import for captured delegates
このコミットが含まれているのは:
1 親
fdc06c2b45
コミット
4c50309bdd
5ファイル変更
+140
-142
検索条件に一致するファイルはありません
@@ -39,7 +39,7 @@ namespace ComputeSharp.SourceGenerators.Helpers
|
||||
/// <para>
|
||||
/// That it, it applies the following transformation:
|
||||
/// <code>
|
||||
/// { ("K1", "V1", I1), ("K2", "V2", null) } => new object[] { new object[] { "K1", "V1", I1 }, new object[] { "K2", "V2", null } }
|
||||
/// { ("K1", "V1"), ("K2", "V2") } => new object[] { new string[] { "K1", "V1" }, new string[] { "K2", "V2" } }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
@@ -55,74 +55,12 @@ namespace ComputeSharp.SourceGenerators.Helpers
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(groups.Select(static group =>
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.A)),
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.B))))).ToArray()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="object"/>[] array expression with the nested groups in the input sequence.
|
||||
/// <para>
|
||||
/// That it, it applies the following transformation:
|
||||
/// <code>
|
||||
/// { ("K1", "V1", I1), ("K2", "V2", null) } => new object[] { new object[] { "K1", "V1", I1 }, new object[] { "K2", "V2", null } }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="values">The input sequence of <see cref="string"/> groups.</param>
|
||||
/// <returns>An <see cref="ArrayCreationExpressionSyntax"/> instance with the described contents.</returns>
|
||||
[Pure]
|
||||
public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable<(string A, string B, int? C)> groups)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(groups.Select(static group =>
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.A)),
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.B)),
|
||||
group.C is int i
|
||||
? LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i))
|
||||
: LiteralExpression(SyntaxKind.NullLiteralExpression)))).ToArray()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="string"/>[]? array expression from the given tuple instance.
|
||||
/// <para>
|
||||
/// That it, it applies the following transformation:
|
||||
/// <code>
|
||||
/// null => null
|
||||
/// ("S1", "S2") => new string[] { "S1", "S2" }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="values">The input tuple instance.</param>
|
||||
/// <returns>An <see cref="ExpressionSyntax"/> instance with the described contents.</returns>
|
||||
[Pure]
|
||||
public static ExpressionSyntax ArrayExpression((string First, string Second)? values)
|
||||
{
|
||||
if (values is null)
|
||||
{
|
||||
return LiteralExpression(SyntaxKind.NullLiteralExpression);
|
||||
}
|
||||
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(values.Value.First)),
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(values.Value.Second))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -490,6 +490,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
List<StatementSyntax> statements = new();
|
||||
StringBuilder textBuilder = new();
|
||||
int capturedDelegates = 0;
|
||||
int prologueStatements = 0;
|
||||
int sizeHint = 64;
|
||||
|
||||
void AppendLF()
|
||||
@@ -514,7 +515,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
textBuilder.Append('\n');
|
||||
}
|
||||
|
||||
void AppendStatement(string text)
|
||||
void AppendParsedStatement(string text)
|
||||
{
|
||||
FlushText();
|
||||
|
||||
@@ -538,28 +539,26 @@ namespace ComputeSharp.SourceGenerators
|
||||
}
|
||||
|
||||
// Go through all existing delegate fields, if any
|
||||
foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields)
|
||||
foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields.Where(static t => t.Symbol.Type.TypeKind == TypeKind.Delegate))
|
||||
{
|
||||
if (fieldSymbol.Type.TypeKind == TypeKind.Delegate)
|
||||
{
|
||||
// ShaderMethodSourceAttribute __<DELEGATE_NAME>Attribute = ShaderMethodSourceAttribute.GetForDelegate(<DELEGATE_NAME>, "<DELEGATE_NAME>");
|
||||
statements.Add(
|
||||
LocalDeclarationStatement(VariableDeclaration(IdentifierName(nameof(ShaderMethodSourceAttribute)))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier($"__{fieldName}Attribute"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute)),
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate))))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName(fieldName)),
|
||||
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName)))))))));
|
||||
// ShaderMethodSourceAttribute __<DELEGATE_NAME>Attribute = ShaderMethodSourceAttribute.GetForDelegate(<DELEGATE_NAME>, "<DELEGATE_NAME>");
|
||||
statements.Add(
|
||||
LocalDeclarationStatement(VariableDeclaration(IdentifierName(nameof(ShaderMethodSourceAttribute)))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier($"__{fieldName}Attribute"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute)),
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate))))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName(fieldName)),
|
||||
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName)))))))));
|
||||
|
||||
capturedDelegates++;
|
||||
}
|
||||
capturedDelegates++;
|
||||
prologueStatements++;
|
||||
}
|
||||
|
||||
// Declare the hashsets to track imported members and types from delegates, if needed
|
||||
@@ -576,6 +575,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
VariableDeclarator(Identifier(name))
|
||||
.WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))));
|
||||
|
||||
prologueStatements++;
|
||||
|
||||
// <NAME>.Add("<ITEM>");
|
||||
foreach (var item in items)
|
||||
{
|
||||
@@ -588,6 +589,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
IdentifierName("Add")))
|
||||
.AddArgumentListArguments(Argument(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))))));
|
||||
|
||||
prologueStatements++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -608,11 +611,14 @@ namespace ComputeSharp.SourceGenerators
|
||||
// Group size constants
|
||||
AppendLF();
|
||||
AppendLine("#define __GroupSize__get_X ");
|
||||
AppendStatement("builder.Append(threadsX);");
|
||||
AppendParsedStatement("builder.Append(threadsX);");
|
||||
AppendLF();
|
||||
AppendLine("#define __GroupSize__get_Y ");
|
||||
AppendStatement("builder.Append(threadsY);");
|
||||
AppendParsedStatement("builder.Append(threadsY);");
|
||||
AppendLF();
|
||||
AppendLine("#define __GroupSize__get_Z ");
|
||||
AppendStatement("builder.Append(threadsZ);");
|
||||
AppendParsedStatement("builder.Append(threadsZ);");
|
||||
AppendLF();
|
||||
|
||||
// Define declarations
|
||||
foreach (var (name, value) in definedConstants)
|
||||
@@ -620,6 +626,12 @@ namespace ComputeSharp.SourceGenerators
|
||||
AppendLineAndLF($"#define {name} {value}");
|
||||
}
|
||||
|
||||
// Defines from captured delegates
|
||||
foreach (var (_, fieldName, _) in instanceFields.Where(static t => t.Symbol.Type.TypeKind == TypeKind.Delegate))
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendConstants(ref builder, __constantNames);");
|
||||
}
|
||||
|
||||
// Static fields
|
||||
if (staticFields.Any())
|
||||
{
|
||||
@@ -658,12 +670,9 @@ namespace ComputeSharp.SourceGenerators
|
||||
}
|
||||
|
||||
// User-defined values
|
||||
foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields)
|
||||
foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields.Where(static t => t.Symbol.Type.IsUnmanagedType))
|
||||
{
|
||||
if (fieldSymbol.Type.IsUnmanagedType)
|
||||
{
|
||||
AppendLineAndLF($" {fieldType} {fieldName};");
|
||||
}
|
||||
AppendLineAndLF($" {fieldType} {fieldName};");
|
||||
}
|
||||
|
||||
AppendCharacterAndLF('}');
|
||||
@@ -742,13 +751,11 @@ namespace ComputeSharp.SourceGenerators
|
||||
}
|
||||
|
||||
// Captured delegate methods
|
||||
foreach (var (fieldSymbol, fieldName, _) in instanceFields)
|
||||
foreach (var (fieldSymbol, fieldName, _) in instanceFields.Where(static t => t.Symbol.Type.TypeKind == TypeKind.Delegate))
|
||||
{
|
||||
if (fieldSymbol.Type.TypeKind == TypeKind.Delegate)
|
||||
{
|
||||
AppendLF();
|
||||
AppendStatement($"builder.Append(__{fieldName}Attribute.{nameof(ShaderMethodSourceAttribute.GetMappedInvokeMethod)}(\"{fieldName}\"));");
|
||||
}
|
||||
AppendLF();
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendMappedInvokeMethod(ref builder, \"{fieldName}\");");
|
||||
AppendLF();
|
||||
}
|
||||
|
||||
// Entry point
|
||||
@@ -758,13 +765,9 @@ namespace ComputeSharp.SourceGenerators
|
||||
|
||||
FlushText();
|
||||
|
||||
// The builder declaration needs to go after the captured delegate attribute
|
||||
// declaration, and also after the 3 name mapping hashsets inserted at the top.
|
||||
int builderDeclarationIndex = capturedDelegates > 0 ? capturedDelegates + 3 : 0;
|
||||
|
||||
// builder = ArrayPoolStringBuilder.Create(<SIZE_HINT>);
|
||||
statements.Insert(
|
||||
builderDeclarationIndex,
|
||||
prologueStatements,
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.__Internals
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public sealed partial class ShaderMethodSourceAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Appends the mapped source code for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="mapping">The mapping of already discovered constant names.</param>
|
||||
public void AppendConstants(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> constant in this.constants)
|
||||
{
|
||||
if (mapping.Add(constant.Key))
|
||||
{
|
||||
builder.Append($"#define {constant.Key} {constant.Value}\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the mapped source code for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="name">The name to bind the method to.</param>
|
||||
public void AppendMappedInvokeMethod(ref ArrayPoolStringBuilder builder, string name)
|
||||
{
|
||||
builder.Append(this.invokeMethod.Replace(InvokeMethodIdentifier, name));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace ComputeSharp.__Internals
|
||||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This attribute is not intended to be used directly by user code")]
|
||||
public sealed class ShaderMethodSourceAttribute : Attribute
|
||||
public sealed partial class ShaderMethodSourceAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// The identifier for the invoke method, for late binding.
|
||||
@@ -28,6 +28,26 @@ namespace ComputeSharp.__Internals
|
||||
/// </summary>
|
||||
private readonly string invokeMethod;
|
||||
|
||||
/// <summary>
|
||||
/// The fully qualified name of the current method.
|
||||
/// </summary>
|
||||
private readonly string methodName;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of custom types.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> types;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of processed methods.
|
||||
/// </summary>
|
||||
private readonly string[] methods;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of discovered constants.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> constants;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderMethodSourceAttribute"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -39,42 +59,10 @@ namespace ComputeSharp.__Internals
|
||||
public ShaderMethodSourceAttribute(string methodName, object[] types, string invokeMethod, string[] methods, object[] constants)
|
||||
{
|
||||
this.invokeMethod = invokeMethod;
|
||||
|
||||
MethodName = methodName;
|
||||
Types = types.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
Methods = methods;
|
||||
Constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fully qualified name of the shader type.
|
||||
/// </summary>
|
||||
internal string MethodName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of processed custom types.
|
||||
/// </summary>
|
||||
internal IReadOnlyDictionary<string, string> Types { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of processed methods.
|
||||
/// </summary>
|
||||
internal IReadOnlyCollection<string> Methods { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of discovered constants.
|
||||
/// </summary>
|
||||
internal IReadOnlyDictionary<string, string> Constants { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped source code for the current method.
|
||||
/// </summary>
|
||||
/// <param name="name">The name to bind the method to.</param>
|
||||
/// <returns>The mapped source code for the current mehtod.</returns>
|
||||
[Pure]
|
||||
public string GetMappedInvokeMethod(string name)
|
||||
{
|
||||
return this.invokeMethod.Replace(InvokeMethodIdentifier, name);
|
||||
this.methodName = methodName;
|
||||
this.types = types.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
this.methods = methods;
|
||||
this.constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -96,7 +84,7 @@ namespace ComputeSharp.__Internals
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (attribute.MethodName.Equals(methodName))
|
||||
if (attribute.methodName.Equals(methodName))
|
||||
{
|
||||
return attribute;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ namespace ComputeSharp.Tests
|
||||
|
||||
// Missing attribute
|
||||
public static int Identity(int x) => x;
|
||||
|
||||
[ShaderMethod]
|
||||
public static int FunctionWithConstants(int x) => (int)(x + FourAndAHalf);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
@@ -111,5 +114,36 @@ namespace ComputeSharp.Tests
|
||||
Assert.AreEqual(result[i], Activations.AddOne(Activations.Square(i)));
|
||||
}
|
||||
}
|
||||
|
||||
public const float FourAndAHalf = 4.5f;
|
||||
public const int MagicNumber = 42;
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct DelegateWithConstantsShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<int> buffer;
|
||||
public readonly Func<int, int> activation;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
buffer[ThreadIds.X] = MagicNumber + activation(ThreadIds.X);
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public unsafe void DelegateWithConstants(Device device)
|
||||
{
|
||||
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
|
||||
|
||||
device.Get().For(100, new DelegateWithConstantsShader(buffer, Activations.FunctionWithConstants));
|
||||
|
||||
int[] result = buffer.ToArray();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.AreEqual(result[i], MagicNumber + Activations.FunctionWithConstants(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする