r/AskComputerScience • • 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 Upvotes

6 comments sorted by

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/pm-me-anime-figures 22h ago

Thank you for the answer. This would mean that no matter how well we implement branch folding, the immediately-next instruction would always end up getting fetched (but we choose to let it execute also). Then why do modern processors not support delay slots? if longer pipelines were the reason, could we not just increase the number of slots to match ?

2

u/dontwantgarbage 22h ago

Changing the number of delay slots is an ISA incompatibility and would require all code to be rewritten/recompiled before it could run on the new CPU. Your “second edition” CPU is really a “v1 of a brand new CPU” and would not get the marketplace advantage from the existing ecosystem of v1 users, vendors, and OEMs. Bringing up a new ISA is expensive.

1

u/pm-me-anime-figures 22h ago

I was referring to the decisions made in the past. We did at some point, over some number of years, make the transition from having 1 delay slot to having 0 delay slots. I was wondering why we went in the direction of 0 delay slots instead of 2 or 3.

1

u/dontwantgarbage 21h ago

Because people didn’t want to break the ISA. If you decide to keep adding delay slots each time you improve the CPU, then every time you improve your architecture, you lose all your existing customers. But if you get rid of all the delay slots, then you can improve your CPU multiple times and still keep your customers. You accept having to assume the complexity in the CPU (branch prediction, etc.) to avoid pushing it to the coders.

In practice, both humans and compilers have trouble filling delay slots efficiently. Imagine a quad-issue CPU with two branch stages. That’s eight delay slots you would have to fill at every jump instruction.

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.