@thi.ng/shader-ast-optimize 0.4.14 → 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 +32 -42
- package/defoptimized.d.ts +2 -1
- package/package.json +13 -13
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,38 +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)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (bool2 !== void 0)
|
|
105
|
-
return replaceBooleanNode(node, bool2);
|
|
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);
|
|
106
104
|
} else if (op === "*") {
|
|
107
105
|
if (isNumL && l.val === 0)
|
|
108
|
-
return
|
|
106
|
+
return __replaceWithConst(node, r, FLOAT0);
|
|
109
107
|
if (isNumR && r.val === 0)
|
|
110
|
-
return
|
|
111
|
-
if (isNumL && l.val === 1)
|
|
112
|
-
|
|
113
|
-
if (isNumR && r.val === 1)
|
|
114
|
-
return replaceNode(node, l);
|
|
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);
|
|
115
111
|
} else if (op === "/") {
|
|
116
112
|
if (isNumL && l.val === 0)
|
|
117
|
-
return
|
|
118
|
-
if (isNumR && r.val === 0)
|
|
119
|
-
|
|
120
|
-
if (isNumR && r.val === 1)
|
|
121
|
-
return replaceNode(node, l);
|
|
113
|
+
return __replaceWithConst(node, r, FLOAT0);
|
|
114
|
+
if (isNumR && r.val === 0) illegalArgs("division by zero");
|
|
115
|
+
if (isNumR && r.val === 1) return __replaceNode(node, l);
|
|
122
116
|
} else if (op === "+") {
|
|
123
|
-
if (isNumL && l.val === 0)
|
|
124
|
-
|
|
125
|
-
if (isNumR && r.val === 0)
|
|
126
|
-
return replaceNode(node, l);
|
|
117
|
+
if (isNumL && l.val === 0) return __replaceNode(node, r);
|
|
118
|
+
if (isNumR && r.val === 0) return __replaceNode(node, l);
|
|
127
119
|
} else if (op === "-") {
|
|
128
|
-
if (isNumL && l.val === 0)
|
|
129
|
-
|
|
130
|
-
if (isNumR && r.val === 0)
|
|
131
|
-
return replaceNode(node, l);
|
|
120
|
+
if (isNumL && l.val === 0) return __replaceNode(node, neg(r));
|
|
121
|
+
if (isNumR && r.val === 0) return __replaceNode(node, l);
|
|
132
122
|
}
|
|
133
123
|
},
|
|
134
124
|
call_i: (node) => {
|
|
@@ -136,7 +126,7 @@ const foldNode = defmulti(
|
|
|
136
126
|
if ($node.args.every((x) => isLitNumericConst(x))) {
|
|
137
127
|
const op = BUILTINS[$node.id];
|
|
138
128
|
if (op !== void 0) {
|
|
139
|
-
return
|
|
129
|
+
return __replaceNumericNode(
|
|
140
130
|
node,
|
|
141
131
|
op($node.args.map((x) => x.val))
|
|
142
132
|
);
|
|
@@ -149,13 +139,13 @@ const foldNode = defmulti(
|
|
|
149
139
|
const $node = node;
|
|
150
140
|
if (isLitNumericConst($node.val)) {
|
|
151
141
|
if (isFloat($node.val)) {
|
|
152
|
-
return
|
|
142
|
+
return __replaceNode(node, float($node.val.val));
|
|
153
143
|
}
|
|
154
144
|
if (isInt($node.val)) {
|
|
155
|
-
return
|
|
145
|
+
return __replaceNode(node, int($node.val.val));
|
|
156
146
|
}
|
|
157
147
|
if (isUint($node.val)) {
|
|
158
|
-
return
|
|
148
|
+
return __replaceNode(node, uint($node.val.val));
|
|
159
149
|
}
|
|
160
150
|
}
|
|
161
151
|
},
|
|
@@ -164,7 +154,7 @@ const foldNode = defmulti(
|
|
|
164
154
|
const val = $node.val;
|
|
165
155
|
if (isLitVecConst(val)) {
|
|
166
156
|
if (isFloat(node)) {
|
|
167
|
-
return
|
|
157
|
+
return __replaceNode(
|
|
168
158
|
node,
|
|
169
159
|
float(val.val[COMPS[$node.id]])
|
|
170
160
|
);
|
|
@@ -181,19 +171,19 @@ const foldBuiltin = defmulti(
|
|
|
181
171
|
exp2: (node) => {
|
|
182
172
|
const a = node.args[0];
|
|
183
173
|
if (isLitNumOrVecConst(a, 0)) {
|
|
184
|
-
return
|
|
174
|
+
return __replaceWithConst(node, a, FLOAT1);
|
|
185
175
|
}
|
|
186
176
|
if (isLitNumOrVecConst(a, 1)) {
|
|
187
|
-
return
|
|
177
|
+
return __replaceWithConst(node, a, FLOAT2);
|
|
188
178
|
}
|
|
189
179
|
},
|
|
190
180
|
pow: (node) => {
|
|
191
181
|
const [a, b] = node.args;
|
|
192
182
|
if (isLitNumOrVecConst(b, 0)) {
|
|
193
|
-
return
|
|
183
|
+
return __replaceWithConst(node, a, FLOAT1);
|
|
194
184
|
}
|
|
195
185
|
if (isLitNumOrVecConst(b, 1)) {
|
|
196
|
-
return
|
|
186
|
+
return __replaceNode(node, a);
|
|
197
187
|
}
|
|
198
188
|
}
|
|
199
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.
|
|
49
|
-
"typedoc": "^0.25.
|
|
50
|
-
"typescript": "^5.
|
|
47
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
48
|
+
"esbuild": "^0.21.5",
|
|
49
|
+
"typedoc": "^0.25.13",
|
|
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
|
}
|