@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
|
@@ -9,28 +9,26 @@ import {
|
|
|
9
9
|
} from "../src/cli.mjs";
|
|
10
10
|
import {
|
|
11
11
|
compileObjectToLLVM,
|
|
12
|
-
compileLLVMArtifactFromKVM
|
|
13
|
-
compileLLVMArtifactFromPKIR
|
|
12
|
+
compileLLVMArtifactFromKVM
|
|
14
13
|
} from "../src/llvm.mjs";
|
|
15
14
|
import { inputPatternForObjectRelation } from "../src/executable.mjs";
|
|
16
15
|
|
|
17
16
|
function helpText() {
|
|
18
17
|
const prog = argv[1] || "k-llvm-compile.mjs";
|
|
19
18
|
return [
|
|
20
|
-
"Compile a k .ko/.klib object, source,
|
|
19
|
+
"Compile a k .ko/.klib object, source, or .kvm into LLVM IR.",
|
|
21
20
|
"",
|
|
22
21
|
`Usage: ${prog} [options]`,
|
|
23
22
|
" [source-snippet | input-file [output.ll]]",
|
|
24
23
|
"",
|
|
25
24
|
"Arguments:",
|
|
26
25
|
" source-snippet Inline k source snippet (in the style of k.mjs).",
|
|
27
|
-
" input-file Source .k, .ko, .klib,
|
|
26
|
+
" input-file Source .k, .ko, .klib, or .kvm file.",
|
|
28
27
|
" Reads UTF-8 source from stdin when omitted.",
|
|
29
28
|
" output.ll Output LLVM IR path (default: stdout).",
|
|
30
29
|
"",
|
|
31
30
|
"Options:",
|
|
32
31
|
" -o, --output path Specify output file path explicitly.",
|
|
33
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
34
32
|
" --retype rel Relation to compile (default: object main).",
|
|
35
33
|
" --lib file Load one .klib or .k library before compiling.",
|
|
36
34
|
" --use spec Use library alias in scope (repeatable).",
|
|
@@ -40,7 +38,6 @@ function helpText() {
|
|
|
40
38
|
"Examples:",
|
|
41
39
|
` ${prog} 'x = |x; x x' program.ll`,
|
|
42
40
|
` ${prog} program.k -o program.ll`,
|
|
43
|
-
` ${prog} --pkir program.k -o program.ll`,
|
|
44
41
|
` ${prog} --lib core.klib --use add program.k program.ll`
|
|
45
42
|
].join("\n");
|
|
46
43
|
}
|
|
@@ -78,7 +75,7 @@ try {
|
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
|
|
81
|
-
const { libraries, useSpecs
|
|
78
|
+
const { libraries, useSpecs } =
|
|
82
79
|
parseCompileOptions(remainingArgs);
|
|
83
80
|
const input = resolveProgramInput(remainingArgs, {
|
|
84
81
|
allowStdinSource: true
|
|
@@ -89,15 +86,7 @@ try {
|
|
|
89
86
|
const outputPath = explicitOutput || positionalOutput || null;
|
|
90
87
|
|
|
91
88
|
let llvm;
|
|
92
|
-
if (input.kind === "
|
|
93
|
-
const pkir = JSON.parse(fs.readFileSync(input.path, "utf8"));
|
|
94
|
-
const entry = relation || pkir.entry || pkir.name || "__main__";
|
|
95
|
-
llvm = compileLLVMArtifactFromPKIR(pkir, {
|
|
96
|
-
relation: entry,
|
|
97
|
-
inputPattern: pkir.inputPattern,
|
|
98
|
-
outputPattern: pkir.outputPattern
|
|
99
|
-
});
|
|
100
|
-
} else if (input.kind === "kvm") {
|
|
89
|
+
if (input.kind === "kvm") {
|
|
101
90
|
const kvm = JSON.parse(fs.readFileSync(input.path, "utf8"));
|
|
102
91
|
const entry = relation || kvm.entry || kvm.relation || "__main__";
|
|
103
92
|
const mainFunc = kvm.functions?.[entry];
|
|
@@ -117,8 +106,7 @@ try {
|
|
|
117
106
|
const targetRel = relation || object.main;
|
|
118
107
|
const res = compileObjectToLLVM(object, {
|
|
119
108
|
relation: targetRel,
|
|
120
|
-
inputPattern: inputPatternForObjectRelation(object, targetRel)
|
|
121
|
-
usePKIR
|
|
109
|
+
inputPattern: inputPatternForObjectRelation(object, targetRel)
|
|
122
110
|
});
|
|
123
111
|
llvm = res.llvm;
|
|
124
112
|
}
|
|
@@ -17,8 +17,7 @@ import {
|
|
|
17
17
|
} from "../src/cli.mjs";
|
|
18
18
|
import {
|
|
19
19
|
compileKVMToExecutable,
|
|
20
|
-
compileObjectToExecutable
|
|
21
|
-
compilePKIRToExecutable
|
|
20
|
+
compileObjectToExecutable
|
|
22
21
|
} from "../src/executable.mjs";
|
|
23
22
|
|
|
24
23
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
@@ -27,7 +26,7 @@ function helpText() {
|
|
|
27
26
|
const prog = argv[1] || "k-llvm.mjs";
|
|
28
27
|
const defaultCache = path.join(os.tmpdir(), "k-llvm-cache");
|
|
29
28
|
return [
|
|
30
|
-
"Compile k source, .ko,
|
|
29
|
+
"Compile k source, .ko, or .kvm to native machine code via",
|
|
31
30
|
"LLVM and execute it over a binary pattern+value wire stream.",
|
|
32
31
|
"",
|
|
33
32
|
`Usage: ${prog} [options]`,
|
|
@@ -35,14 +34,13 @@ function helpText() {
|
|
|
35
34
|
"",
|
|
36
35
|
"Arguments:",
|
|
37
36
|
" source-snippet Inline k source snippet (in the style of k.mjs).",
|
|
38
|
-
" input-file Source .k, .ko, .klib,
|
|
37
|
+
" input-file Source .k, .ko, .klib, or .kvm file.",
|
|
39
38
|
" input-wire-file Optional binary wire input (default: stdin).",
|
|
40
39
|
"",
|
|
41
40
|
"Options:",
|
|
42
41
|
" --lib file Load one .klib or .k library before compiling.",
|
|
43
42
|
" --use spec Use library alias in scope (repeatable).",
|
|
44
43
|
" spec is 'name' or 'libname:localname'.",
|
|
45
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
46
44
|
" --retype rel Relation to specialize (default: object main).",
|
|
47
45
|
" --cache-dir path Directory to store compiled native executables.",
|
|
48
46
|
" --no-cache Compile in a temporary directory and discard.",
|
|
@@ -95,7 +93,6 @@ function computeCacheKey({
|
|
|
95
93
|
inputKey,
|
|
96
94
|
relation,
|
|
97
95
|
inputPattern,
|
|
98
|
-
usePKIR,
|
|
99
96
|
clangOpt
|
|
100
97
|
}) {
|
|
101
98
|
const hash = crypto.createHash("sha256");
|
|
@@ -106,7 +103,7 @@ function computeCacheKey({
|
|
|
106
103
|
hash.update("\0");
|
|
107
104
|
hash.update(JSON.stringify(inputPattern || null));
|
|
108
105
|
hash.update("\0");
|
|
109
|
-
hash.update(
|
|
106
|
+
hash.update("monomorphic\0");
|
|
110
107
|
hash.update(String(clangOpt || ""));
|
|
111
108
|
hash.update("\0");
|
|
112
109
|
hash.update(inputKey);
|
|
@@ -174,7 +171,7 @@ async function main() {
|
|
|
174
171
|
}
|
|
175
172
|
}
|
|
176
173
|
|
|
177
|
-
const { libraries, useSpecs
|
|
174
|
+
const { libraries, useSpecs } =
|
|
178
175
|
parseCompileOptions(remainingArgs);
|
|
179
176
|
const programInput = resolveProgramInput(remainingArgs);
|
|
180
177
|
const inputWirePath = remainingArgs.shift();
|
|
@@ -207,28 +204,18 @@ async function main() {
|
|
|
207
204
|
inputKey,
|
|
208
205
|
relation,
|
|
209
206
|
inputPattern: inputEnvelopePattern,
|
|
210
|
-
usePKIR,
|
|
211
207
|
clangOpt
|
|
212
208
|
});
|
|
213
209
|
exePath = path.join(cacheDir, `${key}.exe`);
|
|
214
210
|
if (!fs.existsSync(exePath)) {
|
|
215
211
|
const tmpPath = path.join(cacheDir, `${key}.${process.pid}.tmp`);
|
|
216
|
-
if (programInput.kind === "
|
|
217
|
-
const pkir = JSON.parse(
|
|
218
|
-
fs.readFileSync(programInput.path, "utf8")
|
|
219
|
-
);
|
|
220
|
-
compilePKIRToExecutable(pkir, tmpPath, {
|
|
221
|
-
inputPattern: inputEnvelopePattern,
|
|
222
|
-
clangOpt
|
|
223
|
-
});
|
|
224
|
-
} else if (programInput.kind === "kvm") {
|
|
212
|
+
if (programInput.kind === "kvm") {
|
|
225
213
|
const kvm = JSON.parse(
|
|
226
214
|
fs.readFileSync(programInput.path, "utf8")
|
|
227
215
|
);
|
|
228
216
|
compileKVMToExecutable(kvm, tmpPath, {
|
|
229
217
|
inputPattern: inputEnvelopePattern,
|
|
230
|
-
clangOpt
|
|
231
|
-
usePKIR
|
|
218
|
+
clangOpt
|
|
232
219
|
});
|
|
233
220
|
} else {
|
|
234
221
|
const object = await compileProgramInputToObject(programInput, {
|
|
@@ -239,8 +226,7 @@ async function main() {
|
|
|
239
226
|
compileObjectToExecutable(object, tmpPath, {
|
|
240
227
|
relation: relation || object.main,
|
|
241
228
|
inputPattern: inputEnvelopePattern,
|
|
242
|
-
clangOpt
|
|
243
|
-
usePKIR
|
|
229
|
+
clangOpt
|
|
244
230
|
});
|
|
245
231
|
}
|
|
246
232
|
fs.renameSync(tmpPath, exePath);
|
|
@@ -248,18 +234,11 @@ async function main() {
|
|
|
248
234
|
} else {
|
|
249
235
|
tempExeDir = fs.mkdtempSync(path.join(os.tmpdir(), "k-llvm-run-"));
|
|
250
236
|
exePath = path.join(tempExeDir, "prog.exe");
|
|
251
|
-
if (programInput.kind === "
|
|
252
|
-
const pkir = JSON.parse(fs.readFileSync(programInput.path, "utf8"));
|
|
253
|
-
compilePKIRToExecutable(pkir, exePath, {
|
|
254
|
-
inputPattern: inputEnvelopePattern,
|
|
255
|
-
clangOpt
|
|
256
|
-
});
|
|
257
|
-
} else if (programInput.kind === "kvm") {
|
|
237
|
+
if (programInput.kind === "kvm") {
|
|
258
238
|
const kvm = JSON.parse(fs.readFileSync(programInput.path, "utf8"));
|
|
259
239
|
compileKVMToExecutable(kvm, exePath, {
|
|
260
240
|
inputPattern: inputEnvelopePattern,
|
|
261
|
-
clangOpt
|
|
262
|
-
usePKIR
|
|
241
|
+
clangOpt
|
|
263
242
|
});
|
|
264
243
|
} else {
|
|
265
244
|
const object = await compileProgramInputToObject(programInput, {
|
|
@@ -270,8 +249,7 @@ async function main() {
|
|
|
270
249
|
compileObjectToExecutable(object, exePath, {
|
|
271
250
|
relation: relation || object.main,
|
|
272
251
|
inputPattern: inputEnvelopePattern,
|
|
273
|
-
clangOpt
|
|
274
|
-
usePKIR
|
|
252
|
+
clangOpt
|
|
275
253
|
});
|
|
276
254
|
}
|
|
277
255
|
}
|
|
@@ -37,7 +37,6 @@ export function parseCompileOptions(args) {
|
|
|
37
37
|
const libraries = [];
|
|
38
38
|
const useSpecs = [];
|
|
39
39
|
let loadedLibrary = false;
|
|
40
|
-
let usePKIR = false;
|
|
41
40
|
|
|
42
41
|
while (args.length > 0) {
|
|
43
42
|
if (args[0] === "--") {
|
|
@@ -62,9 +61,6 @@ export function parseCompileOptions(args) {
|
|
|
62
61
|
);
|
|
63
62
|
}
|
|
64
63
|
useSpecs.push(spec);
|
|
65
|
-
} else if (args[0] === "--pkir") {
|
|
66
|
-
args.shift();
|
|
67
|
-
usePKIR = true;
|
|
68
64
|
} else if (args[0].startsWith("--")) {
|
|
69
65
|
break;
|
|
70
66
|
} else {
|
|
@@ -72,7 +68,7 @@ export function parseCompileOptions(args) {
|
|
|
72
68
|
}
|
|
73
69
|
}
|
|
74
70
|
|
|
75
|
-
return { libraries, useSpecs
|
|
71
|
+
return { libraries, useSpecs };
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
export function fileInputType(filePath) {
|
|
@@ -80,7 +76,6 @@ export function fileInputType(filePath) {
|
|
|
80
76
|
if (ext === ".ko") return "ko";
|
|
81
77
|
if (ext === ".kvm") return "kvm";
|
|
82
78
|
if (ext === ".klib") return "klib";
|
|
83
|
-
if (ext === ".pkir") return "pkir";
|
|
84
79
|
return "source-file";
|
|
85
80
|
}
|
|
86
81
|
|
|
@@ -97,7 +92,7 @@ export function resolveProgramInput(args, { allowStdinSource = false } = {}) {
|
|
|
97
92
|
return { kind, path: inputArg, sourceName: inputArg };
|
|
98
93
|
}
|
|
99
94
|
|
|
100
|
-
if ([".k", ".ko", ".kvm", ".klib"
|
|
95
|
+
if ([".k", ".ko", ".kvm", ".klib"].includes(ext)) {
|
|
101
96
|
throw new Error(`Input file not found: ${inputArg}`);
|
|
102
97
|
}
|
|
103
98
|
|
|
@@ -149,11 +144,6 @@ export async function compileProgramInputToObject(
|
|
|
149
144
|
"Input is already lowered to .kvm bytecode; use " +
|
|
150
145
|
"compileLLVMArtifactFromKVM or compileKVMToExecutable."
|
|
151
146
|
);
|
|
152
|
-
} else if (input.kind === "pkir") {
|
|
153
|
-
throw new Error(
|
|
154
|
-
"Input is already lowered to .pkir IR; use " +
|
|
155
|
-
"compileLLVMArtifactFromPKIR or compilePKIRToExecutable."
|
|
156
|
-
);
|
|
157
147
|
} else {
|
|
158
148
|
throw new Error(`Unsupported input kind: ${input.kind}`);
|
|
159
149
|
}
|
|
@@ -7,8 +7,7 @@ import { isProduct, isVariant } from "@fraczak/k/Value.mjs";
|
|
|
7
7
|
import { exportPatternGraph } from "@fraczak/k/codecs/runtime/codec.mjs";
|
|
8
8
|
import { patternToPropertyList } from "@fraczak/k/codecs/runtime/pattern-json.mjs";
|
|
9
9
|
import { specializeKVM } from "@fraczak/k/kvm.mjs";
|
|
10
|
-
import {
|
|
11
|
-
import { compileObjectToLLVM, compileLLVMArtifactFromPKIR } from "./llvm.mjs";
|
|
10
|
+
import { compileObjectToLLVM } from "./llvm.mjs";
|
|
12
11
|
import { compileKVMModuleToLLVM } from "./kvm2llvm.mjs";
|
|
13
12
|
|
|
14
13
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
@@ -557,8 +556,7 @@ export function compileObjectToExecutable(
|
|
|
557
556
|
relation = object.main,
|
|
558
557
|
inputPattern = null,
|
|
559
558
|
runtimeMode = "fast",
|
|
560
|
-
clangOpt = "-O3"
|
|
561
|
-
usePKIR = false
|
|
559
|
+
clangOpt = "-O3"
|
|
562
560
|
} = {}
|
|
563
561
|
) {
|
|
564
562
|
const { inputPattern: compiledInputPattern, outputPattern, llvm } =
|
|
@@ -566,8 +564,7 @@ export function compileObjectToExecutable(
|
|
|
566
564
|
relation,
|
|
567
565
|
inputPattern:
|
|
568
566
|
inputPattern || inputPatternForObjectRelation(object, relation),
|
|
569
|
-
runtimeMode
|
|
570
|
-
usePKIR
|
|
567
|
+
runtimeMode
|
|
571
568
|
});
|
|
572
569
|
compileLLVMToExecutable(llvm, outputPath, {
|
|
573
570
|
driver: stdioDriverSource({
|
|
@@ -582,8 +579,7 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
582
579
|
entry = null,
|
|
583
580
|
inputPattern = null,
|
|
584
581
|
runtimeMode = "fast",
|
|
585
|
-
clangOpt = "-O3"
|
|
586
|
-
usePKIR = false
|
|
582
|
+
clangOpt = "-O3"
|
|
587
583
|
} = {}) {
|
|
588
584
|
let kvm = kvmInput;
|
|
589
585
|
if (kvm?.format === "k-vm") {
|
|
@@ -602,8 +598,7 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
602
598
|
relation: entryFunc,
|
|
603
599
|
inputPattern: compiledInputPattern,
|
|
604
600
|
outputPattern,
|
|
605
|
-
runtimeMode
|
|
606
|
-
usePKIR
|
|
601
|
+
runtimeMode
|
|
607
602
|
});
|
|
608
603
|
compileLLVMToExecutable(llvm, outputPath, {
|
|
609
604
|
driver: stdioDriverSource({
|
|
@@ -614,102 +609,19 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
614
609
|
});
|
|
615
610
|
}
|
|
616
611
|
|
|
617
|
-
export function compilePKIRToExecutable(
|
|
618
|
-
pkirInput,
|
|
619
|
-
outputPath,
|
|
620
|
-
{
|
|
621
|
-
entry = null,
|
|
622
|
-
inputPattern = null,
|
|
623
|
-
runtimeMode = "fast",
|
|
624
|
-
clangOpt = "-O3"
|
|
625
|
-
} = {}
|
|
626
|
-
) {
|
|
627
|
-
let kvmProgram;
|
|
628
|
-
let entryFunc;
|
|
629
|
-
if (
|
|
630
|
-
(pkirInput.format === "pk-ir" || pkirInput.format === "k-pkir") &&
|
|
631
|
-
pkirInput.functions
|
|
632
|
-
) {
|
|
633
|
-
kvmProgram = {};
|
|
634
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
635
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
636
|
-
}
|
|
637
|
-
entryFunc =
|
|
638
|
-
entry ||
|
|
639
|
-
pkirInput.entry ||
|
|
640
|
-
Object.keys(kvmProgram)[0] ||
|
|
641
|
-
"__main__";
|
|
642
|
-
} else if (pkirInput.instructions) {
|
|
643
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
644
|
-
entryFunc = pkirInput.name || entry || "__main__";
|
|
645
|
-
kvmProgram = { [entryFunc]: kvm };
|
|
646
|
-
const scan = (insts) => {
|
|
647
|
-
for (const inst of insts || []) {
|
|
648
|
-
if (inst.op === "call" && inst.func && !kvmProgram[inst.func]) {
|
|
649
|
-
kvmProgram[inst.func] = kvm;
|
|
650
|
-
}
|
|
651
|
-
if (inst.branches) {
|
|
652
|
-
for (const b of inst.branches) scan(b.body);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
scan(kvm.body);
|
|
657
|
-
} else if (pkirInput.functions) {
|
|
658
|
-
kvmProgram = {};
|
|
659
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
660
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
661
|
-
}
|
|
662
|
-
entryFunc =
|
|
663
|
-
entry ||
|
|
664
|
-
pkirInput.entry ||
|
|
665
|
-
Object.keys(kvmProgram)[0] ||
|
|
666
|
-
"__main__";
|
|
667
|
-
} else {
|
|
668
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
669
|
-
entryFunc = pkirInput.name || entry || "__main__";
|
|
670
|
-
kvmProgram = { [entryFunc]: kvm };
|
|
671
|
-
}
|
|
672
|
-
return compileKVMToExecutable(
|
|
673
|
-
{
|
|
674
|
-
format: "k-vm",
|
|
675
|
-
layer: "KVM",
|
|
676
|
-
entry: entryFunc,
|
|
677
|
-
functions: kvmProgram
|
|
678
|
-
},
|
|
679
|
-
outputPath,
|
|
680
|
-
{
|
|
681
|
-
entry: entryFunc,
|
|
682
|
-
inputPattern,
|
|
683
|
-
runtimeMode,
|
|
684
|
-
clangOpt,
|
|
685
|
-
usePKIR: true
|
|
686
|
-
}
|
|
687
|
-
);
|
|
688
|
-
}
|
|
689
|
-
|
|
690
612
|
export function compileObjectAndRun(
|
|
691
613
|
object,
|
|
692
614
|
{
|
|
693
615
|
relation = object.main,
|
|
694
616
|
input,
|
|
695
617
|
expected = null,
|
|
696
|
-
inputPattern = null
|
|
697
|
-
usePKIR = false
|
|
618
|
+
inputPattern = null
|
|
698
619
|
}
|
|
699
620
|
) {
|
|
700
621
|
const { llvm } = compileObjectToLLVM(object, {
|
|
701
622
|
relation,
|
|
702
623
|
inputPattern:
|
|
703
|
-
inputPattern || inputPatternForObjectValue(object, input, relation)
|
|
704
|
-
usePKIR
|
|
624
|
+
inputPattern || inputPatternForObjectValue(object, input, relation)
|
|
705
625
|
});
|
|
706
626
|
return compileAndRunLLVM(llvm, { input, expected });
|
|
707
627
|
}
|
|
708
|
-
|
|
709
|
-
export function compilePKIRAndRun(
|
|
710
|
-
pkirInput,
|
|
711
|
-
{ input, expected = null, options = {} } = {}
|
|
712
|
-
) {
|
|
713
|
-
const llvm = compileLLVMArtifactFromPKIR(pkirInput, options);
|
|
714
|
-
return compileAndRunLLVM(llvm, { input, expected });
|
|
715
|
-
}
|
|
@@ -27,7 +27,6 @@ const K_RT_BLOCKS_OFFSET = 0;
|
|
|
27
27
|
const K_RT_UNIT_CACHE_OFFSET = 8;
|
|
28
28
|
const K_RT_BIT0_CACHE_OFFSET = 16;
|
|
29
29
|
const K_RT_BIT1_CACHE_OFFSET = 24;
|
|
30
|
-
const K_RT_HAS_REUSABLE_BLOCKS_OFFSET = 32;
|
|
31
30
|
const K_ARENA_BLOCK_USED_OFFSET = 8;
|
|
32
31
|
|
|
33
32
|
function alignArenaSize(size) {
|
|
@@ -303,7 +302,6 @@ function lowerRtRewind(ctx, mark) {
|
|
|
303
302
|
storePtrAt(ctx, "%rt", K_RT_UNIT_CACHE_OFFSET, "null");
|
|
304
303
|
storePtrAt(ctx, "%rt", K_RT_BIT0_CACHE_OFFSET, "null");
|
|
305
304
|
storePtrAt(ctx, "%rt", K_RT_BIT1_CACHE_OFFSET, "null");
|
|
306
|
-
storeI32At(ctx, "%rt", K_RT_HAS_REUSABLE_BLOCKS_OFFSET, 0);
|
|
307
305
|
ctx.lines.push(` br label %${doneBlock}`);
|
|
308
306
|
|
|
309
307
|
ctx.lines.push(`${slowBlock}:`);
|
|
@@ -457,9 +455,7 @@ function getFieldEdgeIndex(inst, regPatterns, kvmFunc, ctx = {}) {
|
|
|
457
455
|
const srcPattern = regPatterns.get(srcReg) || (srcReg === "in" ? kvmFunc.inputPattern : null);
|
|
458
456
|
if (Array.isArray(srcPattern) && srcPattern.length > 0) {
|
|
459
457
|
const root = srcPattern[0];
|
|
460
|
-
const allowed =
|
|
461
|
-
? root[0] === "closed-product"
|
|
462
|
-
: (root[0] === "open-product" || root[0] === "closed-product");
|
|
458
|
+
const allowed = root[0] === "open-product" || root[0] === "closed-product";
|
|
463
459
|
if (allowed && Array.isArray(root[1])) {
|
|
464
460
|
const idx = root[1].findIndex(([l]) => l === inst.label);
|
|
465
461
|
if (idx >= 0) return idx;
|
|
@@ -518,7 +514,6 @@ export function compileKVMModuleToLLVM(entryName, kvmProgram, options = {}) {
|
|
|
518
514
|
labels,
|
|
519
515
|
functionNames,
|
|
520
516
|
runtimeMode,
|
|
521
|
-
usePKIR: Boolean(options.usePKIR),
|
|
522
517
|
armIndex: 0,
|
|
523
518
|
unionExprIndex: 0,
|
|
524
519
|
syntheticFunctions: [],
|
|
@@ -589,7 +584,6 @@ function lowerKVMFunction(kvmFunc, symbol, funcName, moduleCtx, linkage = "", op
|
|
|
589
584
|
labels: moduleCtx.labels,
|
|
590
585
|
functionNames: moduleCtx.functionNames,
|
|
591
586
|
runtimeMode: moduleCtx.runtimeMode,
|
|
592
|
-
usePKIR: moduleCtx.usePKIR,
|
|
593
587
|
allocas: [],
|
|
594
588
|
lines: [],
|
|
595
589
|
currentBlock: "entry",
|
|
@@ -7,11 +7,7 @@ import {
|
|
|
7
7
|
objectToKVMArtifact,
|
|
8
8
|
specializeKVM,
|
|
9
9
|
lowerKIRToKVM,
|
|
10
|
-
objectToKIRP
|
|
11
|
-
compileRelationToPKIR,
|
|
12
|
-
lowerPKIRToKVM,
|
|
13
|
-
exportPatternGraph,
|
|
14
|
-
patternToPropertyList
|
|
10
|
+
objectToKIRP
|
|
15
11
|
} from "@fraczak/k/backend-api.mjs";
|
|
16
12
|
import { compileKVMModuleToLLVM } from "./kvm2llvm.mjs";
|
|
17
13
|
|
|
@@ -39,7 +35,6 @@ const K_RT_BLOCKS_OFFSET = 0;
|
|
|
39
35
|
const K_RT_UNIT_CACHE_OFFSET = 8;
|
|
40
36
|
const K_RT_BIT0_CACHE_OFFSET = 16;
|
|
41
37
|
const K_RT_BIT1_CACHE_OFFSET = 24;
|
|
42
|
-
const K_RT_HAS_REUSABLE_BLOCKS_OFFSET = 32;
|
|
43
38
|
const K_ARENA_BLOCK_USED_OFFSET = 8;
|
|
44
39
|
const K_ARENA_BLOCK_CAPACITY_OFFSET = 16;
|
|
45
40
|
const K_ARENA_BLOCK_DATA_OFFSET = 24;
|
|
@@ -418,7 +413,6 @@ function lowerRtRewind(ctx, mark) {
|
|
|
418
413
|
storePtrAt(ctx, "%rt", K_RT_UNIT_CACHE_OFFSET, "null");
|
|
419
414
|
storePtrAt(ctx, "%rt", K_RT_BIT0_CACHE_OFFSET, "null");
|
|
420
415
|
storePtrAt(ctx, "%rt", K_RT_BIT1_CACHE_OFFSET, "null");
|
|
421
|
-
storeI32At(ctx, "%rt", K_RT_HAS_REUSABLE_BLOCKS_OFFSET, 0);
|
|
422
416
|
ctx.lines.push(` br label %${doneBlock}`);
|
|
423
417
|
|
|
424
418
|
ctx.lines.push(`${slowBlock}:`);
|
|
@@ -1024,26 +1018,7 @@ export function compileObjectToLLVM(object, options = {}) {
|
|
|
1024
1018
|
let entryInputPattern = inputPattern;
|
|
1025
1019
|
let outputPattern = null;
|
|
1026
1020
|
|
|
1027
|
-
if (
|
|
1028
|
-
const rel = object.rels?.[relation] ||
|
|
1029
|
-
(object.relAlias?.[relation] &&
|
|
1030
|
-
object.rels?.[object.relAlias[relation]]) ||
|
|
1031
|
-
object.rels?.[object.main];
|
|
1032
|
-
if (rel?.typePatternGraph) {
|
|
1033
|
-
const graph = rel.typePatternGraph;
|
|
1034
|
-
if (!entryInputPattern &&
|
|
1035
|
-
rel.def?.patterns?.[0] !== undefined) {
|
|
1036
|
-
const inId = graph.find(rel.def.patterns[0]);
|
|
1037
|
-
entryInputPattern =
|
|
1038
|
-
patternToPropertyList(exportPatternGraph(graph, inId));
|
|
1039
|
-
}
|
|
1040
|
-
if (rel.def?.patterns?.[1] !== undefined) {
|
|
1041
|
-
const outId = graph.find(rel.def.patterns[1]);
|
|
1042
|
-
outputPattern =
|
|
1043
|
-
patternToPropertyList(exportPatternGraph(graph, outId));
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
} else if (inputPattern) {
|
|
1021
|
+
if (inputPattern) {
|
|
1047
1022
|
const retyped = retypeObjectRelationForBackend(
|
|
1048
1023
|
object, relation, inputPattern, {
|
|
1049
1024
|
source: options.source || "<k-llvm>"
|
|
@@ -1054,35 +1029,9 @@ export function compileObjectToLLVM(object, options = {}) {
|
|
|
1054
1029
|
outputPattern = retyped.outputPattern;
|
|
1055
1030
|
}
|
|
1056
1031
|
|
|
1057
|
-
let functions;
|
|
1058
|
-
let retypedKir = null;
|
|
1059
1032
|
const entry = relation || targetObject.main || "__main__";
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
functions = {};
|
|
1063
|
-
for (const [name, relDef] of Object.entries(targetObject.rels || {})) {
|
|
1064
|
-
const pkir = compileRelationToPKIR(relDef, name);
|
|
1065
|
-
functions[name] = lowerPKIRToKVM(pkir);
|
|
1066
|
-
}
|
|
1067
|
-
for (const [alias, target] of Object.entries(targetObject.relAlias || {})) {
|
|
1068
|
-
if (functions[target] && !functions[alias]) {
|
|
1069
|
-
functions[alias] = functions[target];
|
|
1070
|
-
}
|
|
1071
|
-
const stripped = target.startsWith("@") ? target.slice(1) : target;
|
|
1072
|
-
if (functions[stripped] && !functions[alias]) {
|
|
1073
|
-
functions[alias] = functions[stripped];
|
|
1074
|
-
}
|
|
1075
|
-
if (functions[alias] && !functions[target]) {
|
|
1076
|
-
functions[target] = functions[alias];
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
if (relation && !functions[relation] && functions[targetObject.main]) {
|
|
1080
|
-
functions[relation] = functions[targetObject.main];
|
|
1081
|
-
}
|
|
1082
|
-
} else {
|
|
1083
|
-
retypedKir = objectToKIRP(targetObject);
|
|
1084
|
-
functions = lowerKIRToKVM(retypedKir);
|
|
1085
|
-
}
|
|
1033
|
+
const retypedKir = objectToKIRP(targetObject);
|
|
1034
|
+
const functions = lowerKIRToKVM(retypedKir);
|
|
1086
1035
|
|
|
1087
1036
|
const specializedKvm = {
|
|
1088
1037
|
format: "k-vm",
|
|
@@ -1120,63 +1069,6 @@ export function compileLLVMArtifactFromKVM(kvmInput, options = {}) {
|
|
|
1120
1069
|
return compileKVMModuleToLLVM(entry, functions, options);
|
|
1121
1070
|
}
|
|
1122
1071
|
|
|
1123
|
-
export function compileLLVMArtifactFromPKIR(pkirInput, options = {}) {
|
|
1124
|
-
let kvmProgram;
|
|
1125
|
-
let entry;
|
|
1126
|
-
if (
|
|
1127
|
-
(pkirInput.format === "pk-ir" || pkirInput.format === "k-pkir") &&
|
|
1128
|
-
pkirInput.functions
|
|
1129
|
-
) {
|
|
1130
|
-
entry =
|
|
1131
|
-
pkirInput.entry ||
|
|
1132
|
-
options.entry ||
|
|
1133
|
-
Object.keys(pkirInput.functions)[0] ||
|
|
1134
|
-
"__main__";
|
|
1135
|
-
kvmProgram = {};
|
|
1136
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
1137
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
1138
|
-
}
|
|
1139
|
-
} else if (pkirInput.instructions) {
|
|
1140
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
1141
|
-
entry = pkirInput.name || options.entry || "__main__";
|
|
1142
|
-
kvmProgram = { [entry]: kvm };
|
|
1143
|
-
const scan = (insts) => {
|
|
1144
|
-
for (const inst of insts || []) {
|
|
1145
|
-
if (inst.op === "call" && inst.func && !kvmProgram[inst.func]) {
|
|
1146
|
-
kvmProgram[inst.func] = kvm;
|
|
1147
|
-
}
|
|
1148
|
-
if (inst.branches) {
|
|
1149
|
-
for (const b of inst.branches) scan(b.body);
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
};
|
|
1153
|
-
scan(kvm.body);
|
|
1154
|
-
} else if (pkirInput.functions) {
|
|
1155
|
-
entry =
|
|
1156
|
-
pkirInput.entry ||
|
|
1157
|
-
options.entry ||
|
|
1158
|
-
Object.keys(pkirInput.functions)[0] ||
|
|
1159
|
-
"__main__";
|
|
1160
|
-
kvmProgram = {};
|
|
1161
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
1162
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
1163
|
-
}
|
|
1164
|
-
} else {
|
|
1165
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
1166
|
-
entry = pkirInput.name || options.entry || "__main__";
|
|
1167
|
-
kvmProgram = { [entry]: kvm };
|
|
1168
|
-
}
|
|
1169
|
-
return compileLLVMArtifactFromKVM(
|
|
1170
|
-
{
|
|
1171
|
-
format: "k-vm",
|
|
1172
|
-
layer: "KVM",
|
|
1173
|
-
entry,
|
|
1174
|
-
functions: kvmProgram
|
|
1175
|
-
},
|
|
1176
|
-
options
|
|
1177
|
-
);
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
1072
|
export function compileLLVMArtifactFromObject(object, options = {}) {
|
|
1181
1073
|
return compileObjectToLLVM(object, options);
|
|
1182
1074
|
}
|
|
@@ -1192,7 +1084,6 @@ export default {
|
|
|
1192
1084
|
compileBufferToLLVM,
|
|
1193
1085
|
compileObjectToLLVM,
|
|
1194
1086
|
compileLLVMArtifactFromKVM,
|
|
1195
|
-
compileLLVMArtifactFromPKIR,
|
|
1196
1087
|
compileLLVMArtifactFromObject,
|
|
1197
1088
|
emitLLVMModule,
|
|
1198
1089
|
llvmIdentifier
|
|
@@ -21,7 +21,6 @@ function helpText() {
|
|
|
21
21
|
" --retype rel Relation to specialize (default: main).",
|
|
22
22
|
" --input-pattern value Input pattern property-list JSON or file.",
|
|
23
23
|
" --expect value Expected output value JSON/text or file.",
|
|
24
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
25
24
|
" -h, --help Show this help.",
|
|
26
25
|
"",
|
|
27
26
|
"Without --expect, the runner prints the result as compact JSON.",
|
|
@@ -66,7 +65,6 @@ try {
|
|
|
66
65
|
let relation = null;
|
|
67
66
|
let inputPattern = null;
|
|
68
67
|
let expectedText = null;
|
|
69
|
-
let usePKIR = false;
|
|
70
68
|
while (args.length > 0 && args[0].startsWith("--")) {
|
|
71
69
|
const option = args.shift();
|
|
72
70
|
if (option === "--retype") {
|
|
@@ -82,8 +80,6 @@ try {
|
|
|
82
80
|
if (!expectedText) {
|
|
83
81
|
throw new Error("--expect requires a value or file path");
|
|
84
82
|
}
|
|
85
|
-
} else if (option === "--pkir") {
|
|
86
|
-
usePKIR = true;
|
|
87
83
|
} else {
|
|
88
84
|
throw new Error(`Unknown option: ${option}`);
|
|
89
85
|
}
|
|
@@ -106,8 +102,7 @@ try {
|
|
|
106
102
|
relation: relation || object.main,
|
|
107
103
|
input,
|
|
108
104
|
expected,
|
|
109
|
-
inputPattern: readPattern(inputPattern)
|
|
110
|
-
usePKIR
|
|
105
|
+
inputPattern: readPattern(inputPattern)
|
|
111
106
|
});
|
|
112
107
|
stdout.write(expected == null ? result.stdout : "OK\n");
|
|
113
108
|
} catch (error) {
|
|
@@ -156,8 +156,7 @@ export function compileCaseExecutable({
|
|
|
156
156
|
cacheDir,
|
|
157
157
|
sourceLabel,
|
|
158
158
|
runtimeMode = "fast",
|
|
159
|
-
clangOpt = "-O3"
|
|
160
|
-
usePKIR = false
|
|
159
|
+
clangOpt = "-O3"
|
|
161
160
|
}) {
|
|
162
161
|
const key = sha256([
|
|
163
162
|
"k-llvm-perf-v1",
|
|
@@ -168,7 +167,7 @@ export function compileCaseExecutable({
|
|
|
168
167
|
relationName,
|
|
169
168
|
relHash,
|
|
170
169
|
JSON.stringify(inputPattern),
|
|
171
|
-
|
|
170
|
+
"kir"
|
|
172
171
|
].join("\0"));
|
|
173
172
|
const exePath = path.join(cacheDir, `${key}.exe`);
|
|
174
173
|
if (!fs.existsSync(exePath)) {
|
|
@@ -180,8 +179,7 @@ export function compileCaseExecutable({
|
|
|
180
179
|
relation: relationName,
|
|
181
180
|
inputPattern,
|
|
182
181
|
runtimeMode,
|
|
183
|
-
clangOpt
|
|
184
|
-
usePKIR
|
|
182
|
+
clangOpt
|
|
185
183
|
}
|
|
186
184
|
);
|
|
187
185
|
fs.renameSync(tmpPath, exePath);
|