TLB Size vs. Timing: Why Small TLBs Make Sense in Hardware
TLB Size vs. Timing: Why Small TLBs Make Sense in Hardware
For years, I wondered why instruction and data TLBs in many CPU designs have surprisingly few entries—often just 16 or 32. Coming from a software background, this seemed unnecessarily restrictive. Modern operating systems manage thousands of pages, so why not have larger TLBs to reduce miss rates?
After debugging a critical path issue in my LoongArch CPU implementation, I finally understand: it’s all about timing.
The Problem: A 58-Minute Synthesis
I recently made what seemed like a minor optimization to my TLB implementation—adding pipeline registers to the search inputs (s_vppn_g, s_asid_g) to “improve timing.” The synthesis results were catastrophic:
- Fitter time: 58 minutes (up from 17 minutes)
- Maximum frequency: 34.83 MHz (down from 44.13 MHz, a 21% reduction)
- Critical path slack: -15.378 ns (vs. -9.326 ns before)
The critical path traced through:
TLB registered inputs (s_vppn_g, s_asid_g)
→ 32 parallel VPPN comparators (19 bits each)
→ 32 parallel ASID comparators (10 bits each)
→ 32-way match logic (with global bit)
→ 4-level priority encoder (enc32)
→ 32:1 output multiplexer
→ Exception checking (5 types)
→ Load/store pending logic
→ Register enable generation
→ Setup time
Total: 18-22 logic levels, 28.588 ns delay in a 13.333 ns clock period.
The Root Cause: Parallel Matching is Expensive
A TLB is essentially a fully-associative cache. Every lookup must compare the search key against all entries in parallel:
generate
for (i = 0; i < 32; i = i + 1) begin : match_gen
assign match[i] = (s_vppn_g == tlb_vppn[i]) && // 19-bit compare
((s_asid_g == tlb_asid[i]) || tlb_g[i]) && // 10-bit compare + OR
tlb_e[i]; // AND with valid bit
end
endgenerate
For a 32-entry TLB, this creates:
- 32 × 19-bit comparators for virtual page number matching
- 32 × 10-bit comparators for ASID matching
- 32 × 3-input gates for the final match logic
After matching, we need a priority encoder to find the first (lowest index) matching entry. For 32 entries, this requires a 4-level hierarchical encoder (enc4→enc8→enc16→enc32), adding 4 more logic levels.
Finally, the match index selects from 32 TLB entries using a 32:1 multiplexer (5 more logic levels) for each output field (PFN, permissions, etc.).
The Math: Why TLB Size Kills Timing
The logic depth grows with TLB size:
| TLB Entries | Comparators | Priority Encoder Levels | Mux Levels | Total Added Levels |
|---|---|---|---|---|
| 8 | 8 | 2 (enc8) | 3 | ~11-13 |
| 16 | 16 | 3 (enc16) | 4 | ~13-15 |
| 32 | 32 | 4 (enc32) | 5 | ~15-17 |
| 64 | 64 | 5 (enc64) | 6 | ~17-19 |
Each additional power of 2 adds:
- 1 priority encoder level
- 1 mux level
- Double the comparator fanout
At 90nm-180nm FPGA technology (typical for hobby/educational designs), each logic level costs roughly 0.5-1.5 ns. A 32-entry TLB can easily consume 20-30 ns of a critical path, leaving little room for the rest of the CPU pipeline.
The Solution: Smaller TLBs + Smarter Pipelining
I applied two fixes:
1. Remove Internal Pipeline Registers
The original “optimization” of registering TLB search inputs actually concentrated all logic into one clock cycle. By removing these registers and making the TLB fully combinational:
// Before: Registered inputs
reg [18:0] s_vppn_g;
reg [9:0] s_asid_g;
always @(posedge clk) begin
s_vppn_g <= s_vppn; // LS1 stage
s_asid_g <= s_asid;
end
// Match logic uses s_vppn_g, s_asid_g in LS2 → concentrated timing
// After: Combinational inputs
// Match logic uses s_vppn, s_asid directly → distributed timing
assign match[i] = (s_vppn == tlb_vppn[i]) && ...
This distributes the timing across the LS1→LS2 boundary instead of cramming it all into LS2→LS3.
2. Downsize from 32 to 16 Entries
Cutting the TLB in half:
- 50% fewer comparators (16 vs 32)
- 1 fewer priority encoder level (enc16 vs enc32)
- 1 fewer mux level (4 vs 5 bits for select)
- ~2-3 logic levels removed
`ifndef TLB_ENTRIES
`define TLB_ENTRIES 16 // Downsized from 32
`endif
`if (`TLB_ENTRIES == 16)
enc16 u_enc16 (.in(match), .idx(match_index[3:0]));
`elsif (`TLB_ENTRIES == 32)
enc32 u_enc32 (.in(match), .out(match_index));
`endif
Expected improvement: ~6-8 logic levels removed from critical path, targeting 50-60 MHz (vs. 34.83 MHz before).
Why Commercial CPUs Also Use Small TLBs
Looking at real processors:
| Processor | I-TLB | D-TLB | Technology |
|---|---|---|---|
| ARM Cortex-A7 | 10 | 10 | 28nm |
| RISC-V Rocket | 32 | 32 | Varies |
| MIPS R4000 | 48 | 48 | 1µm |
| x86 (L1) | 64-128 | 64-128 | Modern |
| My LoongArch | 32→16 | 32→16 | FPGA |
Even high-end CPUs keep L1 TLBs relatively small (64-128 entries) and rely on:
- L2 TLBs for capacity (512-1024 entries, slower but larger)
- Page walk caches for multi-level page tables
- Huge pages to reduce TLB pressure (2MB/1GB pages vs. 4KB)
The constraint isn’t transistor count—it’s timing. A 128-entry fully-associative TLB would need 7 logic levels just for the priority encoder, making it very hard to fit in a 2-3 GHz clock period even in advanced processes.
Lessons Learned
-
Fully-associative lookups scale poorly with size. Each doubling adds significant logic depth.
-
Pipelining can backfire. Adding registers in the wrong place can concentrate logic instead of distributing it.
-
Timing, not area, is the constraint. Modern FPGAs have millions of LUTs, but deep logic cones kill frequency.
-
Software assumptions don’t always apply to hardware. A “small” 16-entry TLB handles most working sets fine, and trying to make it “better” can make performance worse.
-
Measure, don’t guess. I thought registered inputs would help timing. The synthesis tool told me I was wrong—loudly, with a 58-minute Fitter run.
The Irony
I spent weeks debugging this, only to realize the solution was to make the TLB smaller and simpler. Sometimes the best optimization is subtraction, not addition.
The modified TLB is now purely combinational with 16 entries instead of 32. It’s “worse” on paper (fewer entries, no input registers), but I expect 40-50% better frequency and 70% faster synthesis time.
In hardware design, less is often more—especially when “more” means more logic levels in your critical path.
Technical Details
Files modified:
c7btlb.v: Removeds_vppn_g,s_asid_g,s_odd_page_g,s_vld_gregisters- TLB size configurable via
`TLB_ENTRIESmacro (default 16) - Priority encoder automatically selected:
enc16for 16 entries,enc32for 32
Critical path before: 28.588 ns (s_vppn_g → lsu_addr_ls3_reg)
Expected after: ~15-20 ns (distributed across LS1→LS2)
Synthesis time before: 58 minutes
Expected after: 10-15 minutes
This post describes a debugging session on a custom LoongArch CPU implemented in Verilog on a Cyclone IV FPGA. The same timing principles apply to any synchronous digital design where logic depth matters.
Note: This describes the analysis and proposed fix. Actual synthesis results will be added after running the modified design through Quartus.