Skip to content

About

C optimization framework that evaluates candidates proposed by AI agents, evolutionary coding (EC), symbolic regression (SR) with an oracle

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

dense-superoptimizer (dso)

dso judges replacements for one hot C source file. Someone proposes a new version of the file: a person, a coding agent, a knob tuner or a language model. dso decides, on your machine and with calibrated statistics, whether that version is still correct and whether it is faster, and keeps the evidence for a human to review.

The evaluator is the product. Whoever writes a candidate cannot see or change how it is judged:

stage what it checks outcome when it fails
SCOPE the file stays inside the task's contract: allowed includes, no inline assembly, no I/O, no tricks scope
BUILD it compiles with the project's flags, exports and imports exactly what the contract lists, keeps no writable state build
CHECK an independent oracle drives it through tens of thousands of cases and every stored counterexample incorrect, unsafe
SANITIZE the same cases under AddressSanitizer and UndefinedBehaviorSanitizer unsafe
SCREEN paired, interleaved timing against the baseline on the development workloads slower, no_gain
ASSEMBLE the project builds and passes its own tests with the file in place; its benchmarks are timed end to end assembly, e2e_regressed, no_gain
CONFIRM a fresh measurement on held-out workloads and seeds, at higher confidence unconfirmed

A candidate that passes everything under a calibrated evaluator is qualified, and dso writes a review bundle (the file, a patch against the baseline, the measurements and an evidence summary). dso never applies a change to your project.

Everything the evaluator runs for a candidate, and for the baseline it is compared with, runs in a sandbox made of what the kernel offers an unprivileged process: writes only in the run's own directory (Landlock and a read-only home), no sockets (seccomp and a network namespace), no capabilities, and a PID namespace that ends whatever the run started. No container engine and no privileges are needed; dso status says which layers this host has (docs/CONFINE.md).

Status: experimental, version 0.0.10. Linux x86-64 only. dso does not prove equivalence, and its sandbox is containment, not an audited security boundary: candidates run as your user. Read KNOWN_ISSUES.md before relying on a result.

Agents: read AGENTS.md before running anything here.

Requirements

  • Linux x86-64, a C11 compiler with AddressSanitizer and UndefinedBehaviorSanitizer, make, binutils (nm), diffutils, Python 3.10+.
  • For the sandbox, nothing to install: Linux 5.13 or later for Landlock (6.2 or later covers truncation too), and unprivileged user namespaces for the read-only home and the PID namespace (on by default on Debian and Devuan). An older or stricter kernel drops those layers and dso status says so.
  • Optional, for the window: pkg-config, SDL3, FreeType, and OpenGL 3.3 or OpenGL ES 3.0.
  • Optional, for the language-model generators: curl (Ollama, an OpenAI-compatible endpoint or the Anthropic API), or any command that reads a prompt and prints a reply.
  • Optional, for DSO Lab (lab/): Python 3.11+.

The command-line program needs no SDL and downloads nothing.

Quick start

Two small C projects and a task for each are included, so everything below runs as it is, from the repository root:

make cli
make test
build/bin/dso --config config/base64.json status
build/bin/dso --config config/base64.json check examples/base64-encode/table.c
build/bin/dso --config config/base64.json calibrate
build/bin/dso --config config/base64.json --hypothesis "hand-written: 64-entry alphabet table" evaluate examples/base64-encode/table.c
build/bin/dso --config config/base64.json report
  • status prints the task, the host, the calibration and, on its sandbox line, the containment layers in use.
  • check is a dry run of the four correctness gates. It takes seconds, archives nothing and measures nothing.
  • calibrate measures this machine's noise (the baseline against itself) and runs the task's controls: files that are known to be slower, incorrect, unsafe or overfitted, and must be judged so. Run it on an otherwise idle machine; it takes a minute or two here. If it does not qualify, read its output; do not repeat it until it happens to pass.
  • evaluate runs the whole funnel on one file and prints every table. no_gain is a valid result: it means the gain was not shown to be above this machine's calibrated threshold.
example project what it shows
base64-encode (config/base64.json) examples/base64-project: a base64 encoder writing into a caller's buffer, built by a shell script bounds as well as values, a shrinking oracle, seven controls, a tuner knob, three workloads per suite
bitcount32 (config/demo.json) examples/bitcount-project: a bit counter, built by make the smallest possible task: one pure function, everything written by hand

Both exemplars (examples/base64-encode/table.c, examples/bitcount32/swar.c) are textbook techniques written by hand, and both workload sets are synthetic. They show how the pieces fit together. They are not performance claims about anything else.

Commands

dso [options] COMMAND
  tasks                list the tasks
  status               the task, the host and whether the calibration applies
  calibrate            measure noise and run every control
  check FILE           dry run of SCOPE, BUILD, CHECK and SANITIZE
  evaluate FILE        judge one file (a finalist is confirmed too)
  reevaluate NAME      judge an archived candidate again (needs --reason)
  show NAME            one candidate: record, p50/p95/p99 tables, diagnostics
  report               the archive as a table
  bundle NAME          write the review bundle of a qualified candidate
  prompt [NAME]        print the prompt a language-model generator gets (and whether it fits each model's context)
  run                  an unattended campaign with the enabled generators
  prepare              build the oracle and the baseline runners

--json puts exactly one JSON document on stdout; events go to stderr. Exit status: 0 done, 1 refused by a gate (check) or not archived, 2 usage or unreadable input, 3 not calibrated, 4 evaluator or host failure, 130 interrupted. dso --help lists every option.

Results live in the state directory (state_dir in the config): tasks/<task>/candidates/<name>/ holds each candidate's source, record, logs and measurements, and every attempt that was started.

Using it on your own project

A task is a directory under tasks/: a task.json with the contract, the build flags and the measurement plan, an oracle that knows the right answers, a runner that links the candidate, and controls with known outcomes. Start one with:

python3 scripts/new_task.py --id my-task --project ../my-project \
    --target src/hot.c --header include/hot.h \
    --build "make -j{jobs}" --test "make test" --bench "build/bench 1"

It reads the symbols, includes and file pins from your project and writes the task with a harness kit, so what remains is the part only you can write: the reference answer, the cases, the controls and the workloads. docs/WRITING-A-TASK.md goes through that step by step with the base64 example, and python3 scripts/check_controls.py --config config/my-task.json tells you in seconds whether your oracle catches what it should. docs/TASKS.md describes every key.

dso handles one C file per task, on Linux x86-64. The file needs a stable interface, no state between calls, and a right answer you can compute independently. Changes that span several files, change an API or belong at the call sites are ordinary project work and are out of scope. Beyond the bundled examples, a test fixture and the two libdense tasks, dso has not been used on other code bases yet; expect to find rough edges, and please report them.

The libdense tasks

dso was first written for libdense. Two tasks are the larger, real-world examples; the libdense source is not included in this repository.

libdense-id-map is the task for current libdense (0.4.6.18): the Swiss-table id map in libdense_sim/src/collections/id_map.c, which is 12 to 34 percent of the instructions of a simulation tick. Every call is checked against a model under injected allocation failures and guard zones, nine controls cover the contract, and four end-to-end scenes time complete ticks of the library built around the candidate. tasks/libdense-id-map/capture/README.md says how the target and the workloads were chosen. With the unchanged 0.4.6.18 source tree in external/libdense-0.4.6.18-reference (or edit project_src in the config):

make test LIBDENSE_0418_SRC=external/libdense-0.4.6.18-reference
build/bin/dso --config config/libdense-id-map.json calibrate

sim-u64-sort (the radix sort in libdense_sim/src/collections/u64_sort.c, libdense 0.4.5) is the older task: a call-site capture, sixteen controls, five end-to-end benchmarks and an optional aarch64 compile check. With an extracted libdense 0.4.5 source tree:

sh scripts/prepare-libdense.sh LIBDENSE_SOURCE_DIR NEW_PREPARED_DIR
make test LIBDENSE_SRC=NEW_PREPARED_DIR
build/bin/dso --project NEW_PREPARED_DIR status

Each task is pinned to its release by file hashes. Another libdense release is not an interchangeable baseline: it needs a task of its own.

The window

make gui builds build/bin/dense-superoptimizer, a view of the same engine: the funnel, measurements with confidence bars, the archive, source and diff of a candidate, the calibration, generators and lineage.

The overview of the window

The pictures in docs/screens/ show version 0.0.8 on the base64-encode example after a calibration, a few hand-written candidates and a short tuner campaign, on a small virtual machine: they show the interface, and their numbers are not performance claims.

The funnel Measurements
The archive One candidate

Documentation

file content
AGENTS.md instructions for coding agents: the loop, the rules, how to read and report results
lab/README.md DSO Lab: a journal of hypotheses, context, candidates and runs for agent-led campaigns
docs/DESIGN.md the funnel, calibration, the trust model, the archive
docs/CONFINE.md the sandbox: its layers, its settings, what it does not do
docs/GENERATORS.md the language-model generators: providers, settings, how a reply is read
docs/TASKS.md the task format
docs/WRITING-A-TASK.md writing a task for your own project, step by step
docs/VERDICT.md the statistics and the verdict rules
docs/AGENT-WORKFLOW.md one investigation step by step, with measured tables
KNOWN_ISSUES.md what dso does not do
CHANGELOG.md what changed in each version

Development

make -j2 test check-ascii     # unit tests, the standalone and scaffold tests, the ASCII check
make test-sanitize            # the same tests with dso itself under ASan and UBSan
python3 tests/demo_integration.py --out NEW_DIR --example base64-encode --full   # an example end to end, calibration included
make dist                     # a deterministic source zip next to the tree

Sources and documentation are ASCII only (make check-ascii). A change to a task or its harness is a new contract, and a change to dso is a new runtime identity: both need a new calibration.

License

dense-superoptimizer is released under the MIT License: see LICENSE. The bundled third-party components (cJSON, the embedded fonts) keep their own licenses, and so does libdense: its source is not included, and the files of the sim-u64-sort task and exemplar that are derived from it remain under libdense's license. THIRD_PARTY_NOTICES.md has the details.

About

C optimization framework that evaluates candidates proposed by AI agents, evolutionary coding (EC), symbolic regression (SR) with an oracle

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages