Skip to content

cgen: remaining SIMD lowering for hot C loops (#739) - #1363

Merged
godofecht merged 7 commits into
mainfrom
cursor/simd-remaining-739-f7ba
Oct 7, 2026
Merged

godofecht merged 7 commits into
mainfrom
cursor/simd-remaining-739-f7ba

Conversation

@godofecht

@godofecht godofecht commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Intent

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

Merged PR #1339 added portable OpenMP/clang/GCC hints and pointer memmove copies. This slice fills the remaining #739 gaps on the production C backend:

  • Target-guarded GCC/clang vector_size(16) kernels (fill, scale, axpy, add, sum, dot) behind FLOWC_HAS_V128 (SSE2 / AVX / NEON / aarch64), with a scalar tail and a pragma-hinted #else.
  • Counted while i < n { ...; i = i + k } rotated to a C for so the same hints and copies apply (continue and @max_iterations stay while).
  • Span copies through .data via memmove (one range check when FLOWC_CHECKS is on).
  • Clip / min / max same-index if stores lowered to a C ternary so the auto-vectorizer sees a select.

Scalar results stay exact. MLIR is not required.

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: #1339 is merged. Open #1333 still claims the issue with 0 files changed and is left alone. Open PRs around this window are other issues.

Evidence

Baseline before this change: on a1b62114 stepless for i in 0 to n had pragmas/memmove for pointers only. Counted while stayed while. Span copies stayed scalar. Clip if stayed a branch and was unhinted. No vector_size kernels and no Tier-1 assembly check.

Targeted proof that exercises the changed behaviour:

  • ./flow tool tests/cgen/run.flow for_simd_while for_simd_span_copy for_simd_clip for_simd_hot_loops for_simd_carried for_vectorize_pragma for_vectorize_pragma_skip inline_exp_nested_while → 8 passed, 0 failed (default bootstrap after regen)
  • ./flow tool tests/scripts/simd_asm.flow → 3 passed, 0 failed, 1 skipped: generated vector_size types; native x86-64 -O3 -S SIMD ops; aarch64-linux-gnu-gcc -march=armv8-a -S SIMD ops (.4s / fmul / fadd); x86 cross skipped because this host is already x86-64
  • ./flow tool wiki_nav → nav is consistent with docs/

Why that proof would detect the old failure or regression: goldens require vector_size/FLOWC_HAS_V128, for (; i < (n), span .data memmove, and clip ?:; the assembly checks fail if the kernels stay scalar on either Tier-1 ISA.

Repository gate(s) run:

  • env -u FLOWC_IN -u FLOWC_OUT "$(./flow tool compiler/scripts/flowc_host.flow)" → flowc: PASS
  • ./compiler/scripts/bootstrap_from_c.sh --regen → FIXED POINT at gen3
  • ./compiler/scripts/bootstrap_from_c.sh --verify → PASS (exit 0)
  • cgen SIMD subset, simd_asm, wiki_nav as above

Generated/bootstrap state: compiler/src/cgen.flow changed; bootstrap C regenerated in a separate commit (ace35235) after --regen / --verify.

Risk

Generated C gains #if FLOWC_HAS_V128 vector loops, while-to-for, span memmove, and ternary selects. Compilers without 128-bit SIMD keep the previous pragma/scalar path. Overlapping while-copies stay elementwise so they keep loop order. Results match 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.

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 

cursoragent and others added 7 commits October 7, 2026 12:59
Finish the production C-backend gaps left after the portable-pragma
slice: target-guarded 128-bit vector_size kernels, counted while-to-for
rotation, span memmove copies, and clip/min-max selects as ternaries.
Goldens and a Tier-1 assembly check cover the new forms.

Co-authored-by: Abhishek Shivakumar <[email protected]>
memmove is not equivalent to a forward overlapping dst[i]=src[i] loop.
Counted while rotation now emits a C for with SIMD hints instead.
Rename the assembly-check parameter off the C keyword asm, and compile
-S with the same C flags the cgen goldens use.

Co-authored-by: Abhishek Shivakumar <[email protected]>
Native -O3 -S already required 128-bit ops on the host ISA. When
aarch64-linux-gnu-gcc is available, also emit armv8-a assembly so an
x86-64 runner still has Tier-1 arm64 evidence (and vice versa with
x86_64-linux-gnu-gcc). Record the #739 C lowering in the changelog.

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

Folded into the rebased feature commit while resolving it against #1417:

- Span copies use memcpy through .data only when flowc_region_disjoint
  proves the regions disjoint; otherwise the ordered loop stays. The
  branch's memmove path would snapshot a shifted view (dst = src + 1)
  that the scalar loop treats as a recurrence.
- The scale, axpy and add vector_size kernels run only when every read
  base is the destination itself or proven disjoint from it, the scalar
  factor reads no memory and calls nothing (it is hoisted), all bases
  share the element type, and FLOWC_CHECKS is off (the kernels move whole
  lanes past the per-index bounds tests).
- Salvaged from closed #1396 (commit 2eedcac): flowc_cgen_if_is_select
  returns flowc_cgen_select_exprs_ok, and an if without else is never a
  select, so `if c { a[i] = v }` no longer reads a[i] when c is the
  bounds guard (test_hybrid_events, test_stdlib_regex).

Tests: for_simd_kernel_overlap (runtime recurrence on shifted views; the
branch's original compiler exits 1), for_simd_kernel_disjoint,
for_simd_kernel_checked, for_simd_select_noelse, and for_simd_span_copy
(parameters keep the loop, disjoint locals memcpy). All of them fail on
the branch's original compiler.
The runtime archive is built with OpenMP, and clang -fopenmp rejects
`#pragma omp simd` before `for (; i < n; ...)` (no init clause), so
lib/runtime/audio_simd.flow stopped compiling and every JIT and MLIR run
CLI test failed. OpenMP simd would also privatize the counter, which a
while loop often reads after the loop. Rotated whiles now get the clang
loop hint and, for omp-safe bodies, GCC ivdep.

The kernel and select goldens run with --no-checks: checks are on by
default and the kernels only apply without them.
Generated C now carries the FLOWC_HAS_V128 vector_size typedefs, so the
hello.c checksums in compile_hello, compile_isolated_root and
python_source change. Recorded with tests/cli/run.flow --update on macOS;
the Linux goldens held the same values as macOS before and after.
@godofecht
godofecht force-pushed the cursor/simd-remaining-739-f7ba branch from ee0d75c to 4636d77 Compare October 7, 2026 12:00
@godofecht

Copy link
Copy Markdown
Contributor Author

Rebased onto main f43efb7 for merge (4636d77). Changes during the rebase:

  • Fix/732 preserve overlap copy semantics #1417 overlap rule applied to the new paths. Span copies use one memcpy through .data only when region inference proves the two regions disjoint; otherwise the ordered loop stays (the branch emitted memmove, which snapshots a shifted view). The scale, axpy and add vector_size kernels now require every read base to be the destination itself or proven disjoint, a scalar factor that reads no memory and calls nothing (it is hoisted), one element type, and FLOWC_CHECKS off (the kernels move whole lanes past the per-index bounds checks).
  • Folded from closed Fix array index out of bounds in ternary select #1396 (2eedcac): an if without else is never a select, and flowc_cgen_if_is_select returns flowc_cgen_select_exprs_ok.
  • Rotated whiles no longer get #pragma omp simd: clang -fopenmp rejects it before for (;, which broke lib/runtime/audio_simd.flow and every JIT/MLIR-run CLI test. They keep the clang hint and GCC ivdep.
  • CLI file goldens (darwin and linux) regenerated for the vector typedef header; spec-index, CHANGELOG and c-simd-loops.md updated.

New goldens, each failing on this branch's original compiler: for_simd_kernel_overlap (the original exits 1: the kernels changed a recurrence on shifted views), for_simd_kernel_checked, for_simd_select_noelse, for_simd_span_copy, for_simd_while (no omp simd); plus for_simd_kernel_disjoint as the positive case.

Gates (macOS arm64): cgen 137/0, test-lang all pass including test_hybrid_events and test_stdlib_regex, test-runtime, simd_asm 2 passed / 2 skipped, self_host_full, roundtrip, parity_mlir, ratchets. tests/cli fails only jit_hello and test_mlir, as on main here.

Related: #1424 (GCC ivdep on possibly overlapping parameters, pre-existing on main) gets its own PR after this one.

@godofecht
godofecht merged commit e2205a7 into main Oct 7, 2026
42 checks passed
godofecht added a commit that referenced this pull request Oct 7, 2026
#pragma omp simd and GCC ivdep tell the C compiler that iterations are
independent. An indexed store does not prove that on its own: dst may be
src + 1. flowc_cgen_loop_omp_safe now accepts a body of `let` statements
followed by one statement that is either a reduction whose accumulator no
`let` reads, or a store or select `if` into dst[v] where the noalias
analysis made dst restrict (#731) and every expression reads dst only as
dst[v]. A reduction whose other operand reads the accumulator
(acc = acc + acc * 2) is a recurrence and no longer counts. The rotated
while loops from #1363 use the same predicate for their ivdep, and
flowc_cgen_body_omp_safe_skip is removed. Other loops keep the clang
vectorize request.

Carried from the SIMD half of the original #1416 branch; its copy half
landed as #1417.
godofecht added a commit that referenced this pull request Oct 7, 2026
self_host_full picked the first executable among the roundtrip.flow
drivers and compiler/build/flowc_bootstrap, with no check against
compiler/bootstrap/flowc_stage_a.c. A flowc_bootstrap built before #1363
made gen1.c != gen2.c locally (missing vector_size preamble, TOK_* lines
moved), while CI, building from a clean checkout, passed. The checked-in
bootstrap C is at the fixed point.

gen0 now comes from gl_flowc_cached: compiler/build/flowc_bootstrap,
rebuilt when missing or older than the bootstrap C. FLOWC_BOOTSTRAP still
overrides it. tests/scripts/self_host_gen0.flow runs the real gate against
a scratch root with a stale flowc_bootstrap and a stale roundtrip driver;
both checks fail on the old selection.
godofecht added a commit that referenced this pull request Oct 7, 2026
self_host_full picked the first executable among the roundtrip.flow
drivers and compiler/build/flowc_bootstrap, with no check against
compiler/bootstrap/flowc_stage_a.c. A flowc_bootstrap built before #1363
made gen1.c != gen2.c locally (missing vector_size preamble, TOK_* lines
moved), while CI, building from a clean checkout, passed. The checked-in
bootstrap C is at the fixed point.

gen0 now comes from gl_flowc_cached: compiler/build/flowc_bootstrap,
rebuilt when missing or older than the bootstrap C. FLOWC_BOOTSTRAP still
overrides it. tests/scripts/self_host_gen0.flow runs the real gate against
a scratch root with a stale flowc_bootstrap and a stale roundtrip driver;
both checks fail on the old selection.
godofecht added a commit that referenced this pull request Oct 7, 2026
#pragma omp simd and GCC ivdep tell the C compiler that iterations are
independent. An indexed store does not prove that on its own: dst may be
src + 1. flowc_cgen_loop_omp_safe now accepts a body of `let` statements
followed by one statement that is either a reduction whose accumulator no
`let` reads, or a store or select `if` into dst[v] where the noalias
analysis made dst restrict (#731) and every expression reads dst only as
dst[v]. A reduction whose other operand reads the accumulator
(acc = acc + acc * 2) is a recurrence and no longer counts. The rotated
while loops from #1363 use the same predicate for their ivdep, and
flowc_cgen_body_omp_safe_skip is removed. Other loops keep the clang
vectorize request.

Carried from the SIMD half of the original #1416 branch; its copy half
landed as #1417.
@godofecht
godofecht deleted the cursor/simd-remaining-739-f7ba branch October 12, 2026 23:25
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.

[Performance #727] Add portable SIMD-aware lowering for hot loops on the C backend

2 participants