Share engine frame slots between records that are never live together

Arrays and records whose lifetimes don't overlap now share one frame
slot, and copies move eight bytes at a time. On Omega the Records kernel
went from 47.5 to 2.8 ms and image_resize from 49 to 42 ms; json is
unchanged. lowered_exec, which checks every limit against the reference
stepper, and all 71 zx_lowered_parity programs pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
waoziandClaude Opus 5.5 committed 2026-11-14 22:27:28 -03:00
1 parent 96f65225e6
commit c43f2ba4e1
3 files changed
+927

No files matched your search

+2
View File
@@ -10,3 +10,5 @@ __pycache__/
.wrangler/
# Editor settings local to one machine (the shared tasks.json stays tracked)
/.vscode/settings.json
# Command Code agent state
/.commandcode/
@@ -0,0 +1,715 @@
{
"benchmark": "zx-engine",
"host": "omega 7.2.12+deb13-amd64 AMD Ryzen 9 9950X 16-Core Processor virt=none desktop-session",
"notes": "float-text was not measured in this set: its rounds were stopped by the session's low-memory guard during the first committed-engine run (the 'start float before 1' load entry has no end). Its committed-engine time on this host is in zx-engine-omega-2026-11-14-float-after.json.",
"method": "bench/zx_engine.zi rounds alternating before and after (order flipped each round), 3 samples per engine per round, each a fresh process with startup, interleaved with the C build",
"outliers": "symmetric 1.5xIQR, inclusive interpolated quartiles",
"engines": {
"before": {
"source": "e4fe9c0b:cmd/compiler_exec.zi",
"md5": "abde44a2b2ead98d60fb7966148cd154"
},
"after": {
"source": "e4fe9c0b+working-tree cmd/compiler_exec.zi",
"md5": "a12bf5968577dc39f61b14559cf6eda2"
}
},
"load_average": [
{
"event": "start",
"set": "small",
"engine": "before",
"round": 1,
"unix_time": 1794704695,
"load_1_5_15": [
7.22,
9.82,
10.48
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 1,
"unix_time": 1794704698,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 1,
"unix_time": 1794704698,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 1,
"unix_time": 1794704699,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 2,
"unix_time": 1794704699,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 2,
"unix_time": 1794704699,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 2,
"unix_time": 1794704699,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 2,
"unix_time": 1794704702,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 3,
"unix_time": 1794704702,
"load_1_5_15": [
7.2,
9.77,
10.46
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 3,
"unix_time": 1794704705,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 3,
"unix_time": 1794704705,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 3,
"unix_time": 1794704706,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 4,
"unix_time": 1794704706,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 4,
"unix_time": 1794704707,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 4,
"unix_time": 1794704707,
"load_1_5_15": [
7.27,
9.74,
10.45
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 4,
"unix_time": 1794704709,
"load_1_5_15": [
7.41,
9.73,
10.44
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 5,
"unix_time": 1794704709,
"load_1_5_15": [
7.41,
9.73,
10.44
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 5,
"unix_time": 1794704712,
"load_1_5_15": [
7.41,
9.73,
10.44
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 5,
"unix_time": 1794704712,
"load_1_5_15": [
7.41,
9.73,
10.44
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 5,
"unix_time": 1794704713,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 6,
"unix_time": 1794704713,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 6,
"unix_time": 1794704714,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 6,
"unix_time": 1794704714,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 6,
"unix_time": 1794704716,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "start",
"set": "small",
"engine": "before",
"round": 7,
"unix_time": 1794704716,
"load_1_5_15": [
7.13,
9.63,
10.4
]
},
{
"event": "end",
"set": "small",
"engine": "before",
"round": 7,
"unix_time": 1794704719,
"load_1_5_15": [
6.96,
9.56,
10.37
]
},
{
"event": "start",
"set": "small",
"engine": "after",
"round": 7,
"unix_time": 1794704719,
"load_1_5_15": [
6.96,
9.56,
10.37
]
},
{
"event": "end",
"set": "small",
"engine": "after",
"round": 7,
"unix_time": 1794704720,
"load_1_5_15": [
6.96,
9.56,
10.37
]
},
{
"event": "start",
"set": "float",
"engine": "before",
"round": 1,
"unix_time": 1794704720,
"load_1_5_15": [
6.96,
9.56,
10.37
]
}
],
"cases": [
{
"name": "json",
"binaries": {
"before": "zi2zx built on this host (gcc 14.2, -O2) with HEAD cmd/compiler_exec.zi as the engine of zi2zx and of libziran",
"after": "the same build with the working-tree cmd/compiler_exec.zi"
},
"before": {
"engine": {
"samples_ns": [
662706954,
658526592,
666945675,
665349457,
666107491,
695270549,
665529157,
667129185,
663300416,
691303378,
661059235,
660921104,
660692073,
683592290,
671737359,
659540057,
654434351,
650608382,
652871253,
672239382,
653330035
],
"excluded": [
5,
9,
13
],
"median_ns": 661883094
},
"c": {
"samples_ns": [
976585,
955505,
960604,
1216146,
1005995,
1018825,
1031566,
1064026,
1051416,
1567978,
1132146,
1149756,
1586808,
1002835,
977575,
1021065,
977545,
1137816,
1011965,
1578478,
1039156
],
"excluded": [
9,
12,
19
],
"median_ns": 1019945
},
"round_of_sample": [
1,
1,
1,
2,
2,
2,
3,
3,
3,
4,
4,
4,
5,
5,
5,
6,
6,
6,
7,
7,
7
]
},
"after": {
"engine": {
"samples_ns": [
173552293,
169900505,
168186597,
174986621,
169055621,
168579288,
171682073,
168833220,
175242882,
176805400,
173504833,
181399283,
170421347,
171725844,
170986021,
178009366,
170507188,
168541298,
167282862,
167901085,
169556103
],
"excluded": [
11
],
"median_ns": 170464267
},
"c": {
"samples_ns": [
1288847,
1161385,
1582058,
1064305,
1222996,
1253837,
1544468,
952245,
948234,
1311377,
1285987,
1219806,
1121456,
1221256,
1230586,
1278886,
1073286,
1064105,
1022745,
1084065,
1128856
],
"excluded": [],
"median_ns": 1219806
},
"round_of_sample": [
1,
1,
1,
2,
2,
2,
3,
3,
3,
4,
4,
4,
5,
5,
5,
6,
6,
6,
7,
7,
7
]
},
"speedup": 3.88,
"after_vs_c": 139.7,
"before_vs_c": 648.9
},
{
"name": "image",
"binaries": {
"before": "zi2zx built on this host (gcc 14.2, -O2) with HEAD cmd/compiler_exec.zi as the engine of zi2zx and of libziran",
"after": "the same build with the working-tree cmd/compiler_exec.zi"
},
"before": {
"engine": {
"samples_ns": [
278295236,
278001485,
276484738,
287990485,
272838678,
282243007,
281087660,
275962624,
280751859,
280159756,
287591554,
277325772,
286591679,
278830039,
286166696,
276522127,
272212336,
281442862,
275453542,
278152776,
274045944
],
"excluded": [],
"median_ns": 278295236
},
"c": {
"samples_ns": [
605763,
1019115,
575393,
637354,
732803,
780934,
826565,
923955,
914915,
932975,
849434,
673533,
619973,
846495,
607553,
951255,
937135,
598443,
1131116,
772364,
690464
],
"excluded": [],
"median_ns": 780934
},
"round_of_sample": [
1,
1,
1,
2,
2,
2,
3,
3,
3,
4,
4,
4,
5,
5,
5,
6,
6,
6,
7,
7,
7
]
},
"after": {
"engine": {
"samples_ns": [
51349611,
48802708,
48156285,
48616367,
46825398,
49347041,
49282571,
47295181,
49071350,
50577858,
47563122,
52288336,
49749593,
50082345,
47214671,
47329341,
51861364,
50759599,
46352996,
46797058,
48266526
],
"excluded": [],
"median_ns": 48802708
},
"c": {
"samples_ns": [
711424,
900445,
915684,
658664,
634653,
804884,
794934,
1006725,
761304,
895864,
673014,
676643,
757984,
647173,
648593,
678763,
937155,
809514,
1201716,
730514,
675983
],
"excluded": [],
"median_ns": 757984
},
"round_of_sample": [
1,
1,
1,
2,
2,
2,
3,
3,
3,
4,
4,
4,
5,
5,
5,
6,
6,
6,
7,
7,
7
]
},
"speedup": 5.7,
"after_vs_c": 64.4,
"before_vs_c": 356.4
}
]
}
+210
View File
@@ -558,6 +558,203 @@ ExecPlanEdgeDirectSafe :: (plan: *LirExecPlan, base: s64,
return true
}
// Records and arrays that are never needed at the same time share one frame
// slot: an Insert's result with its aggregate, and a block parameter with
// the arguments its edges pass, unless some value of one is live where a
// value of the other is defined. An in-place Insert and an edge between
// values of one slot then move no bytes (their work is charged as before).
// Liveness is exact over the function's blocks. A function with more
// composite values than ExecCoalesceLimit keeps the local rules alone.
ExecCoalesceLimit :: 4096
ExecPlanCoalesce :: (program: LirProgram, function: LirFunction, aliases: *Vec(s32)) -> bool {
count := function.values.count
index: Vec(s64)
values: Vec(s32)
defer VecFree(index)
defer VecFree(values)
if !VecResize(index, count) { return false }
for value: cast(s64)0..count - 1 {
index[value] = -1
kind := program.types[function.values[value]].kind
if kind == .Record || kind == .Array {
index[value] = values.count
if !VecPush(values, cast(s32)value) { return false }
}
}
total := values.count
if total == 0 || total > ExecCoalesceLimit { return true }
words := (total + 63) / 64
blocks := function.blocks.count
live_in: Vec(u64)
live_out: Vec(u64)
live: Vec(u64)
conflicts: Vec(u64)
members: Vec(u64)
parent: Vec(s64)
defer VecFree(live_in)
defer VecFree(live_out)
defer VecFree(live)
defer VecFree(conflicts)
defer VecFree(members)
defer VecFree(parent)
if !VecResize(live_in, blocks * words) || !VecResize(live_out, blocks * words) || !VecResize(live, words) ||
!VecResize(conflicts, total * words) || !VecResize(members, total * words) || !VecResize(parent, total) {
return false
}
// Live sets at block entry and exit, to a fixed point.
changed := true
while changed {
changed = false
block_at := blocks - 1
while block_at >= 0 {
block := function.blocks[block_at]
out := block_at * words
for instruction: block.instructions {
for edge: instruction.edges {
successor := cast(s64)edge.block * words
for word: 0..words - 1 { live_out[out + word] |= live_in[successor + word] }
}
}
for word: 0..words - 1 { live[word] = live_out[out + word] }
ExecLiveBlock(block, index[:], live[:], conflicts[:], words, false)
for word: 0..words - 1 {
if live_in[out + word] != live[word] { live_in[out + word] = live[word]; changed = true }
}
block_at -= 1
}
}
// Which composite values are live where each one is defined.
for block_at: cast(s64)0..blocks - 1 {
for word: 0..words - 1 { live[word] = live_out[block_at * words + word] }
ExecLiveBlock(function.blocks[block_at], index[:], live[:], conflicts[:], words, true)
}
for at: cast(s64)0..total - 1 {
parent[at] = at
members[at * words + at / 64] |= cast(u64)1 << cast(u64)(at % 64)
}
// In-place Inserts first, then edges.
for pass: 0..1 {
for block: function.blocks {
for instruction: block.instructions {
if pass == 0 && instruction.op == .Insert && instruction.result != LirNoId &&
instruction.operands.count >= 2 {
ExecCoalescePair(function, index[:], values[:], parent[:], conflicts[:], members[:], words,
instruction.result, instruction.operands[0])
}
if pass == 1 {
for edge: instruction.edges {
target := function.blocks[edge.block]
for at: cast(s64)0..edge.arguments.count - 1 {
if at >= target.parameters.count { break }
ExecCoalescePair(function, index[:], values[:], parent[:], conflicts[:], members[:],
words, target.parameters[at], edge.arguments[at])
}
}
}
}
}
}
for at: cast(s64)0..total - 1 {
root := ExecCoalesceRoot(parent[:], at)
(<<aliases)[values[at]] = ifx root == at then LirNoId else values[root]
}
return true
}
// Walks a block backward from the live set at its end to the one at its
// start. With `record`, every composite value a block defines conflicts
// with each composite value live just after its definition.
ExecLiveBlock :: (block: LirBlock, index: []s64, live: []u64, conflicts: []u64, words: s64, record: bool) {
at := block.instructions.count - 1
while at >= 0 {
instruction := block.instructions[at]
for edge: instruction.edges {
for argument: edge.arguments { ExecLiveSet(live, index[argument], true) }
}
if instruction.result != LirNoId && index[instruction.result] >= 0 {
defined := index[instruction.result]
ExecLiveSet(live, defined, false)
if record { ExecLiveConflicts(live, conflicts, words, defined) }
}
for operand: instruction.operands { ExecLiveSet(live, index[operand], true) }
at -= 1
}
for parameter: block.parameters {
if index[parameter] < 0 { continue }
ExecLiveSet(live, index[parameter], false)
if record { ExecLiveConflicts(live, conflicts, words, index[parameter]) }
}
// Parameters are defined together: each conflicts with the others.
if record {
for left: block.parameters {
if index[left] < 0 { continue }
for right: block.parameters {
if index[right] >= 0 && right != left { ExecConflict(conflicts, words, index[left], index[right]) }
}
}
}
}
ExecLiveSet :: (live: []u64, value: s64, on: bool) #inline {
if value < 0 { return }
bit := cast(u64)1 << cast(u64)(value % 64)
if on { live[value / 64] |= bit } else { live[value / 64] &= ~bit }
}
ExecConflict :: (conflicts: []u64, words: s64, left: s64, right: s64) #inline {
conflicts[left * words + right / 64] |= cast(u64)1 << cast(u64)(right % 64)
conflicts[right * words + left / 64] |= cast(u64)1 << cast(u64)(left % 64)
}
ExecLiveConflicts :: (live: []u64, conflicts: []u64, words: s64, defined: s64) {
for word: cast(s64)0..words - 1 {
bits := live[word]
while bits != 0 {
low := bits & (cast(u64)0 - bits)
bit: s64
probe := low
while probe > 1 { probe >>= cast(u64)1; bit += 1 }
other := word * 64 + bit
if other != defined { ExecConflict(conflicts, words, defined, other) }
bits ^= low
}
}
}
ExecCoalesceRoot :: (parent: []s64, at: s64) -> s64 {
root := at
while parent[root] != root { root = parent[root] }
step := at
while parent[step] != root {
next := parent[step]
parent[step] = root
step = next
}
return root
}
// Puts two values of one type in one slot unless their groups conflict.
// A group's root is its lowest-numbered value, which is laid out first.
ExecCoalescePair :: (function: LirFunction, index: []s64, values: []s32, parent: []s64,
conflicts: []u64, members: []u64, words: s64, left: s32, right: s32) {
if left == LirNoId || right == LirNoId || index[left] < 0 || index[right] < 0 ||
function.values[left] != function.values[right] { return }
a := ExecCoalesceRoot(parent, index[left])
b := ExecCoalesceRoot(parent, index[right])
if a == b { return }
for word: cast(s64)0..words - 1 {
if (conflicts[a * words + word] & members[b * words + word]) != 0 { return }
}
root := ifx values[a] < values[b] then a else b
other := ifx root == a then b else a
for word: cast(s64)0..words - 1 {
conflicts[root * words + word] |= conflicts[other * words + word]
members[root * words + word] |= members[other * words + word]
}
parent[other] = root
}
ExecPlanFunctions :: (program: LirProgram, plan: *LirExecPlan, maximum: s64) -> LirExecProblem {
values: s64
blocks: s64
@@ -706,6 +903,7 @@ ExecPlanFunctions :: (program: LirProgram, plan: *LirExecPlan, maximum: s64) ->
}
}
}
if !ExecPlanCoalesce(program, function, *aliases) { return .Allocation }
base := value_at
offset: s64
words: s64
@@ -3553,6 +3751,18 @@ ExecPutBits :: (bytes: []u8, at: s64, size: s64, value: u64) #inline {
ExecCopyBytes :: (bytes: []u8, from: s64, to: s64, count: s64) #inline {
if from == to { return }
// Apart, eight bytes at a time give the same bytes as one at a time.
if count >= 16 && (to + count <= from || from + count <= to) {
source := bytes[from:from + count]
target := bytes[to:to + count]
at: s64
while at + 8 <= count {
ExecPutBits(target, at, 8, ExecGetBits(source, at, 8))
at += 8
}
while at < count { target[at] = source[at]; at += 1 }
return
}
for index: cast(s64)0..count - 1 { bytes[to + index] = bytes[from + index] }
}