# Pipeline hazards question

i know that resources duplicate is solution to Structure Hazard but what about code scheduling ....
Can code scheduling considered as a solution to Structure Hazard ?

• Scheduling in what sense? By the programmer/compiler when writing the code or at automatically at runtime? – harold Aug 2 '17 at 0:40
• @harold does not metter .. , i think code scheduling not considered solution to Structure Hazard because it not always works – kalra Aug 6 '17 at 11:40

Assuming you're referring to how out of order execution can resolve a lack of resources. As resources are used and code is executed temporally, by reordering the code, the usage of resources can be rearranged in time, benefiting execution by a more efficient (less time waiting, more time using) use of resources.

I'm not a big fan of examples but let's suppose two super scalar pipelines want to use resources A, B, C and D, at the same time:

time = 0 | A | A
time = 1 | B | B
time = 2 | C | C
time = 3 | D | D


The CPU can reorder these instructions

time = 0 | B | A
time = 1 | C | B
time = 2 | D | C
time = 3 | A | D


Another example: Suppose one instruction pipeline has the following instructions:

multiply_then_add
do_something_else


Then the usage of resources might look like this:

time = 0 | M |   |
time = 1 |   | A |
time = 2 |   | A |
time = 3 | M |   |
time = 4 |   |   | D


where M = multiplier, A = adder, and D = Doing something else. As you can see, the two add instructions back to back are an issue, if they were separated by a gap we could shorten the time taken, and out of order execution can resolve this:

multiply_then_add
do_something_else

time = 0 | M |   |
time = 1 |   | A |
time = 2 |   |   | D
time = 3 |   | A |
time = 4 | M |   |


Which can be optimised:

time = 0 | M |   |
time = 1 |   | A |
time = 2 |   | A | D
time = 4 | M |   |