feat(v4): Phase 7 Task 3b -- compudynamics on the 32 F18 opcodes as on the words, behind OPCODE_PHYSICS (#189)

Captain Bob, 2026-11-14: "we're supposed to be working on f18 opcode as well as our composed f18
dictionary"; ruled every loop on both, "and an easy revert if opcodes are too granular".

- The engine: a hook at retire (exec.h on_retire), as Part A's at call; NULL unless set. test_exec.c.
- The mesh: 32 opcode records per node, an opcode window, opcode transitions under L4; at every retired
  instruction decay at the opcodes' own slope, heat, temperature, the window, the transition; in each cycle
  all 32 cooled, the opcode window's service, inference on its heat. L8 scores from the words and its gates
  govern both; the tick stays paced by word calls. The window is now one type for words and opcodes.
- The revert: OPCODE_PHYSICS (Kconfig.physics, default y; the kernel's flags) -- 0 and the opcodes are
  counted and charged to the Stadium as in Phase 1. make -C v4 mesh-boot-noop builds and tests it.
- Tests first: test_mesh_boot.c 77 checks as built, 86 with pipelining on, 74 with the revert.
- Measured on amd64, back to back: 219 s on, 196 s off (+12% under TCG). Both boots' logs labelled.
Not pushed (held to the end of Phase 7).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-14 00:29:08 +00:00
parent b5d99ba98b
commit e1b731f444
16 changed files with 296 additions and 57 deletions
+129 -41
View File
@@ -220,15 +220,15 @@ static uint16_t temperature(v4_uheat_t heat, uint16_t prior)
}
/* Loop #2 (v3's rolling_window_record_execution): the word's ID into the window */
static void window_record(v4_mesh_physics *p, uint32_t id)
static void window_record(v4_mesh_window *w, uint32_t id)
{
p->history[p->window_pos] = id;
p->window_pos = (p->window_pos + 1u) % V4_MESH_WINDOW;
p->total_executions++;
if (p->total_executions >= V4_MESH_WINDOW_WARM && !p->is_warm) p->is_warm = 1;
if (p->is_warm && ++p->adaptive_check_accumulator >= V4_MESH_ADAPTIVE_CHECK) {
p->adaptive_check_accumulator = 0;
p->adaptive_pending = 1;
w->history[w->window_pos] = id;
w->window_pos = (w->window_pos + 1u) % V4_MESH_WINDOW;
w->total_executions++;
if (w->total_executions >= V4_MESH_WINDOW_WARM && !w->is_warm) w->is_warm = 1;
if (w->is_warm && ++w->adaptive_check_accumulator >= V4_MESH_ADAPTIVE_CHECK) {
w->adaptive_check_accumulator = 0;
w->adaptive_pending = 1;
}
}
@@ -366,38 +366,38 @@ static void background_decay(unsigned place, v4_mesh_physics *p)
/* the window's adaptive pass (v3's rolling_window_run_adaptive_pass), when due: its diversity -- how often a
* word differs from the one before, over the window -- against the last; little growth and it halves,
* to no less than 256; growth and it doubles, to no more than 4096 */
static void window_service(v4_mesh_physics *p)
static void window_service(v4_mesh_window *w)
{
uint64_t diversity = 0, delta, growth_q48, threshold_q48;
uint32_t limit, i, prev = 0;
int first = 1;
if (!p->adaptive_pending) return;
p->adaptive_pending = 0;
if (!p->is_warm || p->total_executions < 2) return;
limit = p->effective_window_size;
if (!w->adaptive_pending) return;
w->adaptive_pending = 0;
if (!w->is_warm || w->total_executions < 2) return;
limit = w->effective_window_size;
for (i = 0; i < limit; i++) {
uint32_t cur = p->history[(p->window_pos + V4_MESH_WINDOW - limit + i) % V4_MESH_WINDOW];
uint32_t cur = w->history[(w->window_pos + V4_MESH_WINDOW - limit + i) % V4_MESH_WINDOW];
if (!first && cur != prev) diversity++;
prev = cur;
first = 0;
}
p->diversity_checks++;
if (p->last_pattern_diversity == 0) { p->last_pattern_diversity = diversity; return; }
delta = diversity > p->last_pattern_diversity ? diversity - p->last_pattern_diversity : 0;
growth_q48 = (delta << 16) / p->last_pattern_diversity;
w->diversity_checks++;
if (w->last_pattern_diversity == 0) { w->last_pattern_diversity = diversity; return; }
delta = diversity > w->last_pattern_diversity ? diversity - w->last_pattern_diversity : 0;
growth_q48 = (delta << 16) / w->last_pattern_diversity;
threshold_q48 = ((uint64_t)V4_MESH_GROWTH_THRESHOLD << 16) / 100u;
if (growth_q48 < threshold_q48) {
if (p->effective_window_size > V4_MESH_WINDOW_MIN) {
uint32_t n = (p->effective_window_size * V4_MESH_SHRINK_RATE) / 100u;
if (w->effective_window_size > V4_MESH_WINDOW_MIN) {
uint32_t n = (w->effective_window_size * V4_MESH_SHRINK_RATE) / 100u;
if (n < V4_MESH_WINDOW_MIN) n = V4_MESH_WINDOW_MIN;
if (n < p->effective_window_size) p->effective_window_size = n;
if (n < w->effective_window_size) w->effective_window_size = n;
}
} else if (p->effective_window_size < V4_MESH_WINDOW) {
uint32_t n = (p->effective_window_size * ((100u * 100u) / V4_MESH_SHRINK_RATE)) / 100u;
} else if (w->effective_window_size < V4_MESH_WINDOW) {
uint32_t n = (w->effective_window_size * ((100u * 100u) / V4_MESH_SHRINK_RATE)) / 100u;
if (n > V4_MESH_WINDOW) n = V4_MESH_WINDOW;
if (n > p->effective_window_size) p->effective_window_size = n;
if (n > w->effective_window_size) w->effective_window_size = n;
}
p->last_pattern_diversity = diversity;
w->last_pattern_diversity = diversity;
}
/* ---- L8 (v3's vm_heartbeat_update_l8 with its table: ssm_jacquard.c, compudynamics.c) ---- */
@@ -457,10 +457,10 @@ static void l8_seed(v4_mesh_l8 *t)
static uint32_t l8_locality(const v4_mesh_physics *p)
{
uint32_t ids[32], n = 32, i, j, matches = 0;
uint64_t have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW;
uint64_t have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW;
if (have < n) n = (uint32_t)have;
if (n <= 8) return 0;
for (i = 0; i < n; i++) ids[i] = p->history[(p->window_pos + V4_MESH_WINDOW - n + i) % V4_MESH_WINDOW];
for (i = 0; i < n; i++) ids[i] = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - n + i) % V4_MESH_WINDOW];
for (i = 8; i < n; i++)
for (j = i - 8; j < i; j++) if (ids[j] == ids[i]) { matches++; break; }
return (uint32_t)(((uint64_t)matches * 65536u) / (n - 8));
@@ -470,7 +470,7 @@ static uint32_t l8_locality(const v4_mesh_physics *p)
static void l8_update(v4_mesh_physics *p)
{
v4_mesh_l8 *t = &p->l8;
uint32_t locality = l8_locality(p), window = p->effective_window_size, anova, combined, r, c, ci, total, lnt, best = 0, best_ucb = 0;
uint32_t locality = l8_locality(p), window = p->win.effective_window_size, anova, combined, r, c, ci, total, lnt, best = 0, best_ucb = 0;
int entropy_high = (uint64_t)window * 100u >= 75u * (uint64_t)V4_MESH_WINDOW; /* >= 0.75 */
int cv_high = p->prefetch_attempts == 0 ? 1 /* v3: 0.5 when no data */
: 100u * (p->prefetch_attempts - p->prefetch_hits) >= 15u * p->prefetch_attempts; /* 1 - accuracy >= 0.15 */
@@ -524,17 +524,92 @@ void v4_mesh_l8_force(v4_mesh *ms, unsigned place, unsigned card, int hold)
l8_apply(&ms->physics[place]);
}
static uint64_t trajectory[V4_MESH_WINDOW];
/* ---- the opcodes (#189 Phase 7, ruling 7): every loop, as on the words ---- */
#if OPCODE_PHYSICS
/* at every retired instruction (exec.h): as a word's call -- decay by the ticks since at the opcodes' slope, heat
* +1, the temperature, the opcode window (L2), the transition from the one before (L4) */
static void on_retire(void *ctx, v4_node *n, unsigned op)
{
unsigned place = place_of_device(ctx);
v4_mesh_physics *p;
v4_mesh_opcode *o;
uint64_t elapsed, amount;
(void)n;
if (!the->physics || op >= V4_MESH_OPCODES) return;
p = &the->physics[place];
o = &p->op[op];
elapsed = p->tick - o->last_decay_tick;
if (!(o->state_flags & V4_MESH_STATE_FROZEN) && elapsed && p->l3_decay) {
amount = (elapsed * p->op_decay_slope_q48) >> 16;
o->heat = amount >= o->heat ? 0 : o->heat - amount;
}
o->last_decay_tick = p->tick;
o->heat++;
o->temperature_q8 = temperature(o->heat, o->temperature_q8);
if (p->l2_window) window_record(&p->opwin, op);
#if ENABLE_PIPELINING
if (p->l4_pipelining && p->prev_op < V4_MESH_OPCODES) {
v4_mesh_opcode *pr = &p->op[p->prev_op];
pr->transitions[op]++;
pr->total_transitions++;
if (pr->transitions[op] > pr->most_likely_count || (pr->transitions[op] == pr->most_likely_count && op < pr->most_likely_next)) {
pr->most_likely_count = pr->transitions[op];
pr->most_likely_next = (uint8_t)op;
}
}
#endif
p->prev_op = op;
}
/* in the cycle: all 32 cooled (background decay), the opcode window's service */
static void opcodes_cycle(v4_mesh_physics *p)
{
unsigned k;
for (k = 0; k < V4_MESH_OPCODES; k++) {
v4_mesh_opcode *o = &p->op[k];
uint64_t elapsed = p->tick - o->last_decay_tick, amount;
if (!(o->state_flags & V4_MESH_STATE_FROZEN) && elapsed && p->l3_decay) {
amount = (elapsed * p->op_decay_slope_q48) >> 16;
o->heat = amount >= o->heat ? 0 : o->heat - amount;
}
o->last_decay_tick = p->tick;
}
window_service(&p->opwin);
}
/* with the words' inference: the opcode window's heat, its width and the opcodes' slope */
static void opcodes_inference(v4_mesh_physics *p)
{
uint64_t have, span, i;
if (!p->opwin.is_warm) return;
p->op_inference_runs++;
have = p->opwin.total_executions < V4_MESH_WINDOW ? p->opwin.total_executions : V4_MESH_WINDOW;
span = p->opwin.effective_window_size < have ? p->opwin.effective_window_size : have;
for (i = 0; i < span; i++) {
uint32_t op = p->opwin.history[(p->opwin.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW];
trajectory[i] = op < V4_MESH_OPCODES ? (uint64_t)p->op[op].heat : 0;
}
v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->op_inference);
if (!p->op_inference.early_exited) {
if (p->op_inference.adaptive_window_width > 0) p->opwin.effective_window_size = p->op_inference.adaptive_window_width;
if (p->op_inference.adaptive_decay_slope > 0) p->op_decay_slope_q48 = p->op_inference.adaptive_decay_slope;
}
}
#endif
/* ---- inference (v3's vm_tick_inference_engine) and Loop #7 ---- */
static uint64_t trajectory[V4_MESH_WINDOW];
static void inference_tick(unsigned place, v4_mesh_physics *p)
{
uint64_t have, span, i;
v4_fabric_node *x = v4_fabric_node_at(&the->f, place);
if (!p->is_warm || !x) return;
if (!p->win.is_warm || !x) return;
p->inference_runs++;
window_service(p);
window_service(&p->win);
{ /* the dictionary's heat, counted (as v3's) */
uint64_t hot = 0, stale = 0, total = 0;
unsigned k;
@@ -548,21 +623,24 @@ static void inference_tick(unsigned place, v4_mesh_physics *p)
p->hot_at_check = hot; p->stale_at_check = stale; p->total_heat_at_check = total;
}
/* the heat of each word in the window, the newest `span` of them, oldest first (v3's extract_heat_trajectory) */
have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW;
span = p->effective_window_size < have ? p->effective_window_size : have;
have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW;
span = p->win.effective_window_size < have ? p->win.effective_window_size : have;
for (i = 0; i < span; i++) {
uint32_t id = p->history[(p->window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW];
uint32_t id = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW];
trajectory[i] = (id < V4_MESH_WORD_IDS && accounts_of(place)[id].xt) ? (uint64_t)accounts_of(place)[id].heat : 0;
}
v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->inference);
if (!p->inference.early_exited) {
if (p->inference.adaptive_window_width > 0 && p->inference.adaptive_window_width != p->effective_window_size)
p->effective_window_size = p->inference.adaptive_window_width;
if (p->inference.adaptive_window_width > 0 && p->inference.adaptive_window_width != p->win.effective_window_size)
p->win.effective_window_size = p->inference.adaptive_window_width;
if (p->inference.adaptive_decay_slope > 0 && p->inference.adaptive_decay_slope != p->decay_slope_q48)
p->decay_slope_q48 = p->inference.adaptive_decay_slope;
p->last_inference_tick = p->tick;
} else p->inference_early_exits++;
p->inference_early_exited = p->inference.early_exited;
#if OPCODE_PHYSICS
opcodes_inference(p);
#endif
/* Loop #7, the adaptive heartrate: stable, 25% slower; else 20% faster -- within 1/4 to 4 times the nominal */
{
uint64_t cur = p->tick_target_ns, n;
@@ -581,12 +659,15 @@ static void heartbeat_cycle(unsigned place, v4_mesh_physics *p)
p->tick++;
if (p->tick - p->last_inference_tick >= V4_MESH_INFERENCE_EVERY) inference_tick(place, p);
background_decay(place, p);
window_service(p);
window_service(&p->win);
#if OPCODE_PHYSICS
opcodes_cycle(p);
#endif
l8_update(p);
/* the snapshot (v3's heartbeat_publish_snapshot) */
p->snapshot.tick = p->tick;
p->snapshot.window_width = p->effective_window_size;
p->snapshot.fill = (uint32_t)(p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW);
p->snapshot.window_width = p->win.effective_window_size;
p->snapshot.fill = (uint32_t)(p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW);
p->snapshot.decay_slope_q48 = p->decay_slope_q48;
p->snapshot.hot = p->hot_at_check;
p->snapshot.stale = p->stale_at_check;
@@ -611,7 +692,7 @@ static void on_call(void *ctx, v4_node *n, v4_cell target)
a->last_decay_tick = p->tick;
a->heat++;
a->temperature_q8 = temperature(a->heat, a->temperature_q8);
if (p->l2_window) window_record(p, (uint32_t)id);
if (p->l2_window) window_record(&p->win, (uint32_t)id);
#if ENABLE_PIPELINING
if (p->l4_pipelining) pipeline(place, p, (uint32_t)id);
#endif
@@ -684,7 +765,10 @@ static void words_attach(unsigned place, v4_fabric_node *x)
p->tick = tick;
p->cycle_every = V4_MESH_CHECK_FREQUENCY;
p->decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48;
p->effective_window_size = V4_MESH_WINDOW;
p->win.effective_window_size = V4_MESH_WINDOW;
p->opwin.effective_window_size = V4_MESH_WINDOW;
p->prev_op = V4_MESH_NO_WORD;
p->op_decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48;
p->prev_id = V4_MESH_NO_WORD;
/* every gate off, and L8's table on card 8, as v3's kernel boot (vm_bootstrap.c): the first cycle's L8
* applies its card */
@@ -694,6 +778,10 @@ static void words_attach(unsigned place, v4_fabric_node *x)
}
x->es.on_call = on_call;
x->es.on_call_ctx = &the->kernel[place];
#if OPCODE_PHYSICS
x->es.on_retire = on_retire;
x->es.on_retire_ctx = &the->kernel[place];
#endif
}
unsigned v4_mesh_state_flags(const v4_mesh *ms, unsigned place, v4_cell xt)