The divider's iteration measured 8.17 ns on its own, but 17.77 ns once I took it into the multiply/divide unit. Same source, more than a 2x difference. The cause was neither size nor wiring, but whether that path was the longest at the moment ABC maps the logic. I made the divider a standalone module, synthesized it first, and had the parent read its gate netlist, and the whole core went from 52.7 to 54.9 MHz.
What I am building is a 2-way out-of-order RV64IM core. As I wrote at the end of last time, the core's longest path is in the divider's iteration. Measured alone, that iteration is 8.17 ns, still far from the 1 GHz constraint, but an order of magnitude apart from the whole core's 18.98 ns. This time is about that gap.
It Was Just a Difference in Logic Depth
First, I compared the contents of the path when standalone and inside the parent.
| Period | Cell count | Logic depth | maj3 | |
|---|---|---|---|---|
| Hand-written iteration only | 8.17 ns | 1,128 | 36 | 2 |
| Real code with the multiplier rows removed | 9.85 ns | 2,201 | 41 | 1 |
| The multiply/divide unit itself | 17.77 ns | 24,942 | 68 | 43 |
The delay and load per stage do not change. What differs is only the number of stages and the kinds of cells that make up the stages.
maj3 is the carry output of a full adder, and when these line up it becomes a subtraction that carries one bit at a time.
a311oi and o311ai are cells that produce the carry for three to four bits at once, and when these line up the number of stages decreases.
Standalone, the latter lined up, while inside the parent 43 of the former lined up.
In other words, the structure of the subtraction had become something else. From the same source, when seen at 2,201 cells it becomes a folded carry, and when seen at 24,942 cells it becomes a one-bit ripple.
It Was Not the Size
I suspected the cell count, but that was wrong. What determines the logic depth is whether that path is the longest at the moment ABC maps.
The &nf of the abc that yosys calls maps the longest visible path with delay priority, and relaxes every other path with area priority up to that delay.
It does not repeat this.
So a divider that shares the same abc run with the slow multiplier can be relaxed up to the multiplier's delay.
Going from a folded carry to a one-bit ripple is how it gets relaxed.
This happens even if nothing is shared. It is enough for there to be one slow path in the same module.
How I Confirmed It
Next to the real-code divider, I placed an unrelated 64x64 multiplication. The divider's results do not use the multiplication at all.
| Period | Logic depth | Structure | |
|---|---|---|---|
| Divider only | 9.85 ns | 41 | Folded carry |
| Multiplication placed next to it, synthesized flat | 16.08 ns | 111 | 24 maj3 |
| Multiplication placed next to it, synthesized with hierarchy kept | 11.10 ns | 43 | Folded carry |
The same divider in the same file becomes 41 stages or 111 stages depending on what is next to it. Keeping the hierarchy brings it back to 43 stages. This settled the cause.
How I Fixed It
Applying a keep-hierarchy setting to the whole core was not usable.
read_slang --keep-hierarchy changes how the wiring is assembled even if everything is flattened in the end.
The whole core's longest path grew by 1.6 ns, going from 52.7 to 48.6 MHz.
The way that leaves the rest untouched is hierarchical synthesis.
Synthesize the divider's module on its own, and have the parent read its gate netlist.
On the parent's side the divider is a black box, so abc does not look inside it and does not relax it.
I put this into the synthesis flow.
Writing blackbox= on a line of modules.list makes the named module get synthesized earlier in the same run.
Results
On the RTL side, I split the divider out as scariv_div.
| Before | After | |
|---|---|---|
scariv_div alone |
— | 9.86 ns / 101.5 MHz / 41 stages / 1 maj3 |
scariv_muldiv |
17.77 ns | 16.08 ns |
| Whole core | 18.98 ns / 52.7 MHz | 18.20 ns / 54.9 MHz |
The multiply/divide unit's longest path moved to the accumulation of the multiplier's partial products. The whole core's longest path also moved away from the divider and became a path from the issue queue through the physical register file to the branch unit. This is the first time the physical register file has appeared on this path.
The price of the module boundary is area: it grew by 1.3% on SKY130 and 4.5% in FPGA LUTs.
The FPGA frequency does not change.
Vivado synthesizes with -flatten_hierarchy none, so it was keeping the hierarchy to begin with.
What This Installment Cannot Claim
I have not traced why ABC's &nf does not repeat.
What I know is only that every observation can be explained on the premise that it does not repeat.
There are nine hypotheses I ruled out.
Holding both w_diff and w_shifted in registers (it gets slower), the fanout of r_rem (at most 6), the surrounding selection circuit (1.5 ns of the 8 ns), removing synth -flatten (identical), SystemVerilog's (* keep_hierarchy *) (the frontend discards it), setattr alone (nothing to attach it to), &nf -C 500 (identical), &nf -F 0 (ABC exits at 86), and &nf -D in six ways (identical every time).
None of them was the cause, but until you measure that something is not the cause, you cannot tell it apart from a candidate cause.
The whole core is still 18.20 ns. It is far from 1 GHz, and what moved this time is 0.78 ns. The multiply/divide unit alone improved by 1.69 ns, but only part of that reached the whole core.
Article index for this series: Implementing a RISC-V OoO CPU: Article Index
This is an English translation of the original Japanese post: RISC-V OoO CPUを実装する (56. 除算器を単独モジュールに切り出して合成する)