Skip to content

fix(mlir): lower c64 and c128 complex numbers - #1568

Closed
godofecht wants to merge 11 commits into
mainfrom
fix/mlir-complex
Closed

godofecht wants to merge 11 commits into
mainfrom
fix/mlir-complex

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Cause

The MLIR emitter had no lowering for c64 and c128 (C's float complex
and double complex), so it refused every program that names them with
"unknown type name", directly or through lib/stdlib/rf.flow (whose IQ
is c64) and lib/stdlib/complex_linalg.flow. 15 corpus programs stopped
there: tests/lang/test_complex_builtins.flow, test_complex_scaling,
test_print_complex_multi, test_field_named_capital_i,
test_hardware_cdc, tests/cgen/complex_types_arith.flow,
complex_real_scale, rf_types_signal, tests/stdlib/test_complex_linalg.flow,
examples/rf/* and others.

Fix

A complex value is !llvm.struct<(f32, f32)> or !llvm.struct<(f64, f64)>,
real part first, which is C's layout. The struct is synthesized under a
name a program cannot spell (#c64, #c128), so arrays, pointers,
struct fields, parameters and returns of complex type use the existing
struct paths. On top of that, with C's semantics:

  • c64(re, im), c64(x), c128(...); a complex argument converts.
  • + - * / between complex values, and against a real operand part by
    part (C's complex-by-real arithmetic); the result has the wider part
    type, so a f64 operand widens a c64. Unary minus, == and !=.
  • creal and cimag (double, as in C), crealf and cimagf, cabs
    (float for a c64, as cabsf), conj, cexp, each from its real
    formula with math.sqrt, math.exp, math.sin, math.cos.
  • Conversion between c64 and c128, and from a real number, in
    mlg_cast.
  • print / println write (re + im j), as the C backend's
    __flowc_pr_c64 does.

Regression fixture: compiler/fixtures/mlir/complex.flow. On main it fails
with "unknown type name". Its output equals the C backend's line for line.

Counts

mlir_vs_c --only on the 15 programs that stopped at a complex type, plus the new fixture:

same mlir-emit
main 7bbb4b3 0 15
this branch 11 (+ fixture) 4

The 4 left (examples/rf/dft.flow, examples/rf/iq_mixer.flow,
benchmarks/micro/codegen_taxes.flow, benchmarks/micro/complex_vectorization.flow)
declare array<c64, N> without an initializer; that lowering, and the
return of such an array, is the separate PR fix/mlir-aggregate-arrays.
With both applied all 15 build and dft and iq_mixer print what the C
backend prints.

Full corpus run (mlir_vs_c --jobs 8): queued behind the machine-wide corpus lock; the before and after counts will be added here.

Gates

  • selftest: flowc: PASS
  • bootstrap_from_c.sh --regen, then --verify: PASS
  • self_host_full, roundtrip: PASS
  • parity_mlir: PASS (25 fixtures, 2721 corpus programs)
  • tests/cgen: 152 passed, 0 failed
  • test-lang: all 309 passed
  • test-scripts: all pass except mlir_math_override, which fails in its
    C-backend half (flow run --backend=c exits 1). This change touches only
    the MLIR emitter, so the C path is the same as on main.

Regenerated state

  • compiler/bootstrap/flowc_stage_a.c and compiler/bootstrap/flowc_stage_a,
    in their own last commit.
  • compiler/fixtures/mlir/complex.mlir and .out, recorded with
    parity_mlir.flow --record.
  • compiler/fixtures/mlir/corpus.txt: 13 digests added, for the programs flowc now accepts (examples/hardware/blink.flow, examples/rf/sdr_receiver.flow, lib/stdlib/complex_linalg.flow, lib/stdlib/rf.flow, tests/cgen/complex_real_scale.flow, complex_types_arith.flow, rf_types_signal.flow, tests/lang/test_complex_builtins.flow, test_complex_scaling.flow, test_field_named_capital_i.flow, test_hardware_cdc.flow, test_print_complex_multi.flow, tests/stdlib/test_complex_linalg.flow). No existing digest moved. --record also wanted to add 12 programs this change does not affect (corpus drift on main); those lines are left out.

Risk

Moderate.

  • Complex division uses the textbook formula. Clang calls __divsc3 /
    __divdc3, which scale the operands by a power of two first; the
    results agree except near overflow or underflow.
  • cabs of a c128 is sqrt(re*re + im*im), where C calls hypot; they
    can differ in the last bit. For a c64 the sum is formed in double,
    which is exact.
  • clog, csqrt, cpow and carg are still refused.
  • The C backend lowers c128(z) of a complex z to (double)z, which
    drops the imaginary part; the MLIR backend converts both parts. No
    corpus program relies on either (examples/rf/dft.flow passes a value
    with a zero imaginary part).

Needs review by a CODEOWNER (touches compiler/).

The MLIR emitter had no lowering for c64 and c128 and refused every
program naming them. A complex value is now !llvm.struct<(f32, f32)> or
(f64, f64), C's layout, under a synthesized struct name, so arrays,
pointers, fields, parameters and returns use the struct paths. On top:
the constructors; + - * / with C's complex and complex-by-real rules and
the wider part type; unary minus, == and !=; creal, cimag, crealf,
cimagf, cabs, conj and cexp from their real formulas; conversion between
the widths and from a real number; and print as (re + im j).

Fixture: compiler/fixtures/mlir/complex.flow.

Copy link
Copy Markdown
Contributor Author

Merge-blocker audit (2026-11-06): complex c64/c128 MLIR lowering remains outstanding; this old branch conflicts with current main and includes a generated bootstrap plus 14+ corpus digests. Migrate complex type layout, arithmetic, casts, math functions and print support surgically onto current compiler/src/mlirgen.flow, retain the complex fixture, regenerate bootstrap and per-program corpus hashes from the new head, and verify exact-head MLIR-vs-C parity. Avoid wholesale rebasing old generated files that would overwrite newer compiler fixes.

Flow PR integration and others added 6 commits November 6, 2026 02:12
Reapply the complex-number emitter to current main, preserving current native bootstrap binary. Update generated C with conflict-aware identifier mapping for newer MLIR code; retain parity fixture and scoped corpus digests. Exact-head CI must validate generated goldens.
Regenerate scoped normalized-MLIR SHA-256 expectations for complex real scaling, complex arithmetic and language scaling. Retain all coverage; 33/33 fixture execution tests passed.

Copy link
Copy Markdown
Contributor Author

Superseded by merged #1684 (main commit 337599f). Verified against both PR patches: all 17 non-bootstrap-C changed paths have identical added lines, including compiler/src/mlirgen.flow, the complex MLIR fixtures, and every corpus hash. The regenerated compiler/bootstrap/flowc_stage_a.c differs in generated temporary identifiers, and #1684 additionally commits the regenerated Stage-A executable. #1684 passed full CI and Linux/macOS Tier-1 qualification before merge (runs 42288917080 and 42288917007). Closing this stale/conflicted duplicate rather than attempting to merge the same feature twice.

@godofecht godofecht closed this Nov 7, 2026
auto-merge was automatically disabled November 7, 2026 04:35

Pull request was closed

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