@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,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kTVM Native C99 / LLVM Backend API (`k/ktvm/llvm/index.mjs`).
|
|
3
|
+
*
|
|
4
|
+
* Compiles validated concrete `mono` kTVM modules to standalone native binaries
|
|
5
|
+
* via C99 + `clang -O3` (LLVM). Supports `--server` persistent pipe execution,
|
|
6
|
+
* `--bench N` microbenchmarking, and standalone `stdin -> stdout` CLI mode.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import os from "node:os";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
13
|
+
|
|
14
|
+
import { compileKTVMToC } from "./lower-c.mjs";
|
|
15
|
+
import { validateModule } from "../validator.mjs";
|
|
16
|
+
import { specializeKTVM } from "../specialize.mjs";
|
|
17
|
+
import { optimizeKTVM } from "../optimize/index.mjs";
|
|
18
|
+
import { compileKIRToKTVM } from "../lower-kir.mjs";
|
|
19
|
+
|
|
20
|
+
export { compileKTVMToC } from "./lower-c.mjs";
|
|
21
|
+
|
|
22
|
+
let cachedClangAvailable = null;
|
|
23
|
+
|
|
24
|
+
export function isClangAvailable(compiler = "clang") {
|
|
25
|
+
if (compiler === "clang" && cachedClangAvailable !== null) return cachedClangAvailable;
|
|
26
|
+
const res = spawnSync("which", [compiler], { stdio: "ignore" });
|
|
27
|
+
const ok = res.status === 0;
|
|
28
|
+
if (compiler === "clang") cachedClangAvailable = ok;
|
|
29
|
+
return ok;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Compile generated C99 source to a native executable using `clang` (LLVM).
|
|
34
|
+
*/
|
|
35
|
+
export function compileCToExecutable(
|
|
36
|
+
cSource,
|
|
37
|
+
exePath,
|
|
38
|
+
{
|
|
39
|
+
compiler = "clang",
|
|
40
|
+
clangOpt = "-O3",
|
|
41
|
+
keepCSource = false
|
|
42
|
+
} = {}
|
|
43
|
+
) {
|
|
44
|
+
const dir = path.dirname(exePath);
|
|
45
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
46
|
+
const cPath = keepCSource
|
|
47
|
+
? `${exePath}.c`
|
|
48
|
+
: path.join(os.tmpdir(), `ktvm_${process.pid}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}.c`);
|
|
49
|
+
|
|
50
|
+
fs.writeFileSync(cPath, cSource, "utf8");
|
|
51
|
+
try {
|
|
52
|
+
const args = [
|
|
53
|
+
clangOpt,
|
|
54
|
+
"-std=c99",
|
|
55
|
+
"-Wno-unused-function",
|
|
56
|
+
"-Wno-unused-label",
|
|
57
|
+
cPath,
|
|
58
|
+
"-o",
|
|
59
|
+
exePath
|
|
60
|
+
];
|
|
61
|
+
execFileSync(compiler, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
62
|
+
return { exePath, cPath: keepCSource ? cPath : null };
|
|
63
|
+
} catch (err) {
|
|
64
|
+
const stderr = err.stderr ? err.stderr.toString("utf8") : err.message;
|
|
65
|
+
throw new Error(`kTVM clang compilation failed:\n${stderr}`);
|
|
66
|
+
} finally {
|
|
67
|
+
if (!keepCSource) {
|
|
68
|
+
try { fs.unlinkSync(cPath); } catch {}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Compile a concrete `mono` (or polymorphic `poly` with `inputPattern`) kTVM
|
|
75
|
+
* module directly to a native executable.
|
|
76
|
+
*/
|
|
77
|
+
export function compileKTVMToExecutable(
|
|
78
|
+
ktvmModule,
|
|
79
|
+
exePath,
|
|
80
|
+
{
|
|
81
|
+
entry = null,
|
|
82
|
+
inputPattern = null,
|
|
83
|
+
optimize = true,
|
|
84
|
+
optimizeOptions = {},
|
|
85
|
+
validate = true,
|
|
86
|
+
verifyTables = true,
|
|
87
|
+
compiler = "clang",
|
|
88
|
+
clangOpt = "-O3",
|
|
89
|
+
keepCSource = false
|
|
90
|
+
} = {}
|
|
91
|
+
) {
|
|
92
|
+
let monoMod = ktvmModule;
|
|
93
|
+
if (monoMod.layer === "poly") {
|
|
94
|
+
if (!inputPattern) {
|
|
95
|
+
throw new Error("compileKTVMToExecutable requires `inputPattern` for polymorphic 'poly' modules");
|
|
96
|
+
}
|
|
97
|
+
monoMod = specializeKTVM(monoMod, inputPattern, { entry });
|
|
98
|
+
}
|
|
99
|
+
if (optimize) {
|
|
100
|
+
monoMod = optimizeKTVM(monoMod, {
|
|
101
|
+
...optimizeOptions,
|
|
102
|
+
validate,
|
|
103
|
+
verifyTables
|
|
104
|
+
}).module;
|
|
105
|
+
} else if (validate) {
|
|
106
|
+
validateModule(monoMod, { verifyTables });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const acceptedInputPlists = inputPattern ? [inputPattern] : [];
|
|
110
|
+
const lowered = compileKTVMToC(monoMod, {
|
|
111
|
+
entry: entry || monoMod.entry,
|
|
112
|
+
validate: false,
|
|
113
|
+
acceptedInputPlists
|
|
114
|
+
});
|
|
115
|
+
compileCToExecutable(lowered.cSource, exePath, { compiler, clangOpt, keepCSource });
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
exePath,
|
|
119
|
+
entry: lowered.entry,
|
|
120
|
+
ktvmModule: monoMod,
|
|
121
|
+
inPlist: lowered.inPlist,
|
|
122
|
+
outPlist: lowered.outPlist,
|
|
123
|
+
stats: lowered.stats
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Full end-to-end compilation from a K object/library (`lib` / `k-object`) +
|
|
129
|
+
* relation name + concrete input pattern to a native LLVM executable!
|
|
130
|
+
*/
|
|
131
|
+
export function compileObjectToKTVMExecutable(
|
|
132
|
+
objectOrLib,
|
|
133
|
+
relationName,
|
|
134
|
+
inputPattern,
|
|
135
|
+
exePath,
|
|
136
|
+
{
|
|
137
|
+
optimize = true,
|
|
138
|
+
optimizeOptions = {},
|
|
139
|
+
validate = true,
|
|
140
|
+
verifyTables = true,
|
|
141
|
+
compiler = "clang",
|
|
142
|
+
clangOpt = "-O3",
|
|
143
|
+
keepCSource = false
|
|
144
|
+
} = {}
|
|
145
|
+
) {
|
|
146
|
+
const compiled = compileKIRToKTVM(objectOrLib, relationName, inputPattern, { validate: false });
|
|
147
|
+
return compileKTVMToExecutable(compiled.module, exePath, {
|
|
148
|
+
entry: compiled.entry,
|
|
149
|
+
inputPattern,
|
|
150
|
+
optimize,
|
|
151
|
+
optimizeOptions,
|
|
152
|
+
validate,
|
|
153
|
+
verifyTables,
|
|
154
|
+
compiler,
|
|
155
|
+
clangOpt,
|
|
156
|
+
keepCSource
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Execute a compiled kTVM native executable once on a raw `k-wire` buffer
|
|
162
|
+
* (CLI mode: stdin -> stdout), optionally returning parsed telemetry.
|
|
163
|
+
*/
|
|
164
|
+
export function executeKTVMExecutable(
|
|
165
|
+
exePath,
|
|
166
|
+
inputWire,
|
|
167
|
+
{
|
|
168
|
+
telemetry = false,
|
|
169
|
+
quota = null
|
|
170
|
+
} = {}
|
|
171
|
+
) {
|
|
172
|
+
const args = [];
|
|
173
|
+
if (telemetry) args.push("--telemetry");
|
|
174
|
+
if (quota?.maxSteps != null) args.push("--max-steps", String(quota.maxSteps));
|
|
175
|
+
if (quota?.maxNodes != null) args.push("--max-nodes", String(quota.maxNodes));
|
|
176
|
+
if (quota?.maxBitDequeBytes != null) args.push("--max-bytes", String(quota.maxBitDequeBytes));
|
|
177
|
+
|
|
178
|
+
const res = spawnSync(exePath, args, { input: Buffer.from(inputWire) });
|
|
179
|
+
const stderr = res.stderr ? res.stderr.toString("utf8") : "";
|
|
180
|
+
let parsedTelemetry = null;
|
|
181
|
+
const tm = stderr.match(/KTVM_TELEMETRY\s+(.+)/);
|
|
182
|
+
if (tm) {
|
|
183
|
+
parsedTelemetry = {};
|
|
184
|
+
for (const part of tm[1].trim().split(/\s+/)) {
|
|
185
|
+
const [k, v] = part.split("=");
|
|
186
|
+
if (k && v != null) {
|
|
187
|
+
const camel = k.replace(/_([a-z])/g, (_, ch) => ch.toUpperCase());
|
|
188
|
+
parsedTelemetry[camel] = Number(v);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const em = stderr.match(/KTVM_ERROR code=(-?\d+)/);
|
|
193
|
+
const errorCode = em ? Number(em[1]) : 0;
|
|
194
|
+
|
|
195
|
+
if (res.status === 0) {
|
|
196
|
+
return { status: "OK", wire: res.stdout, errorCode: 0, telemetry: parsedTelemetry };
|
|
197
|
+
}
|
|
198
|
+
if (res.status === 1 && errorCode === -1) {
|
|
199
|
+
return { status: "FAILED", wire: null, errorCode: -1, telemetry: parsedTelemetry };
|
|
200
|
+
}
|
|
201
|
+
return { status: "ERROR", wire: null, errorCode, stderr: stderr.trim(), telemetry: parsedTelemetry };
|
|
202
|
+
}
|