MIPS Control Logic Exam Solutions
MIPS Control Logic Exam Solutions
Computer Architecture
EE 4720
Final Examination
1 May 2017, 10:00–12:00 CDT
Good Luck!
Problem 1: (20 pts) The diagram below, based on the solution to Homework 5, shows control logic that
generates a stall signal when the value to be bypassed is too large for 12-bit bypass paths. The logic only
works when the dependency is with the rt register of the consuming instruction and when the producing
instruction is not a load. Modify the control logic so that it will generate a stall signal for a dependency to
an rs register (first example below) and dependencies with loads. Pay attention to the load sizes.
25:0 29:0
IF + ID = EX ME WB
15:0
PC D D MD
rtv In Out
31:11
2'b0 15:0 format
imm
30 2 immed
msb lsb
Mem Decode
dst dst dst
Port Data Dest
IR
Out
STALL
= lb
= lh
= lw
is Type I
is Type R ByME
00
rtv lsb
01
imm
10 msb
ByWB
11
2
Modify the control logic so that it also generates the stall signal for dependencies through the rs register
that can’t use 12-bit bypasses.
Modify the stall control logic for when loads lb, lh, and lw produce the value to bypass, take into
account whether value can use the 12-bit bypasses and
whether the instructions are too close to bypass.
Do not break existing control logic. As always consider cost and performance.
Solution appears below. The logic for stalling due to dependencies through the rs register appears in blue, and the logic for
dependencies related to loads appears in purple.
29:26
25:0 29:0
IF + ID = EX ME WB
15:0
PC D D MD
rtv In Out
31:11
2'b0 15:0 format
imm
30 2 immed
msb lsb
Mem Decode
dst dst dst
Port Data Dest
lb lb
IR
Out lwh lwh
STALL
rt 20:16 =' ='
='
rs 25:21 ='
Unbypassable from ME:
val too big (abig)
or val wouldn't be
= lb avail in time (loads).
= lh
Stall due to
= lw Stall due to dependency dependency
from dest of insn in EX from dest
to rs reg of insn in ID . of insn in ME
to rs reg
is Type I of insn in ID .
is Type R ByME
00
rtv lsb
01
imm
10 msb
ByWB
11
3
29:26
25:0 29:0
IF + ID = EX ME WB
15:0
PC D D MD
rtv In Out
31:11
2'b0 15:0 format
imm
30 2 immed
msb lsb
Mem Decode
dst dst dst
Port Data Dest
lb lb
IR
Out lwh lwh
STALL
rt 20:16 =' ='
='
rs 25:21 ='
Unbypassable from ME:
val too big (abig)
or val wouldn't be
= lb avail in time (loads).
= lh
Stall due to
= lw Stall due to dependency dependency
from dest of insn in EX from dest
to rs reg of insn in ID . of insn in ME
to rs reg
is Type I of insn in ID .
is Type R ByME
00
rtv lsb
01
# Cycle 0 1 2 3 4 5 6 7
imm
lw r1, 2(r3) IF ID EX ME WB 10 msb
# Cycle 0 1 2 3 4 5 6 7
lb r1, 2(r3) IF ID EX ME WB
sub r4, r5, r1 IF ID -> EX ME WB
# aa bb
# aa: Stall because lb would be in ME when sub is in EX, so can’t bypass.
# bb: Don’t stall, can bypass WB->EX and lb-loaded value can fit.
4
Problem 2: (15 pts) Illustrated below is a superscalar implementation taken from the solution to last year’s
final exam and the subject of this semester’s Homework 7. Show the execution of the code sequences below
on the illustrated superscalar MIPS implementation. Don’t forget to check for dependencies.
2'b0
31:2
IF + ID EX ME WB
15:0
rtv I
Addr
Addr
D In
D
alu1 alu1
Addr 15:0
Immed imm0
Mem 15:0
I imm1
Port ir
0
dst0 dst0 dst0
64 Dest. reg
Data 1
ir dst1 dst1 dst1
Out Dest. reg
mp1
isMem
isMem STA
(a) Show the execution of the code below on this implementation. Note that the address of the first instruction
is 0x1000.
Show execution of the following code sequence. Pay attention to ME in the diagram.
Check for dependencies.
The solution appears below. The lw r3 stalls because the ME stage can only accommodate one memory instruction. The last add
stalls due to a dependence.
# SOLUTION
# Cycle 0 1 2 3 4 5 6 7 8
lw r1, 0(r2) IF ID EX ME WB
lw r3, 4(r2) IF ID -> EX ME WB
lw r4, 8(r2) IF -> ID EX ME WB
add r5, r1, r5 IF -> ID EX ME WB
add r5, r3, r5 IF ID EX ME WB
add r5, r4, r5 IF ID -> EX ME WB
# Cycle 0 1 2 3 4 5 6 7 8
5
Problem 2, continued: The illustration below is the same as the one on the previous page.
2'b0
31:2
IF + ID EX ME WB
15:0
rtv
Addr
Addr
D In
alu1 alu1
Addr 15:0
Immed imm0
Mem 15:0
imm1
Port ir
0
dst0 dst0 dst0
64 Dest. reg
Data 1
ir dst 1
dst1
dst1
Out Dest. reg
mp1
isMem
isMem ST
(b) Show the execution of the code below on the illustrated implementation when the branch is taken. Use
the classroom default assumption: fetches are aligned.
# SOLUTION
# Branch is taken. Cycle 0 1 2 3 4 5 6 7 8 9
0x1000: bne r1, r4 TARG IF ID EX ME WB
0x1004: sub r5, r2, r7 IF ID EX ME WB
0x1008: xor r10, r11, r12 IFx
0x100c: lbu r9, 0(r5) IFx
0x1010: andi r8, r9, 12 IFx
# Cycle 0 1 2 3 4 5 6 7 8 9
TARG:
0x1014: or r11, r5, r12 IF ID EX ME WB
0x1018: sb r11, 0(r5) IF ID ----> EX ME WB
# Cycle 0 1 2 3 4 5 6 7 8 9
6
Problem 2, continued: The illustration below is the same as on the previous page.
2'b0
31:2
IF + ID EX ME WB
15:0
Addr
Addr
D In
alu1 alu1
Addr 15:0
Immed imm0
Mem 15:0
! imm1
Port ir
0
dst0 dst0 dst0
64 Dest. reg
Data 1
ir dst1 dst1 dst1
Out Dest. reg
mp1
isMem
isMem $%%&'(&)
ST
(c) Appearing below is an execution of MIPS code on the illustrated superscalar implementation shown for
the first two iterations. Compute the CPI for a large number of iterations. If necessary extend the execution
diagram.
lw r1, 0(r2) IF ID EX ME WB
LOOP: # Cycle 0 1 2 3 4 5 6 7 8 9 10
add r1, r1, r4 IF ID -> EX ME WB # First Iteration
lw r1, 0(r2) IF ID -> EX ME WB
bne r2, r3 LOOP IF -> ID EX ME WB
addi r2, r2, 4 IF -> ID EX ME WB
??? IFx # Fallthrough insn.
??? IFx # Fallthrough insn.
LOOP: # Cycle 0 1 2 3 4 5 67 8 9 10
add r1, r1, r4 IF ID
EX ME WB # Second Iteration
lw r1, 0(r2) IF ID
EX ME WB
bne r2, r3 LOOP IF
ID EX ME WB
addi r2, r2, 4 IF
ID EX ME WB
??? IFx # Fallthrough insn.
??? IFx # Fallthrough insn.
LOOP: # Cycle 0 1 2 3 4 5 6 7 8 9 10
add r1, r1, r4 IF ID EX ME WB # Third Iteration
# Cycle 0 1 2 3 4 5 6 7 8 9 10 11 12
! 1st Itr ! 2nd Itr! 3rd Itr!
! 4 cyc ! 3 cyc ! 3 cyc !
7
Problem 3: (20 pts) Answer the following branch prediction questions.
(a) Code producing the branch patterns shown below is to run on three systems, each with a different branch
predictor. All systems use a 212 entry BHT. One system has a bimodal predictor, one system has a local
predictor with a 8-outcome local history, and one system has a global predictor with a 8-outcome global
history. Branch B2 consists of a repeating pattern that starts with TNTT and is either followed by three
not-taken outcomes, nnn, or four taken outcomes, tttt. (They are shown in lower case for clarity.) The nnn
sequence occurs with probability .4, and is not correlated with anything.
Answer each question below, the answers should be for predictors that have already warmed up. Show work
or provide brief explanations.
↓
B1: T N T T N T N T T N T N T T N
B2: T N T T n n n T N T T t t t t
0 1 0 1 2 1 2 1 2 3 2 3 2 3 3 2 <-- Counter
B1: T N T T N T N T T N T N T T N
x x x x x x x x x x <-- Pred. Outcome
8
What is the accuracy of the bimodal predictor on branch B2? Account for the variable pattern length.
This is best analyzed by considering the four possible cases of the way the random sequence can occur before and after the fixed
sequence (TNTT). These are shown in the table below. For each case the number of mispredictions is computed starting at the
fixed sequence and continuing into the second random sequence. What makes this easy (relatively) is that when the fixed sequence
starts the counter will be either 0 or 3. Therefore we can compute an exact prediction ratio for each of the four cases. These are
shown under the Pred column. The Prob column is the probability that the fixed sequence will be surrounded with the particular
random outcomes. The numbers under the weight column give something like the space taken up by the particular case. These are
used to weight the prediction accuracies. In particular the value under Weight is the product of the value under Pred and the
value under Weight. The sums are shown at the bottom. The prediction accuracy is the weighted value divided by the weight:
4.92
7.6 = .647368 .
3 3 2 3 3 2 1
t t t t T N T T n n n
x x x 3/7 .6 * .4 .6 * .4 * 7 .6 * .4 * 3
0 1 0 1 2 3 3 3
n n n T N T T t t t t
x x x 5/8 .4 * .6 .4 * .6 * 8 .4 * .6 * 5
3 3 2 3 3 3 3 3
t t t t T N T T t t t t
x 7/8 .6 * .6 .6 * .6 * 8 .6 * .6 * 7
--- -------- ----------- -----------
1 7.6 4.92
What is the accuracy of the local predictor on branch B2? Account for the variable pattern length.
Short Answer: Assuming that it always predicts t for the outcome after TNTT, the accuracy will be
7 × .4 × 76 + 8 × .6 × 8
8
= .947368
7 × .4 + 8 × .6
where the prediction accuracy for TNTTnnn, 76 , and for TNTTtttt, 88 , have been weighted by the probability that a B2 outcome
7×.4 8×.6
is part of TNTTnnn, 7×.4+8×.6 , or part of TNTTtttt, 7×.4+8×.6 .
Long Answer: Because the local history length, 8, is long enough to identify the position within the pattern, the only outcome that
can’t be predicted with 100% accuracy is the first branch after the fixed sequence, TNTT. For example, consider TNTTnnn. It will
correctly predict the fixed-sequence outcomes, TNTT and it will correctly predict the last two ns because once it sees the first of the
three ns it will recognize that there will be two more. Or, to put it more precisely, when the local history contains tttTNTTn or
nnnTNTTn the corresponding PHT entries will hold a zero because each time either of the two local histories was encountered in the
past the B2 outcome would be n (that’s the second n) and so the PHT entry would be decremented. By the same logic the third
n would always be correctly predicted (after warmup) as would the second, third, and fourth t. When predicting the first outcome
after the fixed sequence the local history will be either TnnnTNTT or ttttTNTT. We know that 60% of the time the outcome is
t. As an approximation we can assume that the PHT entry would be 2 or 3 since 60% of the time it is incremented and 40% of the
time it is decremented. It is possible to compute an exact probability distribution for the counter values by constructing a four-state
Markov chain and solving the balanced flow equations api = (1 − a)pi+1 for 0 ≥ i ≤ 2, where a is the probability that the
a
−1
i
a
branch is taken, a = .6 here. Solving these yields p0 = 1−a 4 and pi = 1−a p0 . From this we get p0 = .123077 and
( 1−a ) −1
a
9
the probability of a taken prediction p2 + p3 = .692308 and a not taken prediction is p0 + p1 = .307692. We can use these
numbers to compute an overall prediction accuracy
6.307692 7.692308
7 × .4 × 7 + 8 × .6 × 8
= .939271,
7 × .4 + 8 × .6
which is only slightly lower than the estimated accuracy.
What is the minimum local history size for which branch B1 and B2 will not interfere with each other?
Explain.
Seven outcomes. With seven outcomes the B2 local history must contain either three consecutive ts or two consecutive ns, which
never occur in a B1 local history. This means that B1 and B2 will never use the same PHT entries and so won’t interfere with
each other with a seven-outcome local history. Now consider six outcomes. Local history nTNTTn could be for B1 and B2, and so
they would both use the same PHT entry. For B1 the next outcome would be T, but for B2 the next outcome would be n, and so
the shared PHT entry could not predict both branches accurately. (Remember that there’s no difference between n and N and no
difference between t and T, so a local history of nTNTTn is exactly the same as NTNTTN. Upper and lower case are only being used
to show which branch outcomes belong to the fixed part (upper case) and which belong to the repeating part (lower case).
Note that an arrow (↓) points at an execution of B1. Show the value of the GHR at the time that that
execution is being predicted.
The local history will contain TTTTNnnn.
10
Problem 3, continued:
(b) Appearing below is a diagram of a bimodal predictor, showing in detail the logic for predicting the
instruction in IF and for updating the predictor for the resolving branch. Modify the diagram so that it is
a local predictor with an 8-outcome local history.
PC IF ID
PC
Updated local
history
msb
local history
6:0 PHT
7:0
a d
lsb
we
a d in
-1
Post-resolve
+1 2-b counter. Local history used
to predict resolving branch.
From ME (or stage where
11
Problem 4: (15 pts) The diagram below is for a 32 MiB (225 B) four-way set-associative cache with a line
size of 32 B.
(a) Answer the following, formulæ are fine as long as they consist of grade-time constants.
63:23 63:23
Tag Tag
Addr Addr
Tag Tag
22:5
Data
Out
= Data
Out
22:5
Valid Valid
Data Data
Addr Addr
Data Data
22:4 Out 22:4 Out
Complete the address bit categorization below. Label the sections appropriately. (Index, Offset, Tag.)
Tag Index Offset
Address:
63 23 22 5 4 4 3 0
Show the bit categorization for a direct-mapped cache with the same line size and capacity as the cache
above.
The cache above is 32 MiB and 4-way set associative. In a direct mapped cache there is just one way with four times the storage
of a way in the cache above. To get four times the number of entries the number of index bits is increased by two, and so the index
bits will start at position 24 instead of 22. The other bit positions remain the same.
Address:
63 25 24 5 4 4 3 0
12
Problem 4, continued: The problem on this page is not based on the cache from Part a. The code in the
problem belows run on a cache with a line size of 1024 B (210 B). Each code fragment starts with the cache
empty; consider only accesses to the arrays.
int sum = 0;
int *a = 0x2000000; // sizeof(int) == 4
int i;
int ILIMIT = 1 << 11; // = 211
What is the hit ratio running the code above? Show formula and briefly justify.
The line size of 210 = 1024 bytes is given. The size of an array element, which is of type int, is 4 = 22 B, and so there are
210 /22 = 210−2 = 28 = 256 elements per line. The first access, at i=0, will miss but bring in a line with 28 elements, and
so the next 28 − 1 = 255 accesses will be to data on the line, hits. The access at i=256 will miss and the process will repeat.
255
Therefore the hit ratio is 256 .
13
Problem 5: (30 pts) Answer each question below.
(a) Consider a 4-way superscalar system and a scalar system with a 4-lane vector unit. Both can compute
arithmetic at a rate of 4 operations per cycle. The vector system is cheaper but the superscalar system is
more flexible.
Show something the superscalar system can do that the vector system cannot.
Explain why vector system can’t execute equivalent vector code as efficiently.
Vector instructions must apply the same operation to each lane of its operands. A 4-way superscalar system could execute four
different operations, for example, the set of operations below.
# SOLUTION
add.d f0, f2, f4
sub.d f6, f8, f10
mul.d f12, f14, f16
add.s f18, f19, f20
(b) Unlike MIPS, ARM A64 has pre-index and post-index load and store instructions. Show two code
examples, one in A64 that uses a post-index load, and one in MIPS that does the same thing (but without
a post-index load). The exact syntax of the ARM instructions is not important, use comments to clarify
instructions.
@ ARM A64
ldr x1, [x2], #8 @ x1 = Mem[x2]; x2 = x2 + 8
# MIPS
lw r1, 0(r2) # r1 = Mem[r2]
addi r2, r2, 4 # r2 = r2 + 4
(c) What substantial additional hardware is needed to implement ARM A64 pre- and post-index loads when
starting with something like our five-stage MIPS implementation. (Think about Homework 4.) Note: The
words “substantial” and “costly” were not included in the original exam.
14
(d) VLIW ISAs are supposed to do for superscalar implementations what RISC ISAs did for pipelined
implementations. The diagram below shows our 2-way superscalar MIPS. Show how a 2-slot-bundle VLIW
ISA (perhaps one a lot like MIPS) could simplify hardware in this implementation related to the sharing in
ME.
2'b0
31:2
IF + ID EX ME WB
15:0
rtv0 9 5:
34
Addr
Addr
D In
2
alu1 alu1
Addr 15:0
Immed imm0
Mem 15:0
56678 imm1
Port ir
0
dst0 dst0 dst0
64 Dest. reg
Data 1
ir dst 1
dst 1
dst1
Out Dest. reg
mp1
isMem
isMem ST;<<=>?=@
Explain why the control logic driving STALL ID 1 would no longer be needed.
The bundle slot restrictions forbid two memory instructions in a bundle, and so there’s no need to check for it. (In fact, there can’t
be a memory instruction in slot 1 even without a memory instruction in slot 0.)
15
(e) When an exception occurs (or a trap instruction is executed) the processor switches from user mode
into privileged mode (also called system mode). Explain how privileged mode affects instruction execution,
including loads, compared to user mode.
Effect of privileged mode on instruction execution including effect on load instruction execution.
In privileged mode all instructions can be executed, but in user mode only a subset of instructions can be executed. Similarly, in
privileged mode a load or store can access any valid memory address, in user mode loads and stores can only accesses addresses to
which they have been granted access.
(f) It’s hard to choose a line size that makes everyone happy. Explain how a long line size might slow down
some programs in a small cache in comparison to the right line size (for those programs).
With a small cache large lines can slow some programs because:
Short Answer: because the S bytes of data (say) that some program needs cached won’t fit in a 4S byte cache because the program
only uses 18 of the data in a line. It would take an 8S-byte cache to hold the S bytes in such a case.
For example, consider a 64 kiB cache with a 1024 B cache. Such a cache can hold 64 lines. Consider a program that frequently
needs to access 100 bytes of data where the address of byte i is 2000i, for 0 ≤ i < 100. Each line holds just one byte of the needed
10
data plus 1023 unneeded bytes. Now consider a cache with 32-byte lines, but also of 64 kiB. This cache can hold 64×2 32 = 2048
lines. That’s more than enough for the program.
Describe the characteristics of code that works well with long lines.
Code that accesses data sequentially. For example, for (i=0; i<1000000; i++ ) sum += a[i]; . Here, the access to
data is sequential.
16