Skip to content

[Performance] Eliminate codegen taxes for complex scaling and array bounds checks (#729) - #1278

Merged
godofecht merged 7 commits into
mainfrom
fix-729-complex-scaling-14909106430004217066
Oct 4, 2026
Merged

godofecht merged 7 commits into
mainfrom
fix-729-complex-scaling-14909106430004217066

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

This pull request resolves two immediate blockers described in issue #729 for the performance improvement initiative:

  1. Complex x Real scaling (Complex times a real scalar lowers to a __mulsc3 call, blocking vectorisation of complex elementwise loops #630): Handled lowering of C99 complex * real multiplications so they fall back to fast component-wise execution rather than calling __mulsc3. Lowered cabs() correctly to cabsf() for c64 values.
  2. Bounds checks elision (Span indexing has no bounds-check elision, costing 9-29% on numerical kernels #615): When looping bounds structurally establish safety (i.e. 0 to span.len), the corresponding internal base[index] reads drop their array bounds check branch path.

Includes corresponding verification unit tests within tests/lang directly reproducing issue patterns, and bootstrap generation changes.


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

@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.

google-labs-jules Bot and others added 2 commits October 3, 2026 21:42
…checks

- Lower c64 and c128 operations involving real scaling to componentwise operations, avoiding the slow __mulsc3 / C99 complex promotion.
- Ensure cabs on a c64 emits cabsf.
- Omit array and span bounds checks for indices that are structurally proven safe by their enclosing for loop.

Part of #729

Co-authored-by: godofecht <[email protected]>
@cursor
cursor Bot force-pushed the fix-729-complex-scaling-14909106430004217066 branch from d48b69b to 0a5a1ff Compare October 3, 2026 21:49
godofecht and others added 5 commits October 4, 2026 00:03
…ng-14909106430004217066

# Conflicts:
#	compiler/bootstrap/flowc_stage_a

Co-authored-by: Abhishek Shivakumar <[email protected]>
The merge auto-resolved flowc_stage_a.c to the self-reproducing fixed
point (bootstrap_from_c.sh --verify passes). Rebuild the checked-in
binary from that C so it matches the merged compiler/src.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht merged commit 6039477 into main Oct 4, 2026
37 checks passed
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Main tip is now c9ee445 (#1278, #1297). Resolve the bootstrap
binary conflict by taking main's compiler/bootstrap/flowc_stage_a
and flowc_stage_a.c. The generated C will be rewritten next with
bootstrap_from_c.sh --regen so it matches compiler/src after the
merge. AST cache sources in resolve.flow and tests were kept.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Bring the branch onto c9ee445 so #1278 codegen taxes and #1297 vgpu
conformance land under the math-intrinsic *f selection. Keep main's
bootstrap C here; regenerate after the merged compiler/src is settled.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Checked-in Stage-A C must match compiler/src. The previous #1317
bootstrap predates main at c9ee445 (#1278 codegen taxes, #1297 vgpu)
and the imported-signature *f selection. Fixed point at gen2.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
The checked-in flowc_stage_a.c must stay a fixed point of
compiler/src. After merging c9ee445 (#1278, #1297), --regen
reached a fixed point at gen2 so the self-host roundtrip drift
is gone. AST cache sources were unchanged.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
…050446778278

Bring in #1278 (complex scaling / bounds elision) and #1297 (vgpu
conformance runner). Keep FLOWC_PERF_REPORT remarks in cgen.flow.
Bootstrap C is left at the previous fixed point and regenerated next.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
The merge of #1278/#1297 left the previous fixed point. Self-emit
until gen1 and gen2 agree so flowc_stage_a.c matches compiler/src.

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Oct 4, 2026
* perf: implement incremental AST caching for flowc (#736)

Add caching to the frontend parser to speed up recompilations.
Cache files are safely persisted in `~/.cache/flow/ast/`, fully resolving
the trust boundary issue while remaining functionally deterministic with dependency invalidation built-in.

Co-authored-by: godofecht <[email protected]>

* fix: add version and denotation module dependencies for AST caching

The incremental AST caching feature in resolve.flow imports from:
- denotation (for flowc_den_hash_bytes and flowc_den_mix)
- version (for FLOWC_VERSION_MAJOR/MINOR/PATCH)

These modules were missing from the frontend build and self-emit plans,
causing linker errors in roundtrip and self-host tests. Added them to:
- frontend_modules() in gate_lib.flow
- driver_headers() in gate_lib.flow
- self_emit_plan() in gate_lib.flow
- compile_module() dependencies in roundtrip.flow

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

* chore: remove agent scratch artifact

* chore(cache): remove unused platform-dependent mtime stub

* fix: regenerate bootstrap C after merge with main

The checked-in flowc_stage_a.c must stay a fixed point of
compiler/src. After merging main, --regen reached a fixed point
at gen2 so the self-host roundtrip drift is gone.

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

* fix: regenerate bootstrap C after merge with main

The checked-in flowc_stage_a.c must stay a fixed point of
compiler/src. After merging c9ee445 (#1278, #1297), --regen
reached a fixed point at gen2 so the self-host roundtrip drift
is gone. AST cache sources were unchanged.

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

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: godofecht <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
godofecht added a commit that referenced this pull request Oct 4, 2026
* feat: Add compiler performance remarks explaining missed optimizations in idiomatic Flow

Adds `FLOWC_PERF_REPORT` opt-in compiler mode which surfaces optimization decisions directly inside the emitted C code as `/* perf[...] ... */` comments.

This covers three codegen areas:
- `span-bounds-check-retained`: an index into a span keeps its bounds check if the index isn't structurally proven via an ascending loop bound.
- `f32-math-widened-to-f64`: an f32 argument to a standard libm function causes the argument to be widened to double precision.
- `loop-not-vectorized`: a for loop with a step stays scalar because its body has a function call or control flow (is not "simple").

Testing:
- `tests/cgen/perf_remarks.flow` with an `.expect` that invokes `FLOWC_PERF_REPORT=1` validates the output.

Co-authored-by: godofecht <[email protected]>

* fix(perf): stop reporting resolved f32 widening

* test(perf): drop obsolete f32 widening remark

* test(perf): remove obsolete widening fixture

* fix: regenerate bootstrap C after perf remarks merge

The main merge left a stale f32-math-widened-to-f64 remark in
flowc_stage_a.c that compiler/src no longer emits. Regen to the
fixed point so the self-host roundtrip matches compiler/src.

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

* fix: regenerate bootstrap C after merging main into perf remarks

The merge of #1278/#1297 left the previous fixed point. Self-emit
until gen1 and gen2 agree so flowc_stage_a.c matches compiler/src.

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

* fix: rebuild bootstrap binary after merging incremental AST cache

The auto-merged flowc_stage_a.c was already the gen2 fixed point
(perf remarks plus #1302 parse cache). Rebuild the checked-in
binary so it matches that C.

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

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: godofecht <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
godofecht added a commit that referenced this pull request Oct 4, 2026
…1317)

* Fixes #734: Audit precision promotion and math intrinsic selection across f32/f64/c64/c128

This commit ensures that single-precision and complex math functions correctly invoke their precision-accurate intrinsic equivalents (e.g., `sqrtf` and `cexpf`) rather than silently promoting arguments to double precision or double precision complex numbers in C output.

- `cgen.flow`: Added checks inside `flowc_cgen_emit_expr_inner` for `f32` and `c64` types. Re-routes functions like `sqrt`, `sin`, `fabs`, and `cabs` to their `*f` counterparts.
- `mlirgen.flow`: Handled the mappings in `mlg_libc_sig` to emit `MT_F32` and `MT_C64` results for these functions on specific inputs.
- Verified test semantics with `NaN` and `Inf` using the new `test_f32_c64_math_semantics.flow` language tests.
- Recreated generated `flowc_stage_a.c` to reach the fixed point and integrated performance microbenchmarks (`math_loop.flow`).

Co-authored-by: godofecht <[email protected]>

* test: cover f32 libm lowering

* test: add f32 math intrinsic regression

* test: add f32 math intrinsic expectations

* fix: select *f math from callee f32/c64 signature

Expression types only recover idents, so fabs(-x) and exp(0.1) never
selected the single-precision libm names. stdlib math is declared f32,
so read the callee parameter type as well. That is the self-host cgen
golden failure on #1317.

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

* fix: pick *f math from imported stdlib float signatures

Bundle emit keeps each module in its own arena, so fn_id never sees
stdlib/math.flow. Read the cross-module sig table (float / c64) and
walk unary minus on the first argument. That is what the #1317 cgen
goldens actually compile.

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

* fix: regenerate bootstrap C after math intrinsic merge

Checked-in Stage-A C must match compiler/src. The previous #1317
bootstrap predates main at 29f4d86 and the imported-signature *f
selection. Fixed point at gen2.

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

* fix: regenerate bootstrap C after math intrinsic merge

Checked-in Stage-A C must match compiler/src. The previous #1317
bootstrap predates main at c9ee445 (#1278 codegen taxes, #1297 vgpu)
and the imported-signature *f selection. Fixed point at gen2.

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

* fix: regenerate bootstrap C after math intrinsic merge

Reach a fixed point after merging incremental AST caching (#1302)
onto the f32/c64 math-intrinsic cgen changes.

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

* fix: regenerate bootstrap C after math intrinsic merge

Reach a fixed point after merging compiler performance remarks (#1309)
onto the f32/c64 math-intrinsic *f selection. Fixed point at gen2.

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

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: godofecht <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
@godofecht
godofecht deleted the fix-729-complex-scaling-14909106430004217066 branch October 12, 2026 23:02
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.

2 participants