Skip to content

Add --threads N to run-c/run-cu (sets HVM_THREADS on the spawned hvm) - #763

Open
SiliconState wants to merge 1 commit into
HigherOrderCO:mainfrom
SiliconState:pr/threads-flag
Open

Add --threads N to run-c/run-cu (sets HVM_THREADS on the spawned hvm)#763
SiliconState wants to merge 1 commit into
HigherOrderCO:mainfrom
SiliconState:pr/threads-flag

Conversation

@SiliconState

@SiliconState SiliconState commented Jun 10, 2026

Copy link
Copy Markdown

What

A --threads N flag on the run subcommands that sets HVM_THREADS=N on the spawned hvm process. Companion to HigherOrderCO/HVM2#441, which makes the HVM C runtime read that variable at startup and clamp to the compiled TPC.

bend run prog.bend --threads 4 -s

Why

HVM2's parallel C runtime fixes its worker count at compile time, so today there is no way to measure parallel scaling (speedup vs N threads) through Bend — tools can only compare whole backends (run-rs vs run-c vs run-cu), which conflates runtime quality with parallelism. A runtime thread count also lets users on shared machines / CI cap cores, and lets benchmarking tools pin a count for reproducibility.

bend run-c file invokes hvm run-c <file> with no thread argument, so there is nothing to thread a value through today.

Patch

  • CliRunOpts gains --threads: Option<usize>; it flows through RunOpts (new public threads field, None by default) into run_hvm, which sets HVM_THREADS on the child when present.
  • run-rs is unaffected (the Rust interpreter ignores the variable).

Backward compatibility

  • No flag → no env var → byte-for-byte identical behavior.
  • Old hvm binaries ignore the unknown env var, so a new Bend works against an old HVM (no scaling, but no breakage). With the HVM2 patch, -s additionally shows a THREADS: N readback so tools can confirm the count took effect.

Testing

  • Full test suite passes (threads: None default means existing RunOpts { .. } literals and golden tests are untouched).
  • End-to-end against an HVM_THREADS-aware hvm: bend run-c parallel_sum.bend --threads {1,4,16} -s → identical Result/ITRS at every count, THREADS: N readback, 37 → 98 MIPS from 1 → 4 threads on a 16-core machine. Measured efficiency curve on depth-16 parallel_sum.bend: 1.00x/1.55x/2.43x/3.27x at 1/2/4/8 threads (ITRS identical at every count), requested 32 clamped to 16.

Plumbs an optional worker-thread count through CliRunOpts -> RunOpts ->
run_hvm, exported as the HVM_THREADS environment variable on the spawned
hvm process. Inert until the HVM2 runtime reads HVM_THREADS (and against
older hvm binaries, which ignore the unknown env var); run-rs is
unaffected. No flag means no env var: behavior is byte-for-byte
unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant