Unit 3 Risc V HRV - 1
Unit 3 Risc V HRV - 1
Vanamala H R
Department of Electronics and
Communication Engg.
RISC V
ARCHITECTURE
Vanamala H R
Department of Electronics and Communication Engineering
RISC V Instruction Set Architecture (ISA)
Operands of the Computer Hardware
RISC V Registers
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Representing Instructions in the Computer
RISC-V – It’s Instruction Format
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Supporting Procedures in Computer Hardware
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
slt and sltu compare two registers as signed and unsigned numbers,
respectively, then write 1 to a register if the first value is less than the second
value, or 0 otherwise. slti and sltiu perform the same comparisons, but with
an immediate for the second operand.
slti and sltiu perform the same comparisons, but with an immediate for the
second operand.
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Heap memory:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Illustration of four RISC-V addressing modes
Addressing in Branches
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Supporting Procedures in Computer Hardware
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V
ARCHITECTURE
Vanamala H R
Department of Electronics and Communication Engineering
RISC V Instruction Set Architecture (ISA)
LR loads a word from the address in rs1, places the sign-extended value in rd,
and registers a reservation on the memory address.
SC writes a word in rs2 to the address in rs1, provided a valid reservation still
exists on that address. SC writes zero to rd on success or a nonzero code on
failure.
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
The program next acquires a lock at the location in register x20, where the
value of 0 means the lock was free and 1 to mean lock was acquired:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Assembler- pseudoinstructions :
li x9,0x8123
lui x9 0x8
addi x9 x9 291
la x10,num
auipc x10 0x10000
addi x10 x10 0
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Assembler- pseudoinstructions :
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
UNIX follows a suffix convention for files: C source files are named
x.c, assembly files are x.s, object files are named x.o, statically linked
library routines are x.a, dynamically linked library routes are x.so,
and executable files by default are called a.out.
MS-DOS uses the suffixes .C, .ASM, .OBJ, .LIB, .DLL, and .EXE to the
same effect.
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Producing an object file:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
(Typically, this file has the same format as an object file, except that it contains
no unresolved references. It is possible to have partially linked files, such as
library routines, that still have unresolved addresses and hence result in object
files.)
•Could leave location dependencies for fixing by a relocating loader
•When the linker places a module in memory, all absolute references, that is,
memory addresses that are not relative to a register, must be relocated to
reflect its true location
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Loading a Program:
1.Reads the executable file header to determine size of the text and data segments.
2.Creates an address space large enough for the text and data.
3.Copies the instructions and data from the executable file into memory.
4.Copies the parameters (if any) to the main program onto the stack.
5.Initializes the processor registers and sets the stack pointer to the first free location.
6.Branches to a start-up routine that copies the parameters into the argument registers
and calls the main routine of the program. When the main routine returns, the start-up
routine terminates the program with an exit system call
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Dynamic Linking:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
RISC-V assembly version of procedure sort :
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
C sort Program:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Procedure to swap:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Sort Procedure in C:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Inner Loop:
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
2.35.1 0x11
2.35.2 0x88
RISC V Instruction Set Architecture (ISA)
2.36 [5] <§2.10> Write the RISC-V assembly code that creates the 32-bit
constant 0x12345678hex and stores that value to register x10.
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V Instruction Set Architecture (ISA)
2.36 [5] <§2.10> Write the RISC-V assembly code that creates the 32-bit
constant 0x12345678hex and stores that value to register x10.
2.37 [10] <§2.11> Write the RISC-V assembly code to implement the following
C code as an atomic “set max” operation using the lr.d/sc.d instructions. Here,
the argument shvar contains the address of a shared variable which should be
replaced by x if x is greater than the value it points to:
void setmax(int* shvar, int x) {
// Begin critical section
if (x > *shvar)
*shvar = x;
// End critical section}
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
}
RISC V Instruction Set Architecture (ISA)
2.38 [5] <§2.11> Using your code from Exercise 2.37 as an example, explain
what happens when two processors begin to execute this critical section at the
same time, assuming that each processor executes exactly one instruction per
cycle.
Reference : Computer Architecture with RISC V - The Hardware/Software Interface: RISC-V Edition by David A. Patterson and John L. Hennessy
RISC V ARCHITECTURE
The Rest of the RISC-V Instruction Set
(AUIPC) adds the 20-bit immediate value to the upper 20 bits of the program
counter (pc) and stores the result in the destination register (rd).
AUIPC forms a 32-bit temporary offset, by adding the 20-bit immediate value to
the upper 20 bits of temporary offset, filling in the lower 12 bits with zeros.
The temporary offset is added to the pc, to form the pc-relative address. The
result is placed in the destination register (rd).
Syntax
Opcode
Example:
Assuming pc is at 0x800000ff.
auipc x5, 0x00110
imm = 0x00110
x5 ←− 0x00110000 + 0x800000ff
A.E X5= e 0x801100ff.
RISC V ARCHITECTURE
The Rest of the RISC-V
Instruction Set
SLT(Set Less Than)
Syntax
slt rd, rs1, rs2
2) SLTU rd, x0, rs2 sets rd to 1 if rs2 is not equal to zero, otherwise sets rd to zero
RISC V ARCHITECTURE
The Rest of the RISC-V Instruction Set
SLTI (Set Less than Immediate)
Syntax
slti rd, rs1, imm
Syntax
sltiu rd, rs1, imm
Example:
For general signed addition, overflow checking requires three additional
instructions after the addition to identify that the sum should be less than one of
the operands if and only if the other operand is negative.
RISC V Extensions
Comparing assembly code that uses arrays and array indices to the
assembly code that uses pointers offers insights about pointers.
Assembly code
li // i = 0
loop1: slli x6, x5 ,2 // x6 = i * 4
x5,0
add x7, x10, x6 // x7 = address of
array[i] sw x0, 0(x7) // array[i] = 0
addi x5, x5, 1 // i = i + 1
blt x5,x11,loop1 // if (i<size) go
to loop1
RISC V ARCHITECTURE
Arrays versus Pointers
loop2:
sw x0,0(x5) // Memory[p] = 0
addi x5,x5,4 // p = p + 4
bltu x5,x7,loop2 // if (p<&array[size]) go to loop2
RISC V ARCHITECTURE
Arrays versus Pointers
Pointer Version of Clear
Assembly code
RISC V ARCHITECTURE
Arrays versus Pointers
Pointer Version of Clear
Assembly code: xly and add outside the loop:
RISC V ARCHITECTURE
Arrays versus Pointers
• In Array version “multiply” and add inside the loop because i is incremented and
each address must be recalculated from the new index.
• The pointer version moves the scaling shift and the array bound addition outside the
loop,
thereby reducing the instructions executed per iteration from five to three.
Rajeshwari B
Department of Electronics and Communication Engineering
RISC V ARCHITECTURE
Compiling C
Example:
To illustrate the concepts, the C version of a while loop considered
while (save[i] == k)
i += 1;
The function of the front end is to read in a source program; check the
syntax and semantics;
In the above example, the token sequence is while, (, save, [, i, ], ==, k, ), i, +=, 1.
A word like while is recognized as a reserved word in C, but save, i, and j are
recognized as names, and 1 is recognized as a number.
2. Parsing takes the token stream, ensures the syntax is correct, and produces an
abstract syntax tree, which is a representation of the syntactic structure of the
program.
RISC V ARCHITECTURE
Compiling C
3. Semantic analysis takes the abstract syntax tree and checks the program
for semantic correctness. Semantic checks normally ensure that variables
and types are properly declared and that the types of operators and objects
match, a step called type checking.
During this process, a symbol table representing all the named objects—
classes, variables, and functions—is usually created and used to type-check
the program.
The most common intermediate form looks much like the RISC-V instruction
set but with an infinite number of virtual registers;
RISC V ARCHITECTURE
Compiling C
High-Level Optimizations
1. Local optimization works within a single basic block. A local optimization pass is
often run as a precursor and successor to global optimization to “clean up” the
code before and after global optimization.
3. Global register allocation allocates variables to registers for regions of the code.
Register allocation is crucial to getting good performance in modern processors.
RISC V ARCHITECTURE
Compiling C
multiple instances of the same expression and replaces the second one by a
reference to the first. Consider, for example, a code segment to add 4 to an
array element: x[i] = x[i] + 4
Recalculating address x[i] again using
different set of registers
x[i] = x[i] + 4
// x[i] + 4
sw r105,0(r109)
lw r104, 0(r103)
addi r105, r104,4
RISC V ARCHITECTURE
Compiling C
Common subexpression elimination
// x[i] + 4
■ Strength reduction replaces complex operations by simpler ones and can be applied
to this code segment, replacing the mul by a shift left.
■ Constant propagation and its sibling constant folding find constants in code and
propagate them, collapsing constant values whenever possible.
■ Copy propagation propagates values that are simple copies, eliminating the need to
reload values and possibly enabling other optimizations, such as common
subexpression elimination.
■ Dead store elimination finds stores to values that are not used again and eliminates
the store; its “cousin” is dead code elimination, which finds unused code—code that
cannot affect the result of the program—and eliminates it.
RISC V ARCHITECTURE
Compiling C
There are two other important global optimizations: code motion and induction
variable elimination. Both are loop optimizations; that is, they are aimed at code in
loops.
Code motion finds code that is loop invariant: a particular piece of code computes
the same value on every iteration of the loop and, hence, may be computed once
outside the loop.
Induction variable elimination is a combination of transformations that reduce
overhead on indexing arrays, essentially replacing array indexing with pointer
accesses.
RISC V ARCHITECTURE
Compiling C
■ Consider the second lw of i into R107 within the earlier example: how do
we know whether its value is used again?
■ Finally, consider the load of k in our loop, which is a candidate for code
motion. In this simple example, we might argue that it is easy to see that k
is not changed in the loop and is, hence, loop invariant.
RISC V ARCHITECTURE
Compiling C
A control flow graph for the while
loop example.
RISC V ARCHITECTURE
Compiling C
The control flow graph showing the representation of the while
loop example after code motion and induction variable elimination
RISC V ARCHITECTURE
Compiling C
Register Allocation
Pitfalls
The two principles of the stored-program computer are the use of instructions
that are indistinguishable from numbers and the use of alterable memory for
programs.
Design principles
1. Simplicity favors regularity
2. Smaller is faster
3.Good design demands good compromises
Make the common case fast
Layers of software/hardware
Compiler, assembler, hardware
RISC-V: typical of RISC ISAs compared
to x86
THANK YOU
Vanamala H R
Department of Electronics and Communication