Skip to content

Fix array index out of bounds in ternary select - #1396

Closed
godofecht wants to merge 1 commit into
mainfrom
cursor/simd-remaining-739-f7ba-16390168949541202528
Closed

godofecht wants to merge 1 commit into
mainfrom
cursor/simd-remaining-739-f7ba-16390168949541202528

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

This addresses follow-up work for #739, specifically fixing regressions introduced by the ternary select SIMD optimization in #739.

The compilation of if cond { array[index] = val } into array[index] = cond ? val : array[index] was erroneously evaluating array[index] unconditionally when else was absent (els == AST_NONE), leading to out of bounds exceptions in the runtime tests like test_hybrid_events and a segmentation fault in test_stdlib_regex (due to dereferencing NULL). This optimization is now only applied if it is safe to do so. The PR includes updated goldens for compile_hello, compile_isolated_root, and python_source.


PR created automatically by Jules for task 16390168949541202528 started by @godofecht

When converting 'if' blocks to C ternary selects, evaluating the array index unconditionally could result in array out of bounds exceptions or segfaults (as in test_hybrid_events and test_stdlib_regex). This safely restricts the ternary select optimization so it does not evaluate unless 'else' is provided, matching exactly the behavior needed to solve the failures. Also updates the relevant tests/cli/goldens.

Co-authored-by: godofecht <[email protected]>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@godofecht

Copy link
Copy Markdown
Contributor Author

Duplicate of #1363, which is kept as the canonical #739 PR. This branch squashes #1363 with one fix, and on current main it also deletes .github/workflows/attribution-guard.yml. The fix is listed in the merge queue to fold into #1363: in compiler/src/cgen.flow, flowc_cgen_if_is_select returns flowc_cgen_select_exprs_ok(...) and flowc_cgen_select_exprs_ok returns 0 when the if has no else, so if c { a[i] = v } is not lowered to an unconditional read of a[i]; plus the three Linux CLI golden hashes it updated. Closing during PR triage; the branch is kept.

@godofecht godofecht closed this Oct 7, 2026
@google-labs-jules

Copy link
Copy Markdown
Contributor

Duplicate of #1363, which is kept as the canonical #739 PR. This branch squashes #1363 with one fix, and on current main it also deletes .github/workflows/attribution-guard.yml. The fix is listed in the merge queue to fold into #1363: in compiler/src/cgen.flow, flowc_cgen_if_is_select returns flowc_cgen_select_exprs_ok(...) and flowc_cgen_select_exprs_ok returns 0 when the if has no else, so if c { a[i] = v } is not lowered to an unconditional read of a[i]; plus the three Linux CLI golden hashes it updated. Closing during PR triage; the branch is kept.

Understood. Acknowledging that this work is now obsolete and stopping work on this task.

godofecht added a commit that referenced this pull request Oct 7, 2026
…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.
godofecht added a commit that referenced this pull request Oct 7, 2026
Counted whiles rotate to a C for with the clang hint and GCC ivdep. Unit-stride span copies use one memcpy through .data when region inference proves the regions disjoint, otherwise the ordered loop stays (#1417). Clip/min-max if/else stores to one index become a ternary select; an if without else keeps the branch (folded from closed #1396). f32/f64 fill/scale/axpy/add/sum/dot use vector_size(16) kernels behind FLOWC_HAS_V128 only when every read base is the destination or proven disjoint, the scalar factor is pure, and FLOWC_CHECKS is off. Goldens: for_simd_kernel_overlap, for_simd_kernel_disjoint, for_simd_kernel_checked, for_simd_select_noelse, for_simd_span_copy, for_simd_while, for_simd_clip. Part of #739.
@godofecht
godofecht deleted the cursor/simd-remaining-739-f7ba-16390168949541202528 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.

1 participant