Daten-Hazards
Rückblick auf Kapitel 33: Ohne nops rechnet die Pipeline falsch (siehe hazard_tb.v).
Mit nops funktioniert sie, aber mehr als die Hälfte aller ausgeführten Befehle sind nops (85 Takte statt 37).
In diesem Kapitel kümmert sich die Hardware selbst um Daten-Hazards - das Programm muss es nicht mehr tun. Zwei Techniken erledigen das: Forwarding (die meisten Fälle, ohne verlorenen Takt) und Anhalten (Stall, für den einen Fall, den Forwarding nicht lösen kann). Die nops hinter Sprüngen bleiben vorerst - sie sind das Thema von Kapitel 35.
Tip | Alle Beispiele dieses Kapitels liegen im Ordner |
Drei Arten von Hazards
Note | Ein Hazard ist eine Situation, in der der nächste Befehl nicht wie geplant im nächsten Takt ausgeführt werden kann. Es gibt drei Arten:
|
Der Daten-Hazard im Detail
Beispiel aus Kapitel 33: add x1, x1, x2, direkt gefolgt von sw x1, 4(x0).
add schreibt x1 in WB (Takt 5), aber sw liest x1 schon in ID (Takt 3) - zwei Takte zu früh.
Wie die obere Hälfte des Bildes zeigt, zeigen die Pfeile rückwärts in der Zeit - ohne Hilfe unmöglich.
Diese Art von Hazard heißt RAW (read after write, Lesen nach Schreiben). Sie ist die einzige Art von Daten-Hazard in unserer In-Order-Pipeline.
Die entscheidende Beobachtung: Das Ergebnis von add entsteht nicht erst in WB - es ist schon am Ende von EX berechnet
und steht im Register EX/MEM. sw braucht es erst zu Beginn seiner eigenen EX-Stufe, einen Takt später
(untere Hälfte des Bildes).
Der Wert ist rechtzeitig da, er steht nur an der falschen Stelle.
Forwarding
Die Idee
Statt auf den Registersatz zu warten, nehmen wir den Wert direkt aus dem Pipeline-Register, in dem er schon steht, und führen ihn zur ALU. Das nennt man Forwarding (oder Bypassing).
Es gibt zwei Stellen, an denen ein neuerer Wert stehen kann:
in EX/MEM - das Ergebnis des Befehls direkt davor
in MEM/WB - das Ergebnis des Befehls zwei davor oder ein geladener Wert
Den dritten Fall (drei Befehle davor) erledigt schon das Durchreichen (write-through) im Registersatz (siehe Kapitel 33).
Jeder ALU-Eingang bekommt also einen Multiplexer mit drei Eingängen: Registersatz (00), EX/MEM (10), MEM/WB (01). Irgendetwas muss in jedem Takt entscheiden, welcher Eingang der richtige ist: die Forwarding-Unit.
Herleitung der Forwarding-Unit
Wir bauen die Logik Schritt für Schritt aus Fragen auf - für ALU-Eingang A (rs1). Eingang B (rs2) funktioniert genauso.
Schreibt der Befehl in MEM überhaupt ein Register? ⇒
EX/MEM.RegWrite
(swund Sprünge tun das nicht - ihre „rd“-Bits bedeuten etwas anderes)Ist dieses Register x0? ⇒
EX/MEM.rd != 0
(x0 ist immer 0, auch wenn ein Befehl es „schreibt“ - einen Wert weiterzuleiten wäre falsch)Ist es das Register, das der Befehl in EX liest? ⇒
EX/MEM.rd == ID/EX.rs1
Lauten alle drei Antworten ja, haben wir einen „hit M“: Der aktuelle Wert von rs1 steht in EX/MEM. Dieselben drei Fragen für den Befehl in WB ergeben einen „hit W“.
Was, wenn BEIDE zutreffen? Beispiel:
add x1, …;add x1, …;add x5, x1, x1
Beide älteren Befehle schreiben x1 - der neuere (in MEM) hat den aktuellen Wert ⇒ M vor W.
Das Ergebnis als Pseudocode:
if EX/MEM.RegWrite and EX/MEM.rd != 0 and EX/MEM.rd == ID/EX.rs1 => ForwardA = 10 elif MEM/WB.RegWrite and MEM/WB.rd != 0 and MEM/WB.rd == ID/EX.rs1 => ForwardA = 01 else => ForwardA = 00
Dasselbe als Schaltung - reine Kombinatorik, nur Vergleicher und UND-Gatter (kombinatorische Logik); die Priorität funktioniert wie der Prioritätsencoder:
Ein Rundgang durch das Bild:
oben: Fragen 1 und 2 (
mem_writes,wb_writes)Mitte: Frage 3 (die Vergleicher
=)&: alle drei Antworten sind ja (hit M / hit W)rechts: Frage 4 (die Priorität)
In Verilog wird der Pseudocode zu einem einzigen always @(*)-Block mit if / else if / else -
die Reihenfolge der Zweige IST die Priorität:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Forwarding unit (chapter 34): pure combinational logic in the EX stage.
//
// The instruction in EX needs the registers rs1 and rs2. If one of the two older
// instructions in front of it (in MEM or in WB) writes exactly that register, the register
// file still holds the OLD value - but the new one already exists in a pipeline register.
// The forwarding unit finds out where, and sets the multiplexers in front of the ALU:
//
// forward = 00: value from the register file (ID/EX), no hazard
// forward = 10: value from EX/MEM (result of the instruction directly before)
// forward = 01: value from MEM/WB (result of the instruction two before, or a loaded value)
//
// Three instructions before is already handled by the write-through of the register file.
module forwarding_unit (
input wire [4:0] id_ex_rs1, // registers read by the instruction in EX
input wire [4:0] id_ex_rs2,
input wire ex_mem_reg_write, // instruction in MEM: writes a register? which one?
input wire [4:0] ex_mem_rd,
input wire mem_wb_reg_write, // instruction in WB: writes a register? which one?
input wire [4:0] mem_wb_rd,
output reg [1:0] forward_a, // multiplexer in front of ALU input A (rs1)
output reg [1:0] forward_b // multiplexer in front of ALU input B and store data (rs2)
);
// x0 is always 0, even if an instruction "writes" it: never forward to x0
wire mem_writes = ex_mem_reg_write && (ex_mem_rd != 0);
wire wb_writes = mem_wb_reg_write && (mem_wb_rd != 0);
// The order matters: if BOTH older instructions write the same register,
// the newer one (in MEM) wins - its value is the current one.
always @(*) begin
if (mem_writes && ex_mem_rd == id_ex_rs1) forward_a = 2'b10;
else if (wb_writes && mem_wb_rd == id_ex_rs1) forward_a = 2'b01;
else forward_a = 2'b00;
if (mem_writes && ex_mem_rd == id_ex_rs2) forward_b = 2'b10;
else if (wb_writes && mem_wb_rd == id_ex_rs2) forward_b = 2'b01;
else forward_b = 2'b00;
end
endmodule
Die Forwarding-Unit in der CPU
Wo die Unit sitzt: in EX, unter der ALU. Ihre Eingänge kommen aus drei Pipeline-Registern: rs1/rs2 aus ID/EX, rd und RegWrite aus EX/MEM und aus MEM/WB (orange, wie die rd-Leitung in Kapitel 33).
Ihre Ausgänge ForwardA/ForwardB steuern die beiden neuen Multiplexer vor der ALU.
Die grünen Pfade: Das ALU-Ergebnis in EX/MEM (M) und der Ausgang des WB-Multiplexers (W) gehen zurück zu den Multiplexern.
Neu in den Pipeline-Registern: ID/EX muss rs1 und rs2 mitführen, damit EX weiß, welche Register der Befehl liest. rs2 nur bei Befehlen, die ihn wirklich lesen (R-Typ,
sw, Sprünge) - beiaddiundlwgehören diese Bits zur Konstante.swnicht vergessen: Auch die Daten zum Speichern müssen aus dem ForwardB-Multiplexer kommen, nicht direkt aus ID/EX (add x1 …gefolgt vonsw x1, 4(x0)ist genau unser Zählprogramm).Das Bild ist ein vereinfachter Ausschnitt (ID/EX bis WB); vergleiche es mit
cpu_pipelined.svgaus Kapitel 33.
Die Ausgabe der Testbench (00-forwarding/cpu_tb.v) zeigt alle Fälle in drei Takten:
cycle 6 ... EX add 0c ... fwd A -- B W add x1,x1,x2 gets x2 from MEM/WB (addi x2 two before) cycle 7 ... EX sw 10 ... fwd A -- B M sw gets x1 from EX/MEM (add directly before) cycle 8 ... EX bne 14 ... fwd A W B -- bne gets x1 from MEM/WB (add two before)
Die Grenze des Forwardings: der Load-Use-Hazard
lw x4, 4(x0), direkt gefolgt von add x5, x3, x4:
Der geladene Wert steht erst am ENDE von MEM fest, aber add braucht ihn zu BEGINN seiner EX-Stufe - im selben Moment.
Forwarding kann keinen Wert in die Vergangenheit schicken.
Die Lösung: einen Takt lang anhalten (Stall, auch Interlock).
Die Hazard Detection Unit in ID prüft:
ID/EX.MemRead and (ID/EX.rd == IF/ID.rs1 or ID/EX.rd == IF/ID.rs2)Wenn ja, behalten PC und IF/ID ihre Werte (Write-Enable = 0) - IF und ID wiederholen dieselben Befehle - und ID/EX bekommt lauter Steuersignale 0: eine Blase (bubble, ein von der Hardware erzeugtes nop).
Einen Takt später kommt der Wert per Forwarding aus MEM/WB.
Was passiert OHNE den Stall (00-forwarding/load_tb.v, schlägt absichtlich fehl)?
Die Forwarding-Unit sieht „lw schreibt x4“ und leitet weiter, was in EX/MEM steht - bei einem Load ist das aber die ADRESSE, nicht die Daten.
Ergebnis: memory[12] = 3 + 4 (die Adresse!) = 7 statt 8, memory[16] = 12 + 12 = 24 statt 16.
Der Compiler kann helfen: Stellt er einen unabhängigen Befehl zwischen lw und seine Verwendung (Instruction Scheduling),
verschwindet der Stall.
Verilog
00-forwarding: Was sich in cpu.v ändert
Gegenüber der Pipeline aus Kapitel 33 ändert sich cpu.v nur an vier Stellen (im Code mit NEW markiert):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
reg [4:0] id_ex_rd;
+ reg [4:0] id_ex_rs1, id_ex_rs2; // NEW: for the forwarding unit
...
id_ex_rd <= if_id_inst[11:7];
+ id_ex_rs1 <= rs1; // NEW
+ id_ex_rs2 <= (!alu_src || mem_write) ? rs2 : 5'd0;
...
+ // NEW: forwarding - where is the current value of rs1 and rs2?
+ wire [1:0] forward_a, forward_b;
+
+ forwarding_unit fwd (
+ .id_ex_rs1(id_ex_rs1), .id_ex_rs2(id_ex_rs2),
+ .ex_mem_reg_write(ex_mem_reg_write), .ex_mem_rd(ex_mem_rd),
+ .mem_wb_reg_write(mem_wb_reg_write), .mem_wb_rd(mem_wb_rd),
+ .forward_a(forward_a), .forward_b(forward_b)
+ );
+
+ // NEW: one 3-input multiplexer in front of each ALU input
+ wire [31:0] fwd_a = (forward_a == 2'b10) ? ex_mem_alu_result : // from EX/MEM
+ (forward_a == 2'b01) ? wb_data : // from MEM/WB
+ id_ex_rd1; // from the register file
+ wire [31:0] fwd_b = (forward_b == 2'b10) ? ex_mem_alu_result :
+ (forward_b == 2'b01) ? wb_data :
+ id_ex_rd2;
+
- wire [31:0] alu_b = id_ex_alu_src ? id_ex_imm : id_ex_rd2;
+ wire [31:0] alu_b = id_ex_alu_src ? id_ex_imm : fwd_b;
...
- .a(id_ex_rd1), .b(alu_b), .ctrl(alu_ctrl),
+ .a(fwd_a), .b(alu_b), .ctrl(alu_ctrl),
...
- ex_mem_rd2 <= id_ex_rd2; // the value sw stores
+ ex_mem_rd2 <= fwd_b; // the value sw stores, forwarded as well
ID/EX führt rs1 und rs2 mit - rs2 nur, wenn der Befehl ihn wirklich liest.
Die Forwarding-Unit wird in EX eingebunden.
Zwei Multiplexer wählen zwischen Registersatz, EX/MEM und MEM/WB. Der Multiplexer mit drei Eingängen ist als Kette von
? :geschrieben - genau wie die Multiplexer mit zwei Eingängen vorher.Die ALU und die Speicherdaten von
swbekommen ihre Operanden aus diesen Multiplexern statt direkt aus ID/EX.
Das vollständige cpu.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Pipelined RISC-V CPU with forwarding (chapter 34): the pipeline from chapter 33,
// extended by a forwarding unit and two multiplexers in front of the ALU.
//
// IF --IF/ID--> ID --ID/EX--> EX --EX/MEM--> MEM --MEM/WB--> WB
// ^ | |
// +---- forwarding -+-----------------+
//
// New compared to chapter 33 (marked with "NEW" below):
// - ID/EX also carries rs1 and rs2, so EX knows which registers its instruction reads
// - forwarding_unit.v decides where the current value of rs1 and rs2 is
// - a 3-input multiplexer in front of each ALU input takes it from there
// - the store data of sw also comes out of the multiplexer for rs2
//
// Data hazards between ALU instructions are now handled by the hardware: the program no
// longer needs nops between writing and reading a register. Still NOT handled:
// - a loaded value that is used by the very next instruction (load-use, see 01-load-use)
// - the 3 instructions behind a taken branch are still executed (chapter 35)
//
// The registers *_pc and *_inst are carried along only so the testbench can show which
// instruction is in which stage.
module cpu #(
parameter PROGRAM = "program_fwd.hex"
) (
input wire clk,
input wire rst
);
// =========================================================================
// IF: instruction fetch
// =========================================================================
reg [31:0] pc;
wire [31:0] inst;
wire [31:0] pc_plus4 = pc + 4;
instr_mem #(.PROGRAM(PROGRAM)) imem (.addr(pc), .inst(inst));
// the branch decision comes back from the MEM stage (see below)
wire pc_src;
wire [31:0] branch_target_mem;
wire [31:0] pc_next = pc_src ? branch_target_mem : pc_plus4;
always @(posedge clk)
if (rst) pc <= 0;
else pc <= pc_next;
// ---- pipeline register IF/ID ---------------------------------------------
reg [31:0] if_id_pc, if_id_inst;
always @(posedge clk)
if (rst) begin
if_id_pc <= 0;
if_id_inst <= 0; // 0 = empty stage ("bubble"), every control signal is 0
end else begin
if_id_pc <= pc;
if_id_inst <= inst;
end
// =========================================================================
// ID: decode and read registers
// =========================================================================
wire [6:0] opcode = if_id_inst[6:0];
wire [4:0] rs1 = if_id_inst[19:15];
wire [4:0] rs2 = if_id_inst[24:20];
wire reg_write, alu_src, mem_to_reg, mem_read, mem_write, branch;
wire [1:0] alu_op;
control ctl (
.opcode(opcode), .reg_write(reg_write), .alu_src(alu_src),
.mem_to_reg(mem_to_reg), .mem_read(mem_read), .mem_write(mem_write),
.branch(branch), .alu_op(alu_op)
);
wire [31:0] rd1, rd2, imm;
// the write port is driven by the WB stage: a DIFFERENT, older instruction
wire wb_reg_write;
wire [4:0] wb_rd;
wire [31:0] wb_data;
regfile rf (
.clk(clk), .rs1(rs1), .rs2(rs2), .rd1(rd1), .rd2(rd2),
.we(wb_reg_write), .rd(wb_rd), .wd(wb_data)
);
imm_gen ig (.inst(if_id_inst), .imm(imm));
// ---- pipeline register ID/EX ---------------------------------------------
// control signals for the later stages
reg id_ex_reg_write, id_ex_mem_to_reg; // for WB
reg id_ex_mem_read, id_ex_mem_write, id_ex_branch; // for MEM
reg id_ex_alu_src; reg [1:0] id_ex_alu_op; // for EX
// data
reg [31:0] id_ex_pc, id_ex_rd1, id_ex_rd2, id_ex_imm, id_ex_inst;
reg [4:0] id_ex_rd;
reg [4:0] id_ex_rs1, id_ex_rs2; // NEW: for the forwarding unit
reg [2:0] id_ex_funct3;
reg id_ex_funct7_5;
always @(posedge clk)
if (rst) begin
{id_ex_reg_write, id_ex_mem_to_reg, id_ex_mem_read, id_ex_mem_write,
id_ex_branch, id_ex_alu_src, id_ex_alu_op} <= 0;
id_ex_inst <= 0; id_ex_pc <= 0;
end else begin
id_ex_reg_write <= reg_write;
id_ex_mem_to_reg <= mem_to_reg;
id_ex_mem_read <= mem_read;
id_ex_mem_write <= mem_write;
id_ex_branch <= branch;
id_ex_alu_src <= alu_src;
id_ex_alu_op <= alu_op;
id_ex_pc <= if_id_pc;
id_ex_rd1 <= rd1;
id_ex_rd2 <= rd2;
id_ex_imm <= imm;
id_ex_rd <= if_id_inst[11:7];
id_ex_rs1 <= rs1; // NEW
// NEW: rs2 only if the instruction really reads it (R-type, sw, branches). For addi and lw
// these bits are part of the immediate - forwarding them would be harmless, but confusing.
id_ex_rs2 <= (!alu_src || mem_write) ? rs2 : 5'd0;
id_ex_funct3 <= if_id_inst[14:12];
id_ex_funct7_5 <= if_id_inst[30];
id_ex_inst <= if_id_inst;
end
// =========================================================================
// EX: execute (ALU) and compute the branch target
// =========================================================================
// NEW: forwarding - where is the current value of rs1 and rs2?
wire [1:0] forward_a, forward_b;
forwarding_unit fwd (
.id_ex_rs1(id_ex_rs1), .id_ex_rs2(id_ex_rs2),
.ex_mem_reg_write(ex_mem_reg_write), .ex_mem_rd(ex_mem_rd),
.mem_wb_reg_write(mem_wb_reg_write), .mem_wb_rd(mem_wb_rd),
.forward_a(forward_a), .forward_b(forward_b)
);
// NEW: one 3-input multiplexer in front of each ALU input
wire [31:0] fwd_a = (forward_a == 2'b10) ? ex_mem_alu_result : // from EX/MEM
(forward_a == 2'b01) ? wb_data : // from MEM/WB
id_ex_rd1; // from the register file
wire [31:0] fwd_b = (forward_b == 2'b10) ? ex_mem_alu_result :
(forward_b == 2'b01) ? wb_data :
id_ex_rd2;
wire [3:0] alu_ctrl;
wire [31:0] alu_b = id_ex_alu_src ? id_ex_imm : fwd_b; // CHANGED: fwd_b instead of id_ex_rd2
wire [31:0] alu_result;
wire zero, overflow;
alu_control actl (.alu_op(id_ex_alu_op), .funct7_5(id_ex_funct7_5),
.funct3(id_ex_funct3), .alu_ctrl(alu_ctrl));
alu #(.N(32)) alu (
.a(fwd_a), .b(alu_b), .ctrl(alu_ctrl),
.result(alu_result), .zero(zero), .overflow(overflow)
);
wire [31:0] branch_target = id_ex_pc + id_ex_imm;
wire branch_taken = zero ^ id_ex_funct3[0]; // funct3[0] = 1 for bne
// ---- pipeline register EX/MEM --------------------------------------------
reg ex_mem_reg_write, ex_mem_mem_to_reg;
reg ex_mem_mem_read, ex_mem_mem_write, ex_mem_branch;
reg [31:0] ex_mem_branch_target, ex_mem_alu_result, ex_mem_rd2, ex_mem_pc, ex_mem_inst;
reg ex_mem_taken;
reg [4:0] ex_mem_rd;
always @(posedge clk)
if (rst) begin
{ex_mem_reg_write, ex_mem_mem_to_reg, ex_mem_mem_read,
ex_mem_mem_write, ex_mem_branch} <= 0;
ex_mem_inst <= 0; ex_mem_pc <= 0;
end else begin
ex_mem_reg_write <= id_ex_reg_write;
ex_mem_mem_to_reg <= id_ex_mem_to_reg;
ex_mem_mem_read <= id_ex_mem_read;
ex_mem_mem_write <= id_ex_mem_write;
ex_mem_branch <= id_ex_branch;
ex_mem_branch_target <= branch_target;
ex_mem_taken <= branch_taken;
ex_mem_alu_result <= alu_result;
ex_mem_rd2 <= fwd_b; // CHANGED: the value sw stores, forwarded as well
ex_mem_rd <= id_ex_rd;
ex_mem_pc <= id_ex_pc;
ex_mem_inst <= id_ex_inst;
end
// =========================================================================
// MEM: data memory access and branch decision
// =========================================================================
wire [31:0] mem_data;
data_mem dmem (
.clk(clk), .addr(ex_mem_alu_result), .mem_read(ex_mem_mem_read),
.mem_write(ex_mem_mem_write), .wd(ex_mem_rd2), .rd(mem_data)
);
// back to the PC: by now, 3 more instructions have been fetched behind the branch
assign pc_src = ex_mem_branch & ex_mem_taken;
assign branch_target_mem = ex_mem_branch_target;
// ---- pipeline register MEM/WB --------------------------------------------
reg mem_wb_reg_write, mem_wb_mem_to_reg;
reg [31:0] mem_wb_mem_data, mem_wb_alu_result, mem_wb_pc, mem_wb_inst;
reg [4:0] mem_wb_rd;
always @(posedge clk)
if (rst) begin
{mem_wb_reg_write, mem_wb_mem_to_reg} <= 0;
mem_wb_inst <= 0; mem_wb_pc <= 0;
end else begin
mem_wb_reg_write <= ex_mem_reg_write;
mem_wb_mem_to_reg <= ex_mem_mem_to_reg;
mem_wb_mem_data <= mem_data;
mem_wb_alu_result <= ex_mem_alu_result;
mem_wb_rd <= ex_mem_rd;
mem_wb_pc <= ex_mem_pc;
mem_wb_inst <= ex_mem_inst;
end
// =========================================================================
// WB: write back (into the register file in the ID stage, see above)
// =========================================================================
assign wb_reg_write = mem_wb_reg_write;
assign wb_rd = mem_wb_rd;
assign wb_data = mem_wb_mem_to_reg ? mem_wb_mem_data : mem_wb_alu_result;
endmodule
00-forwarding: Programm und Testbench
Das Programm ist das Zählprogramm OHNE die nops für Daten-Hazards - add, sw und bne folgen jetzt direkt aufeinander.
Nur die 3 nops hinter bne bleiben, bis Kapitel 35.
Die Testbench cpu_tb.v gibt das Pipeline-Diagramm und die Forwarding-Signale des Befehls in EX aus
(-- kein Forwarding, M aus EX/MEM, W aus MEM/WB) - die Takte 6–8 standen schon oben. Am Ende:
33 useful instructions + 27 nops in 64 cycles
CPI (useful instructions only) = 1.94
CPU time with 200 ps per cycle: 64 x 200 ps = 12.8 ns
for comparison: single-cycle 26.4 ns, multi-cycle 24.4 ns, pipeline with nops 17.0 ns,
ideal pipeline 37 x 200 ps = 7.4 ns
PASSED: counted to 10, 33 useful instructions in 64 cycles01-load-use: der Stall
Der Load-Use-Stall bringt drei weitere Änderungen in cpu.v (im Code mit STALL markiert):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
+ // STALL: load-use hazard?
+ wire stall;
+
+ hazard_unit hz (
+ .id_ex_mem_read(id_ex_mem_read), .id_ex_rd(id_ex_rd),
+ .if_id_rs1(rs1), .if_id_rs2(uses_rs2 ? rs2 : 5'd0),
+ .stall(stall)
+ );
...
- if (rst) pc <= 0;
- else pc <= pc_next;
+ if (rst) pc <= 0;
+ else if (!stall) pc <= pc_next; // STALL: keep the PC
...
- end else begin
+ end else if (!stall) begin // STALL: keep the instruction in ID
if_id_pc <= pc;
if_id_inst <= inst;
...
- if (rst) begin
+ if (rst || stall) begin // STALL: a bubble instead of the instruction
{id_ex_reg_write, id_ex_mem_to_reg, id_ex_mem_read, id_ex_mem_write,
id_ex_branch, id_ex_alu_src, id_ex_alu_op} <= 0;
Die Hazard-Unit sitzt in ID und erzeugt ein einziges Signal:
stall.stallhält den PC und IF/ID fest - IF und ID wiederholen ihre Befehle.stallmacht aus dem nächsten Eintrag in ID/EX eine Blase, und zwar im selben Zweig wie der Reset: Eine Blase ist nichts anderes als „alle Steuersignale 0“.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Hazard detection unit (chapter 34): pure combinational logic in the ID stage.
//
// Load-use hazard: the instruction in EX is a load (lw), and the instruction in ID needs
// its destination register. The loaded value exists only at the end of the MEM stage -
// too late even for forwarding. The only way out is to wait one cycle:
//
// stall = 1: - PC and IF/ID keep their values: IF and ID repeat the same instructions
// - ID/EX gets all control signals 0: a bubble moves on instead of the instruction
//
// One cycle later, the load is in WB and its value comes to the ALU via forwarding (MEM/WB).
module hazard_unit (
input wire id_ex_mem_read, // instruction in EX is a load?
input wire [4:0] id_ex_rd, // ... and loads into which register?
input wire [4:0] if_id_rs1, // registers read by the instruction in ID
input wire [4:0] if_id_rs2, // (0 if the instruction does not read rs2)
output wire stall
);
assign stall = id_ex_mem_read && (id_ex_rd != 0) &&
(id_ex_rd == if_id_rs1 || id_ex_rd == if_id_rs2);
endmodule
Die Testbench load_tb.v lässt program_load.hex laufen und markiert den Stall (bub = Blase):
cycle 7 IF add 18 ID lw 14 EX lw 10 MEM sw 0c WB sw 08 fwd A -- B -- cycle 8 IF sw 1c ID add 18 EX lw 14 MEM lw 10 WB sw 0c fwd A -- B -- STALL cycle 9 IF sw 1c ID add 18 EX bub 00 MEM lw 14 WB lw 10 fwd A -- B -- cycle 10 IF lw 20 ID sw 1c EX add 18 MEM bub 00 WB lw 14 fwd A -- B W ... 11 instructions + 2 stall cycles in 17 cycles = 11 instructions + 4 cycles to fill the pipeline + 2 stalls CPI = 1.55
In Takt 8 steht add in ID und braucht x4, das lw in EX gerade lädt: STALL.
In Takt 9 wiederholen IF und ID sw und add, und eine Blase rückt nach EX - genau wie im Bild oben.
In Takt 10 steht add in EX und bekommt x4 aus MEM/WB (B W).
Performance: Hat es sich gelohnt?
Zählprogramm (gemessen mit 00-forwarding/cpu_tb.v): 3 + 10 × 3 Befehle + 9 × 3 nops hinter Sprüngen = 60 Befehle ⇒ 60 + 4 = 64 Takte, 64 × 200 ps = 12,8 ns
| CPU | Takte | CPU-Zeit | Speedup |
|---|---|---|---|
Single-Cycle | 33 | 26,4 ns | 1,0 |
Multi-Cycle | 122 | 24,4 ns | 1,08 |
Pipeline mit nops (Kap. 33) | 85 | 17,0 ns | 1,55 |
Pipeline mit Forwarding | 64 | 12,8 ns | 2,06 |
ideale Pipeline | 37 | 7,4 ns | 3,6 |
Alle nops für Daten-Hazards sind weg; übrig bleiben die 27 nops hinter Sprüngen ⇒ Kapitel 35.
Load-Use-Programm (01-load-use/load_tb.v): 11 Befehle + 4 Takte zum Füllen + 2 Stalls = 17 Takte, CPI 1,55.
Der Preis: Die Forwarding-Multiplexer liegen auf dem kritischen Pfad von EX (Kapitel 31) - in einem echten Entwurf wird T etwas länger. Forwarding lohnt sich trotzdem bei Weitem.
Forwarding in echten Prozessoren
Jeder Prozessor mit Pipeline nutzt Forwarding - es ist nicht optional.
Geschichtlich: MIPS = „Microprocessor without Interlocked Pipeline Stages“ - die R2000 (MIPS I) hatte keinen Load-Use-Stall; stattdessen durfte der Befehl nach einem Load dessen Ergebnis nicht verwenden (Load Delay Slot), der Compiler musste ihn füllen. Ab MIPS II (zuerst umgesetzt in der R6000, 1989) gab es keinen Load Delay Slot mehr, und die Hardware hält selbst an; die weit verbreitete R4000 (1991) arbeitet genauso.
Probier es selbst
Vertausche die Priorität in der Forwarding-Unit (MEM/WB vor EX/MEM): Welches Programm aus drei Befehlen schlägt fehl?
Entferne das Forwarding für die Speicherdaten: Welcher Befehl des Zählprogramms speichert den falschen Wert?
Entferne das Durchreichen aus regfile.v: Braucht es einen dritten Forwarding-Pfad, oder funktioniert es trotzdem? Warum?
Schreibe ein kleines Programm mit zwei Loads und zwei Verwendungen; sortiere es so um, dass kein Stall übrig bleibt.
Sprünge vergleichen in EX: Bekommt bne seinen Operanden ebenfalls per Forwarding? Prüfe es in der Testbench.
Zusammenfassung
Daten-Hazards (RAW): Ein Befehl braucht ein Ergebnis, das noch nicht im Registersatz steht.
Forwarding bringt das Ergebnis direkt aus EX/MEM oder MEM/WB zur ALU - ohne verlorenen Takt.
Load-Use: Der geladene Wert kommt sogar für Forwarding zu spät ⇒ ein Stall-Takt (Blase), erkannt in ID.
Das Programm braucht keine nops für Daten-Hazards mehr: 85 ⇒ 64 Takte für das Zählprogramm.
Ausblick: Kapitel 35 nimmt sich die nops hinter Sprüngen vor (Steuer-Hazards).