YouTube2Text

[CS61C FA20] Lecture 23.1 - Pipelining III: Load Data Hazard — Transcript

by CS 61C Departmental · 1,998 words · 373 segments · language en · Watch on YouTube

Full transcript

  1. 0:01[Music]
  2. 0:12hello
  3. 0:13and welcome back to our pipelining
  4. 0:14module
  5. 0:16so far we have studied the principles of
  6. 0:19pipelining
  7. 0:21and then moved on to analyze hazards we
  8. 0:24have seen
  9. 0:24that we generally deal with the
  10. 0:27structural hazards by adding enough
  11. 0:30hardware such that we can
  12. 0:31execute multiple instructions in the
  13. 0:34pipeline
  14. 0:37then we moved on to study the data
  15. 0:41hazards
  16. 0:42that stem from the
  17. 0:45fact that the or the condition that
  18. 0:49the following instruction depends on the
  19. 0:52results
  20. 0:52of the previous instruction that is in
  21. 0:54the pipeline
  22. 0:57let's take a look look at that you know
  23. 0:59let's
  24. 1:00revisit that such that we understand how
  25. 1:02we dealt with that
  26. 1:03by using forwarding so in this case we
  27. 1:06had the instruction
  28. 1:07add that was in the pipeline already and
  29. 1:10then there was a sub
  30. 1:11instruction that was following it and
  31. 1:14used
  32. 1:15the result that was stored in a
  33. 1:18destination register as 0
  34. 1:20as the source for that
  35. 1:23following instruction and then
  36. 1:27the instruction after that also used as
  37. 1:30zero
  38. 1:31as the source register
  39. 1:35so as the ad instruction is
  40. 1:38traveling through a pipeline so it's
  41. 1:39five stages of pipeline
  42. 1:42although the result of the
  43. 1:45execution of this edition is known um at
  44. 1:48the end
  45. 1:49of the execute stage it is not written
  46. 1:52back
  47. 1:52into the register file until
  48. 1:56two stages later until halfway through
  49. 1:59the right backstage
  50. 2:01so the value of s0 still is the old
  51. 2:05value
  52. 2:05of 5 in this case it gets updated to a
  53. 2:08new value
  54. 2:09that is 9 in the first
  55. 2:13phase of the write-back phase
  56. 2:16so if the sub-instruction that follows
  57. 2:20the add instruction
  58. 2:21reads the value from the register when
  59. 2:23it's supposed to be
  60. 2:25during the instruction decode phase it
  61. 2:28would
  62. 2:28read the wrong value which is 5
  63. 2:31the old value instead
  64. 2:35we deal with that by forwarding
  65. 2:39the new value and this is something that
  66. 2:42is possible because
  67. 2:43we already know that the new value is 9
  68. 2:46it is just not written back in the
  69. 2:48register file but it is handy it is
  70. 2:51sitting
  71. 2:51right there at the output of the alu
  72. 2:55in in the register that separates the
  73. 2:59the execute phase with the memory access
  74. 3:02phase
  75. 3:04so what we need to do is to widen the
  76. 3:07multiplexer
  77. 3:09that feeds the alu with the operand in
  78. 3:12this case
  79. 3:13ra and it's there
  80. 3:17it is just gonna show up at the input of
  81. 3:19the alu
  82. 3:21similarly for the subsequent instruction
  83. 3:24or
  84. 3:25we would forward the
  85. 3:28result of this addition before it gets
  86. 3:31retained register file
  87. 3:32from the pipeline register that divides
  88. 3:36the memory access phase and the right
  89. 3:39back face
  90. 3:39because we have it handy it is it still
  91. 3:42traveled a little bit deeper in the
  92. 3:43pipeline
  93. 3:44so it is nicely aligned with this
  94. 3:46instruction
  95. 3:48with the input of the instruction or
  96. 3:52okay so we learn how to deal
  97. 3:56with these data hazards by using
  98. 4:00hardware forwarding
  99. 4:01without hard forwarding we would have
  100. 4:03had
  101. 4:04to stall the pipeline meaning
  102. 4:07we would have to insert knobs and
  103. 4:11don't do something during particular
  104. 4:13cycles
  105. 4:15there is no such easy way out
  106. 4:18in another case of data hazard which
  107. 4:22are the data hazards associated with
  108. 4:24loads
  109. 4:25so here is a sequence of instructions
  110. 4:27that starts with the load
  111. 4:29this load takes the
  112. 4:33data from the memory and stores in the
  113. 4:36destination register s2
  114. 4:38and then subsequent instructions would
  115. 4:40like to use that value
  116. 4:43from s2 as the source operand
  117. 4:47so here is what happens our load word
  118. 4:53goes addresses the memory and this data
  119. 4:56is available
  120. 4:57at the end of the memory access phase
  121. 5:02that is one cycle later than what we
  122. 5:04have had
  123. 5:05in the r type instructions where it was
  124. 5:08available at the end of the execute
  125. 5:10phase so this new data
  126. 5:13is going to be written just one cycle
  127. 5:15after that
  128. 5:16but the subsequent instruction here is
  129. 5:19looking
  130. 5:20at the values that are in the register
  131. 5:22file
  132. 5:23they're stale they're old so
  133. 5:28data from the memory is there but it is
  134. 5:31needed
  135. 5:32one cycle earlier we can go back
  136. 5:35we cannot jump time
  137. 5:39here
  138. 5:43there is no forwarding solution it is
  139. 5:45going to fix
  140. 5:46this here we can forwarding basically
  141. 5:49helps us
  142. 5:50go kind of forward in the pipeline there
  143. 5:53is no solution that allows us to go
  144. 5:55back in the pipeline what can we do
  145. 6:01we need to stall there is no practical
  146. 6:04way
  147. 6:05we don't have a time machine yet here
  148. 6:08not in compute hardware or anywhere else
  149. 6:13so um this end instruction that
  150. 6:17needs s2 as it's its source operand
  151. 6:20cannot proceed until it becomes valid
  152. 6:23until
  153. 6:24it is there
  154. 6:28we can forward from here but that
  155. 6:30forwarding
  156. 6:31would make it only into the
  157. 6:34into the instruction that is delayed by
  158. 6:36one it would make it in time
  159. 6:39for or but and can do it
  160. 6:42so we will stall and forward
  161. 6:46and then all the instructions that are
  162. 6:48down the stream
  163. 6:49would not be affected so
  164. 6:53in a quick summary here load instruction
  165. 6:56all types of load instructions require
  166. 7:00one cycle pipeline stall let's see how
  167. 7:02does that pipeline stall look like
  168. 7:05so our data is here
  169. 7:08in the pipeline register that follows
  170. 7:10the memory access stage
  171. 7:13and it needs to be there it can happen
  172. 7:16so we need to stall the processor
  173. 7:19in order to slow down the execution
  174. 7:23delay the execution of this end
  175. 7:26so instead we convert this end
  176. 7:29into a knob and move all the
  177. 7:32instructions
  178. 7:34by one cycle
  179. 7:37down the down the stream
  180. 7:40okay so um
  181. 7:44how do we convert in flight this end
  182. 7:47that
  183. 7:47is uh that that is an and to a knob
  184. 7:51i mean it's already there it is in the
  185. 7:53pipeline by the time
  186. 7:55we have figured out that this is a load
  187. 7:57at the end of this cycle we have already
  188. 7:59fetched the end we have within
  189. 8:01fetch something else so we have an end
  190. 8:03on our hand that we are executing
  191. 8:07so we have to have a hardware mechanism
  192. 8:09that basically cancels this end
  193. 8:11invalidates the end and
  194. 8:15repeats it in the next cycle
  195. 8:20so how do we do that what does the end
  196. 8:22do well it does
  197. 8:23flip some bits but
  198. 8:26if it doesn't write them back if it
  199. 8:28doesn't change the state of a processor
  200. 8:31it's like it did not exist so that's a
  201. 8:34clue
  202. 8:35all what you need to do quickly when we
  203. 8:38find out that we are we have a load
  204. 8:40on our hands here right in this
  205. 8:43instruction decode phase
  206. 8:45immediately after that turn off set all
  207. 8:49the control signals
  208. 8:51that are associated with writing the the
  209. 8:53state the new state
  210. 8:55into the processor to being disabled
  211. 8:59so we are not writing back into a
  212. 9:00register file
  213. 9:02we are not writing into the memory and
  214. 9:05we are not updating the program counter
  215. 9:08so if there is no state update the next
  216. 9:11instruction
  217. 9:12is going to be the same one it's going
  218. 9:14to be our end and we just proceed
  219. 9:16with executing that end
  220. 9:20and it's as simple as that so
  221. 9:23we need a mechanism here
  222. 9:27a piece of logic that is going to detect
  223. 9:29that we are dealing with
  224. 9:31a load and that
  225. 9:34destination of that load is
  226. 9:37the source for the next instruction that
  227. 9:41requires us to stall and this is a
  228. 9:43mechanism to stall we basically make the
  229. 9:46latch and that door do nothing we
  230. 9:49disable
  231. 9:50the control signals and run that
  232. 9:53following instruction again
  233. 9:56when we run this and again
  234. 9:59when we repeat the end we are able to
  235. 10:02forward
  236. 10:03this using very similar principles that
  237. 10:06what we have seen before actually
  238. 10:08exactly same mechanism as we have seen
  239. 10:10before in the
  240. 10:11in mitigating data hazards
  241. 10:16so it becomes available at the input of
  242. 10:18the alu
  243. 10:20so we are going to repeat this and with
  244. 10:22the forwarding
  245. 10:26so in a quick summary here
  246. 10:30in order to handle the load hazard slow
  247. 10:33data hazards
  248. 10:34we have to have one cycle pipeline stall
  249. 10:37implemented in hardware
  250. 10:39that prevents completion of the
  251. 10:41instruction
  252. 10:42does not update the processor state and
  253. 10:45forces re-execution of the following and
  254. 10:48satisfaction
  255. 10:50okay
  256. 10:54so this is a very important concept in
  257. 10:56pipelines
  258. 10:58it happens fairly frequently so that
  259. 11:00instruction
  260. 11:02that is sitting after the load word
  261. 11:06is called the load delay slot
  262. 11:10if that instruction uses the result of
  263. 11:12the load then the hardware
  264. 11:14has to to stall for one cycle
  265. 11:17so what do we do um
  266. 11:19[Music]
  267. 11:20with that well you know that's like
  268. 11:22inserting a knob in the slot
  269. 11:24but you know knobs can be inserted
  270. 11:27during compilations
  271. 11:29phase um and result in a in a
  272. 11:33code bloat and performance loss
  273. 11:36so the key idea here is something that
  274. 11:38we mentioned before
  275. 11:40let's go through the list of instruction
  276. 11:42that we would like to execute and we can
  277. 11:44find one that does not where
  278. 11:46whose operands do not depend on the
  279. 11:48result of the load
  280. 11:50then just put it into that delay slot
  281. 11:55all right so that's the idea and there
  282. 11:58is no performance loss
  283. 11:59so the burden of doing that is generally
  284. 12:02put
  285. 12:03on the compiler
  286. 12:07let's see how does that work so here is
  287. 12:09a piece of
  288. 12:10risk 5 assembly code that does
  289. 12:14two additions
  290. 12:18by using data that is in the memory
  291. 12:21so a3 sums a not
  292. 12:25an a1 and a4 sums a not
  293. 12:28an a2 so let's see a sample
  294. 12:32risk 5 assembly code so here is the
  295. 12:35original order
  296. 12:36of that code we're gonna go ahead um
  297. 12:39load you know t zero points to the
  298. 12:41zeroth element of the array a
  299. 12:44um and we're going to go get uh
  300. 12:48the first element of the array store it
  301. 12:51in t1
  302. 12:52get the second element of the array
  303. 12:54store it in t2
  304. 12:55and immediately proceed with the
  305. 12:57addition
  306. 12:59see the problem here we
  307. 13:02proceeded by executing two loads they do
  308. 13:05not block
  309. 13:06each other there you know there is no
  310. 13:07dependency between the source of the
  311. 13:10of the second load with the destination
  312. 13:12of the first load but look at this
  313. 13:14one over here t2 here t2 there
  314. 13:18so there is a dependency there um
  315. 13:21so this is going to cause a stall so we
  316. 13:25have to stall
  317. 13:26this add execution until
  318. 13:29we can forward from the the
  319. 13:32load pipeline into the add instruction
  320. 13:36and then we're okay we proceed and then
  321. 13:38we find out there is another hazard here
  322. 13:41we see t4 here
  323. 13:42and t4 there that's a hazard we have to
  324. 13:46stall again
  325. 13:47when we insert that one extra cycle we
  326. 13:50continue
  327. 13:52how can we do better than this
  328. 13:55can we reorder this and that's typically
  329. 13:57a java
  330. 13:59for a compiler so here is an alternative
  331. 14:03schedule of instructions we are going to
  332. 14:06go ahead
  333. 14:07and load all three of them
  334. 14:10three operands here that we need a
  335. 14:12naught a1 and a2
  336. 14:14in three consecutive load cycles that
  337. 14:16enabled us to
  338. 14:18insert this load instruction between
  339. 14:21the load of a0 of a2
  340. 14:24of a1 of a1
  341. 14:28and the consumption of a1 in
  342. 14:31this addition here
  343. 14:35so we are using in the delay slot
  344. 14:40load delay slot we are putting another
  345. 14:42independent load
  346. 14:44so we proceed with that and
  347. 14:47these two
  348. 14:51you know the the storing t2 in the
  349. 14:55register file and the consumption of the
  350. 14:58data from
  351. 14:59the register file are now separated by
  352. 15:01one cycle and that's plenty enough for
  353. 15:03our hardware and
  354. 15:05forwarding in hardware to to help us
  355. 15:08avoid
  356. 15:10stalling then
  357. 15:14the other one is also resolved because
  358. 15:17you know t4 loading into t4 and using
  359. 15:21the data from t4 are now separated by
  360. 15:24two instructions
  361. 15:25and that is plenty enough it's not even
  362. 15:27activating
  363. 15:28the forwarding so instead of
  364. 15:32nine cycles that we used initially for
  365. 15:34executing these
  366. 15:35uh seven instructions we can execute all
  367. 15:38seven instructions
  368. 15:39in seven cycles so this is all what we
  369. 15:43had about the
  370. 15:44load delays
  371. 15:47slot and load data hazard
  372. 15:51we're going to move on to the control
  373. 15:53hazards right after a quick break

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 23.1 - Pipelining III: Load Data Hazard by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,998 words across 373 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.