%synth: the cost model is a run option, not a per block metric line

`metric chars|flat|blocks` inside %synth meant the same truth table could be
searched twice for two call sites, and every library block had to spell out
`metric flat` to avoid the default.  The cost model belongs to the whole
assembly, so it is one now:

    r8forge <file.r8asm>                  flat (layout free), the default
    r8forge --synth-chars <file.r8asm>    charge the real .tp walk

flat stays the default because a library block is expanded at dozens of call
sites with different layouts: only a layout free result can share one cache
entry, and it is usually within a few percent (full_add: 95 vs 90 characters).
The chars search is far more expensive (full_add: 415M states, ~21 minutes,
~10 GB), which is exactly why it should be asked for on the command line.

`metric blocks` goes with it: block count is a poor proxy for tape length (and
it was only another way to ask for mode layers).

Implementation: the metric lives in the assembler (ast::synth_metric, default
1) and is set through r8asm::set_synth_metric() before import_file; the CLI in
tests/asm_test.cc parses --synth-chars / --synth-flat.  The cache key already
carried metric, so the committed entries still hit: assembling the library and
asm/test.r8asm gives byte identical tapes (28729 characters, byte_op_test all
green, all 13 examples green, synth_test still 8/8).

A `metric` line now fails with a message that names the flag instead of being
silently ignored.  examples/full_add_flat.r8asm and full_add_flat_spaced.r8asm
are removed: they only existed to demonstrate the per block metric (the flat
default makes them copies of full_add.r8asm).

Also repairs the mojibake in README's metric section: an earlier tool wrote
\x0c / \x08 / \x0d where \f, \b and \r belonged, which ate characters out of
"flat", "blocks", "biz/flb/rnz" and "rnz".  The section is rewritten for the
flag, and the %synth numbers in README/SPEC/AGENTS now match what the tree
prints.

New cache entry c16bf083bb0ccbc8.txt is full_add's stat table with mode layers
+ flat, which full_add_stat.r8asm now produces.
This commit is contained in:
dsh committed 2026-11-07 23:44:01 +08:00
1 parent 66ed7bc263
commit b4ea79ba91
17 files changed
+142 -129

No files matched your search

+7 -11
View File
@@ -533,16 +533,12 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
"' is gone: map already says which parameter is "
"which, use stat for the raw table");
} else if (kw == "metric") {
const std::string &m = it->tokens[1].value;
if (m == "chars")
spec.metric = 0;
else if (m == "flat")
spec.metric = 1;
else if (m == "blocks")
spec.metric = 2;
else
main_tree.fail("unknown metric '" + m +
"' (chars, flat or blocks)");
// it is a property of the run, not of the block: the assembler
// optimises for a layout free block by default, and --synth-chars
// switches every block in the file to the real layout
main_tree.fail("'metric' is gone: blocks are layout free by "
"default, run r8forge --synth-chars to charge the "
"real .tp walk instead");
} else if (kw == "mode") {
const std::string &m = it->tokens[1].value;
if (m == "layers")
@@ -590,7 +586,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, spec.stat, name, spec.layout,
spec.mode, spec.metric);
spec.mode, main_tree.synth_metric);
if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted");
} else {
+1 -1
View File
@@ -132,7 +132,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
<< " layers" << (layers.proven_optimal ? " (shortest)" : "")
<< ", " << layers.states << " states, " << layers.seconds << "s\n";
if (mode == 1 || metric == 2) { // blocks only: skip the tape search
if (mode == 1) { // fewest layers only: skip the tape search
for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
write_cache(out_ins);