forked from MKL-NET/MKL.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperGenerator.cs
More file actions
287 lines (250 loc) · 13 KB
/
WrapperGenerator.cs
File metadata and controls
287 lines (250 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace MKLNET.WrapperGenerator;
[Generator]
public class WrapperGenerator : ISourceGenerator
{
private static readonly string _version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "na";
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new UnsafeClassSyntaxReceiver());
}
private static ParameterSyntax? GetLengthDropCandidate(MethodDeclarationSyntax mds)
{
var candidates = mds.ParameterList.Parameters.Where(p => p.Type is PredefinedTypeSyntax { Keyword: { } keyword }
&& keyword.IsKind(SyntaxKind.IntKeyword)
&& p.Identifier.Text.Length == 1
).ToList();
if (candidates.Count != 1)
{
return null;
}
return candidates[0];
}
private static (IList<ParameterSyntax> changed, ParameterListSyntax newList)
TransformParameters(MethodDeclarationSyntax mds, Func<ParameterSyntax, ParameterSyntax?> f)
{
var changed = mds.ParameterList.Parameters.Select(ps => f(ps) is null ? null : ps).Where(ps => ps != null).ToImmutableList();
var newList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(mds.ParameterList.Parameters.Select(ps => f(ps) ?? ps)));
return (changed!, newList);
}
private enum AdditionalTransformation
{
///<summary>Only transform pointers</summary>
None,
///<summary>Drop a single <c>int n</n> parameter. Infer length from first transformed pointer.</summary>
InferLength,
///<summary>Add <c>iniX</c> parameters that act as offsets.</summary>
AddOffsets
}
void WriterTransformedMethod(
MethodDeclarationSyntax mds, ClassDeclarationSyntax nativeCds,
(IList<ParameterSyntax> changed, ParameterListSyntax newList) transformation, StringBuilder sb,
AdditionalTransformation trafo)
{
(ParameterSyntax lengthParam, string takeLengthFrom)? lengthOptions = null;
ParameterListSyntax parameterList;
static bool IsOffsettable(ParameterSyntax p) => p.Identifier.Text.Length <= 2 && p.Identifier.Text != "A"; // `param` and `A` do not get `ini` in BLAS
switch (trafo)
{
case AdditionalTransformation.InferLength
when GetLengthDropCandidate(mds) is { } lengthParam
&& transformation.changed.FirstOrDefault() is { Identifier.Text: { } takeLengthFrom }:
lengthOptions = (lengthParam, takeLengthFrom);
sb.Append("\t\t///<remarks>This version infers the length parameter <c>")
.Append(lengthParam.Identifier)
.Append("</c> from <paramref name=\"")
.Append(takeLengthFrom)
.AppendLine("\" />'s length.</remarks>");
var noLen = transformation.newList.Parameters.Where(p => p.Identifier.Text != lengthParam.Identifier.Text);
parameterList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(noLen));
break;
case AdditionalTransformation.InferLength:
return;
case AdditionalTransformation.AddOffsets
when transformation.newList.Parameters.Any(p => p.Identifier.Text.StartsWith("inc")):
var withIni = transformation.newList.Parameters.SelectMany(p =>
{
if (IsOffsettable(p)
&& transformation.changed.Any(c => c.Identifier.Text == p.Identifier.Text))
{
return new[] {p,
SyntaxFactory.Parameter(
SyntaxFactory.Identifier("ini"+p.Identifier.Text))
.WithType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)))};
}
return new[] { p };
});
parameterList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(withIni));
sb.AppendLine("\t\t///<remarks>This version takes offsets in the form of <c>iniX</c> arguments. Consider using the Span version with slicing instead.</remarks>");
break;
case AdditionalTransformation.AddOffsets:
return;
default:
parameterList = transformation.newList;
break;
}
sb.Append("\t\t///<summary>Calls the MKL function <c>").Append(mds.Identifier.Text).AppendLine("</c> by pinning the given data during the computation.</summary>");
sb.AppendLine("\t\t[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]");
sb.Append("\t\tpublic static ")
.Append(mds.ReturnType)
.Append(' ')
.Append(trafo == AdditionalTransformation.AddOffsets ? mds.Identifier.Text.TrimEnd('I') : mds.Identifier)
.Append(parameterList.NormalizeWhitespace())
.AppendLine();
sb.AppendLine("\t\t{");
sb.AppendLine("\t\t\tunsafe");
sb.AppendLine("\t\t\t{");
foreach (var changedParam in transformation.changed)
{
if (trafo == AdditionalTransformation.AddOffsets && IsOffsettable(changedParam))
{
sb.Append("\t\t\t\tfixed(").Append(changedParam.Type).Append(' ').Append(changedParam.Identifier).Append("Pinned = &").Append(changedParam.Identifier).Append("[ini").Append(changedParam.Identifier).AppendLine("])");
}
else
{
sb.Append("\t\t\t\tfixed(").Append(changedParam.Type).Append(' ').Append(changedParam.Identifier).Append("Pinned = ").Append(changedParam.Identifier).AppendLine(")");
}
}
sb.AppendLine("\t\t\t\t{");
var maybeReturn = mds.ReturnType is PredefinedTypeSyntax { Keyword: { } keyword }
&& keyword.IsKind(SyntaxKind.VoidKeyword)
? "" : "return ";
sb.Append("\t\t\t\t\t").Append(maybeReturn).Append(nativeCds.Identifier).Append('.').Append(mds.Identifier).Append('(');
var callList = string.Join(", ", mds.ParameterList.Parameters
.Select(ps => ps.Modifiers.ToString() + " " + (
transformation.changed.Contains(ps)
? $"{ps.Identifier}Pinned"
: ps == lengthOptions?.lengthParam
? $"{lengthOptions?.takeLengthFrom}.Length"
: ps.Identifier.ValueText)));
sb.Append(callList);
sb.AppendLine(");"); // End of call
sb.AppendLine("\t\t\t\t}"); // End of fixed
sb.AppendLine("\t\t\t}"); // End of unsafe
sb.AppendLine("\t\t}"); // End of method
}
public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxReceiver is UnsafeClassSyntaxReceiver { Classes: { } classes })
{
foreach (var (parentCds, nativeCds) in classes)
{
ExecuteOneClass(context, parentCds, nativeCds);
}
}
}
public void ExecuteOneClass(GeneratorExecutionContext context, ClassDeclarationSyntax parentCds, ClassDeclarationSyntax nativeCds)
{
var semantics = context.Compilation.GetSemanticModel(parentCds.SyntaxTree);
var parentClassSymbol = semantics.GetDeclaredSymbol(parentCds)!;
var sb = new StringBuilder("// <auto-generated/>");
if (parentCds.SyntaxTree.GetRoot() is CompilationUnitSyntax unit)
{
sb.AppendLine(unit.Usings.ToString());
sb.AppendLine();
}
sb.Append("namespace ").Append(parentClassSymbol.ContainingNamespace).AppendLine();
sb.AppendLine("{");
sb.Append("\t[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"MKL.NET\", \"").Append(_version).AppendLine("\")]");
sb.Append("\tpartial class ").AppendLine(parentClassSymbol.Name);
sb.AppendLine("\t{");
foreach (var member in nativeCds.Members)
{
if (member is MethodDeclarationSyntax mds)
{
// Array version, ignore [In]
var arrayParams = TransformParameters(mds,
originalParam => originalParam.Type is PointerTypeSyntax pts
? originalParam
.WithType(
SyntaxFactory.ArrayType
(
pts.ElementType,
SyntaxFactory.List(new[] { SyntaxFactory.ArrayRankSpecifier() })
)
.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")))
.WithAttributeLists(SyntaxFactory.List<AttributeListSyntax>())
: null);
// Span version, use [In] on pointer to mark possible read-only spans
var inAttribute = semantics.Compilation.GetTypeByMetadataName(typeof(System.Runtime.InteropServices.InAttribute).FullName);
var spanParams = TransformParameters(mds,
originalParam =>
{
if (originalParam.Type is not PointerTypeSyntax pts)
{
return null;
}
var attributeTypes = originalParam.AttributeLists
.SelectMany(al => al.Attributes)
.Select(a => semantics.GetTypeInfo(a).Type)
.ToImmutableHashSet(SymbolEqualityComparer.Default);
return originalParam
.WithType(
SyntaxFactory.GenericName
(
SyntaxFactory.Identifier(
attributeTypes.Contains(inAttribute)
? "global::System.ReadOnlySpan"
: "global::System.Span"
),
SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(new[] { pts.ElementType }))
)
.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")))
.WithAttributeLists(SyntaxFactory.List<AttributeListSyntax>());
});
var lengthParam = GetLengthDropCandidate(mds);
WriterTransformedMethod(mds, nativeCds, arrayParams, sb, AdditionalTransformation.None);
// Dropping the length parameter is nice, but might not make sense on matrix stuff
if (parentCds.Identifier.Text != "Lapack")
{
WriterTransformedMethod(mds, nativeCds, arrayParams, sb, AdditionalTransformation.InferLength);
}
// VML and BLAS have `ini` versions (that are not needed with Span anymore)
if (parentCds.Identifier.Text == "Blas" || (parentCds.Identifier.Text == "Vml" && mds.Identifier.Text.EndsWith("I")))
{
WriterTransformedMethod(mds, nativeCds, arrayParams, sb, AdditionalTransformation.AddOffsets);
}
sb.AppendLine();
WriterTransformedMethod(mds, nativeCds, spanParams, sb, AdditionalTransformation.None);
if (parentCds.Identifier.Text != "Lapack")
{
WriterTransformedMethod(mds, nativeCds, spanParams, sb, AdditionalTransformation.InferLength);
}
sb.AppendLine();
}
}
sb.AppendLine("\t}"); // End of class
sb.AppendLine("}"); // End of namespace
var s = sb.ToString();
context.AddSource($"{parentCds.Identifier}.Wrappers.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
}
}
internal class UnsafeClassSyntaxReceiver : ISyntaxReceiver
{
public List<(ClassDeclarationSyntax parent, ClassDeclarationSyntax native)> Classes { get; } = new();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// we match any `static class Unsafe` nested in a parent class which is static and partial
if (syntaxNode is ClassDeclarationSyntax
{
Identifier.ValueText: "Unsafe",
Modifiers: { } childModifiers,
Parent: ClassDeclarationSyntax { Modifiers: { } parentModifiers } parentCds
} unsafeCds
&& childModifiers.Any(SyntaxKind.StaticKeyword)
&& parentModifiers.Any(SyntaxKind.PartialKeyword)
&& parentModifiers.Any(SyntaxKind.StaticKeyword))
{
Classes.Add((parentCds, unsafeCds));
}
}
}