55bae1d960970140e1b486fb7559891a3bc357bf
[crowdsupply.git] / updates / 003_2018dec04_microarchitecture.mdwn
1 # Microarchitectural Decisions
2
3 The Libre-RISCV core is planning to deploy innovative vectorisation
4 system, known as
5 [Simple-V](https://libre-riscv.org/simple_v_extension/specification).
6 Honestly, it's not very simple at all! The principle is straightforward:
7 mark ordinary registers as "vectorised", and when an instruction uses one
8 such "tagged" register, go into a hardware-unrolled version of a software
9 macro loop, issuing otherwise identical instructions with *contiguous*
10 sequentially-increasing register numbers.
11
12 It sounds easy... except that the "tag" table behind the registers has
13 been extended to:
14
15 * add predication (switching on and off individual "elements" of the vector)
16 * change the element width so that what was previously a 64-bit ADD can be
17 used to do 16-bit or even 8-bit ADDs
18 * reordering of the elements, for 2D and 3D arrays and Matrices: very
19 useful for in-place transposing for Matrix Multiplication)
20 * extend the instructions so that they can access up to 128 registers,
21 where previously that was limited to 32 (and only 8 for Compressed
22 instructions)
23
24 All of these turn out to be important for GPU workloads.
25
26 One of the most challenging aspects of SV is that there is no restriction
27 on the "redirection". Whilst one instruction could use register 5 and
28 another uses register 10, *both* of them could actually be "redirected"
29 to use register 112, for example. One of those could even be changed
30 to 32-bit operations whilst the other is set to 16-bit element widths.
31
32 Our initial thoughts advocated a standard simple in-order SIMD architecture,
33 with predication bits passed down into the SIMD ALUs. If a bit is "off",
34 that "lane" within the ALU does not calculate a result, saving power.
35 However, in SV, when the element width is set to 32, 16 or 8-bit, a
36 pre-issue engine is required that re-orders *parts* of the registers,
37 packing lanes of data together so that it fits into one SIMD ALU, and, on
38 exit from the ALU, it may be necessary to split and "redirect" parts of the
39 data to *multiple* actual 64-bit registers. In other words, bit-level
40 (or byte-level) manipulation is required, both pre- and post- ALU.
41
42 This is complicated!
43
44 As part of the initial design of SV, there was an accidental assumption
45 that it would be perfectly reasonable to use a multi-issue execution
46 engine, and to simply drop multiple of those "hardware-loop-unrolled"
47 operations into the instruction queue. This turns out to be a radically
48 different paradigm from standard vector processors, where a loop allocates
49 elements to "lanes", and if a predication bit is not set, the lane
50 runs "empty". By contrast, with the multi-issue execution model, an
51 operation that is predicated out means that the element-based instruction
52 does not even make it into the instruction queue, leaving it free for
53 use by following instructions, even in the same cycle, and even if the
54 operation is totally different. Thus, unlike in a
55 traditional vectore architecture, ALUs may be occupied by elements from
56 other "Lanes", because of the pre-existing decoupling between the multi-issue
57 instruction queue and the ALUs.
58
59 Simple!
60
61 [[reorder_buffer.jpg]]
62
63 There are many other benefits to a multi-issue microarchitecture, and
64 these are being discussed
65 [here](http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2018-December/000198.html)
66 on the mailing list. Personally I favour a modified version of the
67 [Tomasulo Algorithm](https://en.wikipedia.org/wiki/Tomasulo_algorithm),
68 which includes what is known as a
69 [Reorder Buffer](https://en.wikipedia.org/wiki/Re-order_buffer).
70 What is particularly nice about this algorithm is that it was first introduced
71 in 1967, and came to prominence in the early 1990s when Moore's Law started
72 to hit a speed wall. That in particular means that, firstly, it's extremely
73 commonly taught in Universities, and, secondly, patents on the algorithm
74 have long since expired.
75
76 Also, there are both memory hazards and register hazards that a Reorder
77 Buffer augmented Tomasulo algorithm takes care of, whilst also allowing
78 for branch prediction and really simple roll-back, preservation of
79 execution order even though instructions may actually be done out of order,
80 and, crucially, some ALUs may take longer than others, and the algorithm
81 simply does not care. In addition, there may be a really simple way to
82 extend the Reorder Buffer tags to accomodate SIMD-style characteristics.
83
84 We also may need to have simple Branch Prediction, because some of the
85 loops in [Kazan](https://salsa.debian.org/Kazan-team/kazan/) are particularly
86 tight. A Reorder Buffer can easily be used to implement Branch Prediction,
87 because, just as with an Exception, the ROB needs to be cleared out
88 (flushed) if the branch is mispredicted. As it is necessary to respect
89 Exceptions, the logic has to exist to clear out the ROB: Branch Prediction
90 simply uses this pre-existing logic.
91
92 The other way in which out-of-order execution can be handled is called
93 scoreboarding, as well as explicit register renaming. These schemes
94 seem to have significant disadvantages and complexities when compared
95 to Reorder Buffers:
96
97 * Explicit Register renaming needs a global register file quite a bit larger
98 than the "actual" one. The Libre RISC-V SoC already has two whopping
99 great 128-entry 64-bit register files.
100 * In-order scoreboarding actually *delays* instruction execution (all of it)
101 until such time as the source registers and all other dependencies are
102 ready. The idea seems to be that the register renaming "should have taken
103 care of" as many of these dependencies as possible, in advance.
104 * Unlike Tomasulo with Reorder buffers, there does not appear to be
105 any assistance in dealing with memory LOAD/STOREs.
106 * There's no clear way to handle branch prediction, where the Reorder
107 Buffer of Tomasulo handles it really cleanly.
108
109 Whilst nothing's firmly set in stone, here, as we have a Charter that
110 requires unanimous decision-making from contributors, so far it's leaning
111 towards Reorder Buffers and Tomasulo as a good, clean fit. In part that
112 is down to more research having been done on that particular algorithm.
113 More as it happens...
114