Skip to content

Compile ? so that a Result stays in registers at -O3 - #4485

Merged
hiraditya merged 4 commits into
vx-lang:mainfrom
minzyfox:result-try-inlining
Dec 4, 2026
Merged

hiraditya merged 4 commits into
vx-lang:mainfrom
minzyfox:result-try-inlining

Conversation

@minzyfox

@minzyfox minzyfox commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

At -O3, a function that returns a Result and uses ? kept stack slots for its Results. The extra code made it too large for LLVM to inline. #4484 has the details.

Changes

  1. flatten: a match arm that takes the last remaining variant tests no tag. Before, each match that gives a value also had a path that aborts with "no match arm produced a value". Each ? is such a match, and no input reaches that path.
  2. MLIR: four passes no longer merge blocks in functions where they have no work. MLIR's greedy rewrite driver merges blocks that differ only in their operands. Two ? exits then became one block that takes a stack slot as an argument, and LLVM's SROA cannot remove such a slot.
    • vx-linalg-to-affine-loops asks for Normal region simplification.
    • expand-strided-metadata is replaced by vx-expand-strided-metadata, which has the same patterns and Normal simplification.
    • convert-linalg-to-loops and convert-vector-to-llvm run through a new pass, vx-on-functions-using. It runs them only on functions with a linalg or a vector op. It folds the other functions with no patterns, as the greedy driver did before, without the block merge.

Speed

The benchmark skips 7,096,318 bytes of Thrift compact-protocol data, the footer of a wide Parquet file. The "sticky" reader records the first error in a field. Readers A and B return a Result from every read. Each number is the median of 20 skips in one run. The runs alternate between the two compilers. Each cell gives the median run, then the fastest and slowest run.

Release builds of both compilers (cargo build --release), 5 runs of each:

Skip Before (c764af2) After (this PR, baef112) Rust 1.94.0, 3 runs
sticky 14.29 ms (14.24–14.34) 14.10 ms (14.05–14.29) 13.14 ms (13.11–13.15)
Result on every read (A) 19.97 ms (19.86–19.99) 15.42 ms (15.39–15.53) 14.75 ms (14.60–14.76)
Result over the _status byte reads (B) 18.68 ms (18.66–18.82) 15.73 ms (15.61–15.89)

Vx is still slower than Rust: 0.7 ms (5%) for A and 1.0 ms (7%) for the sticky reader.

The earlier run used compilers whose build type was not recorded. It agrees with the release builds within 0.1 ms:

Skip Before (c764af2) After (this PR, before baef112) Rust 1.94.0
sticky 14.30 ms (14.26–14.38) 14.03 ms (13.98–14.22) 13.16 ms (13.16–13.16)
Result on every read (A) 19.96 ms (19.87–20.06) 15.37 ms (15.31–15.52) 14.71 ms (14.67–14.76)
Result over the _status byte reads (B) 18.75 ms (18.69–18.87) 15.60 ms (15.58–15.69)

Effect of each change, from the earlier run. Change 1 alone was measured in 3 runs alternating with the old compiler:

Build A B
Before 19.88–19.90 ms 18.66–18.76 ms
Change 1 19.44–19.53 ms 18.42–18.51 ms
Changes 1 and 2 15.31–15.52 ms 15.58–15.69 ms

Before each timing, the three readers must fail the same way on 4,168 inputs cut short and 11,607 inputs with one byte changed. They did with every build.

Setup

Shared by both sides
Machine Apple M2 Pro, 16 GB, macOS 27.2.1
Input a Parquet footer of 100 row groups x 1,000 INT32 columns, written by pyarrow 26.0.1
Different Vx Rust
Compiler vxc at c764af2 or this PR, with Homebrew LLVM 22.1.8 rustc 1.94.0
Build type of the compiler release (cargo build --release: thin LTO, one codegen unit). Not recorded in the earlier run. release toolchain
Optimization -O3 -C opt-level=3
Runs 5 for each Vx build 3
Release run Earlier run
Vinyl at 6788e99 Vinyl at 4385fbb
A virtual machine used about 2 cores during all runs (load average 4.5–5.2).

The benchmark program and the commands to run it are in #4484. This comment has the commands of the release run.

Limits: one program on one machine. During the release run, the machine was not idle. The JIT builds the program with -mcpu=native. No other benchmark was timed.

Compile time

vx-on-functions-using runs on one function at a time, where func.func(convert-linalg-to-loops) ran on several threads. This costs no measurable time. The whole PR adds about 25 ms (3%) to vxc -O3 -c benchmarks/cpu_fusion.vx, because fewer merged blocks give larger IR. This comment has the data.

Tests

  • New: a_question_mark_has_no_abort_for_a_missing_arm.vx and a_function_with_several_question_marks_keeps_its_result_in_registers.vx. Both fail with c764af2 and pass with this PR. I undid each change separately, and a check failed each time:
    • change 1: the first fixture.
    • vx-linalg-to-affine-loops, expand-strided-metadata, convert-linalg-to-loops and convert-vector-to-llvm, each by itself: the second fixture. Each one put back 18 stack slots.
  • Changed: file_read_and_write_make_one_runtime_call.vx expects one store fewer. The same merge gave File::read and File::write 10 stack slots and a store on their error path. Both are gone now.
  • Changed: a unit test in src/hir/flatten/tests.rs expects one tag test fewer.
  • cargo test --release: everything passes except 3 tests that need parts this machine does not have. ane_device_test needs coremltools. The two seam_uses_the_vx_solver tests need libvx_solver.dylib.

Fixes #4484

🤖 Generated with Claude Code

minzyfox and others added 2 commits December 2, 2026 13:41
A `match` on an enum compared the tag with each arm's variant in turn.
After the last arm, it added a path that aborts with "no match arm
produced a value". The tag of a value is always one of its variants, so
an arm that takes the only variant left can be the default.

Each `x?` is such a match, so every `?` had a dead compare and an abort
path. They made `Result`-returning functions larger, which kept LLVM
from inlining them. Removing them made a Thrift skip with a `Result` on
every read about 0.4 ms faster (19.9 ms to 19.5 ms).

An arm counts as taking its variant only when each payload pattern is a
name or `_`.

Issue: vx-lang#4484

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MLIR's greedy rewrite driver merges blocks that differ only in their
operands, and makes the operands block arguments. Each `?` ends in a
block that copies the error through its own stack slot, so a merge made
one block that took the slot as an argument. LLVM cannot keep such a
slot in registers. A `Result`-returning function then kept many slots,
and stored the error even on calls that returned `Ok`. It also became
too large for LLVM to inline.

Four passes ran the driver with block merging on every function:
- `vx-linalg-to-affine-loops` now asks for `Normal` simplification.
- `expand-strided-metadata` is replaced by `vx-expand-strided-metadata`,
  with the same patterns and `Normal` simplification.
- `convert-linalg-to-loops` and `convert-vector-to-llvm` run through a new
  pass, `vx-on-functions-using`. It runs them only on functions with a
  linalg or a vector op. It folds the other functions with no patterns,
  as the driver did, but does not merge their blocks.

A Thrift skip with a `Result` on every read now takes 15.3 ms, from
19.5 ms. Rust 1.94 takes 14.7 ms for the same code. `File::read` and
`File::write` lost 10 stack slots and one store on their error path,
so their fixture expects one store fewer.

Fixes: vx-lang#4484

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the careful write-up. I built 54364ea and ran the three fixtures, and they pass. The compiler changes look correct to me:

  • With a vxc that does not have this PR, Reader$header in the second fixture has 36 allocas after opt. With this PR it has none.
  • In the MLIR of the first fixture, add_one and value_or_error now have one tag test each and no abort. Before, each had two tag tests and an abort.
  • E3050 refuses a payload pattern other than a name or _. So an arm that takes the last variant cannot skip a test that its pattern needs. Repeated arms, such as Ok(a), Ok(b), Err(e), also lower correctly.
  • No later pass in the lowering pipeline runs the greedy driver at the default level, so the new comment in driver.rs holds.

One fixture needs a change before this merges. See the comment on a_question_mark_has_no_abort_for_a_missing_arm.vx. The other two inline comments are small.

Two smaller notes:

  • Before, convert-linalg-to-loops ran inside func.func(...), and MLIR runs such a pipeline on functions in parallel. vx-on-functions-using is a module pass, so it runs on one function at a time. The effect is probably small, but the PR does not measure compile time.
  • The PR description does not say if vxc was a release build. Section 3 of docs/benchmarking_policy.md asks for the build type.

A possible follow-up, not for this PR: the speedup depends on four passes that do not merge blocks. A new lowering pass that merges blocks would bring the stack slots back. The second fixture would catch that. The cause is that each return Result::Err(e) builds its Result in its own stack slot. The flat path could build the returned value as an SSA value, or use one return slot for each function. Then a merge would do no harm. That could be an issue of its own.

Comment on lines +13 to +15
// RUN: vxc %s --emit-mlir | FileCheck %s # flat-codegen
// RUN: vxc %s --run | FileCheck %s --check-prefix=OUT
// CHECK-NOT: no match arm produced a value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check does not see the abort in add_one, the function with the ?.

A CHECK-NOT before the first CHECK-LABEL checks only the text above that label. The abort is an op inside each function (cf.assert %false, "no match arm produced a value"). vxc prints the functions in the order main, value_or_error, add_one, half. So this line checks only main and value_or_error.

To test this, I took the output of a vxc without this PR and deleted only the abort in value_or_error. The fixture passed, with the abort still in add_one. When you undid change 1, the fixture failed because value_or_error got its abort back too.

This change checks the whole output. With it, the fixture passes on the output of this PR. It fails on the old output, and on output where only add_one keeps its abort.

Suggested change
// RUN: vxc %s --emit-mlir | FileCheck %s # flat-codegen
// RUN: vxc %s --run | FileCheck %s --check-prefix=OUT
// CHECK-NOT: no match arm produced a value
// RUN: vxc %s --emit-mlir | FileCheck %s --implicit-check-not="no match arm produced a value" # flat-codegen
// RUN: vxc %s --run | FileCheck %s --check-prefix=OUT

for (FunctionOpInterface fn : users)
if (failed(runPipeline(nested, fn)))
return signalPassFailure();
// The upstream pass would also fold and remove dead ops in the other

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This holds for convert-vector-to-llvm, but not fully for convert-linalg-to-loops. In LLVM 22.1.4, lowerLinalgToLoopsImpl in mlir/lib/Dialect/Linalg/Transforms/Loops.cpp also applies the canonicalization patterns of memref.dim, tensor.dim and affine.apply, and FoldAffineOp, to every function. Those patterns no longer run in a function with no linalg op.

This is probably harmless, because lower-affine runs next and CI passed. Please make the comment say so, or apply those patterns to the other functions too.

// in registers. But MLIR passes merged blocks that differ only in their slot: one block then
// took the slot as an argument, and LLVM had to keep every slot in memory. At -O3 `header`
// had stack slots, and it stored the error on each call that returned `Ok`.
// `opt` needs a target: with none, an `i64` in the error has 4-byte alignment, which no real

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: 32-bit x86 aligns an i64 in a struct to 4 bytes, so "no real target" is too strong. "which 64-bit targets do not use" is accurate.

Review follow-up for PR vx-lang#4485.

The fixture a_question_mark_has_no_abort_for_a_missing_arm.vx had a
CHECK-NOT before its first CHECK-LABEL. Such a line checks only the text
above that label, so it did not see an abort in `add_one`. The fixture
now uses --implicit-check-not, which checks the whole output. I put an
abort only in `add_one` and the fixture failed, as it should.

Two comments are now more exact:
- vx-on-functions-using: `convert-linalg-to-loops` also applies the
  canonicalization patterns of memref.dim, tensor.dim and affine.apply.
  Those do not run in a function with no linalg op.
- A 4-byte aligned i64 is a layout that 64-bit targets do not use.
  32-bit x86 uses it, so "no real target" was wrong.

Issue: vx-lang#4484

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@minzyfox

minzyfox commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. baef112 addresses the three inline comments:

  • a_question_mark_has_no_abort_for_a_missing_arm.vx now uses --implicit-check-not, as you suggested. I put the abort only into add_one in the output, and the fixture failed at that line.
  • The comment on vx-on-functions-using now says that convert-linalg-to-loops also canonicalizes memref.dim, tensor.dim and affine.apply, and that those patterns do not run in the other functions.
  • The alignment comment now says "which 64-bit targets do not use".

I measured the two smaller notes. The follow-up is #4570.

1. Compile time

Summary: vx-on-functions-using costs no measurable compile time. The whole PR adds about 25 ms (3%) to cpu_fusion.vx at -O3. The other programs show no change that I can measure.

The passes that this PR changes

I gave the -O0 --emit-mlir output of each compiler to its own vx-opt, with the same -O3 lowering pipeline that its vxc builds, and --mlir-timing. The table gives wall time: the median, then the fastest and slowest run.

Program Runs per compiler The changed passes, before The changed passes, this PR All passes except ConvertVxToLLVMPass, before The same, this PR
Vinyl thrift_result_skip.vx 4 250 ms (247–258) 269 ms (268–270) 1.797 s (1.781–1.893) 1.825 s (1.783–1.845)
benchmarks/sat_solver/solve.vx 6 303 ms (298–312) 256 ms (255–258) 1.719 s (1.667–1.736) 1.676 s (1.632–1.705)
benchmarks/cpu_fusion.vx 6 41 ms (40–41) 39 ms (38–39) 0.272 s (0.271–0.276) 0.274 s (0.272–0.277)
benchmarks/benchmark_simd.vx 6 11 ms (11–12) 10 ms (10–10) 0.080 s (0.079–0.105) 0.079 s (0.078–0.107)

"The changed passes" are convert-linalg-to-loops, expand-strided-metadata and convert-vector-to-llvm before, and the two vx-on-functions-using runs and vx-expand-strided-metadata after.

You were right about the parallel run. On Vinyl, convert-linalg-to-loops took 31 ms of wall time and 289 ms of CPU time before, on several threads. It took 77 ms in vx-on-functions-using, on one thread. But convert-vector-to-llvm became faster (144 ms to 118 ms). So the sum grew by 19 ms on Vinyl and fell by 47 ms on the SAT solver.

I left ConvertVxToLLVMPass out of the last two columns. It takes about 80% of the pass time on Vinyl and on the SAT solver, and it varies by 2 s from run to run with both compilers (6.2–8.5 s on Vinyl). This PR does not change it.

The whole compile

vxc -O3 -c, wall time, with the order of the compilers changed in each round:

Program Runs Before This PR This PR without vx-on-functions-using
cpu_fusion.vx 9 0.827 s (0.806–1.071) 0.852 s (0.848–0.860) 0.858 s (0.842–0.861)
benchmark_simd.vx 10 0.527 s (0.392–0.820) 0.411 s (0.386–0.701)
sat_solver/solve.vx 10 13.71 s (11.79–15.55) 14.26 s (12.54–16.08)
Vinyl thrift_result_skip.vx 10 12.00 s (9.91–12.56) 13.22 s (12.37–13.45)

"Without vx-on-functions-using" is this PR with func.func(convert-linalg-to-loops) and convert-vector-to-llvm put back. It has the same compile time as this PR. So the 25 ms on cpu_fusion comes from the other changes. With fewer merged blocks, the IR is larger: --emit-llvm prints 5,825 lines against 4,980.

Do not trust the Vinyl and SAT solver rows. Their spread is larger than the difference, because of ConvertVxToLLVMPass. A virtual machine also used about 2 cores during the runs. In a later run of -O3 --emit-llvm on Vinyl, the base took 10.26–11.98 s and this PR took 9.13–11.53 s. The pass table above is the better measure for these two programs. LLVM does not take longer on Vinyl with this PR: opt -O3 took 0.28 s with both, and llc -O3 took 0.24 s against 0.28 s. The object is 96,984 bytes against 112,160.

2. Build type, and the speed again with release builds

The PR description did not say how vxc was built. I measured again with release builds of both compilers (cargo build --release: thin LTO, one codegen unit). Each cell is the median of 5 runs, then the fastest and slowest. Each run is itself the median of 20 skips. The runs alternate between the two compilers.

Skip Before (c764af2) This PR (baef112) Rust 1.94.0, 3 runs
sticky 14.29 ms (14.24–14.34) 14.10 ms (14.05–14.29) 13.14 ms (13.11–13.15)
Result on every read (A) 19.97 ms (19.86–19.99) 15.42 ms (15.39–15.53) 14.75 ms (14.60–14.76)
Result over the _status byte reads (B) 18.68 ms (18.66–18.82) 15.73 ms (15.61–15.89)

These numbers agree with the PR description within 0.1 ms. Vx is still slower than Rust: 0.7 ms (5%) for A and 1.0 ms (7%) for the sticky reader. Every run reported "agree on 4168 cuts and 11607 mutations".

Setup: Apple M2 Pro, 16 GB, macOS 27.2.1, Homebrew LLVM 22.1.8, rustc 1.94.0. Vinyl at 6788e99. The PR description used Vinyl at 4385fbb. The input is a Parquet footer of 7,096,318 bytes, written by scripts/generate-wide-footer.py with pyarrow 26.0.1. A virtual machine used about 2 cores during all runs (load average 4.5–5.2).

Limits: one machine. The machine was not idle. The compile-time programs are four files: two have tensor code, two have none.

Commands
# Release builds: this PR in the main checkout, c764af26 in a worktree with its own target/
cargo build --release -j 4

# Speed, from the Vinyl checkout
vxc -O3 --package vinyl=src --run benchmarks/thrift_result_skip.vx -- wide.parquet
rustc --edition 2021 -C opt-level=3 benchmarks/thrift_result_skip.rs && ./thrift_result_skip wide.parquet

# Compile time of the whole compile
vxc -O3 -c <file> -o <file>.o

# Pass times: each compiler's unoptimized MLIR, through its own vx-opt
vxc -O0 --emit-mlir <file> > in.mlir
vx-opt "--pass-pipeline=<the -O3 pipeline of get_optimization_pipeline, without bounds checks>" --mlir-timing in.mlir -o out.mlir

@minzyfox
minzyfox requested a review from hiraditya December 4, 2026 00:43

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick update and the measurements. Approving.

What I checked on baef112:

  • The new commit makes the three changes from my inline comments, and nothing else.
  • I ran the new RUN line of a_question_mark_has_no_abort_for_a_missing_arm.vx against three outputs from my first review:
    • It passes on the output of this PR.
    • It fails on the output of main, which has 2 aborts.
    • It fails on an output with the abort only in add_one.
  • The new comment on vx-on-functions-using is accurate.
  • #4570 describes the follow-up well.
  • The compile-time and release-build numbers answer both of my notes. The differences in the text match the tables.

CI: both test jobs fail only on the four compile_time_of_ifs_in_a_row tests. That is #4567, which is on main: #4568 and #4565 fail on the same four tests. compile_test::test_optimizations passes in both jobs, so both new fixtures pass on the merge with main. This PR can merge when #4567 is fixed and CI passes.

nit: the Setup table in the PR description still says "build type not recorded". Section 3 of docs/benchmarking_policy.md asks for the build type. Please put the release-build table in the PR description, and keep the first table as the earlier run.

@minzyfox

minzyfox commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I updated the PR description:

  • The Speed section now shows the release-build table first. The Setup table says that both compilers were release builds (cargo build --release: thin LTO, one codegen unit).
  • The first table stays, as the earlier run. It says that the build type of that run was not recorded.
  • The Setup also gives the Vinyl commit of each run, and says that the machine was not idle during the release run.

@hiraditya
hiraditya merged commit 18711ea into vx-lang:main Dec 4, 2026
7 checks passed
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.

A function that returns a Result keeps stack slots at -O3, so a Result per read is 1.4x slower than in Rust

2 participants