forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-builder.js
More file actions
313 lines (284 loc) · 8.55 KB
/
function-builder.js
File metadata and controls
313 lines (284 loc) · 8.55 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @desc This handles all the raw state, converted state, etc. of a single function.
* [INTERNAL] A collection of functionNodes.
*/
class FunctionBuilder {
/**
*
* @param {Kernel} kernel
* @param {FunctionNode} FunctionNode
* @param {object} [extraNodeOptions]
* @returns FunctionBuilder
*/
static fromKernel(kernel, FunctionNode, extraNodeOptions) {
const {
argumentNames,
argumentTypes,
argumentSizes,
constants,
constantTypes,
debug,
loopMaxIterations,
nativeFunctions,
output,
plugins,
source,
subKernels,
functions,
} = kernel;
const onNestedFunction = (fnString, returnType) => {
functionBuilder.addFunctionNode(new FunctionNode(fnString, Object.assign({}, nodeOptions, {
returnType
})));
};
const lookupReturnType = (functionName) => {
return functionBuilder.lookupReturnType(functionName);
};
const nodeOptions = Object.assign({
isRootKernel: false,
onNestedFunction,
lookupReturnType,
constants,
constantTypes,
debug,
loopMaxIterations,
output,
plugins,
}, extraNodeOptions || {});
const rootNodeOptions = Object.assign({}, nodeOptions, {
isRootKernel: true,
name: 'kernel',
argumentNames,
argumentTypes,
argumentSizes,
});
if (typeof source === 'object' && source.functionNodes) {
return new FunctionBuilder().fromJSON(source.functionNodes, FunctionNode);
}
const rootNode = new FunctionNode(source, rootNodeOptions);
let functionNodes = null;
if (functions) {
functionNodes = functions.map((fn) => new FunctionNode(fn.source, {
returnType: fn.returnType,
argumentTypes: fn.argumentTypes,
output,
plugins,
constants,
constantTypes,
}));
}
let subKernelNodes = null;
if (subKernels) {
subKernelNodes = subKernels.map((subKernel) => {
const {
name,
source
} = subKernel;
return new FunctionNode(source, Object.assign({}, nodeOptions, {
name,
isSubKernel: true,
isRootKernel: false
}));
});
}
const functionBuilder = new FunctionBuilder({
rootNode,
functionNodes,
nativeFunctions,
subKernelNodes
});
return functionBuilder;
}
/**
*
* @param {IFunctionBuilderSettings} [settings]
*/
constructor(settings) {
settings = settings || {};
this.rootNode = settings.rootNode;
this.functionNodes = settings.functionNodes || [];
this.subKernelNodes = settings.subKernelNodes || [];
this.nativeFunctions = settings.nativeFunctions || [];
this.functionMap = {};
this.nativeFunctionNames = [];
if (this.rootNode) {
this.functionMap['kernel'] = this.rootNode;
}
if (this.functionNodes) {
for (let i = 0; i < this.functionNodes.length; i++) {
this.functionMap[this.functionNodes[i].name] = this.functionNodes[i];
}
}
if (this.subKernelNodes) {
for (let i = 0; i < this.subKernelNodes.length; i++) {
this.functionMap[this.subKernelNodes[i].name] = this.subKernelNodes[i];
}
}
if (this.nativeFunctions) {
for (let i = 0; i < this.nativeFunctions.length; i++) {
this.nativeFunctionNames.push(this.nativeFunctions[i].name);
}
}
}
/**
* @desc Add the function node directly
*
* @param {FunctionNode} functionNode - functionNode to add
*
*/
addFunctionNode(functionNode) {
this.functionMap[functionNode.name] = functionNode;
if (functionNode.isRootKernel) {
this.rootNode = functionNode;
}
}
/**
* @desc Trace all the depending functions being called, from a single function
*
* This allow for 'unneeded' functions to be automatically optimized out.
* Note that the 0-index, is the starting function trace.
*
* @param {String} functionName - Function name to trace from, default to 'kernel'
* @param {String[]} [retList] - Returning list of function names that is traced. Including itself.
* @param {Object} [parent] - Parent node
*
* @returns {String[]} Returning list of function names that is traced. Including itself.
*/
traceFunctionCalls(functionName, retList, parent) {
functionName = functionName || 'kernel';
retList = retList || [];
if (this.nativeFunctionNames.indexOf(functionName) > -1) {
if (retList.indexOf(functionName) >= 0) {
// Does nothing if already traced
} else {
retList.push(functionName);
}
return retList;
}
const functionNode = this.functionMap[functionName];
if (functionNode) {
// Check if function already exists
const functionIndex = retList.indexOf(functionName);
if (functionIndex === -1) {
retList.push(functionName);
if (parent) {
functionNode.parent = parent;
}
functionNode.toString(); //ensure JS trace is done
for (let i = 0; i < functionNode.calledFunctions.length; ++i) {
this.traceFunctionCalls(functionNode.calledFunctions[i], retList, functionNode);
}
} else {
/**
* https://github.com/gpujs/gpu.js/issues/207
* if dependent function is already in the list, because a function depends on it, and because it has
* already been traced, we know that we must move the dependent function to the end of the the retList.
* */
const dependantFunctionName = retList.splice(functionIndex, 1)[0];
retList.push(dependantFunctionName);
}
}
return retList;
}
/**
* @desc Return the string for a function
* @param {String} functionName - Function name to trace from. If null, it returns the WHOLE builder stack
* @returns {String} The full string, of all the various functions. Trace optimized if functionName given
*/
getPrototypeString(functionName) {
return this.getPrototypes(functionName).join('\n');
}
/**
* @desc Return the string for a function
* @param {String} [functionName] - Function name to trace from. If null, it returns the WHOLE builder stack
* @returns {Array} The full string, of all the various functions. Trace optimized if functionName given
*/
getPrototypes(functionName) {
if (this.rootNode) {
this.rootNode.toString();
}
if (functionName) {
return this.getPrototypesFromFunctionNames(this.traceFunctionCalls(functionName, []).reverse());
}
return this.getPrototypesFromFunctionNames(Object.keys(this.functionMap));
}
/**
* @desc Get string from function names
* @param {String[]} functionList - List of function to build string
* @returns {String} The string, of all the various functions. Trace optimized if functionName given
*/
getStringFromFunctionNames(functionList) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const node = this.functionMap[functionList[i]];
if (node) {
ret.push(this.functionMap[functionList[i]].toString());
}
}
return ret.join('\n');
}
/**
* @desc Return string of all functions converted
* @param {String[]} functionList - List of function names to build the string.
* @returns {Array} Prototypes of all functions converted
*/
getPrototypesFromFunctionNames(functionList) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const functionName = functionList[i];
const functionIndex = this.nativeFunctionNames.indexOf(functionName);
if (functionIndex > -1) {
ret.push(this.nativeFunctions[functionIndex].source);
continue;
}
const node = this.functionMap[functionName];
if (node) {
ret.push(node.toString());
}
}
return ret;
}
toJSON() {
return this.traceFunctionCalls(this.rootNode.name).reverse().map(name => {
if (this.nativeFunctions[name]) {
return {
name,
source: this.nativeFunctions[name]
};
} else if (this.functionMap[name]) {
return this.functionMap[name].toJSON();
} else {
throw new Error(`function ${ name } not found`);
}
});
}
fromJSON(jsonFunctionNodes, FunctionNode) {
this.functionMap = {};
for (let i = 0; i < jsonFunctionNodes.length; i++) {
const jsonFunctionNode = jsonFunctionNodes[i];
this.functionMap[jsonFunctionNode.settings.name] = new FunctionNode(jsonFunctionNode.ast, jsonFunctionNode.settings);
}
return this;
}
/**
* @desc Get string for a particular function name
* @param {String} functionName - Function name to trace from. If null, it returns the WHOLE builder stack
* @returns {String} settings - The string, of all the various functions. Trace optimized if functionName given
*/
getString(functionName) {
if (functionName) {
return this.getStringFromFunctionNames(this.traceFunctionCalls(functionName).reverse());
}
return this.getStringFromFunctionNames(Object.keys(this.functionMap));
}
lookupReturnType(functionName) {
const node = this.functionMap[functionName];
if (node && node.returnType) {
return node.returnType;
}
return null;
}
}
module.exports = {
FunctionBuilder
};