Skip to content

tasks: copy each value that a task moves or copies at its spawn - #4356

Merged
hiraditya merged 3 commits into
vx-lang:mainfrom
minzyfox:spawn-copies-moved-captures
Dec 3, 2026
Merged

hiraditya merged 3 commits into
vx-lang:mainfrom
minzyfox:spawn-copies-moved-captures

Conversation

@minzyfox

@minzyfox minzyfox commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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 slot
into the task at the spawn only when the task loaded the whole value. For any other use, for
example 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 Copy struct, with no channel at all:

    for i in 0i64..3i64 {
      let p = Point { id: i, y: 0 };   // Point is Copy
      spawn { println!("{}", p.id); };  // all three tasks printed 2
    }
    

When a task got another pass's Sender, two tasks owned one channel end. So the program dropped
one 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:

  1. src/hir/check/tasks.rs: the checker saves the names that the task moves or copies in a new
    field, UnsafeBlockExpr::takes. The checker leaves out a reference, because the task copies the address
    and not what it points to. rename_in_node in drops.rs renames these names together with
    the code, as it already does for the captures of a closure.
  2. 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.
  3. src/codegen/flat/emit/parallel.rs: TaskTakes becomes 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-0
    memref is only loaded, so copiedAtSpawn copies it as before.
  4. src/dialect/VxLowering.cpp: lowerTask copies each marked slot into the task's environment
    at the spawn. The task stores the value in a slot of its own, and each use in the task goes
    to that slot. Then lowerTask erases the marker.

lowerTask also gets a check. A task that nothing joins must not get the address of a slot
that the loop around its spawn gives a new value in each pass: a store, a store to a field, or
a new lifetime.start. If it does, the compile stops with an error that says the compiler has
a 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-codegen gave the right values before this
change, and it still does.

Tests

  • tests/backend/pass/values_moved_into_tasks_in_a_loop_are_the_values_of_their_pass.vx: six
    cases. Each one runs a loop of three passes, and each task sends 1000 * pass + value. The
    main 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: on
    hand-written MLIR, the spawn loads the whole struct, and the task reads the field from its
    own slot.
  • tests/optimizations/pass/a_task_that_reads_a_slot_a_loop_writes_again_is_refused.mlr: the
    same loop with no marker. The new error stops the lowering.

I broke the code in four ways to check that each test can fail:

What I broke What failed
The old compiler (main) Five of the six backend cases: three gave 2 for each pass, one crashed with SIGBUS, and the system killed one.
The checker also marks variables that the task writes The sixth backend case, borrow_joined_in_the_same_pass: it printed 0, 1, 2 and not 100, 101, 102.
The new check in lowerTask turned off The refusal test: its CHECK for the error.
lowerTask ignores the marker, and the check is off The copy test: its first CHECK, the load of the whole struct at the spawn.

cargo test --workspace on macOS arm64: 1712 tests passed and 4 failed. The 4 failures come
from 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.dylib was not built.
  • pipeline_scale_test::pipeline_emits_identical_mlir_at_benchmark_scale: its generated corpus
    in the temporary directory was missing a file, so the test failed before it compiled anything.

For the reviewer

  • The rustfmt on my machine (1.8.0) also changes code that this PR does not touch. I left
    those changes out, so cargo fmt --all -- --check in CI is the real check of the format.
  • tasks: a channel end moved into a task in a loop can be the end of a later pass #4345 says that the marker is a list of operands on vx.spawn. This PR puts an op inside the
    task region instead. A new operand on vx.spawn would touch each pass that handles the op.

🤖 Generated with Claude Code

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 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. 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); }; }, with w : 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 two take_at_spawn markers, so the new copy runs. A bare *mut Counter cannot go into a task, because it is not Send.

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
@minzyfox

minzyfox commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both requests. I merged main and resolved both conflicts.

The test now uses --implicit-check-not=take_at_spawn. Real output passes, while markers inserted independently at the start of @main and @vx_task_0 fail the intended assertion.

All three branch tests passed, including all six runtime modes. Backend coverage passed too.

@minzyfox
minzyfox requested a review from hiraditya December 3, 2026 00:17

@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.

A test that would help, later: a Drop type that prints its id, moved into tasks in a loop. It would show that each value is dropped once.

@hiraditya
hiraditya merged commit e1366cf into vx-lang:main Dec 3, 2026
7 checks passed
minzyfox added a commit to minzyfox/Vx that referenced this pull request Dec 4, 2026
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>
hiraditya pushed a commit that referenced this pull request Dec 4, 2026
…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>
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.

2 participants