




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































Microarchitecture : the internal structure of a processor, and its datapath : namely, the path where instructions go through when executed. ISA ( I nstruction S et A rchitecture): is the set of machine instructions of a processor. Two distinct processors can have the same ISA but different microarchitectures (that is, two different ways to execute those instructions). The simple RISC microarchitecture that we consider, executes fixed-length 32 bit instructions. We shall consider the “integer” section of the architecture only: instructions operate on integer registers, memory and PC. We shall cover mainly the datapath of the CPU, with only some hints to the Control Unit (CU) : is the section that control and synchronises the different datapath blocks during the instruction execution. The two main features of a modern RISC machine are:
- machine instructions are simple - as a consequence, the control unit is simple too, and there is no need for a complex microprogram to steer (guidare) machine instruction execution within the CPU. The architecture has, at least, 32 general purpose integer registers, each of 32 bits. In MIPS, register R0 is special and always stores 0 (this also in 64 bits architectures). Since we don’t consider floating point operations, the architecture we describe has neither floating point registers nor functional units to process floating point data. A generic MIPS instruction has the following format: - op: represents the class of the operation - reg_1: represents the source register 1 - reg_2: represents the source register 2 - reg_dest: represents the destination register - shift: is used in shift operations on registers - funct: specifies the type of the operation within its class With these fields we can describe the following formats: R-type instructions : have one or two registers as operands and produce the result in a third register. “D” prefix usually denotes an integer instruction, while “F” is used for floating point. In the symbolic notation, the first register is the destination register (R1, R5). I-type instructions : use an immediate as operand (an integer in our case)
I-type format can also be used for load/store instructions. The final format includes: Branch instructions (conditional) Jump instructions (unconditional) MIPS instructions execution is similar for any type of instruction (this is true for all RISC architectures). The first two steps are actually (in realtà) always identical in any instruction, because:
All the instructions, except jump , uses the ALU ( A rithmetic L ogic U nit). Instructions are completed in different modes:
Here is a high level single cycle sketch of MIPS datapath. Note the separate memory units for instructions and for data.
Write operations into the register file are controlled by a specific signal, but read operations are “immediate”: at any time, the register file outputs on SRC1data and SRC2data the content of registers addressed by inputs SRC1addr and SRC2addr. To set up a CU in MIPS it’s necessary to define the control signals for the ALU, that receives two inputs and transform them into an output according to four control signals. As an instance: Control signals for the ALU can be generated by a simple ALU control sub- unit (which is a part of the CU) that receives in input:
instruction;
that depends on the “op” field of the instruction, and establish if the operation to be carried out is a sum, a subtraction or other. ALUcontrol is the logic circuit that realises the truth table which maps its input bits (composed of ALUop + funct bits) into the outputs: the four ALU control bits. The two most significant bits of the “funct” field are not used. The design of the main CU follows the steps outlined for ALU control. The main CU has in input the 6 bits of the “op” field and has to output control signals for:
The CU is simply a combinational circuit , actually a truth table, with inputs from the 6 bits of the “op” field of each instruction, and 9 outputs (e.g. ALUop), which are the control signals in the MIPS datapath. As an instance, let us track the execution of an R-type instruction.
a) I-type instructions or memory access (load/store)
Fifth step: Write Back Register (WB/REG)
- reg[IR[20-16]] <— MDR(Memory Data Register, contains momentarily data from/to CPU) - The load operation is completed by moving the MDR content into the destination register specified in the instruction. During this phase, the CU asserts RegWrite (WE) signal of register file to allow writing of destination register's bits IR[20-16]. In this implementation of MIPS, a branch takes 3 clock cycle, an arithmetic/logical I-type, R-type or store takes 4 and a load takes 5. How does the CU change in a multi-cycle version of MIPS? For sure, it is more complex, because it has to specify which signals have to be asserted in each step, and which is the next step. Each step is described by a truth table whose inputs are: - the type of the instruction being executed (the ‘ op ’ field) - the current step in the sequence describing the instruction execution the output of the truth table at each step consists of: - the set of signals to be asserted - the next step in the sequence This is just an informal description of a Moore machine , which is a type of finite state automation: - each state has an output that only depends on the state - the transition to the next state depends only on the current state and on the input. What is the difference between a datapath controlled by a finite state machine and another controlled by a microprogram? None, really. Microprogramming applies a symbolic representation of control based on instructions, to describe CU operation: microinstructions executed on a simple microprocessor. A microprogram is nothing more than a textual representation of Moore finite state machine, with each microinstruction corresponding to a state in the machine. So, why having a double representation (finite state operation and microprogramming) for the CU operation? It’s only a matter (questione) of complexity of the CU, which actually depends on the number and richness of the ISA instructions. A real RISC architecture features many more instructions than we considered so far and some take various clock cycles to be executed. The control in these architectures is definitely much more complex and consequently the associated automaton will be more complex too. But, beyond (al di la) a certain complexity, the automaton representation is no longer a viable means (non è più un mezzo praticabile). As we will see, CISC architectures usually feature hundreds of instructions, some of them really complex since they combine arithmetic/logic operations with memory access. The number of steps to execute these instructions is very high, resulting in a finite state automaton with thousands of states and hundred of state sequences for many instructions. Microprogramming was the natural choice for architectures in the 60s and 70s; the trend was designing ISA with a very rich set of very complex and “powerful” instructions. The basic idea of microprogramming is to introduce a further (ulteriore) layer between the ISA level and instruction execution, decomposing instructions into finer steps: the microinstructions.
MIPS simplified architecture can be realised by having each instruction executed in a single clock cycle, approximately long as the 5 clock cycles required to complete the 5 phases. Why would this approach be inconvenient? We already know one reason: the longer cycle would waste time in all instructions that take less to execute. There is another relevant reason: by breaking down into more clock cycles, it is possible to overlap (sovrapporsi) the execution of an instruction with other instructions. At each clock cycle, while a section of the datapath takes care of an instruction, another section can be used to execute another instruction. If we start a new instruction at each new clock cycle, each of the 5 phases of the multi- cycle MIPS architecture becomes a stage in the pipeline.
Pipelining only works if one does not attempt to execute at the same time two different operations that use the same datapath resources. Using reduced instructions makes it fairly (abbastanza) easy to determine at each time which datapath resources are free and which are busy. This is very important to best exploit (sfruttare al meglio) CPU resources. Why are phases WB and ID highlighted in clock cycle 5? Let instruction i be a load, and i+3 be an R-type. In cycle 5 the result from instruction i is being written in the register file and the operands for instruction i+3 are being read from the same register file. Well, it looks like we are trying to use the same resource in the same clock cycle for two different instructions… Actually, this is allowed at those functional units that can be used twice during different phases of the same clock cycle. In the following figure depicting the execution pattern of a load instruction, each phase is associated to the unit it use: the grey part shows that the unit is not used in that portion of the clock cycle: The register file is written in WB in the first half of the clock cycle, and it is read during ID in the second half of the cc. The MIPS pipeline can be thought as a series of datapaths shifted in time, each one for each instruction being executed. This pipeline drawing calls (richiede) for two observations:
IF ID EX MEM WB CC1 CC2 CC3 CC4 CC RegFile (read) RegFile (write)
Instruction Decode : the IF/ID register is read to address the register file. Both addressed registers are read, even if should only one be actually used later. The 16 bits of the immediate operand are converted into a 32 bits and the three data are stored with the PC in ID/EX. EXecution : the “load” reads in ID/EX the content of register 1 and the immediate, sums them using the ALU and writes the result into EX/MEM. If the execution is a branch or a jump, the adder uses the PC value (available on ID/EX) to compute the new PC value (it is be used only if the branch is taken). MEMory : the data memory is accessed, using the value available in EX/MEM as address. The data is stored into MEM/WB. Write Back : the load destination register is written with the data read from memory Pipelining is not simple as it appears so far (finora): in this datapath, when an instruction reaches (raggiunge) the WB stage, the IF/ID register already contains the id of the destination register of a successive instruction. To avoid (evitare) loading the data into the wrong register, the load instruction cannot simply store the id of its destination register in IF/ID, because it would be overwritten at the next clock by a new instruction in its IF phase. In the various execution steps of the LOAD, the id of the destination register must be transferred from IF/ID to ID/EX, to EX/MEM and eventually to MEM/WB, to be used to address the correct register during WB.
Here is a correct datapath to handle (gestire) the load: the destination register id flows through all pipeline registers to be available during WB. The pipeline registers must be extended to store this value as well. PERFORMANCE The pipeline makes the complete execution of an instruction longer than its counterpart (controparte) in a not pipelined datapath. But the CPU throughput (portata) increases, so programs execute faster. Let us consider a CPU with no pipeline and the following characteristic:
What is the average execution time for an instruction? Avg. exec time = (0.4 + 0.2) • 4ns + 0.4 • 5ns = 4.4 ns. Let us now assume that the CPU is pipelined, and that the overhead (sovraccarico) caused by the hardware for pipelining is 0.2ns. Then, speedup = avg. execution time without pipelining / avg. execution time with pipeline = 4.4ns / 1.2ns = 3.7. PIPELINING HAZARDS Unfortunately, pipelining is not that simple. There are three types of problems ( hazards ) that limit the effectiveness (efficacia) of pipelining:
Let us consider some basic solutions for the three types of hazards. Usually, structural hazards arise because some hardware resources within the datapath cannot be duplicated, and are requested at the same time by two different instructions in the pipeline. As an instance, a single L1 cache for data and instructions would continuously cause structural hazards, and this is why L1 caches are separate. Another hazard arises if two instructions cannot use the same ALU during the same clock cycle.
pipeline. The pipeline circuitry detects this situation and stalls subsequent instructions, for a single cc in the best case, until the value they require is available. The hazards detection unit , checks for producer-consumers dependencies that cannot be solved through forwarding. It stalls the pipeline (IF and ID phases) and produces a “bubble” (a NOP with proper control signals). Control hazards arise when a branch is executed , since it can either change the PC value or leave it unaffected. Even assuming an aggressive implementation which uses two clock cycle for the branch, if the branch is taken, the PC is not updated until the end of the second clock cycle, in the ID stage, after the address to be stored in the PC is actually computed. Thus, the instruction that is located immediately after the branch (at the address PC+4) has already completed the IF phase. Let us consider the following example: LD R1, 0(R4) BNE R0, R1, else DADD R1, R1, R JMP next else: DSUB R1, R1, R next: OR R4, R5, R When the branch decision will be know, the DADD has already completed its IF phase. A simple approach is to carry out the fetch phase of the instruction that follows the branch. At the end of the ID phase of the branch, it is known if the branch will be taken or not. If not, everything is ok and the DADD proceeds in its ID phase. If the branch is taken, the instruction to be issued (da rilasciare) is DSUB, which starts with its IF phase: so, a clock cycle is lost for the IF phase of the DADD. Obviously there is a waste in clock cycles, and the amount lost depends on the frequency of branches in a program and of the success (or fail) of each specific branch. In the example, the waste due to a branch is one clock cycle at most, since we assume that the datapath is able to complete a branch in two clock cycles. More complex branch instructions can require more clock cycles and the waste can be larger. Control hazards involve the management of three different actions: instruction decoding (D), next PC address computation (A) and condition evaluation (C). Modern processors have superscalar pipelines (with up to 14 stages). Some statistics: jump amount to 2%-8% in integer benchmarks, branch amount to 11%-17%. In loops, 90% of branches are taken and in generic IF 53% are taken, of these 75% are forward. A different approach is to make an a-priori prediction on the outcome of the branch. This is the so-called static branch prediction. As an instance, the pipeline hardware might make the following assumptions :
However, if the prediction turns out to be wrong, the instruction issued “by mistake” must be turned into a no-op, and it is necessary to fetch the correct instruction. Obviously, if most predictions are correct, the waste is minor. As an instance, in a loop executed many times, the backward jump is executed most times, and the forward jump is executed only once, when the loop exits. Forward jumps are usually used for exceptions, to transfer control to the associated routine. Hopefully, exceptions should be rare. This kind of prediction best suites (si adatta meglio) the simple if then else construct. Another technique, common in the first RISC architectures, is called delayed branch. It consist of placing, right after the branch, instructions that do useful work , independently of the outcome of the branch. The instructions are executed throughout, and the branch is evaluated and acts according on the PC. This technique requires action on the compiler side, and cannot be easily applied in all cases. The branch delay slot is the set of instructions that are “conditionally” fetched and sent to the pipeline stages. Scheduling the branch delay slot is not easy, and the probability for the compiler to find “good” instructions to be placed in the delay slot rapidly decreases with the slot length. Let us assume that the branch delay slot is 1-instruction wide (ampio). Case (a) is easily handled by the compiler, but as to the others… (b) is possible ONLY if the assignments in the delay slot cause no harm : registers assigned to in the delay slot are not read before they are assigned again. if R1==0 then go to label DSUB R4, R5, R INSTR A (does not read R4) INSTR B (does not read R4) … ADD R4, R7, R8 OK | SUB R5, R6, R4 WRONG The last execution of DSUB is “wrong” but it is wiped out (cancellata) by the ADD. The SUB instead (invece) would read a wrong value. (c) is possible ONLY if the assignments in the delay slot cause no harm : registers assigned to in the delay slot are not read before they are assigned again. OK DSUB R4, R5, R ADD R7, R8, R WRONG DSUB R4, R5, R SUB R8, R7, R Interrupts, traps, faults, other exceptions that alter the flow of instructions make the operation of a pipeline very difficult to manage, since instructions are overlapped
It is possible to concurrently fetch two instructions from Instruction Memory. The two instructions, issued (rilasciate) together, do not conflict in accessing the registers and they do not depend on each other. There will be a further (ulteriore) gain in computation. Of course, even in this architecture, hazards must be minimised either at compile time or at run-time. Pipeline u could execute any instruction, while pipeline v could handle only simple integer- type instructions. Instructions are executed “ in-order ”, that is in the order specified by the program and a few fixed rules establish if the two instructions are “compatible”, so that they can be executed in parallel. If not, only the first instruction is issued, and a coupling is attempted between the second and the third, and so on… Of course, one can think of multiple (> 2) pipelines operating in parallel, but modern architectures have taken a different route, introducing more functional units in the EX stage. Actually, in a CPU executing a fairly (abbastanza) complete set of instructions, some of these have an EX phase much longer than the other phases: add/sub of floating point numbers, multiply/division of integer numbers and multiply/division of floating point numbers. It is possible to carry out these operations in a single, very long clock cycle, but this would slow down uselessly (inutilmente) the other phases, which can do with a much shorter clock cycle. The EX phase can use a short clock when it carries out an integer operation (add/sub), a logical operation (AND, OR, XOR) or a register comparison. The best approach is:
This is the pipeline with multiple functional units for the MIPS architecture: The previous drawing depicts fairly well the high level operation of modern architectures. It highlights some properties shared by all modern CPUs: