%synth: emit the tape-optimal program, not the fewest-layer one

The layer search still runs first (7.7 seconds) but only to get a bound; the
cost search then takes over and its answer is what goes on the tape.  A caller
that wants its own layout can say 'layout 0 2 4 6 8 10' inside the block.

examples/full_add.r8asm now assembles to 90 characters of tape instead of 95:
cost 75 in 6 blocks after 415M states and about 21 minutes, against the 82 the
five layer program needs.  Assembly getting slower is the whole point -- it is
the price of the shortest tape.

For scale: one bit_raw_add slice is 179 characters with the cells adjacent, and
the synthesised slice is 82.  A whole raw_add call is 1650 characters today, so
eight synthesised slices would land near 700.
This commit is contained in:
starcells committed 2026-11-06 09:48:39 +08:00
1 parent 43af3bb155
commit 388c2b7dbd
4 files changed
+85 -28

No files matched your search

+13 -4
View File
@@ -16,6 +16,7 @@ R8ASM 汇编器 + R8Veritas 搜索器,合成一个仓库。
inputs %1 %2 %3 ; 输入位,必须排在最前面
outputs %4 %5 ; 要算出来的输出位
map 0 1 1 2 1 2 2 3 ; 每个输入组合对应的输出位取值(bit0 = %4)
layout 0 1 2 3 4 5 ; 可选:各参数所在 cell 的地址(默认 0..n-1)
%endsynth
.var A 0
@@ -25,23 +26,31 @@ R8ASM 汇编器 + R8Veritas 搜索器,合成一个仓库。
.var Co 4
.var T 5
fulladd A B Ci S Co T ; 这一行展开成汇编期合成的 5 个块
fulladd A B Ci S Co T ; 这一行展开成汇编期合成的最短纸带
output S
output Co
```
`%synth` 会跑两遍:先用层数搜索拿到一个解(7 秒),再拿它的代价当上界跑纸带长度搜索
(Dijkstra)。第二遍是精确的,弹出目标即停,所以 full_add 大约 12 分钟 —— 汇编时间换最短纸带。
`layout` 写的是真实地址时,搜索按真实 `.tp` 距离算代价;不写就假设参数 cell 紧邻。
汇编:
```
$ r8forge examples/full_add.r8asm
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s
> [-]-... (95 字符纸带)
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
> [-]-... (90 字符纸带)
```
`tests/synth_test.cc` 把这条纸带丢进一台小 ROT-8 虚拟机,8 种输入组合全对:
第一行是层数搜索(7.7 秒),第二行是按纸带长度的精确搜索(约 21 分钟,4.15 亿状态,峰值约
10 GB),最后发射的是第二行的解。`tests/synth_test.cc` 把纸带丢进一台小 ROT-8 虚拟机,
8 种组合全对:
```
tape: 95 characters
tape: 90 characters
000 -> 00 expect 00 ok
...
111 -> 11 expect 11 ok
+5 -3
View File
@@ -13,11 +13,12 @@
#include "r8forge/r8veritas.hh"
// %synth: run the searcher on a truth table and return the block list as
// {sacrifice, mask, or_mode} triples.
// {sacrifice, mask, or_mode} triples. `layout` is the cell address of each
// parameter (empty means adjacent, 0..n-1).
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,
const std::string &name);
const std::string &name, const std::vector<int> &layout);
class r8asm {
using data = int16_t;
@@ -120,7 +121,8 @@ private:
int params = 0;
std::vector<int> in; // param index per input bit, bit 0 first
std::vector<int> out; // param index per output bit, bit 0 first
std::vector<int> map; // 2^|in| entries: output bits of that input
std::vector<int> map; // 2^|in| entries: output bits of that input
std::vector<int> layout; // optional: cell address of each parameter
};
private:
+4 -1
View File
@@ -464,6 +464,9 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
std::vector<int> &dst = (kw == "inputs") ? spec.in : spec.out;
for (size_t i = 1; i < it->tokens.size(); i++)
dst.push_back(std::stoi(it->tokens[i].value) - 1);
} else if (kw == "layout") {
for (size_t i = 1; i < it->tokens.size(); i++)
spec.layout.push_back(std::stoi(it->tokens[i].value));
} else if (kw == "map") {
for (size_t i = 1; i < it->tokens.size(); i++)
spec.map.push_back(std::stoi(it->tokens[i].value));
@@ -472,7 +475,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
}
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, name);
spec.map, name, spec.layout);
if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted");
} else {
+63 -20
View File
@@ -1,4 +1,5 @@
#include "r8forge/r8asm.hh"
#include <chrono>
// ---------------------------------------------------------------- %synth
//
@@ -10,7 +11,8 @@
template <int P>
std::vector<std::array<int, 3>>
r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
const std::vector<int> &map, const std::string &name) {
const std::vector<int> &map, const std::string &name,
const std::vector<int> &layout) {
using V = r8veritas<P>;
V v;
v.initialize_nicknames();
@@ -32,21 +34,61 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
}
auto tt = v.make_truthtable(start, target);
// 1) fewest layers -- cheap, and its tape length gives the cost search a
// bound to work under
typename V::options opt;
opt.max_depth = 5;
const auto res = v.solve(tt, opt);
std::cerr << "r8asm: %synth " << name << ": ";
if (!res.found) {
std::cerr << "no program up to " << opt.max_depth << " layers\n";
const auto layers = v.solve(tt, opt);
std::vector<std::array<int, 3>> out_ins;
if (!layers.found) {
std::cerr << "r8asm: %synth " << name << ": no program up to "
<< opt.max_depth << " layers\n";
return {};
}
std::cerr << res.program.size() << " layers"
<< (res.proven_optimal ? " (shortest)" : "") << ", " << res.states
<< " states, " << res.seconds << "s\n";
std::cerr << "r8asm: %synth " << name << ": " << layers.program.size()
<< " layers" << (layers.proven_optimal ? " (shortest)" : "")
<< ", " << layers.states << " states, " << layers.seconds << "s\n";
std::vector<std::array<int, 3>> out_ins;
for (const auto &i : res.program)
// 2) shortest tape -- exact, so it stops as soon as the goal is popped
typename V::cost_options copt;
copt.max_states = 450'000'000;
if (int(layout.size()) == P)
for (int i = 0; i < P; i++)
copt.addr[i] = layout[size_t(i)];
else
for (int i = 0; i < P; i++)
copt.addr[i] = i;
int bound = 0;
for (const auto &i : layers.program) {
int lo = copt.addr[i.sacrifice], hi = lo, cnt = 0;
for (int b = 0; b < P; b++)
if (i.mask & (1 << b)) {
cnt++;
lo = std::min(lo, copt.addr[b]);
hi = std::max(hi, copt.addr[b]);
}
bound += 3 + 2 * (hi - lo) + cnt * (i.or_mode ? 4 : 1);
}
copt.max_cost = bound;
const auto t0 = std::chrono::steady_clock::now();
const auto tape = v.solve_cost(tt, copt);
const double tape_seconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
.count();
if (!tape.found) {
std::cerr << "r8asm: %synth " << name << ": tape search failed ("
<< tape.message << "), keeping the layer result\n";
for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
return out_ins;
}
std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in "
<< tape.program.size() << " blocks (layer answer was " << bound
<< "), " << tape.states << " states, " << tape_seconds << "s\n";
for (const auto &i : tape.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
return out_ins;
}
@@ -54,7 +96,8 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
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,
const std::string &name) {
const std::string &name,
const std::vector<int> &layout) {
if (params < 1 || params > 8) {
std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n";
return {};
@@ -72,21 +115,21 @@ r8asm_synth_program(int params, const std::vector<int> &in,
}
switch (params) {
case 1:
return r8asm_synth_run<1>(in, out, map, name);
return r8asm_synth_run<1>(in, out, map, name, layout);
case 2:
return r8asm_synth_run<2>(in, out, map, name);
return r8asm_synth_run<2>(in, out, map, name, layout);
case 3:
return r8asm_synth_run<3>(in, out, map, name);
return r8asm_synth_run<3>(in, out, map, name, layout);
case 4:
return r8asm_synth_run<4>(in, out, map, name);
return r8asm_synth_run<4>(in, out, map, name, layout);
case 5:
return r8asm_synth_run<5>(in, out, map, name);
return r8asm_synth_run<5>(in, out, map, name, layout);
case 6:
return r8asm_synth_run<6>(in, out, map, name);
return r8asm_synth_run<6>(in, out, map, name, layout);
case 7:
return r8asm_synth_run<7>(in, out, map, name);
return r8asm_synth_run<7>(in, out, map, name, layout);
default:
return r8asm_synth_run<8>(in, out, map, name);
return r8asm_synth_run<8>(in, out, map, name, layout);
}
}