tape_run --profile: the walking share is measured, not estimated

A tape run now reports, on stderr (stdout keeps the out line):

    profile: 314779 steps, 250112 of them pointer moves (79.5%)
      > 125073 39.7%   < 125039 39.7%   + 22178 7.0%   - 12395 3.9%
      [ 25121 8.0%     ] 4954 1.6%      . 7  , 10
      hottest cells (steps spent there): 11:30471 10:26446 9:25733 12:24879 ...

The flagship tape: 236441 steps, 180665 pointer moves (76.4%), hottest cells
91, 90, 92, 89, 87, 93, 88, 86, 85 -- which is exactly clb_digits' scratch block.

So the "walking is about seventy percent" line in AGENTS turns out to be right, and
now it is measured: this machine has no random access, every access costs
|delta address|, so layout is the performance knob -- which is also why the
peephole pass (that only cancels round trips) buys 17% of the steps.

TODO drops the --profile item; AGENTS gets the number and the conclusion, with the
two commands next to the other baseline entries.
This commit is contained in:
dsh committed 2026-11-18 15:19:57 +08:00
1 parent a56a12f132
commit 01aa13d0f0
3 files changed
+43 -2

No files matched your search

+2
View File
@@ -36,6 +36,7 @@ r8forge_examples_test # 18 个 example + asm/test.r8asm,
r8forge_macro_docs_test # 53 macros in 23 files
r8forge --opt asm/test.r8asm # 纸带窥孔优化 64513 -> 58060(>< 净位移、+ mod W、- mod 2)
r8forge_tape_opt in.rot8 out.rot8 # 同一优化,但作用在已有纸带上
r8forge_tape_run t.rot8 --profile # 剖面:指针移动占 76~80% 步数(实测,见下)
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一套工具链 + 断言(约 20 分钟)
@@ -66,6 +67,7 @@ powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一
* **交互程序这样测**:`r8forge_tape_run t.rot8 -i "12+34="`(字符喂给 `inp`,低 7 位,读空后为 0),
`r8forge_derive <src> 0 -i "..."` 是同一份输入的第二个实现;例程把输入写在头部
`; in ...`,`r8forge_examples_test` 会自动喂。实时终端那一侧在 ROT-8-viz(默认串行)。
* **代价的大头是"走"不是"算"**:`tape_run --profile` 实测,计算器 79.5%、全家桶 76.4% 的步数是 `>`/`<`,旋转只占 7%、翻转 4~6%。这台机器没有随机寻址,每次访问的代价就是 `|Δ地址|`,所以**布局才是性能旋钮**;`--opt` 省 17% 步数正是因为它砍的是往返移动。
* 一次会话很难把结论留在对话里 —— **落进 commit message 和文件注释**。
## 语义上会咬人的几条
+1 -2
View File
@@ -25,8 +25,7 @@
重复清位…… 要能重算跳转偏移。配套 `r8forge_tape_diff`(比较两条纸带的 OUT 流和步数)
证明等价 —— 一次改动全库受益。
* **`--profile`**:统计每段宏/每个 cell 的步数占比和 `.tp` 行走距离,用数据决定下一步优化谁
(现在"`.tp` 行走占七成"还是估的)。
* **`%synth --verify`**:把 `.r8forge_cache/` 里每条结果按对应表的所有输入重跑一遍,
给"缓存已入库"兜底 —— 一条坏条目会让每次构建静默用错块。
* **CI**:暂时不做。本地 `ctest --test-dir build -j 4`(8 条,9 秒)和 `tests/widths.ps1`
+40
View File
@@ -19,6 +19,8 @@
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
int main(int argc, char **argv) {
@@ -37,12 +39,17 @@ int main(int argc, char **argv) {
std::vector<uint32_t> cells(kDefaultMemoryCells, 0);
std::deque<uint32_t> input;
bool trace = false;
bool profile = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
if (a == "--trace" || a == "-t") {
trace = true;
continue;
}
if (a == "--profile" || a == "-p") {
profile = true;
continue;
}
if (a == "-i" || a == "--input") {
if (i + 1 >= argc) {
std::cerr << "-i needs a string of input characters\n";
@@ -64,6 +71,9 @@ int main(int argc, char **argv) {
const size_t limit = 20'000'000; // same as r8forge_derive
size_t pc = 0;
size_t steps = 0;
size_t ins_count[256] = {};
std::vector<size_t> cell_hits(cells.size(), 0);
size_t walking = 0;
for (; pc < tape.size(); pc++) {
if (++steps > limit) {
std::cout << "HANG after " << limit << " steps (pc " << pc << ", dp " << dp
@@ -71,6 +81,12 @@ int main(int argc, char **argv) {
<< ")\n";
return 3;
}
if (profile) {
ins_count[(unsigned char)tape[pc]]++;
cell_hits[dp]++;
if (tape[pc] == 0x3e || tape[pc] == 0x3c)
walking++;
}
if (trace) {
// cells 0..3 are the interesting ones when debugging a library macro
std::cout << "pc " << pc << " " << tape[pc] << " dp " << dp << " [";
@@ -136,6 +152,30 @@ int main(int argc, char **argv) {
}
}
if (profile) {
const char *kinds = "><+-[].,";
std::fprintf(stderr, "profile: %zu steps, %zu of them pointer moves (%.1f%%)\n",
steps, walking, 100.0 * double(walking) / double(steps));
for (const char *k = kinds; *k; k++) {
const size_t n = ins_count[(unsigned char)*k];
std::fprintf(stderr, " %c %8zu %5.1f%%\n", *k, n,
100.0 * double(n) / double(steps));
}
std::vector<size_t> order(cell_hits.size());
for (size_t i = 0; i < order.size(); i++)
order[i] = i;
std::sort(order.begin(), order.end(), [&](size_t a, size_t b) {
return cell_hits[a] > cell_hits[b];
});
std::fprintf(stderr, " hottest cells (steps spent there):");
for (size_t i = 0; i < order.size() && i < 10; i++) {
if (cell_hits[order[i]] == 0)
break;
std::fprintf(stderr, " %zu:%zu", order[i], cell_hits[order[i]]);
}
std::fprintf(stderr, "\n");
}
std::cout << "out";
for (uint32_t v : out)
std::cout << " " << int(v);