Skip to content

fix(cgen): emit SIMD independence pragmas only with a proof (#739) - #1416

Closed
godofecht wants to merge 3 commits into
mainfrom
fix/739-simd-dependence-gates
Closed

godofecht wants to merge 3 commits into
mainfrom
fix/739-simd-dependence-gates

Conversation

@godofecht

@godofecht godofecht commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of #739. Does not close it. The #732 copy half of the original branch landed as #1417, so this PR carries only the SIMD half, rebuilt on main e2205a7 (after #1363).

Change

#pragma omp simd and #pragma GCC ivdep tell the C compiler that loop iterations are independent. flowc_cgen_loop_omp_safe, and the copy of its rule that #1363 added for rotated while loops (flowc_cgen_body_omp_safe_skip), granted them to any body of indexed stores, select ifs and recognized reductions. That is unsound in two ways:

  • dst[i] = src[i] + 1 with dst = src + 1 carries a value from one iteration to the next.
  • acc = acc + acc * 2 matched the reduction pattern although its other operand reads the accumulator, so it got reduction(+:acc).

Now one predicate serves both loop forms. A body qualifies when it is let statements followed by one statement that is either:

Other simple loops keep the clang vectorize request, which leaves the dependence check to the C compiler. Rotated while loops still never get OpenMP simd, as #1363 set. flowc_cgen_body_omp_safe_skip is removed. CHANGELOG entry added.

Not carried: tests/native/copy_overlap_semantics.c, because main has no runner for tests/native/.

Related and not changed here: #1363's vector_size kernels and span-copy memmove make their own aliasing assumptions; they are worth a separate review against the same rule.

Evidence

  • New goldens: for_simd_dependence (shifted pointers plus the recurrence), for_simd_recurrence (the recurrence alone), for_simd_proven_reduction (a plain sum keeps reduction(+:total)). for_vectorize_pragma.expect updated. Before this change for_simd_dependence and for_vectorize_pragma fail (unexpected: #pragma omp simd, unexpected: #pragma GCC ivdep), and the old compiler emits #pragma omp simd reduction(+:acc) and #pragma GCC ivdep for the recurrence.
  • noalias_vectorize, for_simd_hot_loops, for_simd_clip and for_simd_while pass unchanged.
  • Edited compiler selftest: flowc: PASS. bootstrap_from_c.sh --regen: fixed point at gen3; --verify: PASS. roundtrip.flow: ALL PASS. Bootstrap regenerated in its own commit.
  • ./flow tool tests/cgen/run.flow: 140 passed, 0 failed. ./flow tool tests/scripts/simd_asm.flow: 2 passed, 2 skipped.
  • ./flow test-lang 274/0, ./flow test-runtime 34/0, ./flow tool tests/cli/run.flow 152 passed, 2 failed (jit_hello, test_mlir: golden drift on main).
  • self_host_full.flow fails here with gen1.c != gen2.c (the vector_size preamble and some TOK_* constants). It fails the same way on main e2205a7 with no change, so it is a baseline failure from cgen: remaining SIMD lowering for hot C loops (#739) #1363.

Merge note: cgen.flow merges cleanly with #1414; only the generated bootstrap files conflict, so the second to land needs its own regeneration.

@godofecht godofecht changed the title fix(cgen): never promise SIMD independence for potentially aliased loops (#739) fix(cgen): preserve alias-dependent loop semantics in SIMD and copy lowering (#739 #732) Oct 6, 2026
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. A loop body now gets them only when it is one statement that is
either a reduction or a store dst[i] = e where the noalias analysis made
dst restrict (#731) and e reads dst only at [i]. A reduction whose other
operand reads the accumulator (acc = acc + acc * 2) is a recurrence and
is no longer treated as a reduction. Other loops keep the clang vectorize
request. flowc_cgen_assign_is_index_store is removed as unused.

Carried from the SIMD half of the original #1416 branch; its copy half
landed as #1417.
@godofecht
godofecht force-pushed the fix/739-simd-dependence-gates branch from 05bfb5c to 6d5d9c3 Compare October 7, 2026 12:15
@godofecht godofecht changed the title fix(cgen): preserve alias-dependent loop semantics in SIMD and copy lowering (#739 #732) fix(cgen): emit SIMD independence pragmas only with a proof (#739) Oct 7, 2026
@godofecht
godofecht marked this pull request as ready for review October 7, 2026 12:15
@godofecht

Copy link
Copy Markdown
Contributor Author

Note from the merge train: #1363 (remaining SIMD lowering) merged as e2205a7. It adds the vector_size kernels, select-converted ifs and while-to-for rotation in compiler/src/cgen.flow, with the #1417 overlap rule applied to kernels and span copies. Please rebase this draft's for_simd_dependence and for_simd_proven_reduction work onto it.

#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 force-pushed the fix/739-simd-dependence-gates branch 2 times, most recently from 6d5d9c3 to 7be7ea4 Compare October 7, 2026 13:19
@godofecht

Copy link
Copy Markdown
Contributor Author

Overlap note: #1433 (mine) fixed #1424 the same way, so I am closing it in favour of this PR, which also covers the reduction case. Two things from it that may be worth carrying here:

Please add Fixes #1424 if this PR closes it.

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

Copy link
Copy Markdown
Contributor Author

Landed in #1429 as 3891827 (lane C train).

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