navid/haze / commit

67bab09d6ce4

Navid M <navidmomtahen@gmail.com> · 2026-12-02

parents: f34a670d2935

Browse files at this commit · Download source

all: add pandoc rendered version
--- /dev/null+++ b/docs/HAZE_IMMIX_COLLECTOR_SPECIFICATION.html@@ -0,0 +1,1497 @@+<h1 id="haze-immix-collector-specification">Haze Immix Collector+Specification</h1>+<p><em>Draft 0.2 · C++23 runtime · UQBE backend</em></p>+<h2 id="goals-and-non-goals">1. Goals and non-goals</h2>+<h3 id="goals">Goals</h3>+<ul>+<li>Precise, parallel, stop-the-world mark-region garbage+collection.</li>+<li>Opportunistic evacuation of fragmented blocks.</li>+<li>Extremely cheap allocation through inline thread-local bump+pointers.</li>+<li>No write barrier for immutable heap objects.</li>+<li>Dense side metadata with predictable memory overhead.</li>+<li>Bounded allocation and collection overhead for heaps reaching tens+of GiB.</li>+<li>Native integration with UQBE without introducing an LLVM+dependency.</li>+<li>Portable runtime implementation across supported UQBE targets.</li>+</ul>+<h3 id="non-goals-v1">Non-goals (v1)</h3>+<ul>+<li>Concurrent marking.</li>+<li>Conservative scanning.</li>+<li>Finalizers.</li>+<li>Interior pointers stored in heap objects.</li>+<li>Pointer tagging exposed to Haze programs.</li>+<li>Incremental collection.</li>+<li>Concurrent evacuation.</li>+<li>General-purpose weak references.</li>+</ul>+<h3 id="design-principles">Design principles</h3>+<ol type="1">+<li>The compiler owns managed-pointer correctness.</li>+<li>The runtime owns heap management, tracing, evacuation, and+reclamation.</li>+<li>UQBE generates ordinary machine-code calls, not GC-specific+LLVM-style intrinsics.</li>+<li>Managed references are always identifiable as exact object-base+pointers.</li>+<li>Every potential collection point has a compiler-generated root+map.</li>+<li>The collector never needs to inspect arbitrary machine+registers.</li>+</ol>+<hr />+<h2 id="the-key-bet-immutability-removes-the-write-barrier">2. The key+bet: immutability removes the write barrier</h2>+<p>Haze heap objects are immutable after construction.</p>+<p>An object can only reference objects that already existed when it was+constructed. Consequently, an older object can never acquire a reference+to a younger object.</p>+<p>The resulting graph has a useful directional property:</p>+<ul>+<li>Older objects may reference older objects.</li>+<li>Younger objects may reference older objects.</li>+<li>Younger objects may reference younger objects created before+them.</li>+<li>Older objects cannot reference younger objects.</li>+</ul>+<p>This provides two important properties:</p>+<ul>+<li>No write barrier is needed.</li>+<li>No remembered set is needed for generational collection.</li>+</ul>+<h3 id="compiler-enforcement">Compiler enforcement</h3>+<p>The compiler must guarantee that heap fields cannot be modified after+object publication.</p>+<p>Object construction follows this sequence:</p>+<ol type="1">+<li>Allocate zero-initialized storage.</li>+<li>Initialize all scalar fields.</li>+<li>Initialize all managed references.</li>+<li>Publish the object.</li>+<li>Never modify its fields again.</li>+</ol>+<p>The collector is permitted to modify object storage internally when+rewriting references during evacuation.</p>+<h3 id="future-mutable-objects">Future mutable objects</h3>+<p>If Haze eventually introduces mutable heap cells, such as+<code>Ref</code> or lazy thunk updates, they must use an explicit+mutation API:</p>+<p><code>haze_wb(obj, slot, value)</code></p>+<p>This operation will:</p>+<ol type="1">+<li>Update the reference.</li>+<li>Set the <code>REMEMBERED</code> header flag.</li>+<li>Append the object to a per-thread remembered buffer.</li>+</ol>+<p>Mutable objects must not bypass this API.</p>+<p>This is not required by v1.</p>+<hr />+<h2 id="heap-layout">3. Heap layout</h2>+<h3 id="virtual-reservation">3.1 Virtual reservation</h3>+<p>The collector reserves one large contiguous virtual address+range.</p>+<p>Default configuration:</p>+<ul>+<li>Reservation: 64 GiB.</li>+<li>Alignment: 2 MiB.</li>+<li>Block size: 32 KiB.</li>+<li>Commitment: on demand.</li>+<li>Huge pages: opportunistic.</li>+<li>Unused memory: released or reclaimed using platform-specific+virtual-memory operations.</li>+</ul>+<p>The heap uses a single base address:</p>+<p><code>heap_base</code></p>+<p>Block lookup is a shift:</p>+<div class="sourceCode" id="cb1"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">constexpr</span> u32 BLOCK_SHIFT <span class="op">=</span> <span class="dv">15</span><span class="op">;</span></span>+<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">constexpr</span> u64 BLOCK_SIZE <span class="op">=</span> <span class="dv">1</span><span class="bu">ull</span> <span class="op">&lt;&lt;</span> BLOCK_SHIFT<span class="op">;</span></span>+<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">inline</span> u32 block_index<span class="op">(</span><span class="at">const</span> <span class="dt">void</span><span class="op">*</span> ptr<span class="op">)</span> <span class="op">{</span></span>+<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> <span class="op">(</span><span class="kw">static_cast</span><span class="op">&lt;</span><span class="at">const</span> u8<span class="op">*&gt;(</span>ptr<span class="op">)</span> <span class="op">-</span></span>+<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>            heap_base<span class="op">)</span> <span class="op">&gt;&gt;</span> BLOCK_SHIFT<span class="op">;</span></span>+<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>+<p>The implementation must validate that the pointer belongs to the+managed reservation before performing this calculation.</p>+<h3 id="object-layout-constants">3.2 Object layout constants</h3>+<table>+<thead>+<tr class="header">+<th>Constant</th>+<th style="text-align: right;">Value</th>+<th>Description</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td>Granule</td>+<td style="text-align: right;">16 B</td>+<td>Minimum object alignment</td>+</tr>+<tr class="even">+<td>Header</td>+<td style="text-align: right;">8 B</td>+<td>Object metadata</td>+</tr>+<tr class="odd">+<td>Line</td>+<td style="text-align: right;">128 B</td>+<td>Allocation/reclamation granularity</td>+</tr>+<tr class="even">+<td>Block</td>+<td style="text-align: right;">32 KiB</td>+<td>Immix allocation unit</td>+</tr>+<tr class="odd">+<td>Lines per block</td>+<td style="text-align: right;">256</td>+<td>Fixed</td>+</tr>+<tr class="even">+<td>Small object</td>+<td style="text-align: right;">≤128 B</td>+<td>Normal allocation</td>+</tr>+<tr class="odd">+<td>Medium object</td>+<td style="text-align: right;">≤8 KiB</td>+<td>Multi-line allocation</td>+</tr>+<tr class="even">+<td>Large object</td>+<td style="text-align: right;">&gt;8 KiB</td>+<td>Large object space</td>+</tr>+</tbody>+</table>+<p>Object sizes include their headers and are rounded up to 16-byte+alignment.</p>+<p>Small objects may span two lines. An object is live only when its+header has been marked; every line it intersects is subsequently marked+live.</p>+<p>The collector must not assume that an object occupies exactly one+line.</p>+<h3 id="side-metadata">3.3 Side metadata</h3>+<p>Metadata is allocated separately from object storage.</p>+<p><strong>Line metadata</strong></p>+<p>One byte per line.</p>+<p>256 bytes per block.</p>+<p>A line is considered live when its metadata byte equals the current+collection epoch.</p>+<p>During allocation, the relevant line metadata is also updated to+reflect the current allocation epoch.</p>+<p>The collector uses line metadata to determine which regions of a+block can be reused.</p>+<p>Importantly, line-level reclamation is conservative: a line+containing even one live object cannot be reclaimed.</p>+<p><strong>Block metadata</strong></p>+<p>16 bytes per block.</p>+<div class="sourceCode" id="cb2"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> BlockMeta <span class="op">{</span></span>+<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>    u32 next<span class="op">;</span></span>+<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>    u16 holes<span class="op">;</span></span>+<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    u16 marked_lines<span class="op">;</span></span>+<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>    u8  state<span class="op">;</span></span>+<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>    u8  generation<span class="op">;</span></span>+<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>    u8  flags<span class="op">;</span></span>+<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>    u8  age<span class="op">;</span></span>+<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>    u32 owner<span class="op">;</span></span>+<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p>Block states:</p>+<div class="sourceCode" id="cb3"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">enum</span> <span class="kw">class</span> BlockState <span class="op">:</span> u8 <span class="op">{</span></span>+<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>    FREE<span class="op">,</span></span>+<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>    ALLOCATING<span class="op">,</span></span>+<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>    RECYCLABLE<span class="op">,</span></span>+<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>    FULL<span class="op">,</span></span>+<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>    EVAC_SOURCE<span class="op">,</span></span>+<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>    EVAC_TARGET<span class="op">,</span></span>+<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>    LOS_RESERVED</span>+<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p><code>generation</code> identifies whether the block belongs to the+nursery or old generation.</p>+<p><code>owner</code> identifies the mutator responsible for the block.+Unowned blocks use a reserved identifier.</p>+<p><strong>Metadata reservation</strong></p>+<p>At the default 64 GiB heap size:</p>+<ul>+<li>Line metadata: 512 MiB.</li>+<li>Block metadata: 32 MiB.</li>+</ul>+<p>Total: approximately 0.83% of the reserved heap.</p>+<h3 id="object-header">3.4 Object header</h3>+<div class="sourceCode" id="cb4"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Header <span class="op">{</span></span>+<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>    u32 type_id<span class="op">;</span></span>+<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>    u8  mark<span class="op">;</span></span>+<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>    u8  flags<span class="op">;</span></span>+<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>    u16 aux<span class="op">;</span></span>+<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span>+<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="kw">static_assert</span><span class="op">(</span><span class="kw">sizeof</span><span class="op">(</span>Header<span class="op">)</span> <span class="op">==</span> <span class="dv">8</span><span class="op">);</span></span></code></pre></div>+<p>Flags:</p>+<div class="sourceCode" id="cb5"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">enum</span> HeaderFlags <span class="op">:</span> u8 <span class="op">{</span></span>+<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>    PINNED     <span class="op">=</span> <span class="dv">1</span> <span class="op">&lt;&lt;</span> <span class="dv">0</span><span class="op">,</span></span>+<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>    REMEMBERED <span class="op">=</span> <span class="dv">1</span> <span class="op">&lt;&lt;</span> <span class="dv">1</span><span class="op">,</span></span>+<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>    LARGE      <span class="op">=</span> <span class="dv">1</span> <span class="op">&lt;&lt;</span> <span class="dv">2</span></span>+<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p>The header is eight-byte aligned and accessed atomically when+concurrent collector workers may inspect it.</p>+<p>The header has three logical states:</p>+<ul>+<li>Normal object header.</li>+<li>Busy evacuation state.</li>+<li>Forwarded object.</li>+</ul>+<p>Forwarding replaces the complete 64-bit header with:</p>+<p><code>new_address | 1</code></p>+<p>The low bit is available because managed objects are at least 16-byte+aligned.</p>+<p>A forwarded object must never be interpreted as a normal header.</p>+<p>The collector uses acquire/release operations when publishing and+consuming forwarding pointers.</p>+<h3 id="type-descriptors">3.5 Type descriptors</h3>+<p>The runtime maintains a type descriptor table.</p>+<div class="sourceCode" id="cb6"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> TypeDesc <span class="op">{</span></span>+<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>    u32 size<span class="op">;</span></span>+<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>    u16 pointer_count<span class="op">;</span></span>+<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>    u16 flags<span class="op">;</span></span>+<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">const</span> u32<span class="op">*</span> pointer_offsets<span class="op">;</span></span>+<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p>Pointer offsets are relative to the beginning of the object.</p>+<p>For larger structures, descriptors may use compressed pointer-offset+sequences or bitmaps.</p>+<p>Arrays contain their element type and length in their allocation+metadata.</p>+<p>The collector must support:</p>+<ul>+<li>Fixed-size structures.</li>+<li>Arrays of scalar values.</li>+<li>Arrays of managed references.</li>+<li>Structures containing mixed scalar and reference fields.</li>+</ul>+<p>All pointer fields contain exact managed object-base pointers.</p>+<p>The compiler and runtime must agree on the descriptor layout.</p>+<hr />+<h2 id="allocation">4. Allocation</h2>+<h3 id="mutator-state">4.1 Mutator state</h3>+<p>Each mutator owns a cache-line-aligned <code>Mutator</code>+structure.</p>+<div class="sourceCode" id="cb7"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Mutator <span class="op">{</span></span>+<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> cursor<span class="op">;</span></span>+<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> limit<span class="op">;</span></span>+<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> ocursor<span class="op">;</span></span>+<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> olimit<span class="op">;</span></span>+<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> nursery_cursor<span class="op">;</span></span>+<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> nursery_limit<span class="op">;</span></span>+<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a>    RootFrame<span class="op">*</span> root_frame<span class="op">;</span></span>+<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a>    u32 thread_id<span class="op">;</span></span>+<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a>    u32 flags<span class="op">;</span></span>+<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a>    u8 poll<span class="op">;</span></span>+<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a>    u8 reserved<span class="op">[</span><span class="dv">7</span><span class="op">];</span></span>+<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p>The normal allocation region uses the current block’s available+lines.</p>+<p>The overflow allocator handles medium allocations.</p>+<p>The nursery allocator is used when generational mode is enabled.</p>+<p>The mutator structure is accessed through thread-local storage.</p>+<h3 id="inline-allocation">4.2 Inline allocation</h3>+<p>The common allocation path must be emitted directly by UQBE.</p>+<p>Conceptually:</p>+<div class="sourceCode" id="cb8"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">inline</span> <span class="dt">void</span><span class="op">*</span> alloc<span class="op">(</span>Mutator<span class="op">&amp;</span> m<span class="op">,</span> u32 size<span class="op">,</span> u32 type<span class="op">)</span> <span class="op">{</span></span>+<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>    <span class="kw">auto</span> p <span class="op">=</span> m<span class="op">.</span>cursor<span class="op">;</span></span>+<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>    <span class="kw">auto</span> n <span class="op">=</span> <span class="kw">static_cast</span><span class="op">&lt;</span>u8<span class="op">*&gt;(</span>p<span class="op">)</span> <span class="op">+</span> size<span class="op">;</span></span>+<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> <span class="op">(</span>n <span class="op">&gt;</span> m<span class="op">.</span>limit<span class="op">)</span></span>+<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>        <span class="cf">return</span> alloc_slow<span class="op">(</span>m<span class="op">,</span> size<span class="op">,</span> type<span class="op">);</span></span>+<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>    m<span class="op">.</span>cursor <span class="op">=</span> n<span class="op">;</span></span>+<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a>    store_header<span class="op">(</span>p<span class="op">,</span> type<span class="op">);</span></span>+<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a></span>+<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> p<span class="op">;</span></span>+<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>+<p>The compiler supplies a compile-time object size whenever+possible.</p>+<p>The fast path:</p>+<ul>+<li>Performs no locking.</li>+<li>Performs no system calls.</li>+<li>Does not allocate auxiliary metadata.</li>+<li>Does not call the runtime.</li>+<li>Does not perform a collection.</li>+</ul>+<p>Target: approximately six machine instructions on x86-64, excluding+any required header initialization.</p>+<h3 id="slow-allocation">4.3 Slow allocation</h3>+<p>The slow path follows this order:</p>+<ol type="1">+<li>Find another available hole in the current block.</li>+<li>Acquire a recyclable block from the mutator’s local cache.</li>+<li>Refill the cache from global block lists.</li>+<li>Acquire a free block.</li>+<li>Acquire an additional committed block if permitted by the growth+budget.</li>+<li>Trigger collection.</li>+<li>Retry allocation.</li>+</ol>+<p>Global block lists must use a concurrency-safe algorithm.</p>+<p>A block must have exactly one allocating owner at any given time.</p>+<h3 id="zero-initialization">4.4 Zero initialization</h3>+<p>All allocated object storage is zero-initialized.</p>+<p>Freshly committed pages are already zeroed by the operating+system.</p>+<p>Reused holes are cleared when acquired.</p>+<p>Zeroing occurs outside the inline fast path.</p>+<p>The runtime may use architecture-specific vectorized clearing.</p>+<h3 id="prefetching">4.5 Prefetching</h3>+<p>When acquiring a new hole, the allocator may prefetch 256 bytes ahead+of the allocation cursor.</p>+<p>Prefetching must remain an optimization, not a correctness+requirement.</p>+<h3 id="medium-objects">4.6 Medium objects</h3>+<p>Medium allocations use whole-line regions.</p>+<p>An allocation that cannot fit in the current hole must not consume+unrelated remaining space.</p>+<p>Medium allocations are placed into dedicated multi-line regions or+blocks obtained from the overflow allocator.</p>+<h3 id="large-object-space">4.7 Large object space</h3>+<p>Objects larger than 8 KiB use the large object space (LOS).</p>+<p>LOS objects:</p>+<ul>+<li>Are never evacuated.</li>+<li>Have separate allocation metadata.</li>+<li>Participate in normal marking.</li>+<li>Are reclaimed during LOS sweeping.</li>+<li>Are independently committed and released.</li>+</ul>+<p>Large objects must not consume ordinary Immix blocks.</p>+<hr />+<h2 id="collection">5. Collection</h2>+<p>The collector uses parallel stop-the-world mark-region collection+with opportunistic evacuation.</p>+<p>All collection workers operate against a stable heap.</p>+<h3 id="safepoint-rendezvous">5.1 Safepoint rendezvous</h3>+<p>Every mutator periodically checks its safepoint poll flag.</p>+<p>The compiler inserts polls:</p>+<ul>+<li>At loop back-edges.</li>+<li>At recursive function entry.</li>+<li>At allocation slow paths.</li>+<li>At calls that may trigger collection.</li>+</ul>+<p>The fast poll is a single conditional branch.</p>+<p>When a collection is requested:</p>+<ol type="1">+<li>The collector sets the global collection request.</li>+<li>Mutators observe their poll flags.</li>+<li>Each mutator publishes its active root frame.</li>+<li>Mutators enter a parked state.</li>+<li>The collector begins once all required mutators are safe.</li>+</ol>+<p>Threads executing native code require special handling.</p>+<p>A thread may be excluded from the rendezvous only if it has entered a+GC-safe native region and guarantees that it holds no unpinned managed+pointers.</p>+<p>Otherwise, the collector must wait for it to reach a safepoint.</p>+<p>A native thread holding managed references must either:</p>+<h1 id="haze-immix-collector-specification-1">Haze Immix Collector+Specification</h1>+<p><em>Draft 0.1 · C++23 runtime · LLVM statepoint backend</em></p>+<h2 id="goals-and-non-goals-1">1. Goals and non-goals</h2>+<p><strong>Goals:</strong> precise, parallel, stop-the-world mark-region+collector with opportunistic evacuation; the cheapest possible+allocation (inline bump pointer); <strong>no write barrier</strong>;+dense side metadata; predictable pauses for heaps up to tens of GiB.</p>+<p><strong>Non-goals (v1):</strong> concurrent marking, conservative+scanning, finalizers, interior pointers stored in the heap, pointer+tagging.</p>+<h2 id="the-key-bet-immutability-removes-the-barrier">2. The key bet:+immutability removes the barrier</h2>+<p>Haze heap data is immutable (no <code>mut</code>), so an object can+only point to objects that existed before it. Old objects therefore+never point to young ones. This gives two things for free:</p>+<ul>+<li>no write barrier and no remembered set;</li>+<li>sticky-mark-bit minor collections (section 6) that trace only from+roots and stop at any already-marked object.</li>+</ul>+<p>Any future mutable heap cell (<code>Ref</code>, lazy thunk update)+must go through <code>haze_wb(obj)</code>, which sets a+<code>REMEMBERED</code> header bit and appends to a per-thread buffer.+Nothing else in the design changes.</p>+<h2 id="heap-layout-1">3. Heap layout</h2>+<p>One large <code>PROT_NONE</code> virtual reservation (default 64 GiB,+2 MiB aligned, <code>MADV_HUGEPAGE</code>), committed on demand. Block+lookup is a shift:+<code>idx = (addr - heap_base) &gt;&gt; 15</code>.</p>+<table>+<thead>+<tr class="header">+<th>Constant</th>+<th>Value</th>+<th>Notes</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td>Granule / alignment</td>+<td>16 B</td>+<td>all objects, minimum object 16 B</td>+</tr>+<tr class="even">+<td>Line</td>+<td>128 B</td>+<td>8 granules</td>+</tr>+<tr class="odd">+<td>Block</td>+<td>32 KiB</td>+<td>256 lines</td>+</tr>+<tr class="even">+<td>Small</td>+<td>≤ 128 B</td>+<td>marks one line (+ implicit next line)</td>+</tr>+<tr class="odd">+<td>Medium</td>+<td>≤ 8 KiB</td>+<td>marks every spanned line; uses overflow allocator</td>+</tr>+<tr class="even">+<td>Large</td>+<td>&gt; 8 KiB</td>+<td>large object space (LOS), never moved</td>+</tr>+</tbody>+</table>+<p><strong>Side tables</strong> (separate reservation, never interleaved+with objects):</p>+<ul>+<li><em>Line marks:</em> 1 byte per line (256 B per block). A line is+live iff its byte equals the current epoch.</li>+<li><em>Block metadata:</em> 16 B per block: <code>state</code> (free,+recyclable, full, evac-source, evac-target), <code>holes</code>,+<code>marked_lines</code>, <code>age</code>, <code>next</code> (32-bit+list link).</li>+</ul>+<p><strong>Object header (8 B):</strong></p>+<div class="sourceCode" id="cb9"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Header <span class="op">{</span>          <span class="co">// little-endian, 8 bytes</span></span>+<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>  u32 type_id<span class="op">;</span>           <span class="co">// index into the type table</span></span>+<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>  u8  mark<span class="op">;</span>              <span class="co">// == epoch when marked</span></span>+<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>  u8  flags<span class="op">;</span>             <span class="co">// PINNED | REMEMBERED | LARGE</span></span>+<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>  u16 aux<span class="op">;</span>               <span class="co">// e.g. small array length</span></span>+<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="op">};</span>                       <span class="co">// forwarded: whole word = (new_addr | 1)</span></span></code></pre></div>+<p>The type table entry holds <code>size</code>, a pointer count, and a+pointer bitmap (up to 62 words) or run-length offsets. Arrays carry+<code>{elem_type_id, len}</code> after the header. Heap fields hold base+pointers only.</p>+<h2 id="allocation-1">4. Allocation</h2>+<p>Each mutator owns a <code>Mutator</code> struct (TLS,+<code>initial-exec</code>, cache-line aligned) holding two bump regions:+<code>cursor/limit</code> for the current hole and+<code>ocursor/olimit</code> for overflow (medium objects).</p>+<div class="sourceCode" id="cb10"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">inline</span> <span class="dt">void</span><span class="op">*</span> alloc<span class="op">(</span>Mutator<span class="op">&amp;</span> m<span class="op">,</span> u32 size<span class="op">,</span> u32 type<span class="op">)</span> <span class="op">{</span>   <span class="co">// size: constexpr, 16-aligned</span></span>+<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>  <span class="kw">auto</span> p <span class="op">=</span> m<span class="op">.</span>cursor<span class="op">,</span> n <span class="op">=</span> p <span class="op">+</span> size<span class="op">;</span></span>+<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>  <span class="cf">if</span> <span class="op">(</span>n <span class="op">&gt;</span> m<span class="op">.</span>limit<span class="op">)</span> <span class="op">[[</span><span class="at">unlikely</span><span class="op">]]</span> <span class="cf">return</span> alloc_slow<span class="op">(</span>m<span class="op">,</span> size<span class="op">,</span> type<span class="op">);</span></span>+<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>  m<span class="op">.</span>cursor <span class="op">=</span> n<span class="op">;</span></span>+<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a>  store_header<span class="op">(</span>p<span class="op">,</span> type<span class="op">);</span></span>+<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>  <span class="cf">return</span> p<span class="op">;</span>                                             <span class="co">// payload already zero</span></span>+<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>+<p>The compiler emits this inline as IR (about 6 instructions). The slow+path, in order:</p>+<ol type="1">+<li>Next hole in the current block, found with SIMD compare of line+bytes against the epoch, then <code>countr_zero</code> on the+movemask.</li>+<li>Next recyclable block from the thread’s local cache, refilled in+batches of 4 from the global lock-free lists.</li>+<li>A free block.</li>+<li>Trigger a collection.</li>+</ol>+<p><strong>Zeroing</strong> happens per hole at acquisition+(<code>memset</code>; fresh <code>mmap</code> pages are already zero).+Prefetch-for-write 256 B ahead of <code>cursor</code> when entering a+hole. Medium objects that don’t fit the hole go to the overflow+allocator, which uses whole free blocks, so holes are not burned.</p>+<h2 id="collection-parallel-stop-the-world">5. Collection (parallel,+stop-the-world)</h2>+<ol type="1">+<li><strong>Rendezvous:</strong> polls and handshakes bring mutators to+safepoints. Threads in native code are marked <code>IN_NATIVE</code>+with their last Haze frame published, so the GC does not wait for+them.</li>+<li><strong>Choose evacuation set</strong> (only if fragmented): from+last cycle’s histogram, take blocks with the most holes and least live+data, until their live bytes fit in the reserved target space (≥ 2% of+heap, minimum 8 blocks).</li>+<li><strong>Roots:</strong> stack maps (section 7), the global root+table, and the pinned set.</li>+<li><strong>Trace + evacuate in one pass.</strong> Per-thread mark+stacks (1K-entry chunks) with Chase-Lev work stealing, and an 8-entry+prefetch FIFO between popping a reference and scanning the object.+<ul>+<li>Marking is test-then-CAS on <code>mark</code>, followed by relaxed+line-mark stores.</li>+<li>If the object lies in an evac-source block and is not pinned: CAS+the header to <code>BUSY</code>, copy into the thread’s evac block,+release-store the forwarding word; losers spin and follow it.</li>+<li>Every visited slot is rewritten in place with the new address.</li>+</ul></li>+<li><strong>Sweep:</strong> parallel over blocks, touching only the+line-mark table (0.8% of heap). Count live lines with SIMD, reclassify+the block, splice into per-thread lists, then merge. No object is+touched. The epoch increments instead of clearing marks (clear only on+wrap at 255).</li>+<li><strong>LOS sweep</strong>, and release of surplus blocks+(<code>MADV_FREE</code> after a lag).</li>+<li><strong>Resize:</strong> target heap = max(64 MiB, live × 1.5),+tunable.</li>+</ol>+<p>A collection is triggered when free + recyclable blocks are exhausted+and the growth budget is spent.</p>+<h2 id="generational-mode-sticky-immix-v3">6. Generational mode (sticky+Immix, v3)</h2>+<ul>+<li><strong>Minor GC:</strong> do not bump the epoch. Trace from roots+and stop at any marked object. Survivors stay marked (old) and unmarked+lines are reusable. No remembered set, no barrier.</li>+<li><strong>Full GC:</strong> bump the epoch, which implicitly unmarks+everything, and run the normal cycle with evacuation.</li>+<li>Minor GCs never evacuate. Trigger a full GC when old-generation+growth exceeds a threshold or fragmentation does.</li>+</ul>+<h2 id="llvm-integration">7. LLVM integration</h2>+<ul>+<li><strong>Managed pointers</strong> are <code>ptr addrspace(1)</code>.+Functions carry <code>gc "statepoint-example"</code>, and the+<code>RewriteStatepointsForGC</code> pass converts calls to+<code>gc.statepoint</code> with <code>gc.relocate</code> /+<code>gc.result</code>. Derived pointers are handled by LLVM as (base,+derived) pairs.</li>+<li><strong>Safepoint polls:</strong> the frontend inserts them at loop+back-edges and on entry to recursive functions:+<code>if (tls-&gt;poll) haze_gc_poll()</code>. A guard-page poll is a+later optimization.</li>+<li><strong>Stack maps:</strong> parse <code>.llvm_stackmaps</code> (v3)+once at startup into an open-addressing hash from return address to+record. Per statepoint record: skip the 3 leading constants and the+deopt operands, then read the (base, derived) location pairs. After+relocation, rewrite+<code>derived = new_base + (derived - old_base)</code>.</li>+<li><strong>Roots are stack slots only.</strong> Keep the default of no+GC pointers in callee-saved registers+(<code>fixup-statepoint-caller-saved</code>), so every root is a spill+slot.</li>+<li><strong>Stack walking:</strong> frame-pointer chain+(<code>"frame-pointer"="all"</code> on every function). The caller’s SP+at a call site is <code>callee_FP + 16</code>; resolve+<code>Indirect [SP + off]</code> locations from that.</li>+<li><strong>Allocation calls:</strong> declare+<code>@haze_alloc_slow</code> with+<code>allockind("alloc,zeroed")</code>, <code>allocsize(0)</code> and+<code>noalias</code> return, so LLVM can delete dead allocations and+forward stores. Do escape analysis in the frontend and stack-allocate+non-escaping values.</li>+<li><strong>Runtime entry points that can GC</strong>+(<code>alloc_slow</code>, <code>poll</code>) are ordinary statepoint+calls. On entry they record the caller’s FP and return address in+<code>Mutator</code>, so the collector can start walking.</li>+<li><strong>Dev builds:</strong> LLVM is linked dynamically into the+<em>compiler only</em>. The runtime is a static <code>libhazert.a</code>+built without LLVM headers, and it parses the stack-map format+itself.</li>+<li><strong>Pin the LLVM version.</strong> The statepoint intrinsics are+still documented as experimental.</li>+</ul>+<h2 id="runtime-abi">8. Runtime ABI</h2>+<table>+<thead>+<tr class="header">+<th>Symbol</th>+<th>Purpose</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td><code>haze_alloc_slow(size, type_id)</code></td>+<td>slow path allocation, may GC</td>+</tr>+<tr class="even">+<td><code>haze_alloc_large(size, type_id)</code></td>+<td>LOS allocation</td>+</tr>+<tr class="odd">+<td><code>haze_gc_poll()</code></td>+<td>safepoint slow path</td>+</tr>+<tr class="even">+<td><code>haze_thread_attach/detach()</code></td>+<td>register a mutator</td>+</tr>+<tr class="odd">+<td><code>haze_native_enter/exit()</code></td>+<td>publish frame, set <code>IN_NATIVE</code></td>+</tr>+<tr class="even">+<td><code>haze_pin(obj)</code> / <code>haze_unpin(obj)</code></td>+<td>set <code>PINNED</code>, for FFI</td>+</tr>+<tr class="odd">+<td><code>haze_root_add/remove(slot*)</code></td>+<td>global roots</td>+</tr>+<tr class="even">+<td><code>haze_wb(obj)</code></td>+<td>only if mutable cells exist</td>+</tr>+</tbody>+</table>+<h2 id="c23-coding-rules">9. C++23 coding rules</h2>+<ul>+<li><code>-fno-exceptions -fno-rtti</code>, no libstdc++ allocation on+any GC path; the runtime owns its memory via <code>mmap</code>.</li>+<li><code>constexpr</code> for all layout constants. Use+<code>std::countr_zero</code> and <code>std::bit_ceil</code> for line+and block math.</li>+<li><code>[[likely]]</code>/<code>[[unlikely]]</code> and+<code>[[assume]]</code> (alignment) on hot paths;+<code>std::unreachable()</code> in impossible states.</li>+<li><code>std::expected</code> for OS failures;+<code>std::start_lifetime_as</code> for header and side-table views+where the toolchain supports it.</li>+<li><code>alignas(std::hardware_destructive_interference_size)</code> on+per-thread state and deques.</li>+<li>SIMD behind one small abstraction with AVX2, NEON, and scalar paths;+verify all three agree in tests.</li>+</ul>+<h2 id="verification-and-milestones">10. Verification and+milestones</h2>+<p><strong>Debug modes:</strong> <code>HAZE_GC_STRESS=n</code> (collect+every n allocations), poison freed lines (<code>0xDD</code>) and+ASan-poison them, a heap verifier run before and after every GC (every+reachable reference points into a live line, no dangling forwards, line+marks cover all live objects), and a deterministic seed for+work-stealing order.</p>+<table>+<colgroup>+<col style="width: 50%" />+<col style="width: 50%" />+</colgroup>+<thead>+<tr class="header">+<th>Milestone</th>+<th>Scope</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td>M0</td>+<td>Single-threaded, non-moving, full GC, statepoints and stack maps+working</td>+</tr>+<tr class="even">+<td>M1</td>+<td>Parallel marking and sweep, SIMD hole finding</td>+</tr>+<tr class="odd">+<td>M2</td>+<td>Opportunistic evacuation, pinning</td>+</tr>+<tr class="even">+<td>M3</td>+<td>Sticky minor GCs, no barrier</td>+</tr>+<tr class="odd">+<td>M4</td>+<td>Tuning: huge pages, prefetch depth, NUMA-aware block caches</td>+</tr>+</tbody>+</table>+<ul>+<li>Pin those objects.</li>+<li>Use registered handles.</li>+<li>Leave the native region before collection proceeds.</li>+</ul>+<p>A collector must never move an object while native code may still be+dereferencing its old address.</p>+<h3 id="evacuation-set-selection">5.2 Evacuation-set selection</h3>+<p>Evacuation is opportunistic.</p>+<p>The collector examines fragmentation statistics from the previous+cycle.</p>+<p>Candidate blocks are ranked by:</p>+<ol type="1">+<li>Lowest live-line ratio.</li>+<li>Highest number of reusable holes.</li>+<li>Lowest evacuation cost.</li>+<li>Lowest number of pinned objects.</li>+</ol>+<p>Blocks containing pinned objects may be excluded.</p>+<p>The collector selects blocks until their live data fits into the+available evacuation target space.</p>+<p>Default target reserve:</p>+<ul>+<li>At least 2% of the heap.</li>+<li>At least eight blocks.</li>+</ul>+<p>Evacuation must be disabled when the available target capacity is+insufficient.</p>+<h3 id="root-discovery">5.3 Root discovery</h3>+<p>Roots are obtained from:</p>+<ol type="1">+<li>Compiler-generated shadow-stack frames.</li>+<li>Global root registrations.</li>+<li>Registered native handles.</li>+<li>Pinned object registrations.</li>+<li>Runtime-owned roots.</li>+</ol>+<p>No arbitrary register scanning is performed.</p>+<p>No arbitrary machine-stack scanning is performed.</p>+<p>The collector does not need to understand the machine register+allocator.</p>+<h3 id="parallel-marking">5.4 Parallel marking</h3>+<p>Each collector worker owns a mark stack.</p>+<p>Stacks use 1K-entry chunks.</p>+<p>Workers use a Chase-Lev work-stealing deque.</p>+<p>An eight-entry prefetch FIFO may be used between popping a reference+and scanning its object.</p>+<p>The marking procedure:</p>+<ol type="1">+<li>Validate the object pointer.</li>+<li>Check its header.</li>+<li>Determine whether it has already been marked.</li>+<li>Atomically claim it if necessary.</li>+<li>Mark all intersecting lines.</li>+<li>Scan its pointer fields.</li>+<li>Push discovered references onto the worker’s mark stack.</li>+</ol>+<p>Object marking uses an atomic test-and-set or compare-and-exchange+operation.</p>+<p>Line metadata updates must be atomic or otherwise synchronized+between workers.</p>+<h3 id="evacuation">5.5 Evacuation</h3>+<p>When an object belongs to an evacuation-source block:</p>+<ol type="1">+<li>Check whether it is pinned.</li>+<li>Atomically transition its header to <code>BUSY</code>.</li>+<li>Allocate destination storage.</li>+<li>Copy the object.</li>+<li>Publish the forwarding pointer with release semantics.</li>+<li>Return the destination address.</li>+</ol>+<p>Other workers encountering <code>BUSY</code> wait until forwarding is+published.</p>+<p>Workers encountering a forwarded object follow its forwarding+pointer.</p>+<p>Objects outside evacuation-source blocks are marked in place.</p>+<p>Every scanned reference slot is rewritten to the resolved+address.</p>+<p>Root slots are also rewritten.</p>+<p>The collector must ensure that each object is scanned exactly once+after its final address has been resolved.</p>+<h3 id="sweeping">5.6 Sweeping</h3>+<p>Sweeping runs in parallel after marking and evacuation complete.</p>+<p>For each ordinary block:</p>+<ol type="1">+<li>Count live lines.</li>+<li>Identify contiguous dead-line regions.</li>+<li>Calculate the remaining hole count.</li>+<li>Update block state.</li>+<li>Reclassify the block.</li>+<li>Add reusable blocks or holes to the appropriate allocation+lists.</li>+</ol>+<p>Sweeping primarily operates on side metadata.</p>+<p>Object headers are not individually inspected during ordinary+sweeping unless required for verification or block-specific+processing.</p>+<p>Partially live lines are retained.</p>+<p>This is an intentional property of line-granularity reclamation.</p>+<h3 id="epoch-management">5.7 Epoch management</h3>+<p>The collector increments the major collection epoch after a+successful full collection.</p>+<p>An epoch wrap requires clearing the affected metadata before reusing+the epoch value.</p>+<p>The implementation must ensure that epoch reuse cannot cause stale+marks to be interpreted as current marks.</p>+<h3 id="los-sweeping">5.8 LOS sweeping</h3>+<p>LOS allocations are swept independently.</p>+<p>Unmarked objects are released.</p>+<p>Marked objects remain in place.</p>+<h3 id="heap-resizing">5.9 Heap resizing</h3>+<p>The target heap size is:</p>+<p><code>max(64 MiB, live_bytes * 1.5)</code></p>+<p>The multiplier is configurable.</p>+<p>The collector may retain surplus committed blocks for subsequent+allocations.</p>+<p>Unused pages are released after a configurable retention period.</p>+<hr />+<h2 id="generational-mode-sticky-immix-v3-1">6. Generational mode+(sticky Immix, v3)</h2>+<p>Generational collection is an extension of the primary Immix+collector.</p>+<p>It relies on the immutable-object construction rule.</p>+<h3 id="generational-allocation">6.1 Generational allocation</h3>+<p>New objects are allocated into nursery blocks.</p>+<p>Objects in nursery blocks may reference:</p>+<ul>+<li>Earlier nursery objects.</li>+<li>Existing old-generation objects.</li>+</ul>+<p>Old-generation objects cannot reference nursery objects.</p>+<p>Blocks have a generation field.</p>+<p>A block is promoted as a unit.</p>+<p>This avoids introducing mixed-generation object tracking into the+line allocator.</p>+<h3 id="minor-collection">6.2 Minor collection</h3>+<p>Minor collection:</p>+<ol type="1">+<li>Stops mutators.</li>+<li>Enumerates roots.</li>+<li>Traces nursery references.</li>+<li>Stops traversal at old-generation objects.</li>+<li>Marks live nursery objects.</li>+<li>Reclaims dead nursery lines.</li>+<li>Promotes surviving nursery blocks.</li>+<li>Reinitializes nursery allocation state.</li>+</ol>+<p>Old-generation objects are not recursively scanned.</p>+<p>No remembered set is needed because old objects cannot reference+nursery objects.</p>+<p>Minor collections do not evacuate objects.</p>+<h3 id="promotion">6.3 Promotion</h3>+<p>Promotion happens at block granularity.</p>+<p>If a nursery block contains surviving objects, the entire block+becomes old-generation storage.</p>+<p>Unmarked lines can be reused according to normal Immix reclamation+rules.</p>+<p>This may retain some dead space when live and dead objects share+lines.</p>+<p>That trade-off is intentional.</p>+<h3 id="full-collection">6.4 Full collection</h3>+<p>Full collection:</p>+<ul>+<li>Advances the major epoch.</li>+<li>Traces all reachable objects.</li>+<li>Reclaims both generations.</li>+<li>Performs normal evacuation.</li>+<li>Promotes surviving objects into the old generation.</li>+</ul>+<h3 id="collection-triggers">6.5 Collection triggers</h3>+<p>A full collection is triggered when:</p>+<ul>+<li>Old-generation growth exceeds a configured threshold.</li>+<li>Fragmentation exceeds a threshold.</li>+<li>The heap growth budget is exhausted.</li>+<li>A configurable number of minor collections have elapsed.</li>+</ul>+<p>Generational mode is optional and should not complicate the initial+implementation.</p>+<hr />+<h2 id="uqbe-integration">7. UQBE integration</h2>+<p>This replaces the entire LLVM integration section.</p>+<p>UQBE is responsible for generating machine code and ordinary calls+into the Haze runtime.</p>+<p>The collector does not depend on LLVM.</p>+<h3 id="backend-model">7.1 Backend model</h3>+<p>The compilation pipeline is:</p>+<pre class="text"><code>Haze source+    |+    v+Haze frontend+    |+    v+Haze IR+    |+    +---- Type descriptors+    |+    +---- Managed-pointer liveness+    |+    +---- Safepoint identification+    |+    +---- Root-frame generation+    |+    v+UQBE IL+    |+    v+UQBE backend+    |+    v+Native object code+    |+    v+Haze linker+    |+    v+Executable + Haze runtime</code></pre>+<p>UQBE continues to handle:</p>+<ul>+<li>SSA lowering.</li>+<li>Register allocation.</li>+<li>Instruction selection.</li>+<li>Calling-convention lowering.</li>+<li>Stack-frame generation.</li>+<li>Native code emission.</li>+</ul>+<p>The frontend handles GC-specific correctness.</p>+<h3 id="no-special-gc-instructions">7.2 No special GC instructions</h3>+<p>The initial implementation requires no GC-specific UQBE IL+instructions.</p>+<p>All collector operations are represented through ordinary function+calls and stack-slot operations.</p>+<p>This is deliberate.</p>+<p>QBE’s existing scalar IL is sufficient for the required+integration.</p>+<p>Architecture-specific optimizations remain inside UQBE or the C+++runtime.</p>+<h3 id="shadow-stack-root-frames">7.3 Shadow-stack root frames</h3>+<p>Instead of parsing LLVM stack maps or attempting to reconstruct roots+from arbitrary machine instructions, Haze uses explicit shadow-stack+frames.</p>+<p>Each managed function that can participate in collection receives a+compiler-generated root frame.</p>+<p>Conceptually:</p>+<div class="sourceCode" id="cb12"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> RootFrame <span class="op">{</span></span>+<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>    RootFrame<span class="op">*</span> previous<span class="op">;</span></span>+<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">const</span> RootMap<span class="op">*</span> descriptor<span class="op">;</span></span>+<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">**</span> <span class="ex">slots</span><span class="op">;</span></span>+<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>+<p>The descriptor identifies the managed-reference slots.</p>+<p>The frame is linked into the current mutator’s root chain.</p>+<p>On function entry:</p>+<div class="sourceCode" id="cb13"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>haze_root_enter<span class="op">(&amp;</span>frame<span class="op">);</span></span></code></pre></div>+<p>On function exit:</p>+<div class="sourceCode" id="cb14"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>haze_root_leave<span class="op">(&amp;</span>frame<span class="op">);</span></span></code></pre></div>+<p>The runtime maintains the root-frame chain in thread-local mutator+state.</p>+<p>The frame must remain valid for the complete duration of its+registration.</p>+<h3 id="root-slots">7.4 Root slots</h3>+<p>Every managed reference that must survive a safepoint has an+associated root slot.</p>+<p>The compiler emits stores into those slots before a potentially+collecting call.</p>+<p>After the call, relocated references are reloaded into their+corresponding SSA values.</p>+<p>Conceptually:</p>+<pre class="text"><code>store managed_pointer, root_slot++call haze_alloc_slow++managed_pointer = load root_slot</code></pre>+<p>The runtime updates the root slots during evacuation.</p>+<p>This guarantees that register allocation cannot accidentally hide a+managed pointer from the collector.</p>+<p>The compiler must calculate liveness at each safepoint and ensure+every live managed reference is represented.</p>+<p>Dead references need not be published.</p>+<h3 id="root-map-descriptors">7.5 Root-map descriptors</h3>+<p>Root maps are generated by the Haze frontend.</p>+<p>A descriptor contains:</p>+<ul>+<li>Number of root slots.</li>+<li>Slot offsets or indices.</li>+<li>Managed-reference type information where required.</li>+<li>Frame size.</li>+<li>Optional debugging metadata.</li>+</ul>+<p>A static descriptor is emitted into the executable’s read-only data+section.</p>+<p>The runtime receives a pointer to this descriptor when entering a+root frame.</p>+<p>There is no runtime stack-map parser.</p>+<p>There is no return-address hash table.</p>+<p>There is no LLVM <code>.llvm_stackmaps</code> section.</p>+<h3 id="uqbe-stack-slots">7.6 UQBE stack slots</h3>+<p>Root slots are represented using ordinary UQBE stack allocation.</p>+<p>For example, conceptually:</p>+<pre class="text"><code>%root =l alloc8 8+storel %object, %root</code></pre>+<p>The frontend must ensure that root slots are not optimized away or+invalidated before a collection.</p>+<p>This requires treating the root-frame registration and root-slot+accesses as observable memory operations.</p>+<p>If necessary, the frontend should introduce explicit runtime calls+around safepoints to prevent invalid compiler transformations.</p>+<p>The UQBE optimizer must preserve the required memory+dependencies.</p>+<h3 id="safepoint-polls">7.7 Safepoint polls</h3>+<p>The frontend inserts ordinary calls to:</p>+<div class="sourceCode" id="cb17"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>haze_gc_poll<span class="op">()</span></span></code></pre></div>+<p>Polls are required at:</p>+<ul>+<li>Loop back-edges.</li>+<li>Recursive function entry.</li>+<li>Potentially unbounded execution paths.</li>+<li>Allocation slow paths.</li>+<li>Explicit runtime safepoints.</li>+</ul>+<p>The fast path reads a thread-local poll flag and returns immediately+when no collection is requested.</p>+<p>A guard-page implementation may be introduced later.</p>+<h3 id="calls-that-may-collect">7.8 Calls that may collect</h3>+<p>Every function that may trigger collection must be identified by the+frontend.</p>+<p>The compiler must distinguish:</p>+<ul>+<li>Functions that cannot collect.</li>+<li>Functions that may collect.</li>+<li>Functions that enter native code.</li>+<li>Functions that allocate managed objects.</li>+</ul>+<p>A function that calls another potentially collecting function must+preserve all live managed references across that call.</p>+<p>This is ordinary interprocedural effect propagation.</p>+<h3 id="allocation-lowering">7.9 Allocation lowering</h3>+<p>Managed allocations use two paths.</p>+<p><strong>Inline allocation</strong></p>+<p>The frontend emits direct cursor/limit operations against the current+mutator.</p>+<p>The generated code initializes the object header and returns the+managed pointer.</p>+<p><strong>Slow allocation</strong></p>+<p>The frontend emits an ordinary call:</p>+<div class="sourceCode" id="cb18"><pre+class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="at">extern</span> <span class="st">&quot;C&quot;</span> <span class="dt">void</span><span class="op">*</span> haze_alloc_slow<span class="op">(</span></span>+<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>    u32 size<span class="op">,</span></span>+<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>    u32 type_id</span>+<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="op">);</span></span></code></pre></div>+<p>The function may collect.</p>+<p>All live managed references must be published before entering it.</p>+<p>Unlike LLVM, UQBE does not need allocation-specific attributes or+<code>allockind</code> metadata.</p>+<p>Escape analysis remains a frontend optimization.</p>+<p>Non-escaping objects may be stack-allocated when their lifetimes and+semantics permit it.</p>+<h3 id="derived-pointers">7.10 Derived pointers</h3>+<p>Managed references stored in the heap and shadow stack are always+object-base pointers.</p>+<p>Interior pointers are not permitted to survive safepoints.</p>+<p>If the compiler generates a temporary derived pointer into an object+payload, it must either:</p>+<ul>+<li>Recompute it after the safepoint using the relocated base+pointer.</li>+<li>Preserve the base pointer and byte offset, then reconstruct the+derived pointer.</li>+</ul>+<p>The collector never needs to relocate arbitrary interior+pointers.</p>+<h3 id="native-abi">7.11 Native ABI</h3>+<p>The runtime is compiled independently from UQBE.</p>+<p>The frontend and runtime agree on the platform’s C ABI.</p>+<p>UQBE is responsible for selecting the correct target calling+convention.</p>+<p>The runtime must support the calling conventions implemented by the+corresponding UQBE target, including:</p>+<ul>+<li>System V AMD64.</li>+<li>Microsoft x64.</li>+<li>AArch64 platform ABIs.</li>+</ul>+<p>The collector must not depend on a particular register assignment or+machine stack layout.</p>+<h3 id="object-files-and-linking">7.12 Object files and linking</h3>+<p>The collector runtime is a static library:</p>+<p><code>libhazert.a</code></p>+<p>The executable links against it using the normal Haze linking+pipeline.</p>+<p>The runtime does not link against UQBE.</p>+<p>UQBE does not link against the collector.</p>+<p>This preserves the separation between compiler, backend, and+runtime.</p>+<hr />+<h2 id="runtime-abi-1">8. Runtime ABI</h2>+<p>The runtime exposes a small C-compatible interface.</p>+<table>+<thead>+<tr class="header">+<th>Symbol</th>+<th>Purpose</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td><code>haze_alloc_slow(size, type_id)</code></td>+<td>Slow managed allocation</td>+</tr>+<tr class="even">+<td><code>haze_alloc_large(size, type_id)</code></td>+<td>LOS allocation</td>+</tr>+<tr class="odd">+<td><code>haze_gc_poll()</code></td>+<td>Safepoint poll</td>+</tr>+<tr class="even">+<td><code>haze_gc_collect()</code></td>+<td>Explicit collection request</td>+</tr>+<tr class="odd">+<td><code>haze_thread_attach()</code></td>+<td>Register a mutator</td>+</tr>+<tr class="even">+<td><code>haze_thread_detach()</code></td>+<td>Unregister a mutator</td>+</tr>+<tr class="odd">+<td><code>haze_root_enter(frame)</code></td>+<td>Register a shadow-stack frame</td>+</tr>+<tr class="even">+<td><code>haze_root_leave(frame)</code></td>+<td>Unregister a shadow-stack frame</td>+</tr>+<tr class="odd">+<td><code>haze_root_add(slot)</code></td>+<td>Register a global root</td>+</tr>+<tr class="even">+<td><code>haze_root_remove(slot)</code></td>+<td>Remove a global root</td>+</tr>+<tr class="odd">+<td><code>haze_native_enter()</code></td>+<td>Enter a GC-safe native region</td>+</tr>+<tr class="even">+<td><code>haze_native_exit()</code></td>+<td>Leave a GC-safe native region</td>+</tr>+<tr class="odd">+<td><code>haze_pin(obj)</code></td>+<td>Pin a managed object</td>+</tr>+<tr class="even">+<td><code>haze_unpin(obj)</code></td>+<td>Unpin a managed object</td>+</tr>+<tr class="odd">+<td><code>haze_wb(obj, slot, value)</code></td>+<td>Future mutable-object barrier</td>+</tr>+</tbody>+</table>+<p>All public entry points use C linkage.</p>+<p>The runtime must document which functions may trigger collection.</p>+<hr />+<h2 id="c23-runtime-implementation">9. C++23 runtime implementation</h2>+<h3 id="compilation">9.1 Compilation</h3>+<p>The runtime is built independently from the compiler.</p>+<p>Recommended flags:</p>+<pre class="text"><code>-std=c++23+-fno-exceptions+-fno-rtti</code></pre>+<p>No LLVM headers or libraries are required.</p>+<p>The runtime must not use general-purpose standard-library allocation+on collector-critical paths.</p>+<p>Memory ownership is handled through platform virtual-memory+primitives.</p>+<h3 id="memory-management">9.2 Memory management</h3>+<p>The runtime uses:</p>+<ul>+<li><code>mmap</code> / <code>munmap</code> on Linux.</li>+<li><code>VirtualAlloc</code> / <code>VirtualFree</code> on+Windows.</li>+<li><code>mmap</code> / <code>munmap</code> on macOS.</li>+</ul>+<p>Platform-specific functionality is isolated behind a small memory+abstraction.</p>+<p>OS failures are reported through an explicit error mechanism.</p>+<p><code>std::expected</code> may be used for non-critical+initialization and configuration paths.</p>+<h3 id="layout">9.3 Layout</h3>+<p>All layout constants are <code>constexpr</code>.</p>+<p>Use:</p>+<ul>+<li><code>std::countr_zero</code> for bit operations.</li>+<li><code>std::bit_ceil</code> for capacity calculations.</li>+<li><code>std::memcpy</code> for object copying.</li>+<li>Explicit atomic operations for concurrent metadata.</li>+<li><code>alignas(std::hardware_destructive_interference_size)</code>+for per-thread state.</li>+</ul>+<p>Use <code>std::start_lifetime_as</code> where supported and+appropriate.</p>+<p>Do not assume that C++ object lifetime rules automatically permit+arbitrary typed access to reserved virtual memory.</p>+<h3 id="simd">9.4 SIMD</h3>+<p>SIMD is implemented inside the C++ runtime.</p>+<p>The initial abstraction supports:</p>+<ul>+<li>AVX2.</li>+<li>NEON.</li>+<li>Scalar fallback.</li>+</ul>+<p>SIMD is used for:</p>+<ul>+<li>Line metadata scanning.</li>+<li>Hole discovery.</li>+<li>Zeroing.</li>+<li>Live-line counting.</li>+<li>Selected bitmap operations.</li>+</ul>+<p>UQBE does not need first-class vector instructions for the initial+collector.</p>+<p>Architecture-specific SIMD code must be tested against the scalar+implementation.</p>+<h3 id="concurrency">9.5 Concurrency</h3>+<p>The collector uses:</p>+<ul>+<li>Atomic block ownership.</li>+<li>Atomic object-marking state.</li>+<li>Atomic forwarding publication.</li>+<li>Parallel work-stealing queues.</li>+<li>Stop-the-world synchronization.</li>+</ul>+<p>The implementation must avoid ABA problems in global block lists.</p>+<p>Thread-local allocation state must be isolated to avoid false+sharing.</p>+<hr />+<h2 id="verification-and-testing">10. Verification and testing</h2>+<h3 id="debug-modes">10.1 Debug modes</h3>+<p>Environment variables:</p>+<pre class="text"><code>HAZE_GC_STRESS=1+HAZE_GC_VERIFY=1+HAZE_GC_POISON=1+HAZE_GC_LOG=1+HAZE_GC_DISABLE_EVAC=1+HAZE_GC_DISABLE_GENERATIONAL=1</code></pre>+<p><code>HAZE_GC_STRESS</code> forces collection after a configurable+number of allocations.</p>+<p><code>HAZE_GC_VERIFY</code> enables heap verification before and+after collection.</p>+<p><code>HAZE_GC_POISON</code> poisons reclaimed memory.</p>+<p><code>HAZE_GC_LOG</code> prints collection statistics.</p>+<h3 id="heap-verification">10.2 Heap verification</h3>+<p>The verifier checks:</p>+<ul>+<li>Every reachable reference points to a valid managed object.</li>+<li>Every reachable object has a valid type descriptor.</li>+<li>Every live object intersects appropriately marked lines.</li>+<li>No object contains an invalid managed reference.</li>+<li>No forwarded object contains an invalid forwarding address.</li>+<li>No two mutators own the same allocation block.</li>+<li>No live pinned object has moved.</li>+<li>No evacuated source block is released while references to it+remain.</li>+<li>No root frame references invalid stack storage.</li>+</ul>+<h3 id="stress-testing">10.3 Stress testing</h3>+<p>Tests must cover:</p>+<ul>+<li>Allocation without collection.</li>+<li>Repeated full collections.</li>+<li>Collection during recursive execution.</li>+<li>Collection during nested function calls.</li>+<li>Objects containing references.</li>+<li>Arrays of references.</li>+<li>Objects spanning multiple lines.</li>+<li>Medium and large allocations.</li>+<li>Fragmented heaps.</li>+<li>Multiple mutators.</li>+<li>Concurrent marking workers.</li>+<li>Evacuation races.</li>+<li>Pinning and unpinning.</li>+<li>Native calls.</li>+<li>Thread attachment and detachment.</li>+<li>Heap exhaustion.</li>+<li>Epoch wraparound.</li>+<li>Generational promotion.</li>+<li>Dead objects sharing lines with live objects.</li>+</ul>+<h3 id="sanitizers">10.4 Sanitizers</h3>+<p>Development builds should support:</p>+<ul>+<li>AddressSanitizer.</li>+<li>UndefinedBehaviorSanitizer.</li>+<li>ThreadSanitizer.</li>+</ul>+<p>ASan poisoning must be coordinated with the allocator and+collector.</p>+<p>TSan builds should exercise concurrent marking, forwarding, and block+ownership.</p>+<h3 id="determinism">10.5 Determinism</h3>+<p>A configurable deterministic seed controls work-stealing order and+stress-test scheduling.</p>+<p>This allows GC failures to be reproduced more reliably.</p>+<hr />+<h2 id="performance-targets">11. Performance targets</h2>+<p>These are engineering targets, not guaranteed performance claims.</p>+<table>+<thead>+<tr class="header">+<th>Metric</th>+<th>Target</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td>Inline allocation</td>+<td>Approximately 6 instructions</td>+</tr>+<tr class="even">+<td>Small-object allocation</td>+<td>No locks</td>+</tr>+<tr class="odd">+<td>Fast safepoint poll</td>+<td>One predictable branch</td>+</tr>+<tr class="even">+<td>Side metadata</td>+<td>Approximately 0.83% of reserved heap</td>+</tr>+<tr class="odd">+<td>Marking</td>+<td>Parallel</td>+</tr>+<tr class="even">+<td>Sweeping</td>+<td>Parallel</td>+</tr>+<tr class="odd">+<td>Evacuation</td>+<td>Opportunistic</td>+</tr>+<tr class="even">+<td>Write barriers</td>+<td>None for immutable objects</td>+</tr>+<tr class="odd">+<td>Runtime LLVM dependency</td>+<td>None</td>+</tr>+<tr class="even">+<td>Stack-map parsing</td>+<td>None</td>+</tr>+<tr class="odd">+<td>Register scanning</td>+<td>None</td>+</tr>+</tbody>+</table>+<p>The most important optimization priority is maintaining a cheap+allocation path.</p>+<p>Collection throughput is secondary to avoiding unnecessary overhead+on ordinary object creation.</p>+<hr />+<h2 id="implementation-milestones">12. Implementation milestones</h2>+<table>+<thead>+<tr class="header">+<th>Milestone</th>+<th>Scope</th>+</tr>+</thead>+<tbody>+<tr class="odd">+<td>M0</td>+<td>Single-threaded, non-moving full Immix collector</td>+</tr>+<tr class="even">+<td>M1</td>+<td>UQBE shadow-stack integration and compiler-generated root maps</td>+</tr>+<tr class="odd">+<td>M2</td>+<td>Safepoint rendezvous and multi-threaded allocation</td>+</tr>+<tr class="even">+<td>M3</td>+<td>Parallel marking and sweeping</td>+</tr>+<tr class="odd">+<td>M4</td>+<td>Opportunistic evacuation and forwarding</td>+</tr>+<tr class="even">+<td>M5</td>+<td>Pinning and native interoperation</td>+</tr>+<tr class="odd">+<td>M6</td>+<td>Sticky generational collection</td>+</tr>+<tr class="even">+<td>M7</td>+<td>Huge pages, prefetching, SIMD tuning, NUMA-aware block caches</td>+</tr>+</tbody>+</table>+<h3 id="m0-basic-collector">M0 — Basic collector</h3>+<p>Implement the heap reservation, block allocator, type descriptors,+marking, and sweeping.</p>+<p>No evacuation or generational mode.</p>+<h3 id="m1-uqbe-integration">M1 — UQBE integration</h3>+<p>Implement root-frame generation in the Haze frontend.</p>+<p>Generate ordinary UQBE stack-slot operations and calls to the+runtime.</p>+<p>Verify that a managed reference survives a collection across nested+function calls.</p>+<p>This milestone establishes the compiler/runtime contract.</p>+<h3 id="m2-multithreading">M2 — Multithreading</h3>+<p>Implement mutator registration, safepoint rendezvous, thread-local+allocation, and concurrent collector workers.</p>+<h3 id="m3-parallel-collection">M3 — Parallel collection</h3>+<p>Introduce work stealing, parallel tracing, parallel sweeping, and+SIMD line discovery.</p>+<h3 id="m4-evacuation">M4 — Evacuation</h3>+<p>Implement forwarding, destination allocation, reference rewriting,+and fragmentation-driven evacuation.</p>+<h3 id="m5-native-interoperation">M5 — Native interoperation</h3>+<p>Implement pinning, native handles, native-region registration, and+safe transitions between managed and native execution.</p>+<h3 id="m6-generational-collection">M6 — Generational collection</h3>+<p>Introduce nursery blocks, sticky marking, block promotion, and minor+collection.</p>+<h3 id="m7-tuning">M7 — Tuning</h3>+<p>Optimize memory commitment, huge-page usage, NUMA placement,+prefetching, and allocator locality.</p>+<hr />+<h2 id="final-architectural-constraints">13. Final architectural+constraints</h2>+<p>The following properties are mandatory:</p>+<ol type="1">+<li>The runtime must not depend on LLVM.</li>+<li>UQBE must not require LLVM-style statepoint support.</li>+<li>All managed roots must be explicitly published.</li>+<li>All heap references must be precise object-base pointers.</li>+<li>No managed pointer may remain hidden in an untracked register across+a safepoint.</li>+<li>No object may be moved while native code can still dereference its+old address.</li>+<li>No old-generation object may acquire a reference to a nursery+object.</li>+<li>Allocation blocks must have exclusive mutator ownership.</li>+<li>Evacuation must never release a source block before all references+have been rewritten.</li>+<li>The normal allocation path must remain independent of collector+synchronization.</li>+</ol>+<p><strong>The collector is a runtime subsystem, not a compiler backend+feature. UQBE only needs to generate the correct allocation operations,+root-frame accesses, and safepoint calls.</strong></p>