Files
R8Forge/tests/asm_test.cc
T
dsh b666799950 %synth: make the tape search thread count a run option (-j N)
The tape search always used std::thread::hardware_concurrency() workers with no
way to ask for fewer (or more).  It is now a run option like the cost model:

    r8forge -j N <file.r8asm>                N workers
    r8forge --synth-threads=N <file.r8asm>   same thing

0 (the default) means one worker per hardware thread, as before.
r8asm::set_synth_threads(unsigned) is the C++ side; the CLI parses -j,
--synth-threads and --synth-threads=.  The layer search stays serial (it only
takes seconds and has no split), so the knob only affects the expensive tape
search, which splits by first block (see r8veritas_phase.hh).

The search line on stderr now ends with ", N threads", which makes the setting
verifiable without timing: with a freshly written table (params 4, or4) and the
cache entry removed between runs,

    -j 1                 -> 230 states, 0.0009s, 1 threads
    -j 4                 -> ..., 4 threads
    --synth-threads=2    -> ..., 2 threads
    (nothing) / -j 0     -> ..., 32 threads (hardware_concurrency here)

Documented in SPEC 3.7, README's cost model section and the AGENTS %synth
section.  asm/test.r8asm is unchanged at 28729 characters; byte_op_test,
examples_test (all 13), macro_docs, synth_test (95 chars, 8/8) and
r8forge_derive all still green.
2026-11-07 23:56:37 +08:00

93 lines
2.7 KiB
C++

// The assembler CLI.
//
// r8forge [--synth-chars] [-j N] <file.r8asm>
//
// %synth charges a block for a layout free tape by default: the block is
// copied to many call sites, so the .tp walk is not part of the search and one
// cache entry serves every layout. --synth-chars charges the real characters
// for the layout written in the source instead (much slower, and the result is
// only optimal for that layout). It is a property of the run, not of a block,
// which is why there is no `metric` line inside %synth any more.
//
// -j N (or --synth-threads N) caps the workers of the %synth tape search; 0
// (the default) means one per hardware thread. Only the tape search is
// parallel -- the layer search is serial whatever this says.
#include "r8forge/r8asm.hh"
#include <cerrno>
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
void usage() {
std::cerr << "usage: r8forge [--synth-chars] [-j N] <file.r8asm>\n";
}
// 0 is allowed (one worker per hardware thread); absurd counts are not.
bool parse_count(const std::string &text, unsigned &out) {
if (text.empty())
return false;
char *end = nullptr;
errno = 0;
const unsigned long value = std::strtoul(text.c_str(), &end, 10);
if (end == nullptr || *end != '\0' || errno != 0 || value > 4096)
return false;
out = unsigned(value);
return true;
}
} // namespace
int main(int argc, char **argv) {
const char *file = nullptr;
int metric = 1; // layout free
unsigned threads = 0; // one worker per hardware thread
for (int i = 1; i < argc; i++) {
const std::string arg = argv[i];
if (arg == "--synth-chars") {
metric = 0;
} else if (arg == "--synth-flat") {
metric = 1;
} else if (arg == "-h" || arg == "--help") {
usage();
return (0);
} else if (arg == "-j" || arg == "--synth-threads") {
if (i + 1 >= argc || !parse_count(argv[++i], threads)) {
std::cerr << "'" << arg << "' wants a thread count\n";
usage();
return (2);
}
} else if (arg.rfind("--synth-threads=", 0) == 0) {
if (!parse_count(arg.substr(16), threads)) {
std::cerr << "bad thread count in '" << arg << "'\n";
usage();
return (2);
}
} else if (arg.size() > 1 && arg[0] == '-') {
std::cerr << "unknown option '" << arg << "'\n";
usage();
return (2);
} else if (file == nullptr) {
file = argv[i];
} else {
usage();
return (2);
}
}
if (file == nullptr) {
usage();
return (2);
}
r8asm a;
a.set_synth_metric(metric);
a.set_synth_threads(threads);
if (!a.import_file(file, kDefaultCellSize))
return (1);
std::string tape = a.export_rot8();
if (a.failed())
return (1);
std::cout << tape;
return (0);
}