Skip to content

codegen: leave out the cancel calls around a drop that cannot reach the runtime - #4626

Open
hiraditya wants to merge 1 commit into
mainfrom
codegen-drop-scopes-only-where-a-wait-can-happen
Open

hiraditya wants to merge 1 commit into
mainfrom
codegen-drop-scopes-only-where-a-wait-can-happen

Conversation

@hiraditya

Copy link
Copy Markdown
Collaborator

Part of #4583.

What changes

vxc puts vx_cancel_off before each call of a Drop impl's drop, and vx_cancel_on after it. Between them, cancellation is off and the drop's deadline holds (F6 and F6a in docs/discussions/waiting_tasks_channels_and_task_errors.md). Only the runtime reads that state: in a wait, cancelled(), within, a tasks block, a task and the failure of a task. The drop of a Vec, a String or a BigInt only frees memory, so it cannot see that state. The solver written in Vx runs such drops in each step, and the two calls took more time than the drops.

  • A new MLIR pass, vx-remove-unseen-drop-scopes (src/dialect/DropScopes.cpp). It reads the whole module and finds each function that can reach the runtime. Such a function has one of these in its body:

    • a call of a function with no body in the module. malloc, calloc, realloc, aligned_alloc, free and vx_string_drop do not count, because they only allocate or free memory.
    • a call through a function value
    • inline assembly
    • an op of a dialect that the pass does not know, such as vx or gpu
    • a call of another function that can reach the runtime

    The pass then removes vx_cancel_off and the vx_cancel_on after it, in one block, when no op between them can reach the runtime. A pair in two blocks stays.

  • Where it runs. After convert-vx-to-standard, which turns the heap ops into calls of malloc and free. Before inlining, while each drop is still a call between the two. At each -O level, in both pipelines (src/driver.rs and src/codegen/mod.rs).

  • The rule. This is an analysis of the call graph that only changes the generated code, as W3 of the same document does. The drops that it changes cannot see the calls, so each drop behaves as before. A paragraph in F6a says so. The call of wait_limit stays, because it can print or have another effect. At -O2, LLVM removes it when it only returns a number.

In the runner of the solver below, the calls of vx_cancel_off went from 1233 to 4. The 4 that stay are in File::open and read_file. Each is around a drop that calls the runtime to close a file, unmap a file or free a C string.

Measurements

The solver written in Vx, on the same 340 queries as #4598:

A program built from run_corpus.vx runs them all, one after the other. I built it twice with vxc -O2: with the vxc of main and with the vxc of this PR. I linked each with two runtimes built with cargo build --release: the runtime of main, and the runtime of #4598. The answers are the same in each run.

Shared by all runs
Machine Apple M4, 32 GB, macOS 27.0.1, caffeinate -dims
Solver std::algo::smt of main at 414bb29
Tools rustc 1.95.0, clang 22.1.4
Runs 5 rounds. Each round runs each program once, in a rotated order, with 2 s between runs.

Total time of each group, in ms: the median of 5 runs, with the fastest and the slowest run.

vxc Runtime 4 if-chains 45 linear 291 nonlinear All 340
main main 827 (823, 838) 21.1 (21.1, 21.7) 3037 (3021, 3085) 3887 (3870, 3944)
main #4598 462 (452, 463) 12.0 (11.6, 12.1) 1683 (1660, 1713) 2157 (2125, 2189)
This PR main 322 (311, 331) 8.5 (8.2, 8.8) 1177 (1175, 1194) 1506 (1497, 1533)
This PR #4598 320 (317, 330) 8.6 (8.3, 8.8) 1182 (1177, 1214) 1510 (1504, 1553)

With this PR, the runtime of #4598 makes no difference to the solver, because its drops no longer make the two calls. #4598 still makes each drop that keeps the calls cheaper.

Each of the 340 queries is faster with this PR. Per query, main takes this long, in times the time with this PR and the runtime of #4598:

Group Geometric mean Smallest Largest
4 if-chains 2.56 2.44 2.62
45 linear 1.98 1.33 3.00
291 nonlinear 2.38 2.10 2.67

The slowest nonlinear query took 1408 ms with main, and takes 550 ms with this PR. Neither this solver nor z3 finds an answer to it. The next 4 are from llama2.vx. Each took about 205 ms with main, and takes about 79 ms with this PR.

Limits

  • One machine, macOS only.
  • The first round started a minute after other work on the machine stopped. The load average over 1 minute was 2.5 at the start and 2.7 at the end. The rotated order gives each program the same share of that.
  • Only code in the module counts. A call of a function that another object defines keeps the calls, so a drop in a package compiled on its own keeps them.

Commands

vxc -O2 run_corpus.vx --action emit-obj -o run_corpus.o
clang run_corpus.o -o run -Wl,-rpath,<runtime>/target/release <llvm>/libmlir_c_runner_utils.dylib <llvm>/libmlir_runner_utils.dylib <runtime>/target/release/libvx_std_core.dylib
VX_SMT_LIST=queries.txt ./run
llvm-objdump -r run_corpus.o | grep -c '_vx_cancel_off$'

Tests

  • tests/optimizations/pass/a_drop_keeps_its_cancel_calls_only_when_it_can_reach_the_runtime.mlr runs the pass with vx-opt on one pair around each kind of code.
  • tests/frontend/pass/a_drop_that_cannot_wait_needs_no_cancel_calls.vx runs it on the MLIR of a program. The drop of a Vec<u64> and the drop of a String lose the calls. The drops that sleep, drop a value that sleeps, call cancelled(), call a function value or start a task keep them.

I broke the pass in 9 ways, one at a time, and each made a test fail:

Break Case of the .mlr test that failed Other tests that failed
A caller of a function that can reach the runtime does not reach it a call of a function that sleeps the .vx test, at the drop of a Nap
A call through a function value counts as local the call of a function value the .vx test, at the drop of a Calls
An op of a dialect that the pass does not know counts as local the vx op
Inline assembly counts as local the inline assembly
malloc and free reach the runtime the field that only frees memory the .vx test, at the drop of a Vec<u64>
vx_string_drop reaches the runtime the drop of a String the .vx test, at the drop of a String
vx_cancel_off and vx_cancel_on reach the runtime a drop that drops such a field
The ops in the regions of an op between the calls do not count the sleep in an scf.if
The pass removes each pair a drop that sleeps the .vx test, and 3 tests of drops that wait in tests/backend/pass/

…he runtime

vxc puts vx_cancel_off before each call of a Drop impl's drop, and
vx_cancel_on after it. Only the runtime reads the state that they set:
in a wait, cancelled(), within, a tasks block, a task and the failure
of a task. The drop of a Vec, a String or a BigInt only frees memory,
so it cannot see that state. The solver written in Vx runs such drops
in each step, and the two calls took more time than the drops.

The new MLIR pass vx-remove-unseen-drop-scopes reads the whole module.
A function can reach the runtime when it calls a function with no body
in the module. The C functions of the heap and vx_string_drop, which
only free or allocate memory, are the exception. A call through a
function value, inline assembly, and an op of a dialect that the pass
does not know count too. Each caller of such a function can reach the
runtime as well. The pass then removes each pair of the two calls in
one block when no op between them can reach the runtime. It runs after
convert-vx-to-standard and before inlining, at each -O level.

This is an analysis of the call graph that only changes the generated
code, as W3 of waiting_tasks_channels_and_task_errors.md does. Each
drop gets the same deadline and the same cancel state as before.

Part of #4583

This branch has not been deployed

No deployments
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