Enabled constants import for captured delegates

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-13 20:19:56 +02:00
1 親 fdc06c2b45
コミット 4c50309bdd
5ファイル変更
+140 -142

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

+2 -64
ファイルの表示
@@ -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))));
}
}
}
+43 -40
ファイルの表示
@@ -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,