Skip to content

feat: remaining #740 memory profiler (sites, promotion, live map, MLIR) - #1366

Closed
godofecht wants to merge 2 commits into
mainfrom
cursor/mem-profile-remaining-740-3359
Closed

godofecht wants to merge 2 commits into
mainfrom
cursor/mem-profile-remaining-740-3359

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Intent

Part of #740 (parent #727). Related: #728. Does not overlap #669 / PR #1328 (static memory lifetimes).

Second slice of the opt-in runtime memory profiler after merged #1341. Adds the remaining runtime accounting: stack/arena promotion bytes, per-compiler-site copy/alloc attribution, a hashed capped live-block map with overflow, and MLIR-backend flow_mem_* wrappers.

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 the remaining #740 profiler gaps. #1341 already landed the first heap/RSS/temp/copy slice. PR #1328 is the #669 static-lifetime MLIR --lifetimes input; this PR only counts stack/arena/compiler sites at runtime and does not change lifetime analysis.

Evidence

Baseline before this change: generated C labelled stack/arena and per-site lines (deferred, needs #669); live map was a 1024-slot linear scan with silent overflow; MLIR called @malloc / @memcpy directly.

Targeted proof that exercises the changed behaviour: see the testing commit / later Evidence update. Planned gates: host selftest, tests/cgen/run.flow mem_profile, tests/scripts/mem_profile.flow, bootstrap --regen/--verify in a follow-up commit.

Why that proof would detect the old failure or regression: old flowc still emits the deferred #669 strings; missing stack notes fail stack_bytes: 16; missing arena hooks fail arena_bytes: 16; missing MLIR wrappers fail func.call @flow_mem_malloc.

Repository gate(s) run:

(pre-testing revision; gates follow)

Generated/bootstrap state, if any: bootstrap C not yet regenerated.

Risk

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

Dormant unless FLOW_MEM_PROFILE is set. Hashed 4096-slot map replaces the linear 1024 scan. One extra note call on sized-array locals and arena bumps. No language syntax change. #669 static lifetimes are untouched.

  • 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

Pre-testing source commit. Bootstrap C regeneration is a separate commit after selftest.

Remaining gaps

Part of #740. Still open after this slice:

Open in Web Open in Cursor 

godofecht added a commit that referenced this pull request Oct 7, 2026
The opt-in runtime memory profiler (FLOW_MEM_PROFILE) now attributes
allocations to their source sites, counts stack and arena promotion
bytes, keeps a live-block map with an overflow counter, and the MLIR
backend routes allocations through the same hooks. It is runtime
accounting; #669 static lifetime analysis is separate.
docs/library/memory-profile.md documents the report keys.

Rebased from PR #1366 onto current main. Conflicts kept main's lazy
profiler init (#746) and memory-suite-only harness profiling, with this
PR's new counters.
@godofecht
godofecht marked this pull request as draft October 7, 2026 19:05
@godofecht
godofecht force-pushed the cursor/mem-profile-remaining-740-3359 branch from b863d83 to d0582be Compare October 7, 2026 19:05
@godofecht

Copy link
Copy Markdown
Contributor Author

Rebased onto main 524633d9 (source f472c053, bootstrap d0582bea) and converted to draft: it is not mergeable yet.

Rebase: conflicts kept main's lazy profiler init (#746) and memory-suite-only harness profiling with this PR's new counters (cgen comment, memory.md, CHANGELOG).

What passes on the rebased tree: flowc selftest; --regen fixed point; --verify; tests/cgen; test-lang; test-runtime; self_host_full; tests/bench_harness; tests/scripts/mem_profile 7/0; doc gates; gen_stdlib_docs --check.

What fails, and it is in this PR's MLIR hooks:

compiler__fixtures__mlir__arrays.flow.mlir:98:15: error: 'llvm.mlir.addressof' op must reference a global defined by 'llvm.mlir.global', 'llvm.mlir.alias' or 'llvm.func' or 'llvm.mlir.ifunc'
        %rp = llvm.mlir.addressof @flow_mem_report : !llvm.ptr

flow_mem_profile_init registers flow_mem_report with atexit through llvm.mlir.addressof, but flow_mem_report is emitted as a func.func, so every MLIR program that includes the profiler fails to lower (parity_mlir: 8 fixtures differ and none of the run fixtures lower; tests/mlir_commands: mlir_arrays, mlir_closures, mlir_data, mlir_defer_addr, mlir_effects differ). The report function has to be an llvm.func (or be reached through func.constant plus a cast), and the fixture goldens re-recorded after that.

The roundtrip failure seen in the same run (stage_a_infer_struct) came from a stale compiler/build/stage_a_driver_flow left by another branch and is not this PR's.

The opt-in runtime memory profiler (FLOW_MEM_PROFILE) now attributes
allocations to their source sites, counts stack and arena promotion
bytes, keeps a live-block map with an overflow counter, and the MLIR
backend routes allocations through the same hooks. It is runtime
accounting; #669 static lifetime analysis is separate.
docs/library/memory-profile.md documents the report keys.

Rebased from PR #1366 onto current main. Conflicts kept main's lazy
profiler init (#746) and memory-suite-only harness profiling, with this
PR's new counters.

Fixed on rebase: the MLIR profiler registered flow_mem_report with
atexit through llvm.mlir.addressof, which cannot name a func.func, so no
MLIR program with the profiler lowered. It now takes the address with
func.constant and an unrealized cast, as function values do elsewhere.
The profiler prelude changes the emitted MLIR of programs that allocate,
so the MLIR fixture goldens, the digests of the 748 affected corpus rows
(no rows added), ten mlir_commands goldens and three CLI .files hashes
(darwin and linux) are re-recorded.
Fixed point at gen2; bootstrap_from_c.sh --verify passes.
@godofecht
godofecht force-pushed the cursor/mem-profile-remaining-740-3359 branch from d0582be to 2800e6e Compare October 7, 2026 19:44
@godofecht
godofecht marked this pull request as ready for review October 7, 2026 19:44
@godofecht

Copy link
Copy Markdown
Contributor Author

Update: fixed and ready (source 60e872f9, bootstrap 2800e6e8, on main 524633d9).

  • The MLIR profiler now takes flow_mem_report's address with func.constant plus builtin.unrealized_conversion_cast (the pattern mlirgen already uses for function values), which --reconcile-unrealized-casts removes after func-to-llvm. parity_mlir runs all 24 fixtures through MLIR again (24/24).
  • Re-recorded because the profiler prelude changes the MLIR of programs that allocate: the MLIR fixture goldens, the digests of the 748 affected corpus rows (no rows added or removed), ten tests/mlir_commands goldens, and the compile_hello / compile_isolated_root / python_source CLI .files hashes (darwin; linux had the same values and gets the same new ones).

Gates: selftest PASS; --regen fixed point; --verify PASS; parity_mlir PASS; tests/mlir_commands 88/0; roundtrip ALL PASS (with stale stage_a_driver* removed first); self_host_full ALL PASS; tests/cgen pass; test-lang pass; test-runtime pass; tests/scripts/mem_profile 7/0; parity_lowering pass; bench_harness pass; doc gates and gen_stdlib_docs --check clean; tests/cli 152/2 after the update (jit_hello, test_mlir: drift on main).

Ordering: #1367 also changes the same three CLI hashes and cgen's profiler code. Whichever lands second has to re-run ./flow tool tests/cli/run.flow --update compile_hello compile_isolated_root python_source and copy the three files to goldens/linux, and re-record parity_mlir.

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