Repository navigation
tasks: copy each value that a task moves or copies at its spawn - #4356
Conversation
A task that ran in a loop could see the value of a later pass of the loop. Each local of a loop body has one stack slot for all the passes. The lowering of a task copied a slot into the task at the `spawn` only when the task loaded the whole value. For any other use, such as a read of one field, the task got the address of the slot. The next pass then wrote its own value into the slot before the task read it. So three kinds of program gave wrong values: - a task that moved a value with a channel end in it. Since vx-lang#3166, such a task starts with a call that reads a field of that value, for the deadlock report. - a task that moved one field of a struct, as in `take(h.id, h.rx)`. - a task that read a field of a `Copy` struct, with no channel at all. Two tasks could then own the same `Sender`, so the program dropped one channel end twice and never dropped another. Now the checker decides, not the shape of the code. The checker already knows whether a task moves, copies, writes or reads each variable from outside. It saves the names that the task moves or copies on the task body. The flat code generator starts the task with a new op, `vx.take_at_spawn`, for each one that is in memory. The lowering copies each marked slot into the task at the `spawn`, and every use in the task goes to a slot of the task's own. A reference is left out: the task copies the address, not what the reference points to. The lowering also checks the result. A task that nothing joins must not get the address of a slot that the loop around its `spawn` writes again in each pass. If it does, the compile stops with an error that says the compiler has a bug. Tests: - values_moved_into_tasks_in_a_loop_are_the_values_of_their_pass.vx: six cases, with each task runtime, two seeds and --legacy-codegen. Five cases gave wrong values or crashed before this change. The sixth checks that a task that writes a borrowed struct still writes the struct itself. - a_task_copies_a_slot_that_it_takes_at_its_spawn.mlr and a_task_that_reads_a_slot_a_loop_writes_again_is_refused.mlr check the lowering and its new error on hand-written MLIR. Part of: vx-lang#4345 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hiraditya
left a comment
There was a problem hiding this comment.
Thanks. The fix works on the cases I tried. Two things need a change before the merge.
1. The branch conflicts with main. A merge into current main (70cc938) stops with conflicts in src/bytecode.rs and src/hir/flatten.rs. Please merge main.
2. The CHECK-NOT in a_task_copies_a_slot_that_it_takes_at_its_spawn.mlr cannot fail. It comes after the check of the getelementptr in @vx_task_0, so FileCheck looks for the marker only after that line. A marker that the lowering forgot to erase is at the start of the task, or in @main. I added "vx.take_at_spawn"() : () -> () to the output of vx-opt in each place, and the test still passed both times. With --implicit-check-not=take_at_spawn on the RUN line, both fail, and the real output passes.
What I checked, on the branch as it is:
- The three new tests pass.
- A task that copies a struct that holds a raw pointer writes through the pointer, not into a copy. In
tasks { spawn { let moved = w; bump(moved.p); }; }, withw : SendPtr { p : *mut Counter }, the caller sees each write. This is true for a parameter and for a local of a loop, with both code generators. The output has twotake_at_spawnmarkers, so the new copy runs. A bare*mut Countercannot go into a task, because it is notSend.
Generated With Claude Code
Preserve main's vector opcodes and assign TaskTakes its own opcode, 90. Update opcode decoding to match. Keep the spawn register budget together with the markers for captures taken at spawn. The spawn capture runtime tests, vector operations, and spawn register budget tests passed after resolving the conflicts. Issue: vx-lang#4345
Use --implicit-check-not=take_at_spawn so FileCheck checks the whole output. Remove the scoped CHECK-NOT that missed markers before the last field access. The real lowered output passed. Markers inserted independently at the start of main and the task failed the intended assertion. The refusal test, all six runtime modes, and backend coverage also passed. Issue: vx-lang#4345
|
Addressed both requests. I merged The test now uses All three branch tests passed, including all six runtime modes. Backend coverage passed too. |
The fixture of the previous commit passed also without vx-lang#4356, the fix that makes each `spawn` copy the values that its task moves. It failed only when the `spawn` lost every copy of a value. The new case `receiver_moved` failed before vx-lang#4356. In each pass of a loop, a task moves a struct that holds the `Receiver` of its own channel, and the `drop` of the struct prints its id. Without vx-lang#4356, the task read the `Receiver` from the slot of the loop. Two tasks got the struct of the last pass, so `dropped 32` printed twice, `dropped 30` never printed, and one task received nothing. The 5 RUN lines with the default code generator then fail at `dropped 30`. The `--legacy-codegen` line passes, because vx-lang#4356 did not change the older code generator. Fixes: vx-lang#4543 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…4544) * tests: check that each value a task moves in a loop is dropped once #4356 made each `spawn` copy the values that its task moves or copies. Its test checked the value that each task sees, but not the drops. So a copy that dropped a value twice, or never, failed no test. The new fixture moves a `Drop` value into a task in each pass of a loop. A second loop moves one field of a struct and reads an id from another field. The drop of each value prints its id. The tasks end in any order, so the RUN lines sort the output, and one CHECK-NEXT line for each id makes sure that it prints exactly once. The fixture runs with both code generators, both task runtimes and two scheduling seeds. A `CHECK-DAG` group cannot do this. FileCheck does not apply `--implicit-check-not` between the lines that the group matches, so a second `dropped 1` passed. With the copy at the `spawn` turned off (and the compiler's guard against it), every task saw the last pass and `dropped 2` printed three times. Each RUN line that uses the default code generator then failed. Fixes: #4543 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * tests: add a task that moves a struct with a Receiver to the drop test The fixture of the previous commit passed also without #4356, the fix that makes each `spawn` copy the values that its task moves. It failed only when the `spawn` lost every copy of a value. The new case `receiver_moved` failed before #4356. In each pass of a loop, a task moves a struct that holds the `Receiver` of its own channel, and the `drop` of the struct prints its id. Without #4356, the task read the `Receiver` from the slot of the loop. Two tasks got the struct of the last pass, so `dropped 32` printed twice, `dropped 30` never printed, and one task received nothing. The 5 RUN lines with the default code generator then fail at `dropped 30`. The `--legacy-codegen` line passes, because #4356 did not change the older code generator. Fixes: #4543 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Issue: #4345
This PR is the compiler part of #4345. The ThreadSanitizer, GenMC, Lean and TLA+ work and the
doc updates of that issue come in later PRs.
The bug
A task that runs in a loop can see the value of a later pass of the loop. A loop body uses one
stack slot for each local in all its passes. The lowering of a task (
lowerTask) copied a slotinto the task at the
spawnonly when the task loaded the whole value. For any other use, forexample a read of one field, the task got the address of the slot. The next pass then wrote its
own value into the slot before the task read it.
On main, these programs give wrong values or crash:
A task moves a value that holds a channel end. Since tasks: the deadlock report knows the channel ends that a task moves #3166, such a task starts with a call that
reads a field of the value (
vx_task_holds_an_end), for the deadlock report.A task moves one field of a struct:
spawn { take(h.id, h.rx); }.A task reads a field of a
Copystruct, with no channel at all:When a task got another pass's
Sender, two tasks owned one channel end. So the program droppedone end twice and never dropped another.
The fix
The checker already decides how a task takes each variable from outside it: it moves, copies,
writes or reads the variable. The lowering did not use that decision, and it guessed from the
shape of the code. Now the checker's decision goes to the lowering:
src/hir/check/tasks.rs: the checker saves the names that the task moves or copies in a newfield,
UnsafeBlockExpr::takes. The checker leaves out a reference, because the task copies the addressand not what it points to.
rename_in_nodeindrops.rsrenames these names together withthe code, as it already does for the captures of a closure.
src/hir/flatten.rs: the region of the task starts with a new flat-HIR instruction,TaskTakes, for each of these names that has a slot.src/codegen/flat/emit/parallel.rs:TaskTakesbecomes a new op,vx.take_at_spawn %slot {value_type = T}(include/VxDialect.td), for a slot that is an!llvm.ptr: a struct, a pointer, or a scalar that the code takes the address of. A scalar in a rank-0memref is only loaded, so
copiedAtSpawncopies it as before.src/dialect/VxLowering.cpp:lowerTaskcopies each marked slot into the task's environmentat the
spawn. The task stores the value in a slot of its own, and each use in the task goesto that slot. Then
lowerTaskerases the marker.lowerTaskalso gets a check. A task that nothing joins must not get the address of a slotthat the loop around its
spawngives a new value in each pass: a store, a store to a field, ora new
lifetime.start. If it does, the compile stops with an error that says the compiler hasa bug. A borrow that the code joins in the same pass stays legal, and a test covers it.
Only the flat code generator had the bug.
--legacy-codegengave the right values before thischange, and it still does.
Tests
tests/backend/pass/values_moved_into_tasks_in_a_loop_are_the_values_of_their_pass.vx: sixcases. Each one runs a loop of three passes, and each task sends
1000 * pass + value. Themain code prints the values in the order of the passes, so the output does not depend on the
order of the tasks. The test runs with the default runtime,
threads,shared, two seeds and--legacy-codegen.tests/optimizations/pass/a_task_copies_a_slot_that_it_takes_at_its_spawn.mlr: onhand-written MLIR, the
spawnloads the whole struct, and the task reads the field from itsown slot.
tests/optimizations/pass/a_task_that_reads_a_slot_a_loop_writes_again_is_refused.mlr: thesame loop with no marker. The new error stops the lowering.
I broke the code in four ways to check that each test can fail:
2for each pass, one crashed with SIGBUS, and the system killed one.borrow_joined_in_the_same_pass: it printed0, 1, 2and not100, 101, 102.lowerTaskturned offCHECKfor the error.lowerTaskignores the marker, and the check is offCHECK, the load of the whole struct at thespawn.cargo test --workspaceon macOS arm64: 1712 tests passed and 4 failed. The 4 failures comefrom that machine, not from this change:
ane_device_test::the_dispatcher_reports_the_device_coreml_chose: no CoreML model.seam_uses_the_vx_solver(2 tests):libvx_solver.dylibwas not built.pipeline_scale_test::pipeline_emits_identical_mlir_at_benchmark_scale: its generated corpusin the temporary directory was missing a file, so the test failed before it compiled anything.
For the reviewer
rustfmton my machine (1.8.0) also changes code that this PR does not touch. I leftthose changes out, so
cargo fmt --all -- --checkin CI is the real check of the format.vx.spawn. This PR puts an op inside thetask region instead. A new operand on
vx.spawnwould touch each pass that handles the op.🤖 Generated with Claude Code