4cacfcbdac
Hypothesis-driven measurement of "did monomorphisation actually buy us performance?" on a 100M-iter LCG hot loop, AILang mono'd code vs. four C reference variants (direct-inlinable, direct- noinline, indirect-monomorphic, indirect-polymorphic). Zen 3, clang -O2, median-of-15. Headline: H1 supported, but the mechanism is inlining, not dispatch shape. AILang mono = hand-C direct (1.000x). Indirect- monomorphic = direct-noinline (1.000x) — saturating branch predictor makes the indirect-call cost vanish on this hardware. Inlining is the actual 3.31x win; polymorphic indirect adds another 21% predictor-miss penalty. DESIGN.md Decision 11 gains a rationale paragraph reframing mono as inlining-enabler rather than indirect-call-eliminator, with explicit pointer to the bench. JOURNAL entry records the full methodology, ratios, limitations, and the side-effect mono-pass env.globals-seeding bug surfaced while building the AILang fixture (separate RED-first debug iter to follow).
40 lines
1.1 KiB
C
40 lines
1.1 KiB
C
// Hand-C reference for bench_mono_dispatch — DIRECT, INLINABLE variant.
|
|
//
|
|
// Mirrors the post-monomorphisation AILang IR: a tail-recursive loop
|
|
// that calls `foo(i)` at every step. `foo` here has no `noinline`
|
|
// attribute, so clang -O2 will inline it into the loop body. This is
|
|
// the OPTIMISTIC bound on what AILang's mono pass can achieve once
|
|
// LLVM optimises the resulting IR.
|
|
//
|
|
// Workload: loop_call(N, 0) with
|
|
// acc' = acc + foo(acc + i)
|
|
// foo(x) = x * 1103515245 + 12345 (LCG-ish, prevents closed-form)
|
|
//
|
|
// N = 100_000_000.
|
|
//
|
|
// The result is data-dependent on every prior iteration, so clang
|
|
// cannot algebraically close-form-fold the loop; the wall-time is
|
|
// dominated by the body.
|
|
//
|
|
// Build: clang -O2 -o bench_mono_direct bench_mono_direct.c
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
static int64_t foo(int64_t x) {
|
|
return x * 1103515245 + 12345;
|
|
}
|
|
|
|
static int64_t loop_call(int64_t i, int64_t acc) {
|
|
while (i != 0) {
|
|
acc = acc + foo(acc + i);
|
|
i = i - 1;
|
|
}
|
|
return acc;
|
|
}
|
|
|
|
int main(void) {
|
|
printf("%lld\n", (long long)loop_call(100000000, 0));
|
|
return 0;
|
|
}
|