Control-flow flattening and the tiered JIT
Flattening rewrites a method into a `while(true){ switch(state) }` dispatcher, and that reshapes how the .NET tiered JIT treats it: a straight-line method becomes a looping one, so it now goes through On-Stack Replacement, and its grown IL usually drops out of the inliner. The dispatch itself is cheap — a dense-integer jump table at tier-1 — but losing inlining and paying tier-0 for an extra loop is where the real cost hides. This post reads what the JIT does with a flattened body across tier-0, OSR and tier-1, and shows how to scope flattening so the hot, inlinable methods stay untouched.
A common way to reason about control-flow flattening is to measure it once — wrap the method in a benchmark, note “about 8% slower,” and treat that as the price. That number is real but it hides the mechanism, and the mechanism is more interesting than the number, because flattening does not just add instructions. It changes the shape of the method from the JIT’s point of view: a straight-line body becomes a looping one. And in modern .NET, whether a method loops is one of the first things the tiered JIT asks about it.
The real question
Flattening rewrites a method into a dispatcher: the original basic blocks are numbered, and a while(true){ switch(state) } loop runs them in an order chosen by a state variable instead of by fall-through and branches. Everyone knows the dispatch adds a little bookkeeping. The question worth asking is the one a level down: what does the tiered JIT do with a method that suddenly contains a hot loop it did not have before — across tier-0, On-Stack Replacement, and tier-1 — and where does the cost actually land?
What flattening produces
Start with an ordinary method and its flattened form, so the shape change is concrete.
// original — straight-line, no loop
static int Classify(int score)
{
if (score < 0) return -1;
if (score < 50) return 0;
return 1;
}
// flattened — a dispatcher loop over numbered blocks
static int Classify(int score)
{
int state = 0, result = 0;
while (true)
{
switch (state)
{
case 0: state = (score < 0) ? 1 : 2; break;
case 1: result = -1; state = 5; break;
case 2: state = (score < 50) ? 3 : 4; break;
case 3: result = 0; state = 5; break;
case 4: result = 1; state = 5; break;
case 5: return result;
}
}
}
The two compile to the same answers, but the second has a loop and a switch. In IL the switch is a single switch opcode over a jump offset table, fed by the state local:
IL_0012: ldloc.0 // state
IL_0013: switch (IL_0030, IL_0040, IL_0050, IL_0060, IL_0070, IL_0080)
IL_002c: br IL_0012 // loop back to the dispatcher head
Nebula numbers the blocks with small, dense integers on purpose: a dense case set is what lets the optimizing JIT lower this to a jump table rather than a comparison chain. That decision is made at tier-1, which is the first place the tiered JIT enters the story.
Tier-0, OSR, and tier-1
.NET’s JIT is tiered. A method is first compiled at tier-0 — “quick JIT,” minimal optimization, produced fast so startup is cheap. The runtime counts calls, and after a threshold it recompiles the method at tier-1 — the full optimizing JIT — on a background thread, then swaps the entry point. For a method that is only ever called, call-counting is enough.
A method that loops is the problem case. If a method enters a long-running loop on its first call, call-counting never gets a chance to promote it; it would be stuck running slow tier-0 code inside the loop. Before .NET 7 the JIT dodged this by compiling any method with a loop straight at tier-1, skipping tier-0 entirely. Since .NET 7, On-Stack Replacement (OSR) handles it instead: the method starts at tier-0 with a patchpoint planted in the loop, and if the loop iterates enough, the runtime compiles an optimized version and transfers the currently executing invocation onto it mid-loop — replacing the frame on the stack.
This is exactly the machinery flattening wakes up. The original Classify has no loop; it rides the plain tier-0 → tier-1 call-count path. The flattened Classify has a dispatcher loop, so it takes the OSR path: tier-0 with a patchpoint, and an OSR transition if the dispatcher spins long enough on one call.
Two practical consequences fall out of that picture.
The dispatch is cheap once optimized. At tier-1 the dense switch becomes a jump table: one bounds check, one indirect jump per block transition. With dynamic PGO on (the default from .NET 8), the tier-0 instrumentation pass even records which cases run hottest and feeds that back, so the common path through the dispatcher is laid out well. The steady-state cost of the dispatcher is small and constant — this is not where the time goes.
The inliner is where the time goes. The JIT inlines a callee only when it looks cheap: small IL, simple control flow, a favorable size-versus-benefit estimate. A method with an explicit loop and a multiplied-out block count is a poor candidate on every axis — it is larger, and a loop makes it an outright reject for inlining in most cases. So a small helper that the JIT used to inline — erasing the call entirely — becomes a genuine out-of-line call once flattened. For a hot, tiny method, that lost inlining dwarfs the dispatch cost. For a method that was already too big to inline, flattening changes nothing about its call cost; it was always a real call.
That inverts the naive intuition. The methods where flattening is “most expensive” in relative terms are the small, hot helpers — precisely the ones you should not be flattening. The cold, valuable methods you do want to flatten were rarely inline candidates to begin with.
How to scope it in Nebula
The guidance follows directly from the mechanism: flatten what is valuable and cold; leave hot, inlinable methods alone. Nebula’s control-flow transform takes include/exclude scoping and an intensity preset, so you target it rather than flipping one global switch.
<Nebula>
<Assembly file="MyApp.dll">
<Rename enabled="true" />
<StringEncryption enabled="true" />
<ControlFlow enabled="true" intensity="normal">
<!-- Flatten the methods that embody the IP you are protecting. -->
<Include type="MyApp.Licensing.LicenseValidator" />
<Include type="MyApp.Core.PricingEngine" method="ComputeQuote" />
<!-- Keep flattening off the tight, inlinable hot path. -->
<Exclude type="MyApp.Core.Geometry" />
<Exclude type="MyApp.Core.Buffers" member="public" />
</ControlFlow>
</Assembly>
</Nebula>
A license check runs once at startup or once per session; it is never on a hot loop, so its tier-0 iterations and lost inlining cost nothing a user can perceive, and flattening it is pure win. A vector-math helper called a million times in a render loop is the opposite case — exclude it, and let it stay small and inlinable. If you are unsure whether a given method is hot, that is a profiler question, not a guess: flatten, measure the real workload (not a micro-benchmark that calls the method in isolation), and look at where time actually moved.
Caveats and when it breaks
The tiering story has sharp edges worth stating plainly.
A micro-benchmark that disables tiering — or measures only tier-0, or only a cold first call — will mislead you in both directions. Tier-0 code is unoptimized for everyone, so flattening looks cheap there; a cold run pays the full JIT-plus-OSR cost once and looks expensive. Neither is your steady state. Benchmark warmed-up code under a realistic call pattern.
OSR only helps a method that actually loops a lot on one call. A flattened method whose dispatcher runs a handful of blocks and returns never trips the patchpoint — it just runs those blocks at tier-0 until call-counting promotes the whole method. That is fine; it is also why the OSR path matters mainly for flattened methods that contain a real inner loop of their own.
[MethodImpl(MethodImplOptions.AggressiveOptimization)] forces a method straight to tier-1 and pins it there, skipping tier-0 and OSR and forfeiting dynamic PGO. On a flattened method that is occasionally the right call — a flattened routine that must be fast on its first invocation — but it costs startup and a profile, so reach for it deliberately, not by default.
And the ground rule underneath all of it: flattening is for protecting your own code. Scope it to the methods whose logic is yours to defend, measure honestly, and let the hot path stay fast.
Takeaway
Control-flow flattening is not a flat tax. It changes a method’s shape from straight-line to looping, and the tiered JIT treats those differently: the flattened method takes the On-Stack Replacement path, its dispatcher lowers to a cheap jump table at tier-1, and its real cost is usually the inlining it loses by growing a loop — not the dispatch. That is why the honest rule is the simple one. Flatten the cold, valuable methods, where none of this is on a hot path; keep it off the small, hot helpers the JIT was inlining for free. Nebula’s include/exclude scoping and intensity presets exist to draw exactly that line, and the Pro tier is where the stronger control-flow and virtualization transforms live for the few methods that warrant them.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.