Repository navigation
cgen: remaining SIMD lowering for hot C loops (#739) - #1363
Conversation
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.
ee0d75c to
4636d77
Compare
|
Rebased onto main f43efb7 for merge (4636d77). Changes during the rebase:
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. |
#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.
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.
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.
#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.
Intent
Closes #739 (parent #727). Related: #665, #666, #733, #751.
Merged PR #1339 added portable OpenMP/clang/GCC hints and pointer
memmovecopies. This slice fills the remaining #739 gaps on the production C backend:vector_size(16)kernels (fill, scale, axpy, add, sum, dot) behindFLOWC_HAS_V128(SSE2 / AVX / NEON / aarch64), with a scalar tail and a pragma-hinted#else.while i < n { ...; i = i + k }rotated to a Cforso the same hints and copies apply (continueand@max_iterationsstaywhile)..dataviamemmove(one range check whenFLOWC_CHECKSis on).ifstores lowered to a C ternary so the auto-vectorizer sees a select.Scalar results stay exact. MLIR is not required.
Overlap check
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
a1b62114steplessfor i in 0 to nhad pragmas/memmovefor pointers only. Countedwhilestayedwhile. Span copies stayed scalar. Clipifstayed a branch and was unhinted. Novector_sizekernels 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: generatedvector_sizetypes; native x86-64-O3 -SSIMD ops; aarch64-linux-gnu-gcc-march=armv8-a -SSIMD 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.datamemmove, 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)simd_asm,wiki_navas aboveGenerated/bootstrap state:
compiler/src/cgen.flowchanged; bootstrap C regenerated in a separate commit (ace35235) after--regen/--verify.Risk
Generated C gains
#if FLOWC_HAS_V128vector loops, while-to-for, spanmemmove, 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.docs/project/Questions.mdupdated if this needs a language-design decision.Compatibility
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.