Skip to content

Fixes #734: Fix implicit widening of f32 and c64 in math intrinsics - #1317

Merged
godofecht merged 16 commits into
mainfrom
fix-734-math-intrinsics-1768170846667842294
Oct 4, 2026
Merged

godofecht merged 16 commits into
mainfrom
fix-734-math-intrinsics-1768170846667842294

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Fixes #734 by preventing f32 and c64 values from implicitly widening to f64/c128 during standard library math calls. Instead, the backend intercepts these typed instances and outputs the precise generic functions to the compiled C (sqrtf, cabsf, cosf).

This includes changes in cgen.flow to handle routing based on parameter typing and modifying mlg_libc_sig in mlirgen.flow. Verified against a comprehensive set of functions: sqrt, pow, abs, cos, atan2, csqrt, cexp etc. Included semantics tests and benchmark loops.


PR created automatically by Jules for task 1768170846667842294 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.

…ross 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]>
@cursor
cursor Bot force-pushed the fix-734-math-intrinsics-1768170846667842294 branch from bae8f21 to 3951432 Compare October 3, 2026 21:46
cursoragent and others added 11 commits October 4, 2026 12:05
Bring the branch onto 29f4d86 so self-host picks up the #1277 ffi
package goldens and #1291 MLIR corpus digests already refreshed on main.
Keep main's bootstrap C here; regenerate after the math-intrinsic source
is settled.

Co-authored-by: Abhishek Shivakumar <[email protected]>
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]>
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]>
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]>
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]>
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]>
…cs-1768170846667842294

# Conflicts:
#	compiler/bootstrap/flowc_stage_a

Co-authored-by: Abhishek Shivakumar <[email protected]>
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]>
Bring the branch onto 64db9fb so #1309 compiler performance remarks
land under the math-intrinsic *f selection. cgen.flow auto-merged both
sides. Keep main's bootstrap C here; regenerate after compiler/src is
settled.

Co-authored-by: Abhishek Shivakumar <[email protected]>
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]>
@godofecht
godofecht merged commit 9a63eb5 into main Oct 4, 2026
37 checks passed
@godofecht
godofecht deleted the fix-734-math-intrinsics-1768170846667842294 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.

[Performance #727] Audit precision promotion and math intrinsic selection across f32/f64/c64/c128

2 participants