---
title: "Provenance"
description: "Source identity through HIR and the core IR — diagnostics, traces, and debug information."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Provenance

Provenance answers two related questions:

1. where in source did this compiler object come from?
2. why does this generated object exist when there is no direct source node?

Those questions power diagnostics, IR dumps, trace stories, source navigation,
line tables, breakpoints, and variable inspection. A byte range alone cannot
answer both, so the compiler preserves identity and classification as separate
facts.

## Chain

The ordinary source chain is:

```text
file + AST node
    ↓ semantic lowering
HIR node + Origin
    ↓ lower_hir
core IR op/terminator → HIR node and source range
    ↓ encoding
function-body byte offset → source/debug record
```

The arrows are many-to-many in practice. One HIR call can become several core
operations, and folding can remove one completely. Provenance records
causality, not a promise of one output instruction per source expression.

**One hop shorter than it was.** The retired back end had a MIR/WasmIR pair
with a lowering between them, so a range travelled through four
representations rather than three — and each hop was a place the mapping could
be dropped. The chain above is `atoll-codegen`'s; the CLI has not been cut
over to it (`#103`), so the debug data a build emits today still comes from
the retired path.

## Identity and location

Identity is stronger than a range. Two expressions can share a zero-width or
recovery range, and a generated operation can use the range of the surrounding
construct. Conversely, an HIR node ID by itself needs its project/file context
to resolve a range.

Consumers therefore prefer:

1. a retained HIR identity for “which source construct?”;
2. a precise operation or local range for presentation;
3. a block, continuation, function, or declaration range as a fallback;
4. an explicit synthetic classification when no source node applies.

Fallback is presentation behavior. It must not be inserted back into the IR as
false precision.

## HIR origins

Every HIR node uses `Origin::Expr`, `Stmt`, `Pattern`, `Decl`, or
`Synthetic`. A synthetic origin includes both a source range and a closed
reason such as loop desugaring, default argument insertion, string template
expansion, optional-member lowering, generic specialization, or error
recovery.

When per-file HIR is merged, AST-backed origins keep their file context and
every HIR child identity is relocated. A local HIR ID must never be compared
directly with a merged project ID.

Specialization retains the generic declaration relationship separately from
the concrete clone. Tools can present the concrete call site while still
explaining which generic definition produced its body.

## Core-IR provenance

Each core-IR operation and terminator carries the HIR node and source range it
came from, and a synthesis reason when nothing in source produced it.

| Fact | Answers |
| --- | --- |
| operation → source range | Fine-grained diagnostics and line data |
| operation → HIR node | Cross-stage navigation and trace identity |
| terminator → HIR node | Return, branch, suspension, and transfer origins |
| synthesis reason | Why a generated operation has no direct HIR node |

An operation may have both an originating HIR node and a synthetic context.
For "where?" tools prefer HIR; for "why was this primitive introduced?" they
can also show the synthetic category.

**A generated body has no source at all**, and that is the common case rather
than the exception: `atoll-codegen` generates release walkers from the layout
table, environment releases for closures, and the continuation dispatcher.
None of those has a source range to inherit, and a tool that treats a missing
range as "the start of the file" will point at the wrong line for all of them.

Where the previous back end needed two hops of bookkeeping — MIR rewrite
helpers migrating positional keys, then a WasmIR builder re-deriving them
after every compaction — there is now one representation between HIR and
bytes, so a range is carried rather than re-mapped.

## Debug data, and what is not built

Encoding can snapshot a function-body byte offset before an originated
instruction, and module assembly converts body-relative offsets into module
offsets for line and debug sections. **This is the retired back end's
machinery.** `atoll-codegen` does not emit line tables or debug sections yet,
and the debugger's own coverage (`#91`) is written against the shipping
runtime rather than against it. Treat this section as the contract a port owes,
not as a description of what a build produces today.

## Variables

Statement location and source-variable value are separate problems. A line
table can say which source construct is executing without knowing where an
optimized variable lives.

Lowering owns source-variable debug bindings because it is the last point with
both source-variable truth and concrete value identities. A binding states that
a source variable is represented by a core-IR value or a small pure
debug expression from a program point onward.

When optimization destroys a value, it can:

- replace references with a surviving value;
- salvage a bounded expression over constants and surviving values;
- represent aggregate pieces explicitly;
- mark the value unavailable with a reason.

It must not display a guessed stale value. Multi-assigned variables,
unsalvageable calls or loads, and expressions exceeding the bounded debug
vocabulary become honestly unavailable.

This variable-location pipeline is still evolving, and its evidence is on the
retired path: the model and verification hooks were the previous back end's,
nothing in `atoll-codegen` serializes them yet, and the debugger's end-to-end
coverage runs against the shipping runtime rather than the new encoder. Treat
the whole of it as provisional.

## Pass checklist

A transformation that changes program shape must answer:

- Which output nodes retain each input's HIR or source origin?
- Which outputs are synthetic, and what is their category?
- Did positional block/op/instruction keys move?
- Were eliminated source-variable values replaced, salvaged, or marked
  unavailable?
- Can diagnostics still select a user-visible range?
- Does disabling tracing or debug data leave program bytes and behavior
  unchanged?

Tests should cover provenance density for user-authored operations, expected
synthetic gaps, key remapping under rewrites, and release/debug semantic
parity.

See [HIR Nodes](https://docs.atoll.workreef.dev/ir/hir/nodes/) and `crates/atoll-codegen/docs/` for the
representation-specific
contracts.

Source: https://docs.atoll.workreef.dev/compiler/provenance/index.mdx
