%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

+13 -3
View File
@@ -17,7 +17,9 @@
// {sacrifice, mask, or_mode} triples. Either `map` (2^K output values, the
// inputs implied to be %1..%K and the outputs the ones after them) or `stat`
// (the whole 2^params state table) says what to compute. `layout` is the cell
// address of each parameter (empty means adjacent, 0..n-1).
// address of each parameter (empty means adjacent, 0..n-1). `metric` picks the
// tape cost model: 1 = layout free (the .tp walk is not counted, one cache
// entry serves every caller), 0 = real characters for this layout.
std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
@@ -109,6 +111,12 @@ private:
// I know template class is the most elegant solution of cell_size
// but I can't be bother to add that.
// How %synth charges a block: 1 = layout free (default), 0 = real tape
// characters for the layout in the source. A library block is called from
// many places, so the default ignores the layout and one cache entry serves
// every call site; `r8forge --synth-chars` flips it for the whole run.
int synth_metric = 1;
// The assembler has no exceptions: anything that cannot be assembled
// sets this flag and says why on stderr.
bool error = false;
@@ -130,8 +138,6 @@ private:
std::vector<int> stat; // or the whole table: 2^params values
std::vector<int> layout; // optional: cell address of each parameter
int mode = 0; // 0 = shortest tape, 1 = fewest layers only
int metric = 0; // 0 = tape chars (needs the layout), 1 = chars without
// the .tp walk (any layout), 2 = blocks
};
private:
@@ -233,6 +239,10 @@ private:
public:
std::string export_rot8();
bool import_file(const std::string &filename, size_t cell_size);
// %synth cost model for the whole run: 1 (default) = layout free, 0 = real
// tape characters for the layout written in the source. Set it before
// import_file; `r8forge --synth-chars` is the command line spelling.
void set_synth_metric(int metric) { main_tree.synth_metric = metric; }
// True once anything (a missing file, an unknown name, ...) has failed.
bool failed() const { return main_tree.error; }
};