b1735c55efcfa253e650600108d6e44a19afd1d4
[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 the pre-existing decoupling between the multi-issue
57 instruction queue and the ALUs is efficiently leveraged.
58
59 Simple!
60
61 # Tomasulo Algorithm and Reorder Buffers
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 [[reorder_buffer.jpg]]
77
78 Also, there are both memory hazards and register hazards that a Reorder
79 Buffer augmented Tomasulo algorithm takes care of, whilst also allowing
80 for branch prediction and really simple roll-back, preservation of
81 execution order even though instructions may actually be done out of order,
82 and, crucially, some ALUs may take longer than others, and the algorithm
83 simply does not care. In addition, there may be a really simple way to
84 extend the Reorder Buffer tags to accomodate SIMD-style characteristics.
85
86 We also may need to have simple Branch Prediction, because some of the
87 loops in [Kazan](https://salsa.debian.org/Kazan-team/kazan/) are particularly
88 tight. A Reorder Buffer can easily be used to implement Branch Prediction,
89 because, just as with an Exception, the ROB can to be cleared out
90 (flushed) if the branch is mispredicted. As it is necessary to respect
91 Exceptions, the logic has to exist to clear out the ROB: Branch Prediction
92 simply uses this pre-existing logic.
93
94 The other way in which out-of-order execution can be handled is called
95 scoreboarding, as well as explicit register renaming. These schemes
96 seem to have significant disadvantages and complexities when compared
97 to Reorder Buffers:
98
99 * Explicit Register renaming needs a global register file quite a bit larger
100 than the "actual" one. The Libre RISC-V SoC already has two whopping
101 great 128-entry 64-bit register files.
102 * In-order scoreboarding actually *delays* instruction execution (all of it)
103 until such time as the source registers and all other dependencies are
104 ready. The idea seems to be that the register renaming "should have taken
105 care of" as many of these dependencies as possible, in advance.
106 * Unlike Tomasulo with Reorder buffers, there does not appear to be
107 any assistance in dealing with memory LOAD/STOREs.
108 * There's no clear way to handle branch prediction, where the Reorder
109 Buffer of Tomasulo handles it really cleanly.
110
111 However there are downsides to Reorder Buffers:
112
113 * The Common Data Bus may become a serious bottleneck, as it delivers
114 data from multiple ALUs which may be generating results simultaneously.
115 To keep up with result generation, *multiple* CDBs may be needed, which
116 results in each receiver having multiple ports
117 * The Destination field in the ROB has to act as a key in a CAM (Content
118 Addressble Memory). As a result, power consumption of the ROB may be
119 quite high. It may or may not be possible to reduce power consumption
120 by testing an "active" bitfield (separate from but augmenting the ROB)
121 to indicate whether Destination Registers are in use. If inactive,
122 the CAM lookup need not take place.
123
124 Whilst nothing's firmly set in stone, here, as we have a Charter that
125 requires unanimous decision-making from contributors, so far it's leaning
126 towards Reorder Buffers and Tomasulo as a good, clean fit. In part that
127 is down to more research having been done on that particular algorithm.
128 For completeness, scoreboarding and explicit register renaming need
129 to be properly and comprehensively investigted.
130
131 # Scoreboarding
132
133 [Scoreboards](https://en.wikipedia.org/wiki/Scoreboarding) are an
134 effort to keep "score" of whether an instruction has all of its
135 operands (and the hardware that uses them) ready to go, without conflict.
136 Everything about the scheme, unfortunately, says that it is an incomplete
137 mechanism. Unlike the Tomasulo algorithm when augmented with a Reorder
138 Buffer, there's no way to "undo" a completed operation: the operation
139 proceeds and modifies registers or memory, in an out-of-order fashion,
140 regardless of consequences. If an exception occurred *tough*!
141
142 Hence, it was quite hard to accept scoreboarding enough to understand it.
143 It wasn't until
144 [this message](http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2018-December/000223.html)
145 that I realised that there is near-direct equivalence between the features
146 of scoreboarding and the Tomasulo Algorithm. It's not complete equivalence,
147 because the Reorder Buffer is what keeps everything transactional and clean.
148 However, at least the scoreboard does have direct equivalence to the
149 Reservation Station concept: the scoreboard records whether the source
150 registers are ready or not (and so does the Reservation Station).
151
152 The problem is: there are far too many things missing from the scoreboard
153 concept:
154
155 * Register-renaming has to be done separately (the Tomasulo Reservation
156 Stations, in combination with the ROB, handle that implicitly).
157 * Scoreboarding introduces the concept of "waffly exceptions"
158 (the ROB can even record exceptions that are only actioned if they
159 make it to the head of the queue).
160 * Scoreboarding does not provide a means to do multi-issue (the ROB
161 does: you just put more than one entry per cycle into the buffer)
162
163 # Next step
164
165 The project is being run along ethical lines. That in particular means
166 unanimous decision-making. Nobody gets to over-rule anyone else: everyone
167 matters, and everyone's input matters. So if I, personally, think that
168 Tomasulo is better, it's up to me to keep on researching and evaluating
169 and reasoning until I have convinced everyone else... or they have convinced
170 me otherwise.
171
172 I have it on good authority from some extremely comprehensive research
173 that this is a hell of a lot better way to do decision-making than
174 "mob-rule" voting. "Mob-rule" voting (aka "democracy") basically
175 *automatically* destroys the morale and discounts the will of the
176 "minority". No wonder democratic countries have "Minority Representation
177 Political Groups", because it's heavily brainwash-ingrained into people
178 living in such countries that the "Minority" view *shall* be disregarded;
179 of *course* they feel the need to shout and get angry!
180
181 Learning from these mistakes, which are often copied and reflected
182 in Software Libre groups, is I feel very important, to try something
183 different. In previous Libre projects that I've run, I was the
184 kind-of informal "technical lead", where I never actually defined any
185 guidelines or rules about how the project should make decisions: basically
186 I let people do what they liked unless it disrupted the project for
187 everyone else. I set myself up as the "arbitrator", so to speak.
188 The way that this project will be run is very much new and experimental,
189 even to me. We'll see how it goes.
190
191