Pipelining
Rückblick auf Kapitel 32: Die Multi-Cycle-CPU zerlegt einen Befehl in Schritte, aber in jedem Takt haben die meisten Teile nichts zu tun (während EX tut der Befehlsspeicher nichts, während MEM tut die ALU nichts …). Idee: Jedes Teil arbeitet gleichzeitig an einem anderen Befehl - wie an einem Fließband oder wie im klassischen Wäsche-Beispiel: Waschmaschine, Trockner, Zusammenlegen - die nächste Ladung kommt in die Waschmaschine, sobald die erste in den Trockner wandert. Ziel dieses Kapitels: die fünfstufige Pipeline in Verilog und ein erster Blick darauf, was dabei schiefgeht.
Tip | Alle Beispiele dieses Kapitels liegen im Ordner |
Das Fließband-Prinzip
Note | Wir unterscheiden hier zwei Begriffe für Performance: Latenz und Durchsatz
|
Abzählen: k Stufen, n Befehle, keine Wartezyklen:
Der erste Befehl braucht k Takte, um die Pipeline zu „füllen“, danach wird in jedem Takt ein Befehl fertig.
Zählprogramm: 5 + (33 - 1) = 37 Takte (die Vorschau-Zahl aus Kapitel 31).
Für großes n geht der CPI gegen 1 - und das mit dem kurzen 200-ps-Takt der Multi-Cycle-CPU.
Die fünf Stufen
Die fünf Stufen sind dieselben Schritte wie bei der Multi-Cycle-CPU (Kapitel 32) - aber jetzt ist jede ein eigenes Stück Hardware: IF (Befehl holen: Befehlsspeicher, PC + 4) | ID (Befehl dekodieren: Decoder, Registersatz, Immediate) | EX (Ausführen: ALU, Sprungziel) | MEM (Speicherzugriff: Datenspeicher, Sprungentscheidung) | WB (Zurückschreiben: Register schreiben)
Jeder Befehl durchläuft alle fünf Stufen, auch wenn er in einer davon nichts tut (add in MEM, sw in WB). Grund: Zwei Befehle dürfen nie im selben Takt dieselbe Stufe brauchen. ⇒ Die Latenz pro Befehl beträgt jetzt 5 × 200 ps = 1000 ps, sogar für add (Single-Cycle: 800 ps für jeden Befehl).
Der Takt wird von der langsamsten Stufe bestimmt (200 ps) - die Stufen sollten ausgeglichen sein. ID und WB brauchen nur 100 ps (derselbe Verlust wie in Kapitel 32).
Warum sich keine Hardware mehr teilen lässt (im Gegensatz zu Kapitel 32):
die ALU ist mit EX eines Befehls beschäftigt, während IF eines anderen PC + 4 braucht ⇒ eigener Addierer
Befehls- und Datenspeicher werden im selben Takt gebraucht (IF und MEM) ⇒ zwei Speicher
Teilen würde einen STRUKTURELLEN HAZARD erzeugen (Begriff, genauer erklärt in Kapitel 34)
Note | RISC vs. CISC - warum Pipelining so gut zu RISC passt In Kapitel 15 haben wir Befehlssätze in RISC (reduced instruction set computer, z. B. RISC-V, ARM, MIPS) und CISC (complex instruction set computer, z. B. x86) eingeteilt. Jetzt sehen wir, warum der Unterschied so wichtig ist:
Deshalb wurde RISC Anfang der 1980er-Jahre zusammen mit dem Pipelining entwickelt (Berkeley RISC von David Patterson, Stanford MIPS von John Hennessy - die Autoren unseres Lehrbuchs). CISC ist nicht verschwunden: Moderne x86-Prozessoren übersetzen ihre komplexen Befehle in einfache, RISC-artige Mikro-Operationen und schicken diese durch die Pipeline. |
Pipeline-Register
Zwischen zwei Stufen sitzt jeweils ein Pipeline-Register (IF/ID, ID/EX, EX/MEM, MEM/WB); bei jeder steigenden Flanke rückt jeder Befehl eine Stufe weiter. Vergleich mit Kapitel 32 (Tabelle): Die Register der Multi-Cycle-CPU (IR, A, B, ALUOut, MDR) halten Werte EINES Befehls, die Pipeline-Register halten Werte von bis zu VIER verschiedenen Befehlen gleichzeitig.
Multi-Cycle (Kap. 32) | Pipeline (Kap. 33) | |
Was steht in den Registern? | Werte eines Befehls | Werte von bis zu vier Befehlen |
Hardware | geteilt (eine ALU) | ein eigenes Stück pro Stufe |
Steuerung | Automat, pro Takt | Signale wandern mit dem Befehl mit |
Was ein Befehl mit sich trägt (aus cpu.v):
IF/ID: PC, Befehl
ID/EX: PC, rd1, rd2, imm, rd, funct3/funct7, Steuersignale für EX, MEM, WB
EX/MEM: Sprungziel, Zero/genommen, ALU-Ergebnis, rd2 (Daten für sw), rd, Steuersignale für MEM, WB
MEM/WB: Speicherdaten, ALU-Ergebnis, rd, Steuersignale für WB
Die orange Leitung: Warum rd bis WB mitwandern und zurückkommen muss - in IF/ID steht schon ein anderer Befehl, wenn das Ergebnis geschrieben wird. (Ein klassischer Fehler im ersten eigenen Pipeline-Entwurf.)
Die Steuerung in der Pipeline
Überraschung: Das Steuerwerk wird wieder einfach - die kombinatorische Steuerung aus Kapitel 17 (control.v),
unverändert, in der ID-Stufe. Kein Automat mehr.
Die Steuersignale werden einmal in ID erzeugt und wandern durch die Pipeline-Register;
jede Stufe nutzt ihre Gruppe und reicht den Rest weiter (EX: ALUSrc, ALUOp; MEM: MemRead, MemWrite, Branch;
WB: RegWrite, MemtoReg).
Leere Stufe nach dem Reset („Blase“, engl. bubble): alle Steuersignale 0 ⇒ nichts passiert.
Zwei Details
Registersatz: Ein Befehl schreibt in WB, während ein späterer in ID liest - im SELBEN Takt. Lehrbücher: in der ersten Hälfte des Takts schreiben, in der zweiten lesen. Hier: Durchreichen (write-through) in
regfile.v(wird das Register gerade geschrieben, wird der neue Wert direkt weitergegeben) - ohne negative Taktflanke (siehe Kapitel 27, warum die negative Flanke auf dem FPGA schadet).Sprungentscheidung in MEM: Der PC bekommt das Sprungziel erst am Ende der MEM-Stufe des Sprungbefehls. Bis dahin sind die drei folgenden Befehle schon in IF, ID und EX - und sie werden ausgeführt.
Die CPU mit Pipeline in Verilog
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
// Pipelined RISC-V CPU (chapter 33): the single-cycle data path from chapter 26,
// cut into 5 stages by 4 pipeline registers.
//
// IF --IF/ID--> ID --ID/EX--> EX --EX/MEM--> MEM --MEM/WB--> WB
// fetch decode, ALU, data memory, write back
// read registers branch target branch decision
//
// Every stage works on a different instruction in the same clock cycle. At every rising
// edge, each instruction moves one stage further, together with everything it still needs
// later: its control signals, its operands, its destination register.
//
// This pipeline has NO hazard handling: it does not notice when an instruction needs a
// result that is not written yet (data hazard, chapter 34), and it does not stop the
// instructions behind a taken branch (control hazard, chapter 35). The program has to take
// care of that itself (program_nops.hex).
//
// The registers *_pc and *_inst are carried along only so the testbench can show which
// instruction is in which stage. The CPU itself does not need them after ID (except the
// PC for the branch target in EX).
module cpu #(
parameter PROGRAM = "program_nops.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 [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_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
// =========================================================================
wire [3:0] alu_ctrl;
wire [31:0] alu_b = id_ex_alu_src ? id_ex_imm : 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(id_ex_rd1), .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 <= id_ex_rd2; // the value sw stores
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
Was sich gegenüber der Single-Cycle-CPU aus Kapitel 26 geändert hat: nur Schnitte und Register, die Bauteile (ALU, Registersatz, Speicher, Steuerung, imm_gen) sind dieselben Module.
regfile.v (Durchreichen), instr_mem.v (mit nop gefüllt, damit Befehle, die hinter einem Sprung geholt werden, harmlos sind).
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
`timescale 1ns / 1ps
// Testbench for the pipelined CPU (chapter 33): runs the counter program with nops
// (program_nops.hex). One line per clock cycle shows which instruction is in which stage,
// like the pipeline diagram in the chapter. At the end, the result and the performance
// are checked and compared with chapters 31 and 32.
module cpu_tb;
reg clk = 0, rst = 1;
always #5 clk = ~clk; // simulation only: 10 ns per cycle
cpu #(.PROGRAM("program_nops.hex")) dut (.clk(clk), .rst(rst));
// copies of single array elements, so they appear in the waveform (see chapter 26)
wire [31:0] x1 = dut.rf.regs[1];
wire [31:0] x3 = dut.rf.regs[3];
wire [31:0] mem4 = dut.dmem.mem[1]; // byte address 4 = word 1
localparam TRACE_CYCLES = 30; // how many cycles are printed
localparam [31:0] BNE_ADDR = 32'h20; // the program is done after the 10th bne
// short name of an instruction for the diagram
function [8*5-1:0] mnemonic(input [31:0] i);
case (i[6:0])
7'b0110011: case (i[14:12])
3'b000: mnemonic = i[30] ? "sub" : "add";
3'b111: mnemonic = "and";
3'b110: mnemonic = "or";
default: mnemonic = "?";
endcase
7'b0010011: mnemonic = (i == 32'h00000013) ? "nop" : "addi";
7'b0000011: mnemonic = "lw";
7'b0100011: mnemonic = "sw";
7'b1100011: mnemonic = i[12] ? "bne" : "beq";
default: mnemonic = "-"; // empty stage after reset
endcase
endfunction
// the instruction in the WB stage is the one that completes in this cycle
wire [31:0] wb_inst = dut.mem_wb_inst;
wire wb_nop = (wb_inst == 32'h00000013) || (wb_inst == 0);
wire wb_bne = (wb_inst[6:0] == 7'b1100011) && (dut.mem_wb_pc == BNE_ADDR);
integer cycles = 0, useful = 0, nops = 0, bnes = 0, writes = 0, errors = 0;
reg finished = 0;
always @(posedge clk)
if (!rst && !finished) begin
cycles = cycles + 1;
if (cycles <= TRACE_CYCLES)
$display("cycle %3d IF %-4s %02h ID %-4s %02h EX %-4s %02h MEM %-4s %02h WB %-4s %02h",
cycles,
mnemonic(dut.inst), dut.pc[7:0],
mnemonic(dut.if_id_inst), dut.if_id_pc[7:0],
mnemonic(dut.id_ex_inst), dut.id_ex_pc[7:0],
mnemonic(dut.ex_mem_inst), dut.ex_mem_pc[7:0],
mnemonic(wb_inst), dut.mem_wb_pc[7:0]);
if (cycles == TRACE_CYCLES + 1) $display("...");
// every store must write the next number
if (dut.ex_mem_mem_write) begin
writes = writes + 1;
if (dut.ex_mem_rd2 !== writes) begin
errors = errors + 1;
$display("ERROR: store %0d writes %0d", writes, dut.ex_mem_rd2);
end
end
// count the instructions that complete in WB
if (wb_nop) begin
if (wb_inst != 0) nops = nops + 1;
end else
useful = useful + 1;
if (wb_bne) begin
bnes = bnes + 1;
if (bnes == 10) finished = 1; // the last bne has left the pipeline
end
end
initial begin
$dumpfile("cpu.vcd");
$dumpvars(0, cpu_tb);
#12 rst = 0;
wait (finished);
@(negedge clk);
$display("");
$display("%0d useful instructions + %0d nops in %0d cycles", useful, nops, cycles);
$display(" CPI (useful instructions only) = %.2f", 1.0 * cycles / useful);
$display(" CPU time with 200 ps per cycle: %0d x 200 ps = %.1f ns", cycles, cycles * 0.2);
$display(" for comparison: single-cycle 26.4 ns, multi-cycle 24.4 ns,");
$display(" ideal pipeline without nops 37 x 200 ps = 7.4 ns");
if (x1 == 10 && x3 == 10 && mem4 == 10 && writes == 10 && errors == 0
&& useful == 33 && cycles == 85)
$display("PASSED: counted to 10, 33 useful instructions in 85 cycles");
else
$display("FAILED: x1 = %0d, memory[4] = %0d, %0d writes, %0d instructions, %0d cycles",
x1, mem4, writes, useful, cycles);
$finish;
end
endmodule
Testbench cpu_tb.v: gibt aus, welcher Befehl in welcher Stufe ist - das Pipeline-Diagramm von oben, erzeugt von der Simulation. Ausgabe (die ersten Takte):
cycle 1 IF addi 00 ID - 00 EX - 00 MEM - 00 WB - 00 cycle 2 IF addi 04 ID addi 00 EX - 00 MEM - 00 WB - 00 cycle 3 IF addi 08 ID addi 04 EX addi 00 MEM - 00 WB - 00 cycle 4 IF nop 0c ID addi 08 EX addi 04 MEM addi 00 WB - 00 cycle 5 IF add 10 ID nop 0c EX addi 08 MEM addi 04 WB addi 00 ...
Achte auf die Diagonale: Ein Befehl wandert nach rechts unten; eine Spalte = ein Zeitpunkt, fünf Befehle.
Es funktioniert noch nicht: Hazards
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
`timescale 1ns / 1ps
// What goes wrong without the nops (chapter 33, outlook to chapters 34 and 35):
// the SAME pipelined CPU runs the ORIGINAL counter program from chapter 26 (program.hex),
// which works perfectly on the single-cycle and the multi-cycle CPU.
//
// Two things go wrong:
// - data hazard: "add x1, x1, x2" reads x2 before "addi x2, x0, 1" has written it,
// "sw" and "bne" read x1 before "add" has written it
// - control hazard: the 3 instructions behind "bne" are executed even when the branch
// is taken, among them "beq x0, x0, done" at 0x18
//
// The testbench prints every store and every branch decision for 40 cycles and then
// compares the result with what the program SHOULD do. The FAILED at the end is expected.
module hazard_tb;
reg clk = 0, rst = 1;
always #5 clk = ~clk;
cpu #(.PROGRAM("program.hex")) dut (.clk(clk), .rst(rst));
wire [31:0] x1 = dut.rf.regs[1];
wire [31:0] mem4 = dut.dmem.mem[1];
integer cycles = 0, writes = 0;
always @(posedge clk)
if (!rst) begin
cycles = cycles + 1;
if (dut.ex_mem_mem_write) begin
writes = writes + 1;
$display("cycle %2d sw at 0x%02h stores %0d (should be %0d)",
cycles, dut.ex_mem_pc[7:0], dut.ex_mem_rd2, writes);
end
if (dut.ex_mem_branch)
$display("cycle %2d %s at 0x%02h %s -> next fetch from 0x%02h",
cycles, dut.ex_mem_inst[12] ? "bne" : "beq", dut.ex_mem_pc[7:0],
dut.pc_src ? "taken " : "not taken", dut.pc_next[7:0]);
end
initial begin
#12 rst = 0;
#400; // 40 cycles: more than enough for 33 instructions
$display("");
$display("after 40 cycles: x1 = %0d, memory[4] = %0d, %0d stores", x1, mem4, writes);
if (x1 == 10 && mem4 == 10 && writes == 10)
$display("PASSED (unexpected!)");
else
$display("FAILED as expected: should be x1 = 10, memory[4] = 10, 10 stores.");
$display("The pipeline needs hazard handling: chapter 34 (data) and chapter 35 (control).");
$finish;
end
endmodule
hazard_tb.v lässt dasselbe Zählprogramm laufen wie cpu_tb.v, aber ohne nops. Es schlägt fehl: Der Speicherbefehl schreibt 0 statt 1, und nach einem einzigen Speichern hängt das Programm in der Endlosschleife bei 0x18 fest.
Important | Zwei Ursachen (nur die Begriffe, die Lösungen folgen in Kapitel 34 und 35):
|
Die Notlösung vorerst: Das Programm hält selbst genug Abstand ⇒ program_nops.hex
mindestens 2 Befehle zwischen dem Schreiben und dem Lesen eines Registers
3 Befehle hinter jedem Sprung
Lücken mit nop gefüllt (= addi x0, x0, 0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// The counter program from chapter 26, prepared for the pipeline WITHOUT hazard handling (chapter 33).
// The pipeline itself does not notice dependencies, so the program has to keep enough distance:
// - a value written by one instruction can be read at the earliest 3 instructions later
// (the producer writes it in WB while the consumer reads it in ID, see regfile.v)
// - a branch is decided in the MEM stage: the 3 instructions after it are already in
// the pipeline and are always executed
// Every gap is filled with "nop" = addi x0, x0, 0, which changes nothing.
00000093 // 0x00: addi x1, x0, 0 x1 = 0 (counter)
00100113 // 0x04: addi x2, x0, 1 x2 = 1 (step)
00a00193 // 0x08: addi x3, x0, 10 x3 = 10 (end value)
00000013 // 0x0C: nop add needs x2: only 2 instructions after 0x04
002080b3 // 0x10: loop: add x1, x1, x2 x1 = x1 + x2
00000013 // 0x14: nop sw needs the new x1 ...
00000013 // 0x18: nop ... so 2 nops in between
00102223 // 0x1C: sw x1, 4(x0) memory[4] = x1
fe3098e3 // 0x20: bne x1, x3, loop offset -16
00000013 // 0x24: nop these 3 instructions are already in the pipeline
00000013 // 0x28: nop when bne is decided, so they must not
00000013 // 0x2C: nop do anything
00000063 // 0x30: done: beq x0, x0, done endless loop, offset 0
Frühe MIPS-Prozessoren überließen einen Teil davon dem Compiler: Der Befehl nach einem Sprung wurde immer ausgeführt (Branch Delay Slot), und der Compiler musste dort etwas Sinnvolles hinstellen - oder ein nop.
Performance: Hat es sich gelohnt?
Gemessen mit cpu_tb.v: 33 nützliche Befehle + 48 nops in 85 Takten
CPI (nützliche Befehle) = 2,58, CPU-Zeit 85 × 200 ps = 17,0 ns
Vergleich (CPU-Leistungsgleichung, Kapitel 31):
CPU | Takte | T | CPU-Zeit | Speedup |
Single-Cycle | 33 | 800 ps | 26,4 ns | 1,0 |
Multi-Cycle | 122 | 200 ps | 24,4 ns | 1,08 |
Pipeline mit nops | 85 | 200 ps | 17,0 ns | 1,55 |
ideale Pipeline | 37 | 200 ps | 7,4 ns | 3,6 |
Mehr als die Hälfte aller ausgeführten Befehle sind nops ⇒ die Lücke zwischen 17,0 ns und 7,4 ns ist die Motivation für die folgenden Kapitel 34 und 35. Noch vernachlässigt: der Register-Overhead pro Stufe (Clock-to-Q + Setup, Kapitel 31) - er schadet umso mehr, je mehr Stufen es gibt.
Pipelines in echten Prozessoren
Die klassische fünfstufige Pipeline geht auf die MIPS R2000 (1985) zurück. Unsere CPU nutzt dieselben fünf Stufen, aber für RISC-V - das Lehrbuch von Patterson & Hennessy hat es genauso gemacht (warum RISC so gut zum Pipelining passt: siehe den Kasten im Abschnitt über die fünf Stufen).
Längere Pipelines ⇒ kürzere Stufen ⇒ höherer Takt - aber mehr Hazards und mehr Register-Overhead. Beispiel: Intel ging von 10 Stufen (Pentium III) auf 20 (Pentium 4, 2000) und 31 (Pentium 4 „Prescott“, 2004) und danach bei den folgenden Core-Prozessoren wieder auf deutlich kürzere Pipelines zurück - der Gewinn beim Takt wog die Verluste nicht mehr auf.
Kleine Mikrocontroller nutzen kurze Pipelines (z. B. ARM Cortex-M0: 3 Stufen, siehe Mikrocontroller vs. Mikroprozessor).
Probier es selbst
Zeichne das Pipeline-Diagramm für die ersten 10 Takte von program_nops.hex von Hand und vergleiche es mit cpu_tb.v.
Schiebe sw hinter bne in einen der drei Plätze („delayed branch“, frühe MIPS). Es wird sowieso ausgeführt - ein nop weniger pro Schleifendurchlauf. Wie viele Takte sind es jetzt? (Die Endbedingung der Testbench muss angepasst werden: das letzte sw kommt jetzt nach dem letzten bne.)
Was wäre, wenn der Sprung in EX statt in MEM entschieden würde? Wie viele nops braucht es hinter bne, und wie viele Takte ergeben sich?
Entferne das Durchreichen aus regfile.v: Welches Programm schlägt fehl, und wie viele nops wären stattdessen nötig?
Mit 20 ps Register-Overhead pro Stufe: T = 220 ps - rechne die Tabelle neu aus.
Zusammenfassung
Pipelining überlappt Befehle: Jede Stufe arbeitet an einem anderen Befehl.
Die Latenz eines Befehls wird nicht besser (sie wird schlechter), der Durchsatz schon: im Idealfall ein Befehl pro Takt.
Pipeline-Register trennen die Stufen und tragen Daten UND Steuersignale mit; rd muss bis WB mitwandern.
Das Steuerwerk ist wieder kombinatorisch (Kapitel 17) und sitzt in ID.
Ohne Hilfe rechnet die Pipeline falsch (Daten- und Steuer-Hazards). Mit nops funktioniert sie, ist aber nur 1,55× statt 3,6× schneller.
Ausblick: Kapitel 34 beseitigt die meisten nops wegen Daten-Hazards (Forwarding, Stalls), Kapitel 35 die nops hinter Sprüngen (Flush, Sprungvorhersage).
(Gegliedert mit Claude AI, geschrieben von Sven Wehrend)