Skip to content

About

The multiply instruction neither GCC nor LLVM can price: measurement harness, captures and compiler audit for integer division by a constant on the Cortex-A72

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Cost-Model Blind Spots in Integer Division by Constants

Measurement harness, hardware captures, compiler audits, and an LLVM patch supporting the paper Cost-Model Blind Spots in Integer Division by Constants (Lev Sylin & Dymytrii Tymofii Sulialin).

On AArch64 cores where umull and umulh carry different microarchitectural costs—most notably the ARM Cortex-A72 (Raspberry Pi 400)—even-divisor constant division can be realized with a widening 32-bit multiply (umull) and pre-shifted operand rather than a 33-bit constant high-half multiply (umulh). Neither GCC nor LLVM prices this multiply-instruction difference accurately in its cost model.

Headline Numbers (Cortex-A72, Divisor 14)

sequence multiply latency (cycles) throughput (cycles / div)
Granlund & Montgomery, Figure 4.1 umull 7.00 2.69
Figure 4.2, high half as printed umulh 7.00 4.00
Figure 4.2, pre-shifted widening form umull 4.00 1.77
Mitsunari & Hoshino, Listing 5 umulh 6.00 4.00

Adding the missing scheduling-model query in LLVM yields a 24.5% wall-clock speedup on an 8-division-per-iteration loop on the Cortex-A72 (and 1.3%–4.0% on mixed loops).

What this repository is for

The claim is checkable, so the repository exists to let someone check it without trusting us:

  • the bench (core/) — one measurement file, one contract, four hosts, no dependencies. It does not assume the numbers above; it produces them.
  • the captures (bench/captures/) — every number here comes from a file in this tree, and each capture carries the SHA-256 of the source that produced it.
  • the probes (bench/probes/) — the four instruments that answer what the bench cannot: a corpus scan over real binaries, a cycle profile, a wall-clock harness, and a foreign harness.
  • the compiler audit (compiler-audit/) — what GCC and LLVM actually emit at pinned commits, the 46-line patch that supplies the missing cost, and the .s files behind every figure.

The paper that argues from this measurement is submitted separately. It is not in this repository: what is here is the evidence, which stands or falls without it.

Run it

sh core/run.sh                          # build and measure every block on this machine
sh core/run.sh BLOCKS=form_latency      # one block
sh core/run.sh HOST=riscv               # another host
OUT=/tmp/cap.csv sh core/run.sh         # write a capture with its passport

core/run.sh measures divisor 7, which is the default. The table above is divisor 14, and the pre-shifted rows exist only for even divisors, so core/run.sh cannot reach them. The command that reproduces the table is:

sh bench/run_refbench.sh --divisor=14 --blocks=form_latency,form_throughput --no-verify  # ~13 s

Drop --no-verify to prove every form exact against the divider first; that is the full run and it takes about six minutes, because it exhausts 2³² dividends per form on x86-64 and strides on hosts where exhaustion does not fit. A capture without the proof says so in its own header, and the proof is what makes the numbers a measurement rather than a timing.

Prefer bench/collect_refbench.sh <ssh-target> to measure a machine you can reach but do not want to build on: it copies the core over, prepares the machine (governor, SMT sibling, pin), runs, restores the machine, and files the capture here.

Needs a C compiler and nothing else. To measure on a machine without one, bench/live/ builds a USB image that boots straight into the bench.

The first run after prepare_machine.sh reads low on the smallest blocks and settles on the second: bench/captures/pi_20261124T004401Z_refbench.csv gives 1.7711 for the last table row, a first run after preparing gave 1.7268, and the run after that gave 1.7710. Take two, and keep the second. A capture that was prepared says so in its machine_prep= header; a capture without that field was not prepared, and its throughput is a number whose clock state is unknown.

Check it

make check

No network, no virtualenv, no Python packages — requirements.txt is empty because there are none. Fifteen gates, the interesting ones being:

sh tools/check_captures.sh    # every capture was taken by the code in this tree, by hash
sh tools/check_hosts.sh       # every host still links, every capability is implemented
python3 tools/gen_events.py --check   # the PMU table matches the vendor snapshot it comes from
sh tools/check_probes.sh      # each probe has a PIN, a question, and at least one real run
sh tools/check_clean.sh core  # function and nesting limits, no dead code, no comments in code
python3 tools/strip_comments.py --check   # no prose in any source file, only directives
python3 compiler-audit/llvm-patch/check-per-core.py  # the per-core decision table adds up
python3 compiler-audit/llvm-patch/check-line-count.py  # 46 lines: 43 mechanism, 3 model entry
python3 compiler-audit/check-shelf-sync.py           # the paper shelf holds prose, never code

Layout

path what it is
core/blocks.c the blocks: what is measured and how.
core/measure.c running a chain, fitting an unroll, calibrating the clock, one cell.
core/clock.c the clock's own drift check, around every block.
core/emit.c one line of capture; buffers a block until the clock's verdict is in.
core/select.c which blocks were asked for, which magic constants a divisor has, hex parsing.
core/refbench.h the contract: capabilities, the host hook set, the option struct.
core/hosts/ four hosts: posix.c (x86, aarch64, riscv), riscv.c, xenon.c, esp32/.
bench/captures/ the captures, including 126 from earlier builds of the measurement code.
bench/probes/ corpus scan, execution profile, wall clock, foreign harness.
bench/live/ the live-USB builder, for machines without a toolchain.
bench/collector/ the Cloudflare worker that receives captures from borrowed machines.
compiler-audit/ the pinned GCC and LLVM trees, both patches, and every .s file.
tools/ the gates. Each fails the build rather than reporting.
data/divider_events_sources.json the vendor PMU snapshot core/common/divider_events.h is generated from.
tools/report.py prints the four rows above from the captures, and is a gate.
compiler-audit/llvm-patch/per-core.py measures both multiply latencies per core and compares the rule with what llc emits; check-per-core.py gates the committed table.
CITATION.cff how to cite this artifact.
CHANGES.md, VERSION.txt what the current release measured, and what it does not cover.

The rules this tree keeps

  1. One owner per quantity. blocks.c measures; nothing else does. A host supplies a clock and a counter and never a number.
  2. Capabilities, not architectures. A block is enabled by RB_HAVE_*, a clock by RB_CLOCK, an instrument by RB_ALT and RB_INST. A capability the target cannot encode is an #error naming it, never a silent fallback.
  3. A skipped block prints its reason. Zero and silence are forbidden: a block that could not run says so in the capture.
  4. A capture names its code. # core_sha256= is the hash of the file that produced it, and tools/check_captures.sh refuses a capture from another build.
  5. No comments in code. If a name cannot carry a meaning, the name is wrong. The prose lives here.
  6. A gate fails; it does not report. A warning is a gate somebody learns to scroll past.

Credits

Lev Sylin designed the bench, wrote the measurement and compiler-audit code, took the captures and drew the conclusions; Dymytrii Tymofii Sulialin provided the machines, took part in the discussions that shaped the measurement and reviewed the results, and both authors share equal credit for the work.

Citing this

CITATION.cff carries the author block, the version and the sources this work rests on, so GitHub renders a "Cite this repository" button. Cite the release you used: the version identifies which captures were current, and CHANGES.md says what those captures measured.

What this does not claim

Three refusals are built into the instrument rather than left to the reader, because each was a way to get a confident wrong number:

  • arm64 has no divider-busy performance counter. ARM publishes an operation count and no busy-cycle event, so the cross-check that runs on x86 and RISC-V is refused on the Cortex-A72, and its captures say so. A floating-point divider event reported as the integer one would have looked like a result.
  • The ESP32 parts cannot read their own clock, so their captures carry clock block=... unknown on every block instead of omitting the check the other hosts pass.
  • A shared CI runner cannot reproduce a 4.00-cycle number. The workflow runs the bench as a smoke test — it must start, measure, and write a passport naming its code — and asserts nothing about the values.

Reproducing the claim

claim what to open
the four sequences cost 7.00, 6.00, 7.00 and 4.00 cycles of latency on a Cortex-A72 python3 tools/report.py, or the three captures it names
with eight divisions in flight, 4.00, 4.00, 2.69 and 1.77 cycles each same, the form_throughput rows
and both are what bench/run_refbench.sh --divisor=14 --blocks=form_latency,form_throughput measures on hardware you have that command, then compare — a different core will not match, and is not expected to
LLVM emits the widened high-half form at d = 14 and can be made to emit the pre-shifted one compiler-audit/llvm-patch/a72-form-widened-x14.s, a72-form-preshift-x14.s
the decision rests on isZExtFree, which asks nothing about the multiply compiler-audit/llvm-patch/README.md quoting TargetLowering.cpp
the missing cost is 46 production lines of change compiler-audit/llvm-patch/0001-aarch64-keep-even-divisor-preshift.patch
GCC's cost model charges one number for both multiply instructions compiler-audit/gcc-a72-probe.patch

The patch's effect, measured on three loop shapes through both llc builds, is in compiler-audit/llvm-patch/README.md; compiler-audit/llvm-patch/wallclock/ is the harness, and it takes the two llc binaries as environment variables so the A/B is one command.

Two limits worth stating. The bench measures the four sequences as written in core/blocks.c and proved exact against the divider; it does not measure either compiler's binary, so "GCC emits this, and it costs that" is an inference from the audit plus the measurement, not a single observation. And every number here is one core at one clock: nothing in this tree claims the direction holds on a core whose cost model prices the two multiplies equally, which is most of them — that is the point of the finding.

About

The multiply instruction neither GCC nor LLVM can price: measurement harness, captures and compiler audit for integer division by a constant on the Cortex-A72

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages