Building a TIR Compiler: From Patent to Prototype

A rule graph, one intermediate representation, and three backends. What was easy, what leaked, and which abstraction I would not build again.

Language versions [EN] [中文]
Cite

TIR started as a patent disclosure and became a working compiler over about seven months. The interesting part is not the front end. It is the moment a single intermediate representation has to serve a spreadsheet add-in, a long-running service, and an FPGA pipeline at the same time.

The shape of the thing

A rule graph is a directed acyclic graph of typed nodes: thresholds, windows, joins, arithmetic, and a small set of domain operations. Evaluation order is not left to the backend, which is the decision the rest of the design hangs off.

public abstract record TirNode(string Id, IReadOnlyList<TirType> Inputs, TirType Output);

public sealed record WindowNode(
    string Id,
    string SourceId,
    TimeSpan Length,
    AggregateKind Aggregate) : TirNode(Id, [TirType.Tick], TirType.Real);

public interface ITirBackend
{
    string Name { get; }
    LoweringResult Lower(TirGraph graph);
}

The graph is the contract. Every backend is a lowering pass plus a runtime shim, and nothing is allowed to reach back into the graph to special-case itself.

Backend one: the spreadsheet add-in

The easiest, because the host already owns a dependency graph and a recomputation engine. The lowering pass emits a cell formula per node with named ranges as inputs. Total effort: eleven days including the packaging.

The lesson was uncomfortable. The easiest backend was easy because I let it dictate evaluation semantics: the host recomputes on a schedule I do not control. That is fine for a spreadsheet and wrong for the other two, so the graph had to grow an explicit evaluation policy.

Backend two: the service

Straightforward, and the place where the abstraction paid off. Nodes lower to a closure tree, the graph is shared across requests, and incremental evaluation falls out of caching per node against an input-version stamp.

Backend three: the FPGA pipeline

This is where the abstraction leaked. Two hard constraints arrived that the graph could not express: pipelines are fixed width, and node latency is a physical resource.

// Width inference was bolted on as an afterthought and it shows.
var plan = TirGraph.PlanWidth(
    graph,
    preferredWidth: 4,
    maxLatency: TimeSpan.FromMicroseconds(320),
    spillPolicy: SpillPolicy.RecomputeOnBackend);

Width planning belongs in the graph, not in the backend. Putting it in the backend meant two backends could disagree about a graph’s meaning, which is precisely the failure the intermediate representation exists to prevent.

What I would do differently

  1. Make resource constraints first-class in the graph from day one. Width, latency and memory are properties of the computation, not of the target.
  2. Keep one canonical evaluation policy on the graph, and let backends negotiate rather than decide.
  3. Write the conformance suite before the second backend. I wrote it after the third, and it immediately found four disagreements between backend one and backend two that had been shipping for weeks.

Where it stands

The compiler is in tir-compiler, Apache-2.0, and it is what several of the embedded experiments are lowered through. The width-planning regret is documented in the README rather than quietly fixed, because the next person to add a backend should know where the seam is.

Related reading

Video//1 min read

One Rule Graph, Three Backends: A TIR Compiler Demo

Lowering the same rule graph to a spreadsheet add-in, a service and an FPGA pipeline, and the four disagreements the conformance suite found.

Patent//5 min read

Lowering a Uniform Rule Graph to Heterogeneous Evaluation Targets

The disclosure behind the TIR compiler, including the width and latency negotiation that the first implementation got wrong.

Writing//14 min read

Enterprise AI Agents in .NET 10: Wiring an Agent to Real Systems

What changes when an agent has to call into a system with an audit log, a permission model and a rate limit, instead of a demo endpoint.

Writing//11 min read

Python Fine-tuning with C# Inference: A Dual-Track Pipeline

Fine-tune where the ecosystem is, serve where the process already runs, and put a diff between the two so they cannot silently drift.

Cite this entry

Copy the block you need.

BibTeX
@misc{huang2026buildingatircompil,
  author  = {Mr Huang},
  title   = {Building a TIR Compiler: From Patent to Prototype},
  year    = {2026},
  url     = {https://selfevo.dev/en/blog/tech/building-tir-compiler},
  note    = {selfevo, blog}
}
RIS
TY  - GEN
AU  - Mr Huang
TI  - Building a TIR Compiler: From Patent to Prototype
PY  - 2026
UR  - https://selfevo.dev/en/blog/tech/building-tir-compiler
PB  - selfevo
ER  - 
APA
Mr Huang (2026). Building a TIR Compiler: From Patent to Prototype. selfevo. https://selfevo.dev/en/blog/tech/building-tir-compiler