Skip to content

fix: OpenMP simd and GCC ivdep only for memory-independent loop bodies (#1424) - #1433

Closed
godofecht wants to merge 3 commits into
mainfrom
fix/1424-simd-pragma-alias
Closed

godofecht wants to merge 3 commits into
mainfrom
fix/1424-simd-pragma-alias

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Fixes #1424.

flowc_cgen_loop_omp_safe accepted any body of indexed stores and reductions, and the pragma path then told the C compiler that iterations are independent (#pragma omp simd, #pragma GCC ivdep). With two pointer parameters that are shifted views of one buffer (dst = src + 1) the scalar loop is a recurrence. GCC 15 at -O3 vectorized it under ivdep and the program got a different result.

The pragmas are now emitted only when flowc_cgen_loop_mem_independent also holds:

  • every store is base[i] with i the loop counter;
  • two store bases are the same name or proven disjoint;
  • every indexed read is a store base at i, or proven disjoint from every store base; no reads through *p.

Disjoint means flowc_region_disjoint (the #1417 rule), or two different parameters where #731 proved one of them disjoint from every other pointer parameter at all call sites (it is emitted restrict). Otherwise only the clang hint is emitted; clang keeps its runtime alias checks. Rotated whiles from #1363 use the same check.

Proof: tests/cgen/for_simd_pragma_alias fails on main (both pragmas emitted). Its C, built from main's flowc with gcc-15 -O3, exits 1; built from this branch, it exits 0. At -O2 GCC does not vectorize that loop, so default builds were not affected; CFLAGS=-O3 builds were.

Gates (macOS arm64): cgen goldens 138/0 (noalias_vectorize and for_simd_clip keep their pragmas through the restrict proof), test-lang, test-runtime, simd_asm 2 passed / 2 skipped, self_host_full, roundtrip. tests/cli fails only jit_hello and test_mlir, as on main here.

Risk: GCC and OpenMP builds lose the independence hint on loops over unproven pointer parameters; they keep their own dependence analysis.

#1424)

flowc_cgen_loop_omp_safe accepted any body of indexed stores and
reductions, and the pragma path then promised the C compiler that
iterations are independent. With two pointer parameters that are shifted
views of one buffer (dst = src + 1) the scalar loop is a recurrence; GCC
15 at -O3 vectorized it under ivdep and the program got a different
result.

flowc_cgen_loop_mem_independent now also requires every store to be
base[i] with i the loop counter, store bases to be one name or proven
disjoint (flowc_region_disjoint, the #1417 rule), and every indexed read
to be a store base at i or proven disjoint from all store bases, with no
reads through *p. Otherwise only the clang hint is emitted, and clang
keeps its runtime alias checks. The same check covers rotated whiles.

tests/cgen/for_simd_pragma_alias fails on the previous compiler, which
emitted both pragmas; its C, built with gcc-15 -O3, exits 1.
… check (#1424)

A parameter emitted restrict is proven disjoint from every other pointer
parameter at every call site, so a loop over two such parameters keeps
OpenMP simd and GCC ivdep (noalias_vectorize).
@godofecht

Copy link
Copy Markdown
Contributor Author

Closing in favour of #1416, which fixes #1424 with a single proof-gated predicate and also fixes the reduction pattern that read the accumulator. The test and gcc-15 -O3 evidence are on #1416.

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.

cgen: OpenMP simd / GCC ivdep hints on indexed stores miscompile shifted overlapping views

1 participant