@fraczak/k 8.9.0 → 9.1.0
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 +28 -0
- package/DOCS/ARCHITECTURE.md +5 -4
- package/DOCS/DICTIONARY.md +2 -2
- package/DOCS/FILE_FORMATS.md +11 -9
- package/DOCS/KIR_V1.md +5 -1
- package/DOCS/KTVM_SPEC.md +907 -0
- package/DOCS/KVM_EXECUTION_MODEL.md +3 -2
- package/DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md +127 -0
- package/DOCS/PROFILING_AND_TRACING.md +13 -4
- package/DOCS/README.md +3 -2
- package/DOCS/REPL.md +16 -12
- package/README.md +138 -2
- package/backend-api.mjs +31 -16
- package/backends/llvm/README.md +4 -5
- package/backends/llvm/bin/k-llvm-build.mjs +7 -19
- package/backends/llvm/bin/k-llvm-compile.mjs +6 -18
- package/backends/llvm/bin/k-llvm.mjs +11 -33
- package/backends/llvm/src/cli.mjs +2 -12
- package/backends/llvm/src/executable.mjs +7 -95
- package/backends/llvm/src/kvm2llvm.mjs +1 -7
- package/backends/llvm/src/llvm.mjs +4 -113
- package/backends/llvm/tests/k-llvm-run.mjs +1 -6
- package/backends/llvm/tests/perf-support.mjs +3 -5
- package/backends/llvm/tests/test-llvm.mjs +2 -16
- package/backends/wasm/README.md +7 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -7
- package/backends/wasm/bin/k-wasm.mjs +4 -6
- package/backends/wasm/src/cli.mjs +8 -24
- package/backends/wasm/src/wasm.mjs +60 -57
- package/backends/wasm/tests/test-wasm.mjs +41 -2
- package/browser/repl-browser.mjs +9 -8
- package/browser/shims/buffer.mjs +31 -2
- package/browser/shims/child_process.mjs +26 -0
- package/browser/shims/fs.mjs +3 -1
- package/codecs/runtime/prefix-codec.mjs +67 -1
- package/conformance/run.mjs +17 -2
- package/harness/adapters/ktvm-llvm.mjs +118 -0
- package/harness/adapters/ktvm-wasm.mjs +104 -0
- package/harness/adapters/ktvm.mjs +133 -0
- package/harness/registry.mjs +9 -0
- package/ktvm/ast.mjs +218 -0
- package/ktvm/bit-deque.mjs +497 -0
- package/ktvm/fixtures/integer-streaming.k +477 -0
- package/ktvm/index.mjs +130 -0
- package/ktvm/llvm/index.mjs +202 -0
- package/ktvm/llvm/lower-c.mjs +1258 -0
- package/ktvm/llvm/runtime.h +3542 -0
- package/ktvm/llvm/test-llvm.mjs +476 -0
- package/ktvm/lower-kir.mjs +2221 -0
- package/ktvm/optimize/fuse-kernel.mjs +2111 -0
- package/ktvm/optimize/index.mjs +116 -0
- package/ktvm/optimize/mwtr.mjs +283 -0
- package/ktvm/optimize/simplify-procs.mjs +5250 -0
- package/ktvm/optimize/synthesize-tables.mjs +1105 -0
- package/ktvm/parser.mjs +1101 -0
- package/ktvm/printer.mjs +281 -0
- package/ktvm/schema.mjs +1134 -0
- package/ktvm/specialize.mjs +538 -0
- package/ktvm/test-conformance.mjs +597 -0
- package/ktvm/test-ktvm.mjs +1805 -0
- package/ktvm/test-optimize.mjs +598 -0
- package/ktvm/validator.mjs +1280 -0
- package/ktvm/vm.mjs +2225 -0
- package/ktvm/wasm/index.mjs +331 -0
- package/ktvm/wasm/lower-wasm.mjs +1272 -0
- package/ktvm/wasm/runtime.wat +4404 -0
- package/ktvm/wasm/test-wasm.mjs +429 -0
- package/ktvm/wire.mjs +679 -0
- package/package.json +1 -9
- package/paper/Makefile +8 -5
- package/paper/k-transducer-compiler.pdf +0 -0
- package/paper/k-transducer-compiler.tex +658 -0
- package/paper/replay-diagnostic.mjs +63 -0
- package/repl.mjs +101 -20
- package/scripts/build-repl-html.mjs +40 -18
- package/scripts/perf-bigint-compare.mjs +12 -16
- package/scripts/perf-ieee.mjs +14 -232
- package/scripts/perf-int.mjs +16 -274
- package/scripts/perf-multiplication.mjs +116 -121
- package/scripts/perf-poly.mjs +9 -263
- package/scripts/run-tests.mjs +5 -2
- package/scripts/test-package.mjs +15 -3
- package/tests/README.md +3 -3
- package/tests/test-repl-html.mjs +72 -15
- package/tests/test-repl.mjs +24 -17
- package/DOCS/POLYMORPHIC_IR.md +0 -86
- package/access-table.mjs +0 -343
- package/pkir.mjs +0 -772
- package/tests/test-access-table.mjs +0 -213
- package/tests/test-pkir.mjs +0 -530
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { BitDeque } from "../ktvm/bit-deque.mjs";
|
|
3
|
+
import { parseModule } from "../ktvm/parser.mjs";
|
|
4
|
+
import { validateModule } from "../ktvm/validator.mjs";
|
|
5
|
+
import { KTVMRuntime } from "../ktvm/vm.mjs";
|
|
6
|
+
|
|
7
|
+
const module = parseModule(`
|
|
8
|
+
ktvm 0.1 mono entry @choice;
|
|
9
|
+
schema $bits = union { 0: $bits, 1: $bits, _: unit };
|
|
10
|
+
|
|
11
|
+
proc @choice(%xs: $bits) -> (%out: $bits) {
|
|
12
|
+
spec (%xs) -> (%out) {
|
|
13
|
+
branch 0: { spawn @zeros(%xs) -> (%out); }
|
|
14
|
+
branch 1: { splice %xs -> %out; }
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
proc @zeros(%xs: $bits) -> (%out: $bits) {
|
|
19
|
+
switch (%xs) {
|
|
20
|
+
case (/0): {
|
|
21
|
+
chan %rest: $bits;
|
|
22
|
+
match /0 %xs -> %rest;
|
|
23
|
+
spawn @zeros(%rest) -> (%out);
|
|
24
|
+
}
|
|
25
|
+
default: {
|
|
26
|
+
chan %unit: unit;
|
|
27
|
+
match /_ %xs -> %unit;
|
|
28
|
+
wrap |_ %unit -> %out;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
`);
|
|
33
|
+
validateModule(module);
|
|
34
|
+
|
|
35
|
+
class ReplayDiagnostic extends KTVMRuntime {
|
|
36
|
+
peakReplayBits = 0;
|
|
37
|
+
|
|
38
|
+
_stepTap(node) {
|
|
39
|
+
const demand = super._stepTap(node);
|
|
40
|
+
this.peakReplayBits = Math.max(this.peakReplayBits, node.replay.length);
|
|
41
|
+
return demand;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const digits = 1024;
|
|
46
|
+
for (const oneAtDigit of [1, digits]) {
|
|
47
|
+
const tags = Array(digits).fill(0);
|
|
48
|
+
tags[oneAtDigit - 1] = 1;
|
|
49
|
+
tags.push(2); // The "_" terminator is the third tag in wire order.
|
|
50
|
+
const input = new BitDeque(10000);
|
|
51
|
+
for (const tag of tags) input.pushBits(tag, 2);
|
|
52
|
+
input.close();
|
|
53
|
+
|
|
54
|
+
const vm = new ReplayDiagnostic(module);
|
|
55
|
+
const { status, outputDeques, telemetry } = vm.run(module.entry, [input]);
|
|
56
|
+
assert.equal(status, "OK");
|
|
57
|
+
assert.equal(telemetry.specRollbacks, 1);
|
|
58
|
+
const [output] = outputDeques;
|
|
59
|
+
assert.equal(output.length, 2 * tags.length);
|
|
60
|
+
for (const tag of tags) assert.equal(output.shiftBits(2), tag);
|
|
61
|
+
output.release();
|
|
62
|
+
console.log({ oneAtDigit, rollbacks: telemetry.specRollbacks, peakReplayBits: vm.peakReplayBits });
|
|
63
|
+
}
|
package/repl.mjs
CHANGED
|
@@ -10,6 +10,9 @@ import { argv, exit, stdin, stdout } from "node:process";
|
|
|
10
10
|
import { annotate, parse } from "./index.mjs";
|
|
11
11
|
import run, { constrainWithPattern, run_rel } from "./run.mjs";
|
|
12
12
|
import { compileWasmArtifactFromObject, instantiateWasmArtifact } from "./backends/wasm/src/wasm.mjs";
|
|
13
|
+
import { compileObjectToKTVMWasm, instantiateKTVMWasm } from "./ktvm/wasm/index.mjs";
|
|
14
|
+
import { compareLabels, isClosedPlist } from "./ktvm/schema.mjs";
|
|
15
|
+
import { encodeValueToEnvelopedWire } from "./ktvm/wire.mjs";
|
|
13
16
|
import { exportPatternGraph } from "./codecs/runtime/codec.mjs";
|
|
14
17
|
import { patternToPropertyList } from "./codecs/runtime/pattern-json.mjs";
|
|
15
18
|
import codes from "./codes.mjs";
|
|
@@ -61,11 +64,13 @@ import {
|
|
|
61
64
|
} from "./repl-codecs.mjs";
|
|
62
65
|
|
|
63
66
|
const NAME_RE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;
|
|
67
|
+
const VALID_ENGINES = ["wasm", "ktvm", "js"];
|
|
68
|
+
const VALID_ENGINES_TEXT = VALID_ENGINES.join(", ");
|
|
64
69
|
const COMMAND_NAMES = [
|
|
65
70
|
"help", "rel", "pattern", "pat", "patterns", "pats",
|
|
66
71
|
"all-patterns", "all-pat",
|
|
67
72
|
"codecs", "rels", "codec", "input", "reset", "klib", "ko", "load",
|
|
68
|
-
"engine", "wasm", "
|
|
73
|
+
"engine", "wasm", "ktvm", "js", "limit",
|
|
69
74
|
"max-pat-length", "maxPatLength",
|
|
70
75
|
"quit", "exit"
|
|
71
76
|
];
|
|
@@ -92,9 +97,9 @@ function emptyValue() {
|
|
|
92
97
|
function createState(options = {}) {
|
|
93
98
|
codes.load(cloneJSON(initialCodes));
|
|
94
99
|
const engine = (options.engine || "wasm").toLowerCase();
|
|
95
|
-
if (engine
|
|
100
|
+
if (!VALID_ENGINES.includes(engine)) {
|
|
96
101
|
throw new Error(
|
|
97
|
-
`Invalid engine '${options.engine}'. Valid engines:
|
|
102
|
+
`Invalid engine '${options.engine}'. Valid engines: ${VALID_ENGINES_TEXT}`
|
|
98
103
|
);
|
|
99
104
|
}
|
|
100
105
|
return {
|
|
@@ -1256,7 +1261,7 @@ function completeCommandArgument(line, state) {
|
|
|
1256
1261
|
}
|
|
1257
1262
|
|
|
1258
1263
|
if (command === "engine") {
|
|
1259
|
-
return completeCommandWord(line, argStart, arg,
|
|
1264
|
+
return completeCommandWord(line, argStart, arg, VALID_ENGINES);
|
|
1260
1265
|
}
|
|
1261
1266
|
|
|
1262
1267
|
if (command === "input") {
|
|
@@ -1545,8 +1550,7 @@ async function executeExpressionWithWasm(annotated, state, lineOffset = 0) {
|
|
|
1545
1550
|
|
|
1546
1551
|
const wasmCompileStart = performance.now();
|
|
1547
1552
|
const wasmBytes = await compileWasmArtifactFromObject(obj, {
|
|
1548
|
-
inputEnvelopePattern: inputValue?.pattern
|
|
1549
|
-
usePKIR: state.engine === "pkir" || state.usePKIR
|
|
1553
|
+
inputEnvelopePattern: inputValue?.pattern
|
|
1550
1554
|
});
|
|
1551
1555
|
const runner = await instantiateWasmArtifact(wasmBytes);
|
|
1552
1556
|
const wasmCompileMs = performance.now() - wasmCompileStart;
|
|
@@ -1558,6 +1562,81 @@ async function executeExpressionWithWasm(annotated, state, lineOffset = 0) {
|
|
|
1558
1562
|
return { result, wasmCompileMs, executeMs };
|
|
1559
1563
|
}
|
|
1560
1564
|
|
|
1565
|
+
function deriveClosedPlistFromValue(val) {
|
|
1566
|
+
const nodes = [];
|
|
1567
|
+
function visit(v) {
|
|
1568
|
+
const id = nodes.length;
|
|
1569
|
+
if (isVariant(v)) {
|
|
1570
|
+
const slot = ["closed-union", []];
|
|
1571
|
+
nodes.push(slot);
|
|
1572
|
+
const childId = visit(v.value);
|
|
1573
|
+
slot[1].push([v.tag, childId]);
|
|
1574
|
+
return id;
|
|
1575
|
+
}
|
|
1576
|
+
const slot = ["closed-product", []];
|
|
1577
|
+
nodes.push(slot);
|
|
1578
|
+
const prod = isProduct(v) ? v.product : {};
|
|
1579
|
+
const keys = Object.keys(prod).sort(compareLabels);
|
|
1580
|
+
for (const k of keys) {
|
|
1581
|
+
const childId = visit(prod[k]);
|
|
1582
|
+
slot[1].push([k, childId]);
|
|
1583
|
+
}
|
|
1584
|
+
return id;
|
|
1585
|
+
}
|
|
1586
|
+
visit(val);
|
|
1587
|
+
return nodes;
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
async function executeExpressionWithKTVM(annotated, state, lineOffset = 0) {
|
|
1591
|
+
const mainRel = annotated.rels.__main__;
|
|
1592
|
+
if (!mainRel) return { result: undefined, wasmCompileMs: 0, executeMs: 0 };
|
|
1593
|
+
|
|
1594
|
+
let inputValue = state.value ?? emptyValue();
|
|
1595
|
+
if (mainRel.typePatternGraph && mainRel.def.patterns) {
|
|
1596
|
+
const graph = mainRel.typePatternGraph;
|
|
1597
|
+
const nodeId = graph.find(mainRel.def.patterns[0]);
|
|
1598
|
+
const inputPattern = patternToPropertyList(exportPatternGraph(graph, nodeId));
|
|
1599
|
+
inputValue = constrainWithPattern(inputValue, inputPattern, mainRel.def);
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
const obj = {
|
|
1603
|
+
format: "k-object",
|
|
1604
|
+
codes: { ...state.codes, ...codes.dump() },
|
|
1605
|
+
rels: annotated.rels,
|
|
1606
|
+
relAlias: annotated.relAlias,
|
|
1607
|
+
compileStats: annotated.compileStats,
|
|
1608
|
+
main: "__main__"
|
|
1609
|
+
};
|
|
1610
|
+
|
|
1611
|
+
let inputPlist = inputValue?.pattern;
|
|
1612
|
+
if (!inputPlist || !isClosedPlist(inputPlist)) {
|
|
1613
|
+
inputPlist = deriveClosedPlistFromValue(inputValue);
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
const wasmCompileStart = performance.now();
|
|
1617
|
+
const artifact = await compileObjectToKTVMWasm(obj, "__main__", inputPlist, {
|
|
1618
|
+
optimize: true,
|
|
1619
|
+
validate: true,
|
|
1620
|
+
verifyTables: true
|
|
1621
|
+
});
|
|
1622
|
+
const runner = await instantiateKTVMWasm(artifact, {
|
|
1623
|
+
quota: {
|
|
1624
|
+
maxSteps: 1_000_000_000,
|
|
1625
|
+
maxNodes: 2_000_000,
|
|
1626
|
+
maxBitDequeBytes: 256 * 1024 * 1024
|
|
1627
|
+
}
|
|
1628
|
+
});
|
|
1629
|
+
const wasmCompileMs = performance.now() - wasmCompileStart;
|
|
1630
|
+
|
|
1631
|
+
const inputWire = encodeValueToEnvelopedWire(inputValue, inputPlist);
|
|
1632
|
+
const executeStart = performance.now();
|
|
1633
|
+
const exec = runner.executeWire(inputWire, { decodeValue: true });
|
|
1634
|
+
const executeMs = performance.now() - executeStart;
|
|
1635
|
+
|
|
1636
|
+
const result = exec.status === "OK" ? exec.value : undefined;
|
|
1637
|
+
return { result, wasmCompileMs, executeMs };
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1561
1640
|
function executeExpressionWithJs(annotated, state, lineOffset = 0) {
|
|
1562
1641
|
const mainRel = annotated.rels.__main__;
|
|
1563
1642
|
if (!mainRel) return { result: undefined, wasmCompileMs: 0, executeMs: 0 };
|
|
@@ -1609,6 +1688,8 @@ async function runSnippet(input, state, options = {}) {
|
|
|
1609
1688
|
try {
|
|
1610
1689
|
if (state.engine === "js") {
|
|
1611
1690
|
execRes = executeExpressionWithJs(annotated, state, lineOffset);
|
|
1691
|
+
} else if (state.engine === "ktvm") {
|
|
1692
|
+
execRes = await executeExpressionWithKTVM(annotated, state, lineOffset);
|
|
1612
1693
|
} else {
|
|
1613
1694
|
execRes = await executeExpressionWithWasm(annotated, state, lineOffset);
|
|
1614
1695
|
}
|
|
@@ -1865,9 +1946,9 @@ async function evaluateCommand(line, state) {
|
|
|
1865
1946
|
if (!mode) {
|
|
1866
1947
|
return [`Current engine: ${state.engine}`];
|
|
1867
1948
|
}
|
|
1868
|
-
if (mode
|
|
1949
|
+
if (!VALID_ENGINES.includes(mode)) {
|
|
1869
1950
|
throw new Error(
|
|
1870
|
-
`Invalid engine '${arg.trim()}'. Valid engines:
|
|
1951
|
+
`Invalid engine '${arg.trim()}'. Valid engines: ${VALID_ENGINES_TEXT}`
|
|
1871
1952
|
);
|
|
1872
1953
|
}
|
|
1873
1954
|
state.engine = mode;
|
|
@@ -1877,9 +1958,9 @@ async function evaluateCommand(line, state) {
|
|
|
1877
1958
|
state.engine = "wasm";
|
|
1878
1959
|
return ["engine set to wasm"];
|
|
1879
1960
|
}
|
|
1880
|
-
case "
|
|
1881
|
-
state.engine = "
|
|
1882
|
-
return ["engine set to
|
|
1961
|
+
case "ktvm": {
|
|
1962
|
+
state.engine = "ktvm";
|
|
1963
|
+
return ["engine set to ktvm"];
|
|
1883
1964
|
}
|
|
1884
1965
|
case "js": {
|
|
1885
1966
|
state.engine = "js";
|
|
@@ -1969,7 +2050,7 @@ async function evaluateCommand(line, state) {
|
|
|
1969
2050
|
|
|
1970
2051
|
function helpText() {
|
|
1971
2052
|
return [
|
|
1972
|
-
":engine [wasm|
|
|
2053
|
+
":engine [wasm|ktvm|js] display or switch evaluation engine",
|
|
1973
2054
|
":rel name show relation definition",
|
|
1974
2055
|
":rels list relation aliases",
|
|
1975
2056
|
":pattern name show named pattern definition (alias :pat)",
|
|
@@ -2008,9 +2089,9 @@ function cliUsage() {
|
|
|
2008
2089
|
console.log("Usage: k-repl [options]");
|
|
2009
2090
|
console.log("");
|
|
2010
2091
|
console.log("Options:");
|
|
2011
|
-
console.log(" --engine=<wasm|
|
|
2092
|
+
console.log(" --engine=<wasm|ktvm|js> Select engine: wasm, ktvm, js");
|
|
2012
2093
|
console.log(" (default: wasm).");
|
|
2013
|
-
console.log(" --
|
|
2094
|
+
console.log(" --ktvm Shortcut for --engine=ktvm.");
|
|
2014
2095
|
console.log(" --wasm Shortcut for --engine=wasm.");
|
|
2015
2096
|
console.log(" --js Shortcut for --engine=js.");
|
|
2016
2097
|
console.log(" --limit=<num> Set maximum characters for output.");
|
|
@@ -2122,13 +2203,13 @@ if (isMainEntrypoint()) {
|
|
|
2122
2203
|
engine = "js";
|
|
2123
2204
|
} else if (arg === "--wasm") {
|
|
2124
2205
|
engine = "wasm";
|
|
2125
|
-
} else if (arg === "--
|
|
2126
|
-
engine = "
|
|
2206
|
+
} else if (arg === "--ktvm") {
|
|
2207
|
+
engine = "ktvm";
|
|
2127
2208
|
} else if (arg.startsWith("--engine=")) {
|
|
2128
2209
|
const val = arg.slice("--engine=".length).toLowerCase();
|
|
2129
|
-
if (val
|
|
2210
|
+
if (!VALID_ENGINES.includes(val)) {
|
|
2130
2211
|
console.error(
|
|
2131
|
-
`Invalid engine '${val}'. Valid engines:
|
|
2212
|
+
`Invalid engine '${val}'. Valid engines: ${VALID_ENGINES_TEXT}`
|
|
2132
2213
|
);
|
|
2133
2214
|
exit(1);
|
|
2134
2215
|
}
|
|
@@ -2136,9 +2217,9 @@ if (isMainEntrypoint()) {
|
|
|
2136
2217
|
} else if (arg === "--engine") {
|
|
2137
2218
|
i++;
|
|
2138
2219
|
const val = (args[i] || "").toLowerCase();
|
|
2139
|
-
if (val
|
|
2220
|
+
if (!VALID_ENGINES.includes(val)) {
|
|
2140
2221
|
console.error(
|
|
2141
|
-
`Invalid engine '${val}'. Valid engines:
|
|
2222
|
+
`Invalid engine '${val}'. Valid engines: ${VALID_ENGINES_TEXT}`
|
|
2142
2223
|
);
|
|
2143
2224
|
exit(1);
|
|
2144
2225
|
}
|
|
@@ -52,9 +52,11 @@ const nodeShimsPlugin = {
|
|
|
52
52
|
"node:url": path.join(projectRoot, "browser/shims/url.mjs"),
|
|
53
53
|
"assert": path.join(projectRoot, "browser/shims/assert.mjs"),
|
|
54
54
|
"node:assert": path.join(projectRoot, "browser/shims/assert.mjs"),
|
|
55
|
+
"child_process": path.join(projectRoot, "browser/shims/child_process.mjs"),
|
|
56
|
+
"node:child_process": path.join(projectRoot, "browser/shims/child_process.mjs"),
|
|
55
57
|
};
|
|
56
58
|
|
|
57
|
-
build.onResolve({ filter: /^(node:)?(fs|path|crypto|buffer|os|process|readline|url|assert)$/ }, (args) => {
|
|
59
|
+
build.onResolve({ filter: /^(node:)?(fs|path|crypto|buffer|os|process|readline|url|assert|child_process)$/ }, (args) => {
|
|
58
60
|
if (shims[args.path]) {
|
|
59
61
|
return { path: shims[args.path] };
|
|
60
62
|
}
|
|
@@ -66,11 +68,15 @@ const nodeShimsPlugin = {
|
|
|
66
68
|
console.log("1. Generating VFS data...");
|
|
67
69
|
const vfsFiles = {};
|
|
68
70
|
|
|
69
|
-
// Add runtime.wat
|
|
71
|
+
// Add runtime.wat (Wasm & kTVM Wasm)
|
|
70
72
|
const runtimeWatPath = path.join(projectRoot, "backends/wasm/runtime.wat");
|
|
71
73
|
if (fs.existsSync(runtimeWatPath)) {
|
|
72
74
|
vfsFiles["backends/wasm/runtime.wat"] = fs.readFileSync(runtimeWatPath, "utf8");
|
|
73
75
|
}
|
|
76
|
+
const ktvmRuntimeWatPath = path.join(projectRoot, "ktvm/wasm/runtime.wat");
|
|
77
|
+
if (fs.existsSync(ktvmRuntimeWatPath)) {
|
|
78
|
+
vfsFiles["ktvm/wasm/runtime.wat"] = fs.readFileSync(ktvmRuntimeWatPath, "utf8");
|
|
79
|
+
}
|
|
74
80
|
|
|
75
81
|
// Add core.k
|
|
76
82
|
const coreKPath = path.join(projectRoot, "core.k");
|
|
@@ -285,44 +291,60 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
285
291
|
letter-spacing: 0.5px;
|
|
286
292
|
}
|
|
287
293
|
|
|
288
|
-
.engine-badge
|
|
294
|
+
.engine-badge,
|
|
295
|
+
button.engine-badge {
|
|
296
|
+
font-family: inherit;
|
|
289
297
|
font-size: 11px;
|
|
290
298
|
font-weight: 600;
|
|
299
|
+
line-height: 1.25;
|
|
291
300
|
background: rgba(88, 166, 255, 0.15);
|
|
292
301
|
color: var(--color-prompt);
|
|
293
302
|
border: 1px solid rgba(88, 166, 255, 0.3);
|
|
294
|
-
padding:
|
|
303
|
+
padding: 3px 10px;
|
|
304
|
+
min-block-size: 24px;
|
|
295
305
|
border-radius: 12px;
|
|
306
|
+
display: inline-flex;
|
|
307
|
+
align-items: center;
|
|
296
308
|
cursor: pointer;
|
|
297
309
|
user-select: none;
|
|
298
|
-
transition:
|
|
310
|
+
transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
|
|
299
311
|
}
|
|
300
312
|
|
|
301
|
-
.engine-badge:hover
|
|
313
|
+
.engine-badge:hover,
|
|
314
|
+
button.engine-badge:hover {
|
|
302
315
|
background: rgba(88, 166, 255, 0.3);
|
|
303
316
|
border-color: rgba(88, 166, 255, 0.6);
|
|
304
317
|
}
|
|
305
318
|
|
|
306
|
-
.engine-badge
|
|
319
|
+
button.engine-badge:focus-visible {
|
|
320
|
+
outline: 2px solid var(--border-focus);
|
|
321
|
+
outline-offset: 2px;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.engine-badge.js-mode,
|
|
325
|
+
button.engine-badge.js-mode {
|
|
307
326
|
background: rgba(240, 180, 41, 0.15);
|
|
308
327
|
color: #e3b341;
|
|
309
328
|
border-color: rgba(240, 180, 41, 0.4);
|
|
310
329
|
}
|
|
311
330
|
|
|
312
|
-
.engine-badge.js-mode:hover
|
|
331
|
+
.engine-badge.js-mode:hover,
|
|
332
|
+
button.engine-badge.js-mode:hover {
|
|
313
333
|
background: rgba(240, 180, 41, 0.3);
|
|
314
334
|
border-color: rgba(240, 180, 41, 0.7);
|
|
315
335
|
}
|
|
316
336
|
|
|
317
|
-
.engine-badge.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
337
|
+
.engine-badge.ktvm-mode,
|
|
338
|
+
button.engine-badge.ktvm-mode {
|
|
339
|
+
background: rgba(57, 197, 207, 0.16);
|
|
340
|
+
color: #56d4dd;
|
|
341
|
+
border-color: rgba(57, 197, 207, 0.45);
|
|
321
342
|
}
|
|
322
343
|
|
|
323
|
-
.engine-badge.
|
|
324
|
-
|
|
325
|
-
|
|
344
|
+
.engine-badge.ktvm-mode:hover,
|
|
345
|
+
button.engine-badge.ktvm-mode:hover {
|
|
346
|
+
background: rgba(57, 197, 207, 0.3);
|
|
347
|
+
border-color: rgba(57, 197, 207, 0.75);
|
|
326
348
|
}
|
|
327
349
|
|
|
328
350
|
.status-indicator {
|
|
@@ -1691,7 +1713,7 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1691
1713
|
<div class="nav-left">
|
|
1692
1714
|
<div class="brand">
|
|
1693
1715
|
<span class="brand-name">k repl</span>
|
|
1694
|
-
<
|
|
1716
|
+
<button type="button" id="engine-badge" class="engine-badge" aria-label="wasm-in-process — switch evaluation engine" title="Click to switch engine (wasm/ktvm/js)">wasm-in-process</button>
|
|
1695
1717
|
</div>
|
|
1696
1718
|
<div class="status-indicator">
|
|
1697
1719
|
<span id="status-dot" class="status-dot ready"></span>
|
|
@@ -1726,7 +1748,7 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1726
1748
|
<option value="expr::rels">:rels (list relations)</option>
|
|
1727
1749
|
<option value="expr::engine">:engine (show engine)</option>
|
|
1728
1750
|
<option value="expr::engine wasm">:engine wasm</option>
|
|
1729
|
-
<option value="expr::engine
|
|
1751
|
+
<option value="expr::engine ktvm">:engine ktvm</option>
|
|
1730
1752
|
<option value="expr::engine js">:engine js</option>
|
|
1731
1753
|
</optgroup>
|
|
1732
1754
|
<optgroup label="Codecs & Format Adapters">
|
|
@@ -1940,7 +1962,7 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1940
1962
|
<div class="help-section">
|
|
1941
1963
|
<h4>REPL Commands</h4>
|
|
1942
1964
|
<table class="help-table">
|
|
1943
|
-
<tr><td><code>:engine [wasm|
|
|
1965
|
+
<tr><td><code>:engine [wasm|ktvm|js]</code></td><td>Display or switch evaluation engine (<code>wasm</code>, <code>ktvm</code>, or <code>js</code>; shortcuts: <code>:wasm</code>, <code>:ktvm</code>, <code>:js</code>)</td></tr>
|
|
1944
1966
|
<tr><td><code>:load [--no-alias] <file></code></td><td>Load a <code>.k</code> source or <code>.klib</code> library into current state (use <code>--no-alias</code> to import by hash only)</td></tr>
|
|
1945
1967
|
<tr>
|
|
1946
1968
|
<td><code>:pattern <name></code></td>
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Compares the full UTF-8 decimal string -> parse -> N squarings -> decimal print
|
|
7
7
|
* pipeline between JavaScript BigInt and K (lib/integer.k) across backends:
|
|
8
|
-
* llvm,
|
|
8
|
+
* llvm, wasm, kvm, js
|
|
9
9
|
*
|
|
10
10
|
* Usage:
|
|
11
11
|
* node scripts/perf-bigint-compare.mjs [options]
|
|
@@ -55,8 +55,8 @@ JS BigInt against K (lib/integer.k) across execution backends.
|
|
|
55
55
|
Options:
|
|
56
56
|
--input <decimal> Starting decimal integer (default: 987654321)
|
|
57
57
|
--steps <n> Number of consecutive squarings (default: 10)
|
|
58
|
-
--backends <csv> K backends: llvm,
|
|
59
|
-
(default: llvm,wasm
|
|
58
|
+
--backends <csv> K backends: llvm,wasm,kvm,js
|
|
59
|
+
(default: llvm,wasm)
|
|
60
60
|
--iters <n> Measured iterations per K backend (default: 3)
|
|
61
61
|
-h, --help Show this help message and exit`);
|
|
62
62
|
process.exit(0);
|
|
@@ -65,7 +65,7 @@ Options:
|
|
|
65
65
|
const INPUT = getArg("--input", "987654321");
|
|
66
66
|
const STEPS = Number.parseInt(getArg("--steps", "10"), 10);
|
|
67
67
|
const ITERS = Number.parseInt(getArg("--iters", "3"), 10);
|
|
68
|
-
const BACKENDS = getArg("--backends", "llvm,wasm
|
|
68
|
+
const BACKENDS = getArg("--backends", "llvm,wasm")
|
|
69
69
|
.split(",")
|
|
70
70
|
.map((b) => b.trim().toLowerCase())
|
|
71
71
|
.filter(Boolean);
|
|
@@ -202,7 +202,7 @@ async function benchWireServer(name, server, iters) {
|
|
|
202
202
|
let prepared = null;
|
|
203
203
|
let preparedState = null;
|
|
204
204
|
let cacheDir = null;
|
|
205
|
-
if (BACKENDS.some((b) => b === "llvm" || b === "
|
|
205
|
+
if (BACKENDS.some((b) => b === "llvm" || b === "kvm")) {
|
|
206
206
|
preparedState = createState();
|
|
207
207
|
await evaluateInput(`:load ${path.join(root, "lib/integer.k")}`, preparedState);
|
|
208
208
|
await evaluateInput("sq = ?int[] { () x, () y } times ?int[];", preparedState);
|
|
@@ -214,14 +214,12 @@ if (BACKENDS.some((b) => b === "llvm" || b === "llvm-pkir" || b === "kvm")) {
|
|
|
214
214
|
|
|
215
215
|
for (const backend of BACKENDS) {
|
|
216
216
|
try {
|
|
217
|
-
if (backend === "llvm"
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
const exe = path.join(cacheDir, `sqN_${backend.replace("-", "_")}`);
|
|
217
|
+
if (backend === "llvm") {
|
|
218
|
+
const label = "K LLVM (-O3)";
|
|
219
|
+
const exe = path.join(cacheDir, `sqN_${backend}`);
|
|
221
220
|
compileObjectToExecutable(prepared.object, exe, {
|
|
222
221
|
clangOpt: "-O3",
|
|
223
|
-
inputPattern: intPattern
|
|
224
|
-
usePKIR
|
|
222
|
+
inputPattern: intPattern
|
|
225
223
|
});
|
|
226
224
|
const srv = new PersistentExecutable(exe, {
|
|
227
225
|
env: { ...process.env, K_ARENA_CAPACITY_MB: "4096" }
|
|
@@ -231,12 +229,10 @@ for (const backend of BACKENDS) {
|
|
|
231
229
|
} finally {
|
|
232
230
|
await srv.close();
|
|
233
231
|
}
|
|
234
|
-
} else if (backend === "wasm"
|
|
235
|
-
const
|
|
236
|
-
const label = usePKIR ? "K PKIR-Wasm" : "K Wasm";
|
|
232
|
+
} else if (backend === "wasm") {
|
|
233
|
+
const label = "K Wasm";
|
|
237
234
|
const artifact = await compileWasmArtifact(sqNAlias, {
|
|
238
|
-
libraries: [intLib]
|
|
239
|
-
usePKIR
|
|
235
|
+
libraries: [intLib]
|
|
240
236
|
});
|
|
241
237
|
const inst = await instantiateWasmArtifact(artifact);
|
|
242
238
|
results.push(
|