@thi.ng/shader-ast-optimize 0.4.15 → 0.4.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -1
- package/README.md +2 -2
- package/constant-folding.js +31 -31
- package/defoptimized.d.ts +2 -1
- package/package.json +12 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
3
|
+
- **Last updated**: 2024-06-21T19:34:38Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
### [0.4.16](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-optimize@0.4.16) (2024-06-21)
|
|
13
|
+
|
|
14
|
+
#### ♻️ Refactoring
|
|
15
|
+
|
|
16
|
+
- enforce uniform naming convention of internal functions ([56992b2](https://github.com/thi-ng/umbrella/commit/56992b2))
|
|
17
|
+
|
|
12
18
|
### [0.4.12](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-optimize@0.4.12) (2024-04-20)
|
|
13
19
|
|
|
14
20
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
10
|
+
> This is one of 193 standalone projects, maintained as part
|
|
11
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
12
|
> and anti-framework.
|
|
13
13
|
>
|
|
@@ -138,7 +138,7 @@ For Node.js REPL:
|
|
|
138
138
|
const opt = await import("@thi.ng/shader-ast-optimize");
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 1.
|
|
141
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.26 KB
|
|
142
142
|
|
|
143
143
|
## Dependencies
|
|
144
144
|
|
package/constant-folding.js
CHANGED
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
} from "@thi.ng/shader-ast/ast/lit";
|
|
30
30
|
import { allChildren, walk } from "@thi.ng/shader-ast/ast/scope";
|
|
31
31
|
import { LOGGER } from "@thi.ng/shader-ast/logger";
|
|
32
|
-
const
|
|
32
|
+
const __replaceNode = (node, next) => {
|
|
33
33
|
if (LOGGER.level <= LogLevel.DEBUG) {
|
|
34
34
|
LOGGER.debug(`replacing AST node:`);
|
|
35
35
|
LOGGER.debug(" old: " + JSON.stringify(node));
|
|
@@ -41,15 +41,15 @@ const replaceNode = (node, next) => {
|
|
|
41
41
|
Object.assign(node, next);
|
|
42
42
|
return true;
|
|
43
43
|
};
|
|
44
|
-
const
|
|
44
|
+
const __replaceNumericNode = (node, res) => {
|
|
45
45
|
node.type === "int" && (res |= 0);
|
|
46
46
|
node.type === "uint" && (res >>>= 0);
|
|
47
|
-
return
|
|
47
|
+
return __replaceNode(node, lit(node.type, res));
|
|
48
48
|
};
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const
|
|
49
|
+
const __replaceBooleanNode = (node, res) => __replaceNode(node, bool(res));
|
|
50
|
+
const __replaceWithConst = (node, ref, n) => __replaceNode(node, matchingPrimFor(ref, n));
|
|
51
|
+
const __maybeFoldMath = (op, l, r) => op === "+" ? l + r : op === "-" ? l - r : op === "*" ? l * r : op === "/" ? r != 0 ? l / r : illegalArgs(`division by zero: ${l}/${r}`) : void 0;
|
|
52
|
+
const __maybeFoldCompare = (op, l, r) => op === "==" ? l === r : op === "!=" ? l !== r : op === "<" ? l < r : op === "<=" ? l <= r : op === ">=" ? l >= r : op === ">" ? l > r : void 0;
|
|
53
53
|
const COMPS = { x: 0, y: 1, z: 2, w: 3 };
|
|
54
54
|
const BUILTINS = {
|
|
55
55
|
abs: ([a]) => Math.abs(a),
|
|
@@ -86,7 +86,7 @@ const foldNode = defmulti(
|
|
|
86
86
|
const $node = node;
|
|
87
87
|
if ($node.op == "-" && isLitNumericConst($node.val)) {
|
|
88
88
|
$node.val.val *= -1;
|
|
89
|
-
return
|
|
89
|
+
return __replaceNode(node, $node.val);
|
|
90
90
|
}
|
|
91
91
|
},
|
|
92
92
|
op2: (node) => {
|
|
@@ -97,28 +97,28 @@ const foldNode = defmulti(
|
|
|
97
97
|
const isNumL = isLitNumericConst(l);
|
|
98
98
|
const isNumR = isLitNumericConst(r);
|
|
99
99
|
if (isNumL && isNumR) {
|
|
100
|
-
const num =
|
|
101
|
-
if (num !== void 0) return
|
|
102
|
-
const bool2 =
|
|
103
|
-
if (bool2 !== void 0) return
|
|
100
|
+
const num = __maybeFoldMath(op, l.val, r.val);
|
|
101
|
+
if (num !== void 0) return __replaceNumericNode(node, num);
|
|
102
|
+
const bool2 = __maybeFoldCompare(op, l.val, r.val);
|
|
103
|
+
if (bool2 !== void 0) return __replaceBooleanNode(node, bool2);
|
|
104
104
|
} else if (op === "*") {
|
|
105
105
|
if (isNumL && l.val === 0)
|
|
106
|
-
return
|
|
106
|
+
return __replaceWithConst(node, r, FLOAT0);
|
|
107
107
|
if (isNumR && r.val === 0)
|
|
108
|
-
return
|
|
109
|
-
if (isNumL && l.val === 1) return
|
|
110
|
-
if (isNumR && r.val === 1) return
|
|
108
|
+
return __replaceWithConst(node, l, FLOAT0);
|
|
109
|
+
if (isNumL && l.val === 1) return __replaceNode(node, r);
|
|
110
|
+
if (isNumR && r.val === 1) return __replaceNode(node, l);
|
|
111
111
|
} else if (op === "/") {
|
|
112
112
|
if (isNumL && l.val === 0)
|
|
113
|
-
return
|
|
113
|
+
return __replaceWithConst(node, r, FLOAT0);
|
|
114
114
|
if (isNumR && r.val === 0) illegalArgs("division by zero");
|
|
115
|
-
if (isNumR && r.val === 1) return
|
|
115
|
+
if (isNumR && r.val === 1) return __replaceNode(node, l);
|
|
116
116
|
} else if (op === "+") {
|
|
117
|
-
if (isNumL && l.val === 0) return
|
|
118
|
-
if (isNumR && r.val === 0) return
|
|
117
|
+
if (isNumL && l.val === 0) return __replaceNode(node, r);
|
|
118
|
+
if (isNumR && r.val === 0) return __replaceNode(node, l);
|
|
119
119
|
} else if (op === "-") {
|
|
120
|
-
if (isNumL && l.val === 0) return
|
|
121
|
-
if (isNumR && r.val === 0) return
|
|
120
|
+
if (isNumL && l.val === 0) return __replaceNode(node, neg(r));
|
|
121
|
+
if (isNumR && r.val === 0) return __replaceNode(node, l);
|
|
122
122
|
}
|
|
123
123
|
},
|
|
124
124
|
call_i: (node) => {
|
|
@@ -126,7 +126,7 @@ const foldNode = defmulti(
|
|
|
126
126
|
if ($node.args.every((x) => isLitNumericConst(x))) {
|
|
127
127
|
const op = BUILTINS[$node.id];
|
|
128
128
|
if (op !== void 0) {
|
|
129
|
-
return
|
|
129
|
+
return __replaceNumericNode(
|
|
130
130
|
node,
|
|
131
131
|
op($node.args.map((x) => x.val))
|
|
132
132
|
);
|
|
@@ -139,13 +139,13 @@ const foldNode = defmulti(
|
|
|
139
139
|
const $node = node;
|
|
140
140
|
if (isLitNumericConst($node.val)) {
|
|
141
141
|
if (isFloat($node.val)) {
|
|
142
|
-
return
|
|
142
|
+
return __replaceNode(node, float($node.val.val));
|
|
143
143
|
}
|
|
144
144
|
if (isInt($node.val)) {
|
|
145
|
-
return
|
|
145
|
+
return __replaceNode(node, int($node.val.val));
|
|
146
146
|
}
|
|
147
147
|
if (isUint($node.val)) {
|
|
148
|
-
return
|
|
148
|
+
return __replaceNode(node, uint($node.val.val));
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
},
|
|
@@ -154,7 +154,7 @@ const foldNode = defmulti(
|
|
|
154
154
|
const val = $node.val;
|
|
155
155
|
if (isLitVecConst(val)) {
|
|
156
156
|
if (isFloat(node)) {
|
|
157
|
-
return
|
|
157
|
+
return __replaceNode(
|
|
158
158
|
node,
|
|
159
159
|
float(val.val[COMPS[$node.id]])
|
|
160
160
|
);
|
|
@@ -171,19 +171,19 @@ const foldBuiltin = defmulti(
|
|
|
171
171
|
exp2: (node) => {
|
|
172
172
|
const a = node.args[0];
|
|
173
173
|
if (isLitNumOrVecConst(a, 0)) {
|
|
174
|
-
return
|
|
174
|
+
return __replaceWithConst(node, a, FLOAT1);
|
|
175
175
|
}
|
|
176
176
|
if (isLitNumOrVecConst(a, 1)) {
|
|
177
|
-
return
|
|
177
|
+
return __replaceWithConst(node, a, FLOAT2);
|
|
178
178
|
}
|
|
179
179
|
},
|
|
180
180
|
pow: (node) => {
|
|
181
181
|
const [a, b] = node.args;
|
|
182
182
|
if (isLitNumOrVecConst(b, 0)) {
|
|
183
|
-
return
|
|
183
|
+
return __replaceWithConst(node, a, FLOAT1);
|
|
184
184
|
}
|
|
185
185
|
if (isLitNumOrVecConst(b, 1)) {
|
|
186
|
-
return
|
|
186
|
+
return __replaceNode(node, a);
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
}
|
package/defoptimized.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { FnBody0 } from "@thi.ng/shader-ast";
|
|
2
|
+
import { defMain } from "@thi.ng/shader-ast/ast/function";
|
|
2
3
|
/**
|
|
3
4
|
* Same as [defMain()](), but applies optimizations (e.g.
|
|
4
5
|
* {@link constantFolding}) to the given function body.
|
|
5
6
|
*
|
|
6
7
|
* @param fn
|
|
7
8
|
*/
|
|
8
|
-
export declare const defOptimized: (fn: FnBody0) =>
|
|
9
|
+
export declare const defOptimized: (fn: FnBody0) => ReturnType<typeof defMain>;
|
|
9
10
|
//# sourceMappingURL=defoptimized.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/shader-ast-optimize",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.16",
|
|
4
4
|
"description": "Shader AST code optimization passes/strategies",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://
|
|
13
|
+
"homepage": "https://thi.ng/shader-ast-optimize",
|
|
14
14
|
"funding": [
|
|
15
15
|
{
|
|
16
16
|
"type": "github",
|
|
@@ -36,18 +36,18 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.11.
|
|
40
|
-
"@thi.ng/defmulti": "^3.0.
|
|
41
|
-
"@thi.ng/errors": "^2.5.
|
|
42
|
-
"@thi.ng/logger": "^3.0.
|
|
43
|
-
"@thi.ng/math": "^5.
|
|
44
|
-
"@thi.ng/shader-ast": "^0.15.
|
|
39
|
+
"@thi.ng/api": "^8.11.3",
|
|
40
|
+
"@thi.ng/defmulti": "^3.0.40",
|
|
41
|
+
"@thi.ng/errors": "^2.5.8",
|
|
42
|
+
"@thi.ng/logger": "^3.0.13",
|
|
43
|
+
"@thi.ng/math": "^5.11.0",
|
|
44
|
+
"@thi.ng/shader-ast": "^0.15.16"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@microsoft/api-extractor": "^7.
|
|
48
|
-
"esbuild": "^0.21.
|
|
47
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
48
|
+
"esbuild": "^0.21.5",
|
|
49
49
|
"typedoc": "^0.25.13",
|
|
50
|
-
"typescript": "^5.
|
|
50
|
+
"typescript": "^5.5.2"
|
|
51
51
|
},
|
|
52
52
|
"keywords": [
|
|
53
53
|
"typescript"
|
|
@@ -82,5 +82,5 @@
|
|
|
82
82
|
"parent": "@thi.ng/shader-ast",
|
|
83
83
|
"year": 2019
|
|
84
84
|
},
|
|
85
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n"
|
|
86
86
|
}
|