Files
sakurac/compiler/scratch.sakura
T
2026-11-19 04:54:17 -06:00

146 lines
5.5 KiB
Plaintext

#! @file scratch.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the arenas what the compiler works out only for a while is held in:
#! what a pass of the optimizer finds before it changes the code, and what
#! lowering, verifying, and assembling a function find as they go. Each of those
#! marks the arena where it begins and rewinds it to the mark where it ends, so
#! what it took is freed at once, and the next takes the same memory again.
#! Handing out a block is then an addition, and giving it back nothing at all.
#!
#! Each thread has an arena of its own, as a mark and a rewind on one thread
#! would otherwise free what another still holds. The first thread takes from
#! one of its own; the threads `share_out` starts, its crew, each take from one
#! of the crew's arenas, which each finds by the number the system knows its
#! thread by, while the crew works. Where none works, as while the compiler
#! works alone, nothing is looked for.
import std::thread;
import std::allocators;
module sakura::compiler;
#! The arena the first thread's scratch memory comes from, in chunks of 256 KiB,
#! which the compiler never closes.
var scratch_arena := arena::new(262144);
#! The arenas of the crew, which stay where they are while it works, so the
#! room for them is made before any of it starts. They are kept, with their
#! chunks, for the next crew.
var crew_arenas := vector::<arena>::new();
#! The number the system knows each thread of the crew by, by its place, or 0
#! until it starts. Every thread reads these, and none writes them once the
#! crew has started, so they stay in every processor's cache.
var crew_threads := vector::<atomic<u64>>::new();
#! The arena each thread of the crew takes from, by its place, which only that
#! thread reads and writes once the crew has started, each on a line of the
#! processor's cache of its own, so one writing its own makes no other read
#! again.
var crew_hands := vector::<hand>::new();
#! How many threads the crew has, or 0 where none works.
var crew_size := atomic::<u32>::new(0);
#! The arena a thread of the crew takes from, padded to a line of the
#! processor's cache.
struct hand {
var arena: u32; #! Its place in `crew_arenas`.
var pad: u64[7]; #! What fills the rest of the line.
}
#! Memory for what the compiler works out only while it does one thing, which
#! is freed where that ends.
#! @returns The arena of the thread calling, as an allocator.
#! @remarks Nothing taken from it may be kept past the mark it was taken after:
#! a vector of it is closed before the mark is rewound to, and never
#! moved into what outlives it, as the code of a function does.
pub def scratch(): maybe<&mut allocator> {
return own_arena();
}
#! Where the scratch memory of the thread calling has been handed out to, which
#! `release_scratch` frees what was handed out after.
#! @returns The mark.
pub def mark_scratch(): arena::point {
return own_arena().mark();
}
#! Frees the scratch memory the thread calling had handed out after a mark.
#! @param to The mark, which `mark_scratch` gave on the same thread.
pub def release_scratch(to: arena::point): void {
own_arena().rewind(to);
}
#! The arena the thread calling takes its scratch memory from: that of its place
#! in the crew, or the first thread's.
#! @returns The arena.
def own_arena(): &mut arena {
val size := crew_size.load(order::acquire);
if size != 0 {
val me := thread::id();
for k in 0..size as u64 {
if crew_threads[k].load(order::relaxed) == me {
return &mut crew_arenas[crew_hands[k].arena as u64];
}
}
}
return &mut scratch_arena;
}
#! Makes the room a crew works in, before any of it starts: a place for each
#! thread, the `k`th taking from the `k`th arena until it takes from another,
#! and as many arenas as are asked for, at least one for each thread.
#! @param threads How many threads the crew has.
#! @param arenas How many arenas it takes from.
pub def gather_crew(threads: u32, arenas: u32): void {
expects(crew_size.load(order::acquire) == 0);
val wanted := arenas > threads ? arenas : threads;
while crew_arenas.length < wanted as u64 {
crew_arenas.append(arena::new(262144));
}
crew_threads.clear();
crew_hands.clear();
for k in 0..threads {
crew_threads.append(atomic::<u64>::new(0));
crew_hands.append(hand { arena = k });
}
crew_size.store(threads, order::release);
}
#! Says the thread calling is the one of a place in the crew, which takes its
#! scratch memory from that place's arena from then on.
#! @param place Its place.
pub def join_crew(place: u32): void {
crew_threads[place as u64].store(thread::id(), order::release);
}
#! Has the thread of a place in the crew, which calls it, take its scratch
#! memory from another arena.
#! @param place Its place.
#! @param arena The arena, by its place among the crew's.
pub def take_scratch_from(place: u32, arena: u32): void {
crew_hands[place as u64].arena = arena;
}
#! One of the crew's arenas, for the thread that holds it alone, as to reset it.
#! @param index Its place among the crew's.
#! @returns The arena.
pub def crew_arena(index: u32): &mut arena {
return &mut crew_arenas[index as u64];
}
#! Says no crew works any more, once every thread of it has ended. Its arenas
#! are reset, keeping their chunks for the next.
pub def dismiss_crew(): void {
crew_size.store(0, order::release);
crew_threads.clear();
crew_hands.clear();
for &mut each in crew_arenas {
each.reset();
}
}