From 1a465880d96a3b5835460844bca6de6905c38c05 Mon Sep 17 00:00:00 2001 From: Cody Date: Wed, 1 Dec 2021 15:54:39 -0700 Subject: [PATCH] complicated function dependency test and fix --- src/backend/function-builder.js | 6 +- .../complicated-function-dependencies.js | 109 ++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 test/internal/complicated-function-dependencies.js diff --git a/src/backend/function-builder.js b/src/backend/function-builder.js index 1633f0e8..5595a975 100644 --- a/src/backend/function-builder.js +++ b/src/backend/function-builder.js @@ -292,9 +292,6 @@ class FunctionBuilder { if (functionIndex === -1) { retList.push(functionName); functionNode.toString(); //ensure JS trace is done - for (let i = 0; i < functionNode.calledFunctions.length; ++i) { - this.traceFunctionCalls(functionNode.calledFunctions[i], retList); - } } else { /** * https://github.com/gpujs/gpu.js/issues/207 @@ -304,6 +301,9 @@ class FunctionBuilder { const dependantFunctionName = retList.splice(functionIndex, 1)[0]; retList.push(dependantFunctionName); } + for (let i = 0; i < functionNode.calledFunctions.length; ++i) { + this.traceFunctionCalls(functionNode.calledFunctions[i], retList); + } } return retList; diff --git a/test/internal/complicated-function-dependencies.js b/test/internal/complicated-function-dependencies.js new file mode 100644 index 00000000..81c58406 --- /dev/null +++ b/test/internal/complicated-function-dependencies.js @@ -0,0 +1,109 @@ +const { assert, skip, test, module: describe } = require("qunit"); +const { GPU } = require("../../src"); + +describe("internal: complicated function dependencies"); + +function a() { + b(); + c(); + d(); + e(); + f(); + return 1; +} +function b() { + g(); +} +function c() { + g(); + return 1; +} +function d() { + g(); + return 1; +} +function e() { + g(); + return 1; +} +function f() { + i(); + m(); + return 1; +} +function g() { + i(); + j(); + k(); + return 1; +} +function h() { + o(); + return 1; +} +function i() { + return 1; +} +function j() { + m(); + n(); + return 1; +} +function k() { + return 1; +} +function l() { + o(); + p(); + return 1; +} +function m() { + return 1; +} +function n() { + return 1; +} +function o() { + return 1; +} +function p() { + b(); + return 1; +} + +function complicatedFunctionDependecies(mode) { + const gpu = new GPU({ mode }); + gpu.setFunctions([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]); + const kernel = gpu + .createKernel(function () { + a(); + return 1; + }) + .setOutput([1]); + + assert.deepEqual(kernel(), new Float32Array([1])); +} + +test("auto", () => { + complicatedFunctionDependecies(); +}); + +test("gpu", () => { + complicatedFunctionDependecies("gpu"); +}); + +(GPU.isWebGLSupported ? test : skip)("webgl", () => { + complicatedFunctionDependecies("webgl"); +}); + +(GPU.isWebGL2Supported ? test : skip)("webgl2", () => { + complicatedFunctionDependecies("webgl2"); +}); + +(GPU.isHeadlessGLSupported ? test : skip)("headlessgl", () => { + complicatedFunctionDependecies("headlessgl"); +}); + +test("cpu", () => { + complicatedFunctionDependecies("cpu"); +}); \ No newline at end of file