Repository navigation
Compile ? so that a Result stays in registers at -O3 - #4485
Conversation
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
left a comment
There was a problem hiding this comment.
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
vxcthat does not have this PR,Reader$headerin the second fixture has 36allocas afteropt. With this PR it has none. - In the MLIR of the first fixture,
add_oneandvalue_or_errornow 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 asOk(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.rsholds.
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-loopsran insidefunc.func(...), and MLIR runs such a pipeline on functions in parallel.vx-on-functions-usingis 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
vxcwas a release build. Section 3 ofdocs/benchmarking_policy.mdasks 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.
| // 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 |
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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>
|
Thanks for the review. baef112 addresses the three inline comments:
I measured the two smaller notes. The follow-up is #4570. 1. Compile timeSummary: The passes that this PR changesI gave the
"The changed passes" are You were right about the parallel run. On Vinyl, I left The whole compile
"Without Do not trust the Vinyl and SAT solver rows. Their spread is larger than the difference, because of 2. Build type, and the speed again with release buildsThe PR description did not say how
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 Limits: one machine. The machine was not idle. The compile-time programs are four files: two have tensor code, two have none. Commands |
hiraditya
left a comment
There was a problem hiding this comment.
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.vxagainst 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-usingis 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.
|
Thanks for the review. I updated the PR description:
|
At
-O3, a function that returns aResultand uses?kept stack slots for itsResults. The extra code made it too large for LLVM to inline. #4484 has the details.Changes
flatten: amatcharm that takes the last remaining variant tests no tag. Before, eachmatchthat gives a value also had a path that aborts with "no match arm produced a value". Each?is such amatch, and no input reaches that path.?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-loopsasks forNormalregion simplification.expand-strided-metadatais replaced byvx-expand-strided-metadata, which has the same patterns andNormalsimplification.convert-linalg-to-loopsandconvert-vector-to-llvmrun 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
Resultfrom 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:Resulton every read (A)Resultover the_statusbyte reads (B)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:
Resulton every read (A)Resultover the_statusbyte reads (B)Effect of each change, from the earlier run. Change 1 alone was measured in 3 runs alternating with the old compiler:
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
cargo build --release: thin LTO, one codegen unit). Not recorded in the earlier run.-O3-C opt-level=3The 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-usingruns on one function at a time, wherefunc.func(convert-linalg-to-loops)ran on several threads. This costs no measurable time. The whole PR adds about 25 ms (3%) tovxc -O3 -c benchmarks/cpu_fusion.vx, because fewer merged blocks give larger IR. This comment has the data.Tests
a_question_mark_has_no_abort_for_a_missing_arm.vxanda_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:vx-linalg-to-affine-loops,expand-strided-metadata,convert-linalg-to-loopsandconvert-vector-to-llvm, each by itself: the second fixture. Each one put back 18 stack slots.file_read_and_write_make_one_runtime_call.vxexpects one store fewer. The same merge gaveFile::readandFile::write10 stack slots and a store on their error path. Both are gone now.src/hir/flatten/tests.rsexpects one tag test fewer.cargo test --release: everything passes except 3 tests that need parts this machine does not have.ane_device_testneeds coremltools. The twoseam_uses_the_vx_solvertests needlibvx_solver.dylib.Fixes #4484
🤖 Generated with Claude Code