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
- Make resource constraints first-class in the graph from day one. Width, latency and memory are properties of the computation, not of the target.
- Keep one canonical evaluation policy on the graph, and let backends negotiate rather than decide.
- 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.
Cite this entry
Copy the block you need.
@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}
} 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 - Mr Huang (2026). Building a TIR Compiler: From Patent to Prototype. selfevo. https://selfevo.dev/en/blog/tech/building-tir-compiler