Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
pixi run run-post | tee post.txt
grep -q 'sum: 499500' post.txt

# Deliberate misuse: four cases the compiler must reject, four it must
# Deliberate misuse: two cases the compiler must reject, four it must
# still accept (and get wrong in the documented way).
- name: Caught and uncaught
run: pixi run check
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ Two files, one program:
- [`src/post.mojo`](src/post.mojo) — the post's whole program, plus the
twenty-line `Ctx[T]` it relies on, exactly as published.
- [`src/origins.mojo`](src/origins.mojo) — the same program with the origin
of `totals` kept in the context type instead of erased. `share(totals)`
captures it, `run[task](n, ctx)` holds it across the `parallel_for`, and the
compiler extends the lifetime of `totals` to cover the call. Sharing an
immutable argument, a temporary, or letting a `Ctx` outlive its state is a
compile error. The erasure to `void *` still happens — once, inside `run` —
because that is all a pthread can carry.
of `totals` kept instead of erased: `parallel_for[task](1000, totals)`
takes the state by `ref` and the task as `def(Int, mut Totals)`. An
argument is alive for the whole call, so the compiler extends the lifetime
of `totals` to cover the joins; a `read` argument or a temporary is a
compile error. The erasure to `void *` still happens — once, inside
threads.mojo — because that is all a pthread can carry. An earlier version
of this file did the same in user code with a `Ctx[T, origin]`, `share` and
`run`; threads-mojo 0.3.0 moved that into `parallel_for` itself.

Both sum `0..1000` from every core through one shared atomic and print

Expand Down Expand Up @@ -53,26 +55,24 @@ which wrote the `threads-mojo = { git = …, rev = … }` line in

## What the compiler catches, and what it does not

`pixi run check` builds eight deliberately wrong programs under
`pixi run check` builds six deliberately wrong programs under
[`tests/`](tests/) and holds the compiler to a claim about each one.

[`tests/caught/`](tests/caught/) must fail to compile, with the diagnostic the
file names:

| file | misuse | diagnostic |
| --- | --- | --- |
| `share_immutable` | `share(x)` on a `read` argument | `cannot be converted from 'Totals' to ref 'Totals'` |
| `share_temporary` | `share(Totals(0))` | same |
| `return_ctx_to_local` | `return share(local)` | `cannot implicitly convert 'Ctx[Totals, origin_of(t)]' value to 'Ctx[Totals]'` |
| `ctx_outlives_block` | a `Ctx` assigned in a block, used after it | same |
| `state_immutable` | `parallel_for[task](n, x)` with `x` a `read` argument | `cannot be converted from 'Totals' to ref 'Totals'` |
| `state_temporary` | `parallel_for[task](n, Totals(0))` | same |

[`tests/uncaught/`](tests/uncaught/) must compile with no diagnostic at all,
and then print the wrong answer the file predicts:

| file | what goes wrong | output |
| --- | --- | --- |
| `untracked_ctx_drops_early` | the post's `Ctx` erases the origin; `totals` is destroyed at its last visible use, before any thread starts | `Totals dropped` printed first; sum `499499` (−1 + 499500, the tasks adding into the poisoned cell) |
| `opaque_escapes_origin` | `share(totals).opaque()` outside `run` — the boundary of what origins cover | same |
| `opaque_escapes_origin` | the opaque `parallel_for` on `opaque_ptr(Int(Pointer(to=totals)))` — `Int(…)` is where the origin stops, and the boundary of what the typed form covers | same |
| `field_deref_after_last_use` | `totals.cell[]` copies the pointer field, which is the struct's last use, and the deref reads a destroyed object | `-1`; the method read `totals.sum()` gives `499500`, and so does the same field deref once the cell is an `OwnedPointer`, whose deref borrows the struct |
| `plain_store_races` | a task writes `sum` with a plain load and store instead of the atomic — every task holds `mut` access to the same value and nothing says the type is safe to share | lost updates, every run; not a lifetime bug but the one an origin cannot express |

Expand All @@ -83,11 +83,12 @@ those tests poisons the cell rather than freeing it, so the misuse stays a
number that can be asserted instead of undefined behaviour.

Three of the four are lifetime bugs, and each has a fix that exists today:
keep the origin in the type (`origins.mojo`), keep the erasure inside `run`,
and own heap memory through a type whose deref borrows the owner
(`OwnedPointer`, `List`) rather than a raw untracked `Pointer`. The fourth is
a different kind: `Ctx` gives a thousand tasks mutable access to one value,
and only an atomic in the task keeps that honest. Rust would refuse the
pass the state as a `ref` argument (the typed `parallel_for`), keep the
erasure inside the library, and own heap memory through a type whose deref
borrows the owner (`OwnedPointer`, `List`) rather than a raw untracked
`Pointer`. The fourth is a different kind: `parallel_for` gives a thousand
tasks `mut` access to one value, and only an atomic in the task keeps that
honest. Rust would refuse the
aliased `&mut` and let the atomic through on the strength of `Sync`; Mojo has
no such trait yet, so the race is invisible to the compiler and lands in the
uncaught set with the others.
Expand Down
12 changes: 6 additions & 6 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ fmt = { cmd = "mojo format src tests" }
mojo = "==1.0.0"
# Added by `pixi shelf add threads-mojo`: the tin comes from GitHub as a pixi
# source dependency, pinned at the revision mojoshelf published.
threads-mojo = { git = "https://github.com/magmalake/threads.mojo.git", rev = "afa64b0e9ebf7f5dc2079f98b3d0e49dcd7bb6ab" }
threads-mojo = { git = "https://github.com/magmalake/threads.mojo.git", rev = "1133606d6de0f48039e7ba2b427575b39b0d2010" }
89 changes: 23 additions & 66 deletions src/origins.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,22 @@ is through an untracked pointer the compiler cannot see, the last visible use
is `Ctx[Totals].to(totals)` — before `parallel_for` runs. With a `Totals` that
owns heap memory, a thousand tasks then write into a freed block.

Here the context carries the origin of the state it points to. `share`
captures it, `run` holds a value of that type across the `parallel_for`, and
the compiler extends the lifetime of `totals` to cover the call — no later use
of `totals` required. What the compiler will now reject: sharing an immutable
argument or a temporary, returning a `Ctx` to a local, and letting a `Ctx`
outlive its block.

The origin stops at `opaque()`. A pthread carries one `void *`, so the task
side gets it back through `Ctx[Totals].of(ptr)` with the untracked default —
inside a task the origin is genuinely unknowable. Keep `opaque()` as the one
place the erasure happens, inside `run`, and nothing else in the program needs
to reason about it.
Here `totals` goes to `parallel_for` as a `ref` argument and reaches every
task as `mut`. An argument is alive for the whole call, joins included, so
the compiler extends the lifetime of `totals` to cover it with no later use
required; and a `ref` demands a mutable binding, so a `read` argument or a
temporary is rejected where the call is made. The erasure to `void *` still
happens — a pthread carries one pointer — but once, inside the library, and
nothing in this file handles it.

An earlier version of this file did the same job in user code: a
`Ctx[T, origin]` built by `share(totals)` and held across the call by
`run[task](n, ctx)`. threads-mojo 0.3.0 moved that into `parallel_for`
itself, which is why the program below is the post's listing minus the
pointer plumbing.
"""

from threads import AtomicCounter, OpaquePtr, WorkFn, num_cpus, parallel_for


@fieldwise_init
struct Ctx[T: AnyType, origin: MutOrigin = MutUntrackedOrigin](
Copyable, Movable
):
"""A typed view of the pointer each task is handed, with an origin.

On the calling side the origin is the state's own — `share` builds one.
Inside a task it defaults to `MutUntrackedOrigin`, because the pointer
arrived through `void *` and there is nothing to track it against.
"""

var _ptr: Pointer[Self.T, Self.origin]

@staticmethod
def of(ptr: OpaquePtr) -> Self:
"""The task-side view: reinterpret the opaque pointer as a `T`."""
return Self(Pointer[Self.T, Self.origin](unsafe_from_address=Int(ptr)))

def __getitem__(self) -> ref[Self.origin] Self.T:
return self._ptr[]

def opaque(self) -> OpaquePtr:
"""Erase the origin. The one operation the compiler cannot check."""
return OpaquePtr(unsafe_from_address=Int(self._ptr))


def share[
T: AnyType, origin: MutOrigin
](ref[origin] state: T) -> Ctx[T, origin]:
"""A `Ctx` whose origin is `state`'s own — `origin_of(state)`.

Requires a mutable binding: an immutable argument or a temporary is
rejected at the call site.
"""
return Ctx[T, origin](Pointer(to=state))


def run[
T: AnyType, origin: MutOrigin, //, work: WorkFn
](n_tasks: Int, ctx: Ctx[T, origin], num_workers: Int = 0) raises:
"""`parallel_for`, taking the tracked context rather than the pointer.

`ctx` is alive for the whole call, so the state it points to is too. The
erasure to `void *` happens here and nowhere else.
"""
parallel_for[work](n_tasks, ctx.opaque(), num_workers)
from threads import AtomicCounter, num_cpus, parallel_for


@fieldwise_init
Expand All @@ -84,13 +37,17 @@ def counter(ref cell: Int64) -> AtomicCounter:
return AtomicCounter.at(Int(Pointer(to=cell)))


def task(i: Int, ptr: OpaquePtr) -> None:
"""The work function: a top-level `def`, since a thread body is thin."""
var t = Ctx[Totals].of(ptr)
_ = counter(t[].sum).fetch_add(Int64(i))
def task(i: Int, mut totals: Totals) -> None:
"""The work function: a top-level `def`, since a thread body is thin.

Every task gets `mut` access to the same `Totals` at the same time; the
atomic is what makes that honest. A plain `totals.sum += i` would compile
just as well — see `tests/uncaught/plain_store_races.mojo`.
"""
_ = counter(totals.sum).fetch_add(Int64(i))


def main() raises:
var totals = Totals(0)
run[task](1000, share(totals))
parallel_for[task](1000, totals)
print("cores:", num_cpus(), " sum:", totals.sum)
13 changes: 0 additions & 13 deletions tests/caught/ctx_outlives_block.mojo

This file was deleted.

15 changes: 0 additions & 15 deletions tests/caught/return_ctx_to_local.mojo

This file was deleted.

12 changes: 0 additions & 12 deletions tests/caught/share_immutable.mojo

This file was deleted.

14 changes: 14 additions & 0 deletions tests/caught/state_immutable.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# A `read` argument has an immutable origin; `parallel_for` takes the state
# by mutable `ref`, so the call is refused where it is made.
# expect-error: cannot be converted from 'Totals' to ref 'Totals'
from origins import Totals, task
from threads import parallel_for


def sum_of(totals: Totals) raises -> Int64:
parallel_for[task](1000, totals)
return totals.sum


def main() raises:
print(sum_of(Totals(0)))
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# A temporary dies at the end of the statement; there is nothing to share.
# expect-error: cannot be converted from 'Totals' to ref 'Totals'
from origins import Ctx, Totals, run, share, task
from origins import Totals, task
from threads import parallel_for


def main() raises:
var c = share(Totals(0))
run[task](1000, c)
print(c[].sum)
parallel_for[task](1000, Totals(0))
17 changes: 7 additions & 10 deletions tests/uncaught/field_deref_after_last_use.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
# expect: Owned dropped
from std.memory import OwnedPointer
from std.memory.alloc import unsafe_alloc
from origins import Ctx, run, share
from threads import AtomicCounter, OpaquePtr
from threads import AtomicCounter, parallel_for


struct Totals(Movable):
Expand Down Expand Up @@ -56,22 +55,20 @@ struct Owned(Movable):
print("Owned dropped")


def task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Totals].of(ptr)
_ = AtomicCounter.at(Int(t[].cell)).fetch_add(Int64(i))
def task(i: Int, mut t: Totals) -> None:
_ = AtomicCounter.at(Int(t.cell)).fetch_add(Int64(i))


def owned_task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Owned].of(ptr)
_ = AtomicCounter.at(Int(Pointer(to=t[].cell[]))).fetch_add(Int64(i))
def owned_task(i: Int, mut o: Owned) -> None:
_ = AtomicCounter.at(Int(Pointer(to=o.cell[]))).fetch_add(Int64(i))


def main() raises:
var totals = Totals()
run[task](1000, share(totals))
parallel_for[task](1000, totals)
print("via method:", totals.sum())
print("via field deref:", totals.cell[])

var owned = Owned()
run[owned_task](1000, share(owned))
parallel_for[owned_task](1000, owned)
print("via OwnedPointer field deref:", owned.cell[])
23 changes: 14 additions & 9 deletions tests/uncaught/opaque_escapes_origin.mojo
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# The tracked `Ctx` fixes the drop — until `opaque()` is called outside `run`.
# The `void *` carries no origin, the temporary `Ctx` dies with the statement,
# and `totals` goes with it. This is the boundary of what origins can cover.
# The typed `parallel_for` fixes the drop — the opaque one does not. `Int(…)`
# turns a pointer whose origin is `totals`'s own into a number, the number
# into an untracked `OpaquePtr`, and from there the compiler sees no use of
# `totals`, so it is destroyed on the spot. This is the boundary of what
# origins can cover: the opaque form is the right tool for a hand-laid-out
# block of cells, and the wrong one for a local you stop mentioning.
# expect: Totals dropped
# expect: before parallel_for
# expect: after parallel_for: 499499
from std.memory.alloc import unsafe_alloc
from origins import Ctx, share
from threads import AtomicCounter, OpaquePtr, parallel_for
from threads import AtomicCounter, OpaquePtr, opaque_ptr, parallel_for


struct Totals(Movable):
Expand All @@ -28,15 +30,18 @@ struct Totals(Movable):
self.cell[] = -1


def at(ptr: OpaquePtr) -> ref[MutUntrackedOrigin] Totals:
return Pointer[Totals, MutUntrackedOrigin](unsafe_from_address=Int(ptr))[]


def task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Totals].of(ptr)
_ = AtomicCounter.at(Int(t[].cell)).fetch_add(Int64(i))
_ = AtomicCounter.at(Int(at(ptr).cell)).fetch_add(Int64(i))


def main() raises:
var totals = Totals()
var ptr = share(totals).opaque()
var ptr = opaque_ptr(Int(Pointer(to=totals)))
print("before parallel_for")
parallel_for[task](1000, ptr)
# Not `totals.cell` — a use of `totals` here would move the drop past it.
print("after parallel_for:", Ctx[Totals].of(ptr)[].cell[])
print("after parallel_for:", at(ptr).cell[])
Loading
Loading