@fraczak/k 6.3.2 → 6.4.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/DOCS/CODECS.md +258 -0
- package/DOCS/DICTIONARY.md +1172 -0
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +51 -46
- package/DOCS/KIR_V1.md +221 -0
- package/DOCS/KVM_EXECUTION_MODEL.md +56 -19
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +2 -1
- package/DOCS/PROFILING_AND_TRACING.md +196 -0
- package/DOCS/REPL.md +76 -14
- package/DOCS/WEB_REPL_REQUIREMENTS.md +135 -0
- package/DOCS/dependency-graph.md +144 -0
- package/Examples/arithmetics.k +51 -48
- package/Examples/bnat.k +1 -1
- package/Examples/buda.k +44 -0
- package/Examples/ieee.k +300 -8671
- package/Examples/poly.k +67 -0
- package/README.md +68 -19
- package/augmentation.mjs +9 -7
- package/backend-api.mjs +11 -2
- package/backends/arm64/README.md +104 -0
- package/backends/arm64/bin/k-arm64-compile.mjs +101 -0
- package/backends/arm64/bin/k-arm64-run.mjs +58 -0
- package/backends/arm64/bin/k-arm64.mjs +88 -0
- package/backends/arm64/scripts/run-tests.mjs +41 -0
- package/backends/arm64/src/arm64.mjs +229 -0
- package/backends/arm64/src/cli.mjs +155 -0
- package/backends/arm64/src/kvm2arm64.mjs +906 -0
- package/backends/arm64/src/runtime.c +728 -0
- package/backends/arm64/tests/integration.sh +112 -0
- package/backends/arm64/tests/test-arm64.mjs +240 -0
- package/backends/llvm/README.md +135 -0
- package/backends/llvm/bin/k-llvm-build.mjs +71 -0
- package/backends/llvm/bin/k-llvm-compile.mjs +68 -0
- package/backends/llvm/bin/k-llvm-jit.mjs +160 -0
- package/backends/llvm/bin/k-llvm-run.mjs +90 -0
- package/backends/llvm/runtime/krt.c +1508 -0
- package/backends/llvm/runtime/krt.h +109 -0
- package/backends/llvm/scripts/conformance.mjs +76 -0
- package/backends/llvm/scripts/run-tests.mjs +28 -0
- package/backends/llvm/src/executable.mjs +562 -0
- package/backends/llvm/src/kvm2llvm.mjs +998 -0
- package/backends/llvm/src/llvm.mjs +1092 -0
- package/backends/llvm/tests/composition-driver.c +22 -0
- package/backends/llvm/tests/identity-driver.c +27 -0
- package/backends/llvm/tests/integration.sh +153 -0
- package/backends/llvm/tests/perf-support.mjs +735 -0
- package/backends/llvm/tests/product-driver.c +23 -0
- package/backends/llvm/tests/projection-driver.c +19 -0
- package/backends/llvm/tests/relation-driver.c +29 -0
- package/backends/llvm/tests/test-llvm.mjs +240 -0
- package/backends/llvm/tests/union-driver.c +31 -0
- package/backends/llvm/tests/variant-driver.c +19 -0
- package/backends/llvm/tests/variant-projection-driver.c +21 -0
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +233 -0
- package/backends/wasm/README.md +97 -0
- package/backends/wasm/TECHNICAL-DEBT.md +60 -0
- package/backends/wasm/bin/k-wasm-compile.mjs +50 -0
- package/backends/wasm/bin/k-wasm-run.mjs +140 -0
- package/backends/wasm/bin/k-wasm.mjs +49 -0
- package/backends/wasm/demo_ieee.mjs +319 -0
- package/backends/wasm/runtime.wat +102 -0
- package/backends/wasm/scripts/run-tests.mjs +41 -0
- package/backends/wasm/src/cli.mjs +133 -0
- package/backends/wasm/src/kvm2wasm.mjs +661 -0
- package/backends/wasm/src/wasm.mjs +1010 -0
- package/backends/wasm/tests/integration.sh +182 -0
- package/backends/wasm/tests/perf-support.mjs +336 -0
- package/backends/wasm/tests/test-wasm.mjs +617 -0
- package/browser/repl-browser.mjs +1584 -0
- package/browser/shims/assert.mjs +19 -0
- package/browser/shims/buffer.mjs +224 -0
- package/browser/shims/crypto.mjs +103 -0
- package/browser/shims/fs.mjs +159 -0
- package/browser/shims/os.mjs +17 -0
- package/browser/shims/path.mjs +54 -0
- package/browser/shims/process.mjs +44 -0
- package/browser/shims/readline.mjs +11 -0
- package/browser/shims/url.mjs +12 -0
- package/browser/vfs-data.mjs +19 -0
- package/codecs/README.md +3 -0
- package/codecs/json.mjs +16 -2
- package/codecs/runtime/show-value.mjs +2 -2
- package/conformance/helper-reuse/case.json +8 -0
- package/conformance/helper-reuse/expected.kv +1 -0
- package/conformance/helper-reuse/input.kv +1 -0
- package/conformance/helper-reuse/program.k +2 -0
- package/conformance/identity/case.json +8 -0
- package/conformance/identity/expected.kv +1 -0
- package/conformance/identity/input.kv +1 -0
- package/conformance/identity/program.k +1 -0
- package/conformance/product-projection/case.json +8 -0
- package/conformance/product-projection/expected.kv +1 -0
- package/conformance/product-projection/input.kv +1 -0
- package/conformance/product-projection/program.k +1 -0
- package/conformance/run.mjs +149 -0
- package/conformance/variant-construction/case.json +8 -0
- package/conformance/variant-construction/expected.kv +1 -0
- package/conformance/variant-construction/input.kv +1 -0
- package/conformance/variant-construction/program.k +1 -0
- package/harness/adapters/arm64.mjs +105 -0
- package/harness/adapters/kvm.mjs +144 -0
- package/harness/adapters/llvm.mjs +118 -0
- package/harness/adapters/native.mjs +77 -0
- package/harness/adapters/wasm.mjs +169 -0
- package/harness/backend.mjs +95 -0
- package/harness/cli.mjs +130 -0
- package/harness/format.mjs +95 -0
- package/harness/index.mjs +25 -0
- package/harness/persistent.mjs +121 -0
- package/harness/registry.mjs +44 -0
- package/harness/suite.mjs +257 -0
- package/harness/suites/poly.mjs +156 -0
- package/harness/trace.mjs +50 -0
- package/harness/util.mjs +92 -0
- package/intrinsics.mjs +41 -0
- package/kir.mjs +399 -0
- package/kvm.mjs +585 -81
- package/objects/README.md +37 -3
- package/objects/compile.mjs +15 -22
- package/objects/inspect.mjs +96 -0
- package/objects/validate.mjs +415 -0
- package/package.json +51 -19
- package/repl-codecs.mjs +133 -20
- package/repl.mjs +408 -59
- package/run.mjs +2 -7
- package/scripts/benchmark.mjs +406 -22
- package/scripts/build-repl-html.mjs +1784 -0
- package/scripts/perf-ieee.mjs +586 -0
- package/scripts/perf-int.mjs +531 -0
- package/scripts/perf-poly.mjs +945 -0
- package/scripts/render-benchmark.mjs +12 -8
- package/scripts/run-tests.mjs +6 -0
- package/tests/integration.sh +19 -2
- package/tests/test-arithmetics.mjs +54 -0
- package/tests/test-ieee-arithmetic.mjs +6 -2
- package/tests/test-kir.mjs +85 -0
- package/tests/test-kvm-polymorphic.mjs +278 -0
- package/tests/test-kvm.mjs +25 -6
- package/tests/test-poly.mjs +129 -0
- package/tests/test-repl-html.mjs +303 -0
- package/tests/test-repl.mjs +175 -1
- package/tests/test-validate-object.mjs +40 -0
- package/a.klib +0 -1
- package/a.kvm +0 -142676
- package/a.wasm +0 -0
- package/benchmark-results.html +0 -532
- package/benchmark-results.json +0 -480
- package/k-vscode/.vscode/launch.json +0 -14
- package/k-vscode/README.md +0 -75
- package/k-vscode/extension.js +0 -273
- package/k-vscode/language-configuration.json +0 -42
- package/k-vscode/package.json +0 -79
- package/k-vscode/snippets/k.json +0 -92
- package/k-vscode/syntaxes/k.tmLanguage.json +0 -599
- package/k-vscode/test/background-ranges.test.js +0 -50
- package/k-vscode/themes/k-contrast-color-theme.json +0 -104
- package/scripts/git-hooks/pre-commit +0 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# KIR: k Intermediate Representation,
|
|
1
|
+
# KIR: k Intermediate Representation, Envelope Specialization, and Conformance
|
|
2
2
|
|
|
3
3
|
The document defines an object IR and conformance platform for LLVM and
|
|
4
4
|
WebAssembly backends.
|
|
@@ -6,9 +6,9 @@ WebAssembly backends.
|
|
|
6
6
|
## Goals
|
|
7
7
|
|
|
8
8
|
- Define a portable polymorphic object IR.
|
|
9
|
-
- Define
|
|
9
|
+
- Define envelope specialization for concrete input envelope patterns.
|
|
10
10
|
- Define conformance fixtures for interpreter, object execution,
|
|
11
|
-
|
|
11
|
+
envelope-specialized execution, and future backends.
|
|
12
12
|
- Keep `.ko` and `.klib` inspectable.
|
|
13
13
|
- Record type-derivation convergence per relation.
|
|
14
14
|
|
|
@@ -32,7 +32,7 @@ KIR-P must:
|
|
|
32
32
|
- preserve filters and pattern operations;
|
|
33
33
|
- store relation input and output patterns;
|
|
34
34
|
- be the canonical `.ko` / `.klib` relation format;
|
|
35
|
-
- be the reference for
|
|
35
|
+
- be the reference for envelope specialization and backend tests.
|
|
36
36
|
|
|
37
37
|
Alternative:
|
|
38
38
|
|
|
@@ -40,29 +40,31 @@ Alternative:
|
|
|
40
40
|
but makes object behavior depend on the compiler version. Do not use this for
|
|
41
41
|
KIR.
|
|
42
42
|
|
|
43
|
-
### KIR-
|
|
43
|
+
### Envelope-Specialized KIR-P
|
|
44
44
|
|
|
45
|
-
KIR-
|
|
45
|
+
Envelope-Specialized KIR-P is produced from `KIR-P + input envelope pattern`.
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
Envelope Specialization must:
|
|
48
48
|
|
|
49
49
|
- be equivalent to typing the entry expression `?P __main__`;
|
|
50
50
|
- derive the output pattern for that invocation;
|
|
51
51
|
- validate filters and pattern constraints before value execution;
|
|
52
52
|
- preserve the same value-level partial function semantics;
|
|
53
|
-
- support multiple pattern contexts for the same helper relation;
|
|
54
|
-
- be cacheable by relation hash and input pattern hash
|
|
53
|
+
- support multiple pattern contexts for the same helper relation internally;
|
|
54
|
+
- be cacheable by relation hash and input pattern hash without serializing that
|
|
55
|
+
cache key into KIR.
|
|
55
56
|
|
|
56
57
|
Alternative:
|
|
57
58
|
|
|
58
59
|
- Re-run full source compilation for each input pattern. This is acceptable for
|
|
59
60
|
the first prototype, but the public API should still be
|
|
60
|
-
`
|
|
61
|
+
`specializeEnvelope(partialFunction, inputEnvelope)`. Current implementation identifiers
|
|
62
|
+
may still use `retype...` until they are renamed.
|
|
61
63
|
|
|
62
64
|
### KIR-M: Backend IR
|
|
63
65
|
|
|
64
|
-
KIR-M is produced from KIR-
|
|
65
|
-
decisions are fixed.
|
|
66
|
+
KIR-M is produced from envelope-specialized KIR-P after call-site pattern contexts, layout,
|
|
67
|
+
and ABI decisions are fixed.
|
|
66
68
|
|
|
67
69
|
KIR-M should:
|
|
68
70
|
|
|
@@ -73,16 +75,19 @@ KIR-M should:
|
|
|
73
75
|
- require converged type derivation for every compiled relation;
|
|
74
76
|
- target LLVM, Wasm, C, or another backend.
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
Current baseline:
|
|
77
79
|
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
+
- Lower KIR-P directly to kVM. This gives a single backend-facing contract for
|
|
81
|
+
both polymorphic and envelope-specialized relations. Later KIR-M layout work can specialize
|
|
82
|
+
the same KIR-P input further instead of adding another serialized relation
|
|
83
|
+
shape.
|
|
80
84
|
|
|
81
85
|
Relationship to kVM:
|
|
82
86
|
|
|
83
87
|
- [KVM_EXECUTION_MODEL.md](KVM_EXECUTION_MODEL.md) sketches kVM as the concrete
|
|
84
88
|
execution contract for KIR-M.
|
|
85
|
-
-
|
|
89
|
+
- envelope specialization remains a pass that emits ordinary KIR-P.
|
|
90
|
+
- kVM lowering consumes KIR-P relation bodies directly.
|
|
86
91
|
- kVM keeps product and union as structured regions so backends can choose
|
|
87
92
|
sequential execution first, then safe parallel scheduling later.
|
|
88
93
|
|
|
@@ -124,7 +129,7 @@ Implementation:
|
|
|
124
129
|
|
|
125
130
|
Alternative:
|
|
126
131
|
|
|
127
|
-
- Change the stored object
|
|
132
|
+
- Change the stored object payload immediately. This is less code, but risks
|
|
128
133
|
mixing backend contracts with current JS evaluator internals.
|
|
129
134
|
|
|
130
135
|
### Step 1a: Record Type-Derivation Status
|
|
@@ -231,9 +236,9 @@ Alternative:
|
|
|
231
236
|
|
|
232
237
|
- Store union-find state as debug metadata. Do not use it as backend input.
|
|
233
238
|
|
|
234
|
-
##
|
|
239
|
+
## Envelope Specialization
|
|
235
240
|
|
|
236
|
-
### Step 5: Add
|
|
241
|
+
### Step 5: Add Envelope Specialization
|
|
237
242
|
|
|
238
243
|
Input:
|
|
239
244
|
|
|
@@ -244,9 +249,9 @@ Input:
|
|
|
244
249
|
|
|
245
250
|
Output:
|
|
246
251
|
|
|
247
|
-
- KIR-
|
|
252
|
+
- envelope-specialized KIR-P relation;
|
|
248
253
|
- output pattern;
|
|
249
|
-
- call-site
|
|
254
|
+
- compiler-local call-site worklist.
|
|
250
255
|
|
|
251
256
|
Algorithm:
|
|
252
257
|
|
|
@@ -261,13 +266,13 @@ Alternative:
|
|
|
261
266
|
- Implement this inside `run.mjs` first. This may be useful for bootstrapping,
|
|
262
267
|
but the final API should be separate from the evaluator.
|
|
263
268
|
|
|
264
|
-
### Step 6: Execution After
|
|
269
|
+
### Step 6: Execution After Envelope Specialization
|
|
265
270
|
|
|
266
271
|
Safe baseline:
|
|
267
272
|
|
|
268
273
|
- keep current envelope-carrying execution;
|
|
269
|
-
- use KIR-
|
|
270
|
-
- treat filters as already validated by
|
|
274
|
+
- use envelope-specialized KIR-P only to derive the top-level output pattern;
|
|
275
|
+
- treat filters as already validated by envelope specialization.
|
|
271
276
|
|
|
272
277
|
Optimized mode:
|
|
273
278
|
|
|
@@ -281,7 +286,7 @@ Alternative:
|
|
|
281
286
|
- Never remove envelopes in the JS evaluator. This is simplest and remains the
|
|
282
287
|
reference path. Envelope-free execution can be left to KIR-M.
|
|
283
288
|
|
|
284
|
-
### Step 7: Call-Site
|
|
289
|
+
### Step 7: Call-Site Envelope Specialization
|
|
285
290
|
|
|
286
291
|
One relation may be called under multiple patterns:
|
|
287
292
|
|
|
@@ -290,7 +295,7 @@ f@P1
|
|
|
290
295
|
f@P2
|
|
291
296
|
```
|
|
292
297
|
|
|
293
|
-
|
|
298
|
+
The envelope specialization cache should model these as separate instances:
|
|
294
299
|
|
|
295
300
|
```text
|
|
296
301
|
relation hash + input pattern hash
|
|
@@ -303,7 +308,7 @@ Alternative:
|
|
|
303
308
|
- Use one annotation per relation. This is easier, but too coarse for precise
|
|
304
309
|
output patterns and unsafe for envelope-free execution.
|
|
305
310
|
|
|
306
|
-
### Step 8: Cache
|
|
311
|
+
### Step 8: Cache Envelope Specialization Results
|
|
307
312
|
|
|
308
313
|
Cache key:
|
|
309
314
|
|
|
@@ -313,15 +318,15 @@ relation-hash + input-pattern-hash
|
|
|
313
318
|
|
|
314
319
|
Cache value:
|
|
315
320
|
|
|
316
|
-
- KIR-
|
|
321
|
+
- envelope-specialized KIR-P relation;
|
|
317
322
|
- output pattern;
|
|
318
|
-
- call-site
|
|
323
|
+
- optional compiler-local call-site worklist;
|
|
319
324
|
- optional backend artifact.
|
|
320
325
|
|
|
321
326
|
Alternative:
|
|
322
327
|
|
|
323
|
-
- Cache by pattern
|
|
324
|
-
is stable.
|
|
328
|
+
- Cache by a stable pattern encoding first. Replace with pattern hashes once
|
|
329
|
+
pattern hashing is stable.
|
|
325
330
|
|
|
326
331
|
## Conformance
|
|
327
332
|
|
|
@@ -345,7 +350,7 @@ Example:
|
|
|
345
350
|
"program": "program.k",
|
|
346
351
|
"input": "input.kv",
|
|
347
352
|
"expected": "expected.kv",
|
|
348
|
-
"modes": ["source", "object", "
|
|
353
|
+
"modes": ["source", "object", "envelope-specialized"],
|
|
349
354
|
"tags": ["runtime", "projection"]
|
|
350
355
|
}
|
|
351
356
|
```
|
|
@@ -362,7 +367,7 @@ Modes:
|
|
|
362
367
|
|
|
363
368
|
- `source`: compile source and run the JS evaluator;
|
|
364
369
|
- `object`: compile `.ko`, load it, and run object evaluation;
|
|
365
|
-
- `
|
|
370
|
+
- `envelope-specialized`: specialize using the input wire pattern and run the envelope-specialized evaluator;
|
|
366
371
|
- future `wasm` / `llvm`: run backend artifacts.
|
|
367
372
|
|
|
368
373
|
Comparison:
|
|
@@ -413,12 +418,12 @@ Codecs:
|
|
|
413
418
|
- Unicode labels;
|
|
414
419
|
- explicit pattern vs derived witness pattern.
|
|
415
420
|
|
|
416
|
-
|
|
421
|
+
Envelope Specialization:
|
|
417
422
|
|
|
418
423
|
- output pattern derived from `?P __main__`;
|
|
419
424
|
- helper called under two different patterns;
|
|
420
|
-
- recursive helper
|
|
421
|
-
-
|
|
425
|
+
- recursive helper envelope specialization convergence;
|
|
426
|
+
- envelope specialization cache reuse.
|
|
422
427
|
|
|
423
428
|
Backend eligibility:
|
|
424
429
|
|
|
@@ -436,32 +441,32 @@ Alternative:
|
|
|
436
441
|
- `k-inspect-object`: print object sections, KIR-P, aliases, metadata, and
|
|
437
442
|
pattern summaries.
|
|
438
443
|
- `k-validate-object`: validate `.ko` and `.klib` schemas and references.
|
|
439
|
-
- `k-
|
|
440
|
-
-
|
|
444
|
+
- `k-specialize-envelope`: precompute envelope-specialized KIR-P for one relation and input pattern.
|
|
445
|
+
- Envelope Specialization heat counters in the JS runtime.
|
|
441
446
|
- Pattern-hash backend artifact cache.
|
|
442
447
|
- Optional binary executable object payload after KIR stabilizes.
|
|
443
448
|
- Minimal C backend before LLVM.
|
|
444
|
-
- Wasm
|
|
449
|
+
- Wasm envelope-specialized KIR interpreter before native LLVM.
|
|
445
450
|
|
|
446
451
|
## Implementation Order
|
|
447
452
|
|
|
448
453
|
1. Write `DOCS/KIR_V1.md`.
|
|
449
|
-
2. Treat current `.ko` / `.klib` storage as the initial KIR-P input
|
|
454
|
+
2. Treat current `.ko` / `.klib` storage as the initial KIR-P input artifact.
|
|
450
455
|
3. Add `kir.mjs` to export KIR-P from current objects.
|
|
451
456
|
4. Add `objects/inspect.mjs --kir`.
|
|
452
457
|
5. Add `objects/validate.mjs`.
|
|
453
458
|
6. Add conformance fixture format and runner.
|
|
454
|
-
7. Add entry
|
|
455
|
-
8. Add
|
|
456
|
-
9. Compare
|
|
457
|
-
10. Add call-site
|
|
458
|
-
11. Add
|
|
459
|
+
7. Add entry envelope specialization for `?P __main__`.
|
|
460
|
+
8. Add an envelope-specialized KIR JS evaluator or envelope-specialized execution wrapper.
|
|
461
|
+
9. Compare envelope-specialized KIR execution against `run.mjs`.
|
|
462
|
+
10. Add call-site envelope specialization for helper relations.
|
|
463
|
+
11. Add envelope specialization cache.
|
|
459
464
|
12. Reject non-converged relations in LLVM/Wasm compilation.
|
|
460
465
|
13. Prototype a small C or Wasm backend through kVM.
|
|
461
466
|
|
|
462
467
|
Rule:
|
|
463
468
|
|
|
464
469
|
- KIR-P is the portable semantic contract.
|
|
465
|
-
- KIR-
|
|
470
|
+
- envelope specialization emits KIR-P and may use local cache keys/worklists.
|
|
466
471
|
- KIR-M is backend material; kVM is the proposed concrete execution model for
|
|
467
472
|
that material.
|
package/DOCS/KIR_V1.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# KIR v1
|
|
2
|
+
|
|
3
|
+
KIR v1 is the first backend-facing artifact contract for compiled k objects. It
|
|
4
|
+
is an inspection and export contract, not a replacement for the current `.ko`
|
|
5
|
+
and `.klib` containers.
|
|
6
|
+
|
|
7
|
+
The first layer is **KIR-P**, the portable polymorphic relation format. KIR-P
|
|
8
|
+
keeps the same pattern-carrying semantics as current object execution, but
|
|
9
|
+
normalizes names and graph IDs so backends can consume object contents without
|
|
10
|
+
depending on parser-shaped fields, current object storage, or
|
|
11
|
+
`TypePatternGraph` internals.
|
|
12
|
+
|
|
13
|
+
## Scope
|
|
14
|
+
|
|
15
|
+
KIR v1 defines:
|
|
16
|
+
|
|
17
|
+
- a serialization-ready contract over current `.ko` and `.klib` payloads;
|
|
18
|
+
- relation input and output pattern roots;
|
|
19
|
+
- a closed expression opcode vocabulary;
|
|
20
|
+
- dense per-relation pattern graph IDs;
|
|
21
|
+
- relation type-derivation status;
|
|
22
|
+
- object aliases, metadata, compile statistics, and code table pass-through.
|
|
23
|
+
|
|
24
|
+
KIR v1 does not define:
|
|
25
|
+
|
|
26
|
+
- KIR-M layout and ABI decisions;
|
|
27
|
+
- LLVM, Wasm, C, or native runtime layout;
|
|
28
|
+
- a new stored object format;
|
|
29
|
+
- a required physical encoding.
|
|
30
|
+
|
|
31
|
+
## Top-Level Shape
|
|
32
|
+
|
|
33
|
+
The current textual export uses this field shape:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"format": "k-ir",
|
|
38
|
+
"version": 1,
|
|
39
|
+
"layer": "KIR-P",
|
|
40
|
+
"sourceFormat": "k-object",
|
|
41
|
+
"kind": "executable",
|
|
42
|
+
"main": "__main__",
|
|
43
|
+
"codes": {},
|
|
44
|
+
"rels": {},
|
|
45
|
+
"relAlias": {},
|
|
46
|
+
"compileStats": {},
|
|
47
|
+
"meta": {}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Rules:
|
|
52
|
+
|
|
53
|
+
- `kind` is `executable` when `main` names an entry relation.
|
|
54
|
+
- `kind` is `library` when `main` is `null`.
|
|
55
|
+
- `codes`, `relAlias`, `compileStats`, and `meta` preserve the current object
|
|
56
|
+
payload content, sorted for stable inspection.
|
|
57
|
+
- KIR consumers must not infer backend eligibility from the top level alone.
|
|
58
|
+
Each relation has its own `typeDerivation.status`.
|
|
59
|
+
|
|
60
|
+
## Relations
|
|
61
|
+
|
|
62
|
+
Each relation entry has this shape:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"inputPattern": 0,
|
|
67
|
+
"outputPattern": 1,
|
|
68
|
+
"typeDerivation": { "status": "converged" },
|
|
69
|
+
"patternGraph": {
|
|
70
|
+
"nodes": [],
|
|
71
|
+
"sourceNodeMap": {}
|
|
72
|
+
},
|
|
73
|
+
"body": {}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Rules:
|
|
78
|
+
|
|
79
|
+
- `inputPattern` and `outputPattern` are node IDs in the relation's
|
|
80
|
+
`patternGraph`.
|
|
81
|
+
- Valid type-derivation statuses are `converged`, `not-converged`, and
|
|
82
|
+
`unknown`.
|
|
83
|
+
- LLVM and optimized Wasm backends should reject relations whose status is not
|
|
84
|
+
`converged`, unless they intentionally implement an envelope-aware reference
|
|
85
|
+
mode.
|
|
86
|
+
- `sourceNodeMap` maps original object pattern graph representative IDs to KIR
|
|
87
|
+
node IDs. It exists for inspection and debugging, not as a semantic contract.
|
|
88
|
+
|
|
89
|
+
## Pattern Graphs
|
|
90
|
+
|
|
91
|
+
KIR-P pattern graph node IDs are dense and local to each relation.
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"id": 0,
|
|
96
|
+
"kind": "open-product",
|
|
97
|
+
"edges": [{ "label": "x", "target": 1 }]
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Valid `kind` values:
|
|
102
|
+
|
|
103
|
+
- `any`
|
|
104
|
+
- `open-product`
|
|
105
|
+
- `closed-product`
|
|
106
|
+
- `open-union`
|
|
107
|
+
- `closed-union`
|
|
108
|
+
- `type`
|
|
109
|
+
|
|
110
|
+
Type nodes carry a type-code hash:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{ "id": 2, "kind": "type", "code": "@..." }
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Rules:
|
|
117
|
+
|
|
118
|
+
- Edge labels are sorted lexically.
|
|
119
|
+
- Parallel edges are represented as repeated edge records with the same label
|
|
120
|
+
and different targets.
|
|
121
|
+
- Edge targets always name nodes in the same relation pattern graph.
|
|
122
|
+
- The graph is already union-find-normalized; KIR consumers should use KIR node
|
|
123
|
+
IDs directly.
|
|
124
|
+
|
|
125
|
+
## Expression Opcodes
|
|
126
|
+
|
|
127
|
+
KIR-P relation bodies use this closed opcode set:
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{ "op": "identity", "patterns": [0, 0] }
|
|
131
|
+
{ "op": "empty", "patterns": [0, 1] }
|
|
132
|
+
{ "op": "dot", "label": "x", "patterns": [0, 1] }
|
|
133
|
+
{ "op": "div", "tag": "some", "patterns": [0, 1] }
|
|
134
|
+
{ "op": "vid", "tag": "some", "patterns": [0, 1] }
|
|
135
|
+
{ "op": "code", "code": "@...", "patterns": [0, 1] }
|
|
136
|
+
{ "op": "filter", "filter": {}, "patterns": [0, 1] }
|
|
137
|
+
{ "op": "ref", "ref": "@...", "patterns": [0, 1] }
|
|
138
|
+
{ "op": "comp", "items": [], "patterns": [0, 1] }
|
|
139
|
+
{ "op": "union", "items": [], "patterns": [0, 1] }
|
|
140
|
+
{ "op": "product", "fields": [{ "label": "x", "expr": {} }], "patterns": [0, 1] }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Rules:
|
|
144
|
+
|
|
145
|
+
- `patterns` entries are KIR pattern graph node IDs.
|
|
146
|
+
- `comp.items` preserve composition order.
|
|
147
|
+
- `union.items` preserve source branch priority.
|
|
148
|
+
- `product.fields` preserve the relation body field list. Backends may sort or
|
|
149
|
+
lay out fields later only after preserving observable semantics.
|
|
150
|
+
- `filter.filter` currently preserves the stored filter tree. A later KIR
|
|
151
|
+
revision may normalize filter internals.
|
|
152
|
+
|
|
153
|
+
## Current Tooling
|
|
154
|
+
|
|
155
|
+
Use `k-inspect-object --kir` to print the current KIR-P export:
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
k-compile program.k program.ko
|
|
159
|
+
k-inspect-object --kir program.ko
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Inside a checkout:
|
|
163
|
+
|
|
164
|
+
```sh
|
|
165
|
+
./objects/compile.mjs '()' /tmp/id.ko
|
|
166
|
+
./objects/inspect.mjs --kir /tmp/id.ko
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
This command decodes the current object format, hydrates relation pattern
|
|
170
|
+
graphs through the normal object loader, and emits the current KIR-P textual
|
|
171
|
+
encoding.
|
|
172
|
+
|
|
173
|
+
Use the conformance runner to compare source and object execution and validate
|
|
174
|
+
KIR-P export for deterministic fixtures:
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
npm run conformance
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Backend Path
|
|
181
|
+
|
|
182
|
+
The intended backend pipeline remains:
|
|
183
|
+
|
|
184
|
+
```text
|
|
185
|
+
k source
|
|
186
|
+
-> .ko / .klib object
|
|
187
|
+
-> KIR-P
|
|
188
|
+
-> envelope-specialized KIR-P for an input envelope
|
|
189
|
+
-> KIR-M / kVM
|
|
190
|
+
-> LLVM / Wasm / C / other backend
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
KIR-P is the shared semantic object contract. Envelope Specialization produces another KIR-P
|
|
194
|
+
object whose entry relation is specialized by an input envelope. KIR-M remains a
|
|
195
|
+
separate backend contract and should not be encoded by overloading KIR-P fields.
|
|
196
|
+
The current kVM lowerer consumes KIR-P relation bodies directly.
|
|
197
|
+
|
|
198
|
+
## Envelope Specialization
|
|
199
|
+
|
|
200
|
+
The current API `retypeObjectRelation(object, relationName, inputPattern)`
|
|
201
|
+
exports envelope-specialized KIR-P. The `retype...` spelling is legacy/current
|
|
202
|
+
implementation naming; new public names should use envelope specialization. The
|
|
203
|
+
pass re-runs existing type derivation as if the entry program were:
|
|
204
|
+
|
|
205
|
+
```k
|
|
206
|
+
?inputPattern relationName
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
The result is a normal KIR-P executable object:
|
|
210
|
+
|
|
211
|
+
- `layer: "KIR-P"`;
|
|
212
|
+
- `kind: "executable"`;
|
|
213
|
+
- `main` naming the specialized entry relation;
|
|
214
|
+
- relation `inputPattern` and `outputPattern` roots in the entry relation's
|
|
215
|
+
`patternGraph`;
|
|
216
|
+
- ordinary KIR relation bodies in `rels`.
|
|
217
|
+
|
|
218
|
+
This is the safe baseline: it preserves envelope-aware execution semantics while
|
|
219
|
+
making the specialized entry pattern explicit through ordinary KIR-P relation
|
|
220
|
+
typing. Relation/input-pattern cache keys and call-site worklists are local
|
|
221
|
+
compiler data; they are not part of the serialized KIR artifact.
|
|
@@ -13,23 +13,32 @@ rules for partial failure, products, unions, calls, and safe scheduling.
|
|
|
13
13
|
The current design already points at three layers:
|
|
14
14
|
|
|
15
15
|
- KIR-P: portable, polymorphic object IR.
|
|
16
|
-
-
|
|
17
|
-
-
|
|
16
|
+
- Polymorphic kVM Template: unspecialized kVM instruction streams with principal pattern signatures.
|
|
17
|
+
- Envelope-Specialized kVM: kVM functions instantiated for a concrete input envelope, with dead branches pruned and redundant guards eliminated.
|
|
18
|
+
- Target Lowering: direct emission from specialized kVM to WebAssembly or native machine code.
|
|
18
19
|
|
|
19
|
-
kVM
|
|
20
|
+
kVM bridges high-level relational expressions and concrete execution targets:
|
|
20
21
|
|
|
21
22
|
```text
|
|
23
|
+
[AOT Compilation Phase]
|
|
22
24
|
k source
|
|
23
25
|
-> AST
|
|
24
|
-
-> type derivation
|
|
25
|
-
-> KIR-P object
|
|
26
|
-
->
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
-> principal type derivation
|
|
27
|
+
-> polymorphic KIR-P object relations
|
|
28
|
+
-> polymorphic kVM template (.kvm)
|
|
29
|
+
|
|
30
|
+
[Request / JIT Phase: Enveloped Value V = (v, Pv) arrives]
|
|
31
|
+
kVM template + input envelope Pv
|
|
32
|
+
-> boundary type compatibility check (validates Pv against Pin; raises Type Error on contradiction)
|
|
33
|
+
-> fast kVM specialization (specializeKVM: guard folding, dead branch pruning, output pattern derivation)
|
|
34
|
+
-> specialized kVM function
|
|
35
|
+
-> Tier 0: direct interpretation in kVM runtime (< 1 ms cold start)
|
|
36
|
+
-> Tier 1: single-pass lowering to WebAssembly (kvm2wasm) or native machine code (~17 ms peak speed)
|
|
29
37
|
```
|
|
30
38
|
|
|
31
|
-
KIR-P remains the portable semantic object format.
|
|
32
|
-
|
|
39
|
+
KIR-P remains the portable semantic object format. The kVM lowerer compiles KIR-P into
|
|
40
|
+
polymorphic kVM templates ahead of time. When concrete input envelopes arrive at runtime,
|
|
41
|
+
`specializeKVM` instantiates and optimizes the kVM instruction stream in microseconds.
|
|
33
42
|
|
|
34
43
|
## Design Center
|
|
35
44
|
|
|
@@ -79,7 +88,7 @@ There are two valid execution modes:
|
|
|
79
88
|
|
|
80
89
|
- **Envelope-aware mode**: each `KRef` may carry a runtime pattern, matching the
|
|
81
90
|
current interpreter and codec model.
|
|
82
|
-
- **Envelope-free mode**:
|
|
91
|
+
- **Envelope-free mode**: envelope specialization has proven the input and output patterns for
|
|
83
92
|
every call site, so inner operations use static layouts and attach the derived
|
|
84
93
|
output pattern only at the boundary.
|
|
85
94
|
|
|
@@ -135,7 +144,7 @@ return %value -> ok(%value)
|
|
|
135
144
|
Composition is ordinary control flow: run the next instruction only after the
|
|
136
145
|
previous one has returned `ok`.
|
|
137
146
|
|
|
138
|
-
Filters lower either to `guard_pattern` or disappear after
|
|
147
|
+
Filters lower either to `guard_pattern` or disappear after envelope specialization proves that
|
|
139
148
|
the guard is redundant. Type/code expressions lower to guards. Variant
|
|
140
149
|
introduction lowers to `make_variant`. Product construction lowers to a
|
|
141
150
|
product region plus `make_product`.
|
|
@@ -235,7 +244,7 @@ failure that an earlier unresolved branch could have prevented. If the
|
|
|
235
244
|
contract is only result equivalence for terminating computations, product
|
|
236
245
|
fail-fast and broader cancellation are valid optimizations.
|
|
237
246
|
|
|
238
|
-
## Lowering From KIR-
|
|
247
|
+
## Lowering From Envelope-Specialized KIR-P
|
|
239
248
|
|
|
240
249
|
Suggested lowering rules:
|
|
241
250
|
|
|
@@ -256,10 +265,38 @@ Suggested lowering rules:
|
|
|
256
265
|
The lowerer should preserve labels and tags as table ids, not strings in hot
|
|
257
266
|
instructions. Debug metadata may retain source labels.
|
|
258
267
|
|
|
268
|
+
## Polymorphic kVM and JIT Specialization
|
|
269
|
+
|
|
270
|
+
A `polymorphic kVM function` is an instruction stream derived from a polymorphic
|
|
271
|
+
KIR-P relation before a concrete input envelope is known. Its input and output
|
|
272
|
+
patterns contain pattern variables (open products, open unions, or type
|
|
273
|
+
variables).
|
|
274
|
+
|
|
275
|
+
### Specialization Contract (`specializeKVM`)
|
|
276
|
+
|
|
277
|
+
When an `input enveloped value` $(v, P_V)$ arrives at runtime, `specializeKVM`:
|
|
278
|
+
|
|
279
|
+
1. **Validates Type Compatibility**:
|
|
280
|
+
Checks whether $P_V$ conforms to the function's principal input pattern.
|
|
281
|
+
If $P_V$ is disjoint or contradicts the input pattern, execution immediately
|
|
282
|
+
signals a **Type Error** (never partial failure) without compiling.
|
|
283
|
+
2. **Substitutes Pattern Variables**:
|
|
284
|
+
Binds the open pattern variables of the principal input pattern to the concrete
|
|
285
|
+
subpatterns carried in $P_V$, deriving the concrete output envelope $P_D$.
|
|
286
|
+
3. **Optimizes Guards and Eliminates Dead Code**:
|
|
287
|
+
- `guard_pattern` instructions proven satisfied by $P_V$ are rewritten to `id`
|
|
288
|
+
or erased.
|
|
289
|
+
- `guard_pattern` instructions proven unsatisfiable by $P_V$ are rewritten to `fail`.
|
|
290
|
+
- In `union` regions, branches that unconditionally fail are pruned. If an
|
|
291
|
+
earlier branch is proven to always succeed, later branches are discarded.
|
|
292
|
+
4. **Binds Concrete Layouts**:
|
|
293
|
+
Product field labels and variant tags are mapped to concrete memory offsets
|
|
294
|
+
matching $P_V$, enabling envelope-free execution in lower tiers.
|
|
295
|
+
|
|
259
296
|
## Layout Tables
|
|
260
297
|
|
|
261
298
|
kVM should not hard-code one memory layout. It should refer to layout tables
|
|
262
|
-
generated from
|
|
299
|
+
generated from envelope-specialized patterns and canonical codes.
|
|
263
300
|
|
|
264
301
|
Useful tables:
|
|
265
302
|
|
|
@@ -385,8 +422,8 @@ only after `f` has failed.
|
|
|
385
422
|
|
|
386
423
|
## Open Questions
|
|
387
424
|
|
|
388
|
-
- Should
|
|
389
|
-
|
|
425
|
+
- Should the first persisted kVM encoding be textual for inspection, compact
|
|
426
|
+
binary for embedding, or both?
|
|
390
427
|
- Should product fail-fast be the only release behavior, or should there be a
|
|
391
428
|
standard diagnostic mode that collects all failed fields?
|
|
392
429
|
- How should effectful debug intrinsics be represented so they cannot silently
|
|
@@ -399,10 +436,10 @@ only after `f` has failed.
|
|
|
399
436
|
## Implementation Order
|
|
400
437
|
|
|
401
438
|
1. Keep KIR-P as the current object-level relation format.
|
|
402
|
-
2. Define a
|
|
403
|
-
3. Lower
|
|
439
|
+
2. Define a kVM artifact schema for specialized relation instances.
|
|
440
|
+
3. Lower KIR-P relation ops to kVM in envelope-aware mode.
|
|
404
441
|
4. Add a sequential JS kVM interpreter and compare it against `run.mjs`.
|
|
405
|
-
5. Lower
|
|
442
|
+
5. Lower envelope-specialized and converged relation instances to envelope-free kVM.
|
|
406
443
|
6. Add kVM validation and conformance fixtures.
|
|
407
444
|
7. Add a minimal C or Wasm backend for sequential kVM.
|
|
408
445
|
8. Add LLVM lowering.
|
|
@@ -145,7 +145,8 @@ Standalone helpers live in `objects/`:
|
|
|
145
145
|
```sh
|
|
146
146
|
k-compile path/to/program.k path/to/program.ko
|
|
147
147
|
k-compile path/to/library.k path/to/library.klib
|
|
148
|
-
k-compile path/to/program.k path/to/program.kvm
|
|
148
|
+
k-compile --input-pattern '[["closed-product",[]]]' path/to/program.k path/to/program.kvm
|
|
149
|
+
k-compile --input-type '$ bit = <{} off, {} on>; $bit' path/to/program.k path/to/program.kvm
|
|
149
150
|
k-decompile path/to/program.ko path/to/program.decompiled.k
|
|
150
151
|
k-extract-aliases path/to/library.klib path/to/aliases.k
|
|
151
152
|
```
|