Skip to content

cgen: portable SIMD-aware lowering for hot C loops - #1339

Merged
godofecht merged 3 commits into
mainfrom
cursor/cgen-portable-simd-hot-loops-92ec
Oct 4, 2026
Merged

godofecht merged 3 commits into
mainfrom
cursor/cgen-portable-simd-hot-loops-92ec

Conversation

@godofecht

@godofecht godofecht commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Part of #739 (parent #727). Related: #665, #666, #733, #751.

The production C backend still emitted scalar counted loops for the usual numeric form for i in 0 to n. Only stepped straight-line loops got clang/GCC vectorize hints, so stdlib kernels (scale_f32, sum_f32, dot_f32, copies) never advertised SIMD to the C compiler. Jules PR #1333 is an empty publish of the same session and is left alone; #1280 merged only scratch C files at the repo root.

This slice adds real flowc codegen:

  • Portable hints: #pragma omp simd when _OPENMP is defined, else clang loop vectorize / GCC ivdep.
  • The same hints on stepless ascending simple-body loops (the hot path).
  • OpenMP reduction(+:x) / (-:) / (*:) when the body is acc = acc ⊕ expr.
  • memmove for unit-stride dst[i] = src[i] (pointer/array, not spans; keeps bounds checks when they would have fired).
  • Loop-carried scalars that are not reductions (acc = acc * 10 + i) get only the clang hint, so OpenMP/ivdep do not promise independence.

Scalar fallback is the same loop body; pragmas and memmove are optional C.

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: Open PR #1333 claims this issue but has 0 files changed — not revived. Merged #1280 did not change compiler/src/cgen.flow. Rebased onto current main (includes #732 record-update copy elision).

Evidence

Baseline before this change: tests/cgen/for_vectorize_pragma passed; stepless for i in 0 to n had no SIMD hints. No memmove copy lowering.

Targeted proof:

  • Host flowc: SIMD + copy-elision cgen subset 9/9 (for_vectorize_pragma, for_vectorize_pragma_skip, for_simd_hot_loops, for_simd_carried, for_range_affine, perf_remarks, parallel_for_openmp, copy_elision, record_update)
  • Default bootstrap path after regen: SIMD + copy_elision 5/5
  • PATH=/tmp/nopy-mlir:$PATH ./flow tool compiler/scripts/parity_mlir.flow → PASS (fixtures 23/23, corpus programs=2469 fail=0)
  • ./compiler/scripts/bootstrap_from_c.sh --verify → PASS

Why that proof would detect the old failure: goldens require the new C hints/memmove; parity_mlir now has the updated corpus digest for tests/cgen/for_vectorize_pragma_skip.flow (the skip-test source dropped no_step).

Repository gate(s) run: SIMD/copy-elision cgen subset, parity_mlir (no-Python PATH shim), bootstrap_from_c.sh --verify.

Generated/bootstrap state: compiler/bootstrap/flowc_stage_a.c regenerated to the gen2 fixed point after the rebase.

Risk

Generated C gains preprocessor-guarded pragmas and, for copies, memmove. Compilers ignore unknown pragmas; results are unchanged for the scalar fallback.

  • 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.

Remaining work for #739

Not claiming Closes #739. Still needed for acceptance:

  • Target-guarded portable vector extensions/intrinsics when structural C is not enough.
  • Tier-1 x86-64 and arm64 assembly evidence that representative kernels hit expected vector width.
  • While-loop rotation to counted for; span copies; branchy clip/min-max forms.

Agent handoff

Cursor cloud agent. Do not merge.

Any remaining blocker is recorded in GitHub rather than only in local/session state.

Open in Web Open in Cursor 

@godofecht
godofecht marked this pull request as ready for review October 4, 2026 18:49
cursoragent and others added 3 commits October 4, 2026 19:30
Emit compiler-recognizable C for the #739 numeric loop forms on the C
backend: OpenMP simd (with reduction clauses) when available, clang/GCC
vectorize hints otherwise, and memmove for contiguous copies. Stepless
`for i in 0 to n` now gets the same treatment as stepped loops. Scalar
loop-carried updates stay off the OpenMP/ivdep path.

Co-authored-by: Abhishek Shivakumar <[email protected]>
The #739 skip-test no longer includes the stepless zeroing loop, so its
recorded normalized-MLIR digest in compiler/fixtures/mlir/corpus.txt
needed a parity_mlir-style re-record of that one program.

Co-authored-by: Abhishek Shivakumar <[email protected]>
Fixed point of compiler/src after rebasing the #739 SIMD slice onto
main's record-update copy elision. bootstrap_from_c.sh --verify holds.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@cursor
cursor Bot force-pushed the cursor/cgen-portable-simd-hot-loops-92ec branch from f55ade5 to 51b0800 Compare October 4, 2026 19:40
@godofecht
godofecht merged commit b9ac6a1 into main Oct 4, 2026
37 checks passed
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
#1339 added for_simd_carried and for_simd_hot_loops. Record their
normalized MLIR via parity_mlir.flow --record.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Fixed point of compiler/src after rebasing the #740 memory profiler
onto main's SIMD C lowering (#1339). bootstrap_from_c.sh --verify holds.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@cursor cursor Bot mentioned this pull request Oct 4, 2026
8 tasks done
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Rebased onto main (#1339). Bootstrap C now includes both portable
SIMD-aware loop lowering and FLOWC_PROFILE phase timers.

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Oct 4, 2026
…1340)

* feat: add compiler-derived type identity and alignof (#775)

Expose type_family_id<T>(), type_schema_id<T>(), type_transport_safe<T>(),
and alignof<T>() as compile-time builtins so libraries can stop assigning
manual type keys. Family and schema IDs are target-independent FNV-1a 64
digests of a versioned canonical description; size and alignment stay the
target ABI.

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

* fix: regenerate bootstrap C after type identity builtins

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

* test: compare compiled type IDs without flow-run banners

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

* test: use let mut for C compiler selection in type identity check

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

* fix: include type_id in cgen single-module roundtrip

compile_module cgen force-includes only listed dep headers, so the
new type-identity helpers were undeclared. Emit type_id first, then
-include it for cgen, and link it with the frontend objects.

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

* fix: regenerate bootstrap C after merging copy elision

Merged cgen now includes both type identity builtins and record-update
copy elision. Rewrite the checked-in bootstrap C to that fixed point.

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

* fix: re-record parity_mlir corpus after type identity

test_typecheck_overload_integration.flow imports typecheck.flow, whose
new type-identity builtins changed the normalized MLIR digest. Refresh
via parity_mlir.flow --record (also records newly accepted programs,
including test_type_identity.flow).

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

* fix: regenerate bootstrap C after merging portable SIMD

Merged cgen now includes type identity builtins and SIMD-aware loop
lowering. Rewrite the checked-in bootstrap C to that fixed point.

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

* fix: record parity_mlir digests for SIMD loop fixtures

#1339 added for_simd_carried and for_simd_hot_loops. Record their
normalized MLIR via parity_mlir.flow --record.

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

---------

Co-authored-by: Cursor Agent <[email protected]>
@godofecht
godofecht deleted the cursor/cgen-portable-simd-hot-loops-92ec 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