Skip to content

feat: opt-in runtime memory profiler (#740) - #1341

Merged
godofecht merged 6 commits into
mainfrom
cursor/mem-profiling-740-cd11
Oct 4, 2026
Merged

godofecht merged 6 commits into
mainfrom
cursor/mem-profiling-740-cd11

Conversation

@godofecht

@godofecht godofecht commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Part of #740 (parent #727). Related: #728, #669.

First useful slice of runtime memory instrumentation so #728-style collection can record heap allocation count/bytes, peak live heap, peak RSS, compiler-temp bytes and copy volume without rewriting workload sources.

This ports the archive/feat/740-mem-profiling design to flowc: an opt-in FLOW_MEM_PROFILE runtime in generated C. When the variable is unset the program pays one predictable branch per routed allocation and prints nothing. When set, it writes a human report plus parseable key: value lines to stderr, or to FLOW_MEM_PROFILE_OUT.

Overlap check

  • I searched open pull requests and recent commits for overlapping work.
  • This does not duplicate another active implementation, or the relationship is explained below.

Overlap / dependency notes: No open PR implements #740. The retired Python reference lives at archive/feat/740-mem-profiling. Nearby #1337 (now on main) fills string/IO harness rows; this PR wires counters rather than overlapping that work. #1265/#1267/#1268 touch the harness for native/parallel/cold-start, not allocator counters. Rebased onto main after #1339 (SIMD), #1340 (type identity), #1337 (string concat / buffered I/O), and #1342 (cross-module @rt_safe/@lifetime summaries). __flowc_str_concatn routes through flow_mem_malloc. FLOW_MEM_PROFILE, SIMD helpers, type_family_id/alignof, and RT summaries are all present.

What changed

  • Compiler: flowc emits a dormant profiler prelude. libc malloc/calloc/realloc/free/memcpy and compiler-injected heap (string concat, n-way __flowc_str_concatn, array<T>(N), sort scratch, escaping closures) route through counting wrappers. Peak RSS comes from getrusage. Stack/arena promotion and per-site copy attribution are labelled deferred (need [MLIR Opt] Static memory lifetime scheduling and zero-runtime allocation #669).
  • Harness: ./flow tool bench_harness sets FLOW_MEM_PROFILE=1 and FLOW_MEM_PROFILE_OUT on every measured subject and records allocations, heap_bytes, peak_live_heap, temp_bytes, copies and median_rss_kb.
  • Workloads: runtime/allocation, memory/allocation_copy and memory/peak_rss now do real allocate/copy/RSS work, with CPython twins.

Evidence

Baseline before this change: generated C had no allocation counters; the memory suite subjects were return 0 / pass; harness JSON only had stdout allocations:/copies: (always 0) plus /usr/bin/time RSS on the memory suite.

Targeted proof that exercises the changed behaviour:

  • Host selftest after rebase onto feat: cross-module @rt_safe/@lifetime summaries (#765) #1342: flowc: PASS (type_id_ok=1, rt_summary_ok=1, profiler prelude and __flowc_str_concatn via flow_mem_malloc)
  • ./flow tool tests/cgen/run.flow mem_profile string_concat string_concat_chain attributes_not_spliced grow_helpers_memory lifetime_domains_no_trace — PASS
  • ./flow tool tests/scripts/mem_profile.flow — 4/4
  • ./flow tool tests/bench_harness/run.flow — pass=46 fail=0
  • ./flow tool tests/cli/run.flow compile_hello compile_isolated_root python_source — PASS (checksums unchanged vs perf: first-slice string concat and buffered text I/O (#747) #1337+profiler)
  • ./flow tool python_ratchet — PASS (new twins are under exempt benchmarks/baselines/python/)

Why that proof would detect the old failure or regression: old flowc omits flow_mem_profile_init; a silent-when-unset failure would print a report with the variable empty; a missing malloc rewrite would fail the golden and the allocations: 1 / heap_bytes: 64 e2e check; peak-live that ignored free would report 64000 instead of 64 on the allocation row; a raw malloc in __flowc_str_concatn would miss n-way concat allocations.

Repository gate(s) run:

  • env -u FLOWC_IN -u FLOWC_OUT "$(./flow tool compiler/scripts/flowc_host.flow)" → flowc: PASS (after rebase onto feat: cross-module @rt_safe/@lifetime summaries (#765) #1342)
  • ./compiler/scripts/bootstrap_from_c.sh --regen → fixed point at gen3
  • ./compiler/scripts/bootstrap_from_c.sh --verify → ALL PASS bootstrap_from_c
  • cgen / scripts / harness / CLI proofs above, against the regenerated Stage-A compiler

Generated/bootstrap state: compiler/bootstrap/flowc_stage_a.c regenerated after rebasing onto main's #1342 @rt_safe/@lifetime squash (ee7432b2).

CI on 24c7bd8e failed flowc self-host because parity_mlir still had the stub-program digest for the three filled harness rows. compiler/fixtures/mlir/corpus.txt now records the flowc MLIR digests for those three files only.

Remaining gaps (not this PR)

Risk

Compatibility, performance, security, lifetime, backend, or release implications:

Dormant unless FLOW_MEM_PROFILE is set. One extra branch per routed allocation on the default path. No language syntax change.

  • No unresolved language/API design decision is being implemented implicitly.
  • No unrelated generated artifacts or local state are included.
  • Docs changed when user-visible behaviour or syntax changed.
  • docs/project/Questions.md updated if this needs a language-design decision.

Compatibility

  • No user-visible compatibility change.
  • Compatibility change is documented in docs/project/CHANGELOG.md.

Agent handoff

Rebased onto origin/main (ee7432b2, #1342). FLOW_MEM_PROFILE and golden retargets kept. __flowc_str_concatn uses flow_mem_malloc. Bootstrap regenerated. Head is d154801e48935bd2b0c238ca62bf0c6a7cdd6d65. Do not merge.

Open in Web Open in Cursor 

@godofecht
godofecht marked this pull request as ready for review October 4, 2026 20:18
@cursor
cursor Bot force-pushed the cursor/mem-profiling-740-cd11 branch 3 times, most recently from dbf6973 to 24c7bd8 Compare October 4, 2026 21:33
cursoragent and others added 6 commits October 4, 2026 22:16
Add a FLOW_MEM_PROFILE runtime in flowc-generated C that counts heap
allocations, peak live heap, peak RSS, compiler-temp bytes and copy
volume without rewriting workload sources. The #728 harness records
those fields from FLOW_MEM_PROFILE_OUT, and the memory/allocation
cross-harness rows now do real allocate/copy/RSS work.

Co-authored-by: Abhishek Shivakumar <[email protected]>
flow_mem_malloc(32) contains the substring malloc(32), so the
negative check now looks for the unwrapped call form.

Co-authored-by: Abhishek Shivakumar <[email protected]>
The profiler prelude wraps malloc/realloc, mentions temp lifetime
bytes, and uses a constructor attribute. Tighten the attribute and
lifetime goldens to the annotation-splice payload, expect the
counting wrappers in grow helpers, and refresh hello.c checksums.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@cursor
cursor Bot force-pushed the cursor/mem-profiling-740-cd11 branch from c12add9 to d154801 Compare October 4, 2026 22:25
@godofecht
godofecht merged commit d0e2a8a into main Oct 4, 2026
40 checks passed
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Fixed-point C after rebasing onto the mem profiler (#1341 / d0e2a8a).
bootstrap_from_c.sh --regen (FIXED POINT at gen3) then --verify PASS.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Rebased onto d0e2a8a (#1341). Source keeps FLOWC_PROFILE plus
FLOW_MEM_PROFILE cgen checks. Regen fixed point at gen2; --verify
matches compiler/src.

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Oct 5, 2026
* feat: opt-in per-phase compile timings for flowc (#735)

Instrument parse, import resolution, typecheck and codegen behind
FLOWC_PROFILE, and teach compile_bench to record cold/warm
compile-to-result plus the top fixed vs scaling cost.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: include profile in self-host frontend module lists

resolve.flow imports .profile for FLOWC_PROFILE spans. Roundtrip
compiled resolve without emitting profile first, so cc failed on
implicit flowc_prof_span_begin / FLOWC_PROF_* undeclared.

Add profile to roundtrip, gate_lib frontend/self-emit, and g2
fixed-point modules. Scripts only; no bootstrap regen.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: regenerate bootstrap C after rebase onto mem profiler

Rebased onto d0e2a8a (#1341). Source keeps FLOWC_PROFILE plus
FLOW_MEM_PROFILE cgen checks. Regen fixed point at gen2; --verify
matches compiler/src.

Co-authored-by: Abhishek Shivakumar <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 5, 2026
{llvm.noalias} and // flowc.noalias comments change normalized MLIR
digests. Refresh via parity_mlir.flow --record (also records newly
accepted programs from #1341/#1342 and the noalias fixtures).

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 5, 2026
{llvm.noalias} and // flowc.noalias comments change normalized MLIR
digests. Refresh via parity_mlir.flow --record (also records newly
accepted programs from #1341/#1342 and the noalias fixtures).

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Oct 5, 2026
* perf: emit proven restrict/noalias in C and MLIR backends (#731)

Add a fail-closed provenance certificate for local arrays and constant
slices, then mark only proven-disjoint internal pointer parameters and
unique local pointers with C restrict and MLIR {llvm.noalias}.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: link noalias into stage-A frontend (#731)

cgen calls flowc_noalias_* but the module was missing from
frontend_modules, the self-emit plan, roundtrip compile_module, and
the g2 fixed-point list. Same inventory pattern as type_id / rt_summary.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: re-record parity_mlir corpus after noalias metadata (#731)

{llvm.noalias} and // flowc.noalias comments change normalized MLIR
digests. Refresh via parity_mlir.flow --record (also records newly
accepted programs from #1341/#1342 and the noalias fixtures).

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: regenerate bootstrap C after noalias rebase onto #1343

Fixed-point C after rebasing onto phase timing (#1343 / c5332d7).
bootstrap_from_c.sh --regen (FIXED POINT at gen3) then --verify PASS.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: retarget mlir_defer_addr golden for flowc.noalias (#731)

flowc now emits // flowc.noalias on proven-local MLIR allocas. Re-record
the mlir_commands expected text so the check matches without weakening
noalias/restrict emission.

Co-authored-by: Abhishek Shivakumar <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
godofecht added a commit that referenced this pull request Oct 7, 2026
Port of the startup cohort from the original #1410 branch onto main after
#1265 (native baselines). The startup_* rows reuse the cold sources and
twins, build the Flow program before timing, and every row now carries a
timing_scope. The draft's profile flag is dropped: main already profiles
only the memory suite (#1341).
@godofecht
godofecht deleted the cursor/mem-profiling-740-cd11 branch October 12, 2026 23:02
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.

2 participants