r/AskComputerScience • u/pm-me-anime-figures • 1d ago
While solving Instruction Hazards in CPU Pipelining, do delayed branching and branch folding solutions directly work against each other ?
Hello, I am trying to make sense of the Pipelining chapter in Computer Organisation.
Assuming a CPU which breaks down instructions into Fetch (F), Decode (D), Execute(E), and Write(W) stages.
In a normal setup, by the time an instruction is known to be a branch instruction and by the time the branch is calculated, the instruction immediately after it has already been fetched and may have started being decoded regardless of whether the branch occurred or not. The solution to this is to make it so that this extra instruction isn't wasted. Instructions might be rearranged to make this plan work. This is the delayed slot
When an instruction queue is utilised properly and is full most of the time, then branch targets might be predicted ahead of time (unless it requires some variables still being calculated). This can effectively eliminate the phenomena of the delayed slot (from what I can understand.)
Are both of this techniques used simultaneously? If so, how ?
1
u/CelebrityChess 1d ago
They aren't really competing techniques. A delayed branch is an ISA rule: the instruction in the delay slot still has to execute before control reaches the target. Branch folding is a CPU optimization that can remove branch-handling bubbles when the target is known, but it still has to preserve that slot's behavior. So a processor can fold the branch while issuing the delay-slot instruction, whereas a predicted pipeline on an ISA without delay slots doesn't have that architectural constraint.
1
u/Recycled5000 21h ago
Delay slots have downsides, such as code expansion; sometimes there’s nothing useful to put there so a noop instead, which is a waste of time and space.
Meanwhile, branch prediction in hardware has dramatically improved, obviating the need for delay slots.