@fraczak/k 8.9.0 → 9.0.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/ARCHITECTURE.md +5 -4
  3. package/DOCS/DICTIONARY.md +2 -2
  4. package/DOCS/FILE_FORMATS.md +11 -9
  5. package/DOCS/KIR_V1.md +5 -1
  6. package/DOCS/KTVM_SPEC.md +902 -0
  7. package/DOCS/KVM_EXECUTION_MODEL.md +3 -2
  8. package/DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md +127 -0
  9. package/DOCS/PROFILING_AND_TRACING.md +6 -4
  10. package/DOCS/README.md +3 -2
  11. package/DOCS/REPL.md +16 -12
  12. package/README.md +132 -2
  13. package/backend-api.mjs +31 -16
  14. package/backends/llvm/README.md +4 -5
  15. package/backends/llvm/bin/k-llvm-build.mjs +7 -19
  16. package/backends/llvm/bin/k-llvm-compile.mjs +6 -18
  17. package/backends/llvm/bin/k-llvm.mjs +11 -33
  18. package/backends/llvm/src/cli.mjs +2 -12
  19. package/backends/llvm/src/executable.mjs +7 -95
  20. package/backends/llvm/src/kvm2llvm.mjs +1 -5
  21. package/backends/llvm/src/llvm.mjs +4 -111
  22. package/backends/llvm/tests/k-llvm-run.mjs +1 -6
  23. package/backends/llvm/tests/perf-support.mjs +3 -5
  24. package/backends/llvm/tests/test-llvm.mjs +0 -16
  25. package/backends/wasm/README.md +2 -3
  26. package/backends/wasm/bin/k-wasm-compile.mjs +4 -7
  27. package/backends/wasm/bin/k-wasm.mjs +4 -6
  28. package/backends/wasm/src/cli.mjs +8 -24
  29. package/backends/wasm/src/wasm.mjs +10 -56
  30. package/browser/repl-browser.mjs +9 -8
  31. package/browser/shims/buffer.mjs +31 -2
  32. package/browser/shims/child_process.mjs +26 -0
  33. package/browser/shims/fs.mjs +3 -1
  34. package/conformance/run.mjs +17 -2
  35. package/harness/adapters/ktvm-llvm.mjs +118 -0
  36. package/harness/adapters/ktvm-wasm.mjs +104 -0
  37. package/harness/adapters/ktvm.mjs +133 -0
  38. package/harness/registry.mjs +9 -0
  39. package/ktvm/ast.mjs +218 -0
  40. package/ktvm/bit-deque.mjs +482 -0
  41. package/ktvm/fixtures/integer-streaming.k +477 -0
  42. package/ktvm/index.mjs +119 -0
  43. package/ktvm/llvm/index.mjs +202 -0
  44. package/ktvm/llvm/lower-c.mjs +1258 -0
  45. package/ktvm/llvm/runtime.h +3542 -0
  46. package/ktvm/llvm/test-llvm.mjs +476 -0
  47. package/ktvm/lower-kir.mjs +2221 -0
  48. package/ktvm/optimize/fuse-kernel.mjs +2111 -0
  49. package/ktvm/optimize/index.mjs +116 -0
  50. package/ktvm/optimize/mwtr.mjs +283 -0
  51. package/ktvm/optimize/simplify-procs.mjs +5250 -0
  52. package/ktvm/optimize/synthesize-tables.mjs +1105 -0
  53. package/ktvm/parser.mjs +1101 -0
  54. package/ktvm/printer.mjs +281 -0
  55. package/ktvm/schema.mjs +543 -0
  56. package/ktvm/specialize.mjs +538 -0
  57. package/ktvm/test-conformance.mjs +597 -0
  58. package/ktvm/test-ktvm.mjs +1376 -0
  59. package/ktvm/test-optimize.mjs +598 -0
  60. package/ktvm/validator.mjs +1280 -0
  61. package/ktvm/vm.mjs +2225 -0
  62. package/ktvm/wasm/index.mjs +331 -0
  63. package/ktvm/wasm/lower-wasm.mjs +1272 -0
  64. package/ktvm/wasm/runtime.wat +4413 -0
  65. package/ktvm/wasm/test-wasm.mjs +426 -0
  66. package/ktvm/wire.mjs +489 -0
  67. package/package.json +1 -9
  68. package/repl.mjs +101 -20
  69. package/scripts/build-repl-html.mjs +40 -18
  70. package/scripts/perf-bigint-compare.mjs +12 -16
  71. package/scripts/perf-ieee.mjs +9 -229
  72. package/scripts/perf-int.mjs +14 -272
  73. package/scripts/perf-multiplication.mjs +8 -54
  74. package/scripts/perf-poly.mjs +9 -263
  75. package/scripts/run-tests.mjs +5 -2
  76. package/scripts/test-package.mjs +15 -3
  77. package/tests/README.md +3 -3
  78. package/tests/test-repl-html.mjs +72 -15
  79. package/tests/test-repl.mjs +24 -17
  80. package/DOCS/POLYMORPHIC_IR.md +0 -86
  81. package/access-table.mjs +0 -343
  82. package/pkir.mjs +0 -772
  83. package/tests/test-access-table.mjs +0 -213
  84. package/tests/test-pkir.mjs +0 -530
@@ -54,7 +54,7 @@ function printHelp() {
54
54
 
55
55
  IEEE 754 Floating-Point Arithmetic Benchmark Harness.
56
56
  Benchmarks add, sub, mul, and div on Examples/ieee-arithmetics.k across
57
- Native JS, kVM interpreter, LLVM, Wasm, and PKIR backends.
57
+ Native JS, kVM interpreter, LLVM, and Wasm backends.
58
58
 
59
59
  Options:
60
60
  --opt=<levels>, --opts=<levels>
@@ -65,20 +65,16 @@ Options:
65
65
  Run full LLVM optimization sweep (-O0, -O1, -O2).
66
66
  --wasm-in-process
67
67
  Run WebAssembly in-process instead of persistent subprocess runner.
68
- --pkir-only
69
- Run only PKIR backend.
70
68
  -h, --help
71
69
  Show this help message and exit.
72
70
 
73
71
  Environment Variables:
74
72
  BACKENDS
75
- Target backend to run: 'llvm', 'wasm', or 'pkir'.
73
+ Target backend to run: 'llvm' or 'wasm'.
76
74
  LLVM_ONLY
77
75
  Run only LLVM backend ('1' to enable).
78
76
  WASM_ONLY
79
77
  Run only WebAssembly backend ('1' to enable).
80
- PKIR_ONLY
81
- Run only PKIR backend ('1' to enable).
82
78
  BACKENDS_ONLY
83
79
  Skip JS and kVM interpreter baselines ('1' to enable).
84
80
  VALUES
@@ -123,49 +119,28 @@ const backendsEnv = process.env.BACKENDS?.toLowerCase();
123
119
  const backendsList = backendsEnv
124
120
  ? backendsEnv.split(",").map(s => s.trim())
125
121
  : null;
126
- const llvmPkirOnly = process.env.LLVM_PKIR_ONLY === "1"
122
+ const llvmOnly = process.env.LLVM_ONLY === "1"
127
123
  || (backendsList && backendsList.length === 1 &&
128
- backendsList[0] === "llvm-pkir")
129
- || process.argv.includes("--llvm-pkir-only");
130
- const pkirOnly = !llvmPkirOnly && (
131
- process.env.PKIR_ONLY === "1"
132
- || (backendsList && backendsList.length === 1 &&
133
- backendsList[0] === "pkir")
134
- || process.argv.includes("--pkir-only")
135
- );
136
- const llvmOnly = !pkirOnly && !llvmPkirOnly && (
137
- process.env.LLVM_ONLY === "1"
138
- || (backendsList && backendsList.length === 1 &&
139
- backendsList[0] === "llvm")
140
- );
141
- const wasmOnly = !pkirOnly && !llvmPkirOnly && !llvmOnly && (
124
+ backendsList[0] === "llvm");
125
+ const wasmOnly = !llvmOnly && (
142
126
  process.env.WASM_ONLY === "1"
143
127
  || (backendsList && backendsList.length === 1 &&
144
128
  backendsList[0] === "wasm")
145
129
  );
146
130
  const runLLVM = backendsList
147
131
  ? backendsList.includes("llvm")
148
- : !wasmOnly && !pkirOnly && !llvmPkirOnly;
149
- const runLLVMPKIR = backendsList
150
- ? backendsList.includes("llvm-pkir")
151
- : !wasmOnly && !pkirOnly && !llvmOnly;
132
+ : !wasmOnly;
152
133
  const runWasm = backendsList
153
134
  ? backendsList.includes("wasm")
154
- : !llvmOnly && !pkirOnly && !llvmPkirOnly;
155
- const runPKIR = backendsList
156
- ? backendsList.includes("pkir")
157
- : !llvmOnly && !wasmOnly && !llvmPkirOnly;
135
+ : !llvmOnly;
158
136
  const runBaselines = !process.env.BACKENDS_ONLY
159
137
  && (backendsList == null || backendsList.includes("kvm") ||
160
138
  backendsList.includes("js"))
161
- && !llvmOnly && !wasmOnly && !pkirOnly && !llvmPkirOnly;
139
+ && !llvmOnly && !wasmOnly;
162
140
 
163
141
  function llvmLaneNameStr() {
164
142
  return llvmLaneName();
165
143
  }
166
- function llvmPkirLaneNameStr() {
167
- return llvmLaneName().replace("LLVM", "LLVM-PKIR");
168
- }
169
144
 
170
145
  const ops = ["add", "sub", "mul", "div"];
171
146
  const values = csvEnv("VALUES", "0.5,-4,0,Infinity,-Infinity,NaN");
@@ -217,9 +192,6 @@ function wasmLaneName() {
217
192
  const mode = wasmPipe ? "persistent" : "in-process";
218
193
  return `WebAssembly (${mode})`;
219
194
  }
220
- function pkirLaneName() {
221
- return "PKIR (Wasm persistent)";
222
- }
223
195
 
224
196
  console.log("==> Initializing IEEE types and arithmetic");
225
197
  const state = createState();
@@ -295,8 +267,7 @@ if (runLLVM) {
295
267
  cacheDir,
296
268
  sourceLabel: "@fraczak/k/Examples/ieee-arithmetics.k#perf_ieee",
297
269
  runtimeMode: llvmRuntimeMode,
298
- clangOpt: opt,
299
- usePKIR: false
270
+ clangOpt: opt
300
271
  });
301
272
  const singleCase = [{
302
273
  op: "perf_ieee",
@@ -313,37 +284,6 @@ if (runLLVM) {
313
284
  codes.load(state.codes);
314
285
  }
315
286
 
316
- // Setup LLVM-PKIR
317
- const llvmPkirCasesByOpt = new Map();
318
- if (runLLVMPKIR) {
319
- for (const opt of llvmOptLevels) {
320
- console.log(`==> Compiling LLVM-PKIR executable (${opt})...`);
321
- const llvm = tryCompileCase({
322
- object: relation.object,
323
- relationName: relation.relationName,
324
- relHash: relation.relHash,
325
- inputPattern,
326
- cacheDir,
327
- sourceLabel: "@fraczak/k/Examples/ieee-arithmetics.k#perf_ieee",
328
- runtimeMode: llvmRuntimeMode,
329
- clangOpt: opt,
330
- usePKIR: true
331
- });
332
- const singleCase = [{
333
- op: "perf_ieee",
334
- label: "params",
335
- inputVal,
336
- inputWire,
337
- inputPattern,
338
- expected,
339
- llvm
340
- }];
341
- llvmPkirCasesByOpt.set(opt, singleCase);
342
- printCompileFailures(singleCase);
343
- }
344
- codes.load(state.codes);
345
- }
346
-
347
287
  // Setup Wasm
348
288
  let wasmExports = null;
349
289
  let wasmPtrIn = null;
@@ -388,28 +328,6 @@ if (runWasm) {
388
328
  }
389
329
  }
390
330
 
391
- // Setup PKIR
392
- let pkirExePath = null;
393
- let pkirCompileError = null;
394
- if (runPKIR) {
395
- console.log("==> Compiling PKIR executable (persistent runner)...");
396
- try {
397
- const pkirPath = path.join(cacheDir, "perf_ieee_pkir.wasm");
398
- pkirExePath = path.join(cacheDir, "perf_ieee_pkir.exe");
399
- const wasmBuf = await compileWasmArtifactFromObject(relation.object, {
400
- entry: "__main__",
401
- usePKIR: true
402
- });
403
- fs.writeFileSync(pkirPath, wasmBuf);
404
- const runBin = fileURLToPath(import.meta.resolve("../backends/wasm/bin/k-wasm-run.mjs"));
405
- fs.writeFileSync(pkirExePath, `#!/bin/sh\nexec node "${runBin}" "${pkirPath}" "$@"\n`, { mode: 0o755 });
406
- } catch (error) {
407
- pkirCompileError = error.stack || error.message || String(error);
408
- console.log("PKIR compilation failed:");
409
- console.log(pkirCompileError.split("\n").slice(0, 8).join("\n"));
410
- }
411
- }
412
-
413
331
  function printBenchmarkDescription() {
414
332
  const lanes = [];
415
333
  if (runBaselines) {
@@ -428,19 +346,7 @@ function printBenchmarkDescription() {
428
346
  );
429
347
  }
430
348
  }
431
- if (runLLVMPKIR) {
432
- for (const opt of llvmOptLevels) {
433
- const mode = llvmLaneName()
434
- .replace(/^LLVM Executable\s*\((.*)\)$/, "$1");
435
- lanes.push(
436
- llvmOptLevels.length > 1
437
- ? `LLVM-PKIR ${opt} (${mode})`
438
- : llvmLaneName().replace("LLVM", "LLVM-PKIR")
439
- );
440
- }
441
- }
442
349
  if (runWasm) lanes.push(wasmLaneName());
443
- if (runPKIR) lanes.push(pkirLaneName());
444
350
 
445
351
  console.log("==> Benchmark description");
446
352
  console.log(" source: @fraczak/k/Examples/ieee-arithmetics.k#perf_ieee (single program)");
@@ -453,11 +359,6 @@ function printBenchmarkDescription() {
453
359
  console.log(` llvm clang opts: ${llvmOptLevels.join(", ")}`);
454
360
  console.log(` llvm cache dir: ${cacheDir}`);
455
361
  }
456
- if (runLLVMPKIR) {
457
- console.log(` llvm-pkir warmup iterations: ${llvmWarmupIterations}`);
458
- console.log(` llvm-pkir runtime mode: ${llvmRuntimeMode}`);
459
- console.log(` llvm-pkir clang opts: ${llvmOptLevels.join(", ")}`);
460
- }
461
362
  if (runWasm) {
462
363
  console.log(` wasm runner mode: ${wasmPipe ? "persistent pipe" : "in-process"}`);
463
364
  console.log(` wasm warmup iterations: ${wasmWarmupIterations}`);
@@ -465,9 +366,6 @@ function printBenchmarkDescription() {
465
366
  console.log(` wasm arena reset: ${wasmReset ? "yes" : "no"}`);
466
367
  }
467
368
  }
468
- if (runPKIR) {
469
- console.log(` pkir warmup iterations: ${wasmWarmupIterations}`);
470
- }
471
369
  console.log(` iterations: ${iterations}`);
472
370
  console.log(` benchmark lanes: ${lanes.join("; ")}`);
473
371
  console.log(" conformance: all outputs are compared to native expected values");
@@ -528,28 +426,6 @@ if (runLLVM) {
528
426
  }
529
427
  }
530
428
 
531
- // 2b. LLVM-PKIR lanes
532
- const llvmPkirResults = new Map();
533
- if (runLLVMPKIR) {
534
- for (const opt of llvmOptLevels) {
535
- const casesForOpt = llvmPkirCasesByOpt.get(opt);
536
- const llvmRunner = createLLVMRunner(casesForOpt);
537
- try {
538
- if (llvmWarmupIterations > 0) {
539
- console.log(`==> Warming LLVM-PKIR (${opt}) executable (${llvmWarmupIterations} iterations)...`);
540
- await llvmRunner.run(llvmWarmupIterations);
541
- }
542
- console.log(`==> Running LLVM-PKIR (${opt}) (${iterations} iterations)...`);
543
- const result = await llvmRunner.run(iterations);
544
- llvmPkirResults.set(opt, result);
545
- } catch (err) {
546
- console.log(`==> LLVM-PKIR (${opt}) execution failed: ${err.message?.split("\n")[0]}`);
547
- } finally {
548
- llvmRunner.close();
549
- }
550
- }
551
- }
552
-
553
429
  // 3. Wasm lane
554
430
  let wasmResult = null;
555
431
  if (runWasm) {
@@ -592,28 +468,6 @@ if (runWasm) {
592
468
  }
593
469
  }
594
470
 
595
- // 4. PKIR lane
596
- let pkirResult = null;
597
- if (runPKIR) {
598
- if (pkirExePath) {
599
- const server = new PersistentExecutable(pkirExePath);
600
- try {
601
- if (wasmWarmupIterations > 0) {
602
- console.log(`==> Warming PKIR (${wasmWarmupIterations} iterations)...`);
603
- for (let i = 0; i < wasmWarmupIterations; i++) {
604
- await server.request(inputWire);
605
- }
606
- }
607
- console.log(`==> Running PKIR (${iterations} iterations)...`);
608
- pkirResult = await runTimedIterationsAsync(iterations, async () => {
609
- await server.request(inputWire);
610
- });
611
- } finally {
612
- server.close();
613
- }
614
- }
615
- }
616
-
617
471
  console.log("\n=================== IEEE BENCHMARK RESULTS ===================");
618
472
  console.log(`Program calls per iteration: 1 (${totalOps} IEEE operations evaluated inside)`);
619
473
  console.log(`Total requested program calls: ${iterations} (${iterations * totalOps} equivalent IEEE operations)`);
@@ -637,27 +491,10 @@ if (runLLVM) {
637
491
  );
638
492
  }
639
493
  }
640
- if (runLLVMPKIR) {
641
- for (const opt of llvmOptLevels) {
642
- const mode = llvmLaneName()
643
- .replace(/^LLVM Executable\s*\((.*)\)$/, "$1");
644
- const title = llvmOptLevels.length > 1
645
- ? `LLVM-PKIR ${opt} (${mode})`
646
- : llvmLaneName().replace("LLVM", "LLVM-PKIR");
647
- console.log(
648
- `${laneIndex++}. ${title.padEnd(29)} ` +
649
- `${formatTiming(llvmPkirResults.get(opt))}`
650
- );
651
- }
652
- }
653
494
  if (runWasm) {
654
495
  const timingStr = wasmResult ? formatTiming(wasmResult) : "compile failed";
655
496
  console.log(`${laneIndex++}. ${wasmLaneName().padEnd(29)} ${timingStr}`);
656
497
  }
657
- if (runPKIR) {
658
- const timingStr = pkirResult ? formatTiming(pkirResult) : "compile failed";
659
- console.log(`${laneIndex++}. ${pkirLaneName().padEnd(29)} ${timingStr}`);
660
- }
661
498
  console.log("==============================================================\n");
662
499
 
663
500
  // Conformance Validation
@@ -695,28 +532,6 @@ if (runLLVM) {
695
532
  }
696
533
  }
697
534
 
698
- if (runLLVMPKIR) {
699
- for (const opt of llvmOptLevels) {
700
- const casesForOpt = llvmPkirCasesByOpt.get(opt);
701
- const tc = casesForOpt[0];
702
- if (tc.llvm.status !== "ok") {
703
- tc.llvmConformance = "compile-failed";
704
- } else {
705
- try {
706
- const outputWire = await runExecutable(tc.llvm.exePath, tc.inputWire);
707
- const actual = decodeWire(outputWire).value;
708
- assert.deepEqual(toPlainObject(actual), toPlainObject(expected));
709
- tc.llvmConformance = "ok";
710
- } catch (error) {
711
- tc.llvmConformance = "failed";
712
- tc.llvm.error = error.stack || error.message || String(error);
713
- console.log(`LLVM-PKIR (${opt}) conformance failure:`);
714
- console.log(tc.llvm.error.split("\n").slice(0, 8).join("\n"));
715
- }
716
- }
717
- }
718
- }
719
-
720
535
  let wasmOk = false;
721
536
  if (runWasm) {
722
537
  if (wasmPipe) {
@@ -753,22 +568,6 @@ if (runWasm) {
753
568
  }
754
569
  }
755
570
 
756
- let pkirOk = false;
757
- if (runPKIR) {
758
- if (!pkirExePath) {
759
- console.log("PKIR conformance failure: executable was not compiled");
760
- } else {
761
- try {
762
- const outputWire = await runExecutable(pkirExePath, inputWire);
763
- const actual = decodeWire(outputWire).value;
764
- assert.deepEqual(toPlainObject(actual), toPlainObject(expected));
765
- pkirOk = true;
766
- } catch (error) {
767
- console.log("PKIR conformance failure:", error.message || error);
768
- }
769
- }
770
- }
771
-
772
571
  const validationSummary = [];
773
572
  if (runBaselines && kvmOk) validationSummary.push(`kVM (all ${totalOps} ops)`);
774
573
  if (runLLVM) {
@@ -778,21 +577,9 @@ if (runLLVM) {
778
577
  validationSummary.push(`LLVM ${opt} (${tc.llvmConformance === "ok" ? `all ${totalOps} ops` : tc.llvmConformance})`);
779
578
  }
780
579
  }
781
- if (runLLVMPKIR) {
782
- for (const opt of llvmOptLevels) {
783
- const casesForOpt = llvmPkirCasesByOpt.get(opt);
784
- const tc = casesForOpt[0];
785
- validationSummary.push(
786
- `LLVM-PKIR ${opt} (${tc.llvmConformance === "ok" ? `all ${totalOps} ops` : tc.llvmConformance})`
787
- );
788
- }
789
- }
790
580
  if (runWasm) {
791
581
  validationSummary.push(`Wasm (${wasmOk ? `all ${totalOps} ops` : "failed"})`);
792
582
  }
793
- if (runPKIR) {
794
- validationSummary.push(`PKIR (${pkirOk ? `all ${totalOps} ops` : "failed"})`);
795
- }
796
583
 
797
584
  console.log(`Conformance validation: ${validationSummary.join(", ")} match expected values.`);
798
585
  if (runLLVM) {
@@ -801,11 +588,4 @@ if (runLLVM) {
801
588
  if (shouldStrictFail(casesForOpt)) process.exitCode = 1;
802
589
  }
803
590
  }
804
- if (runLLVMPKIR) {
805
- for (const opt of llvmOptLevels) {
806
- const casesForOpt = llvmPkirCasesByOpt.get(opt);
807
- if (shouldStrictFail(casesForOpt)) process.exitCode = 1;
808
- }
809
- }
810
591
  if (runWasm && !wasmOk) process.exitCode = 1;
811
- if (runPKIR && !pkirOk) process.exitCode = 1;