Storage Allocation for Embedded Processors By Jan Sjodin & Carl von Platen Present by Xie Lei ( PLS Lab)
Memory In Embedded Sys Several memory areas (on-chip, off-chip) Several memory areas (on-chip, off-chip) SRAM SRAM DRAM DRAM E 2 PROM … E 2 PROM … Different properties Different properties access time access time size size restricted to native pointer types restricted to native pointer types
Pointer Types Different in length Different in length 8-bit 8-bit 16-bit bit... Different in cost Different in cost 8-bit is cheaper than 16-bit 8-bit is cheaper than 16-bit
Problem to solve Allocate data and select pointer types in the most efficient way Allocate data and select pointer types in the most efficient way Frequently accessed data in fast memory Frequently accessed data in fast memory Frequently used pointers & expression have cheap pointer type Frequently used pointers & expression have cheap pointer type Manual Automatic Manual Automatic
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiments & Results Experiments & Results Conclusion Conclusion
Why Allocation ? Different pointer types to access different types of memories improves performance : execution speed execution speed program size program size energy consumption energy consumption production cost... production cost...
Allocation Problem Allocating each variable of a program in a memory segment Allocating each variable of a program in a memory segment Assigning a native pointer type to each pointer expression of the same program Assigning a native pointer type to each pointer expression of the same program Dependency : latter depends on former Dependency : latter depends on former ---take both into consider simultaneously ---take both into consider simultaneously
Current solution & Drawback Manually locate variables and select native pointer types: pragmas or keywords Manually locate variables and select native pointer types: pragmas or keywords Drawbacks Drawbacks time-consuming time-consuming source code non-portable source code non-portable
New Solution A model which can describe architectures with irregular memory organization and several pointer types. A model which can describe architectures with irregular memory organization and several pointer types. Derive integer linear program from the model Derive integer linear program from the model Solve the program = Solve the allocation problem Solve the program = Solve the allocation problem Optimal solution ( under assumption ) Optimal solution ( under assumption )
An Important Condition Condition : All memory accesses in the program should be known Condition : All memory accesses in the program should be known -- whole program optimization ( WPO ) -- whole program optimization ( WPO ) points-to set : Set of the objects a restricted pointer may point to points-to set : Set of the objects a restricted pointer may point to Reason Reason The points-to set of a pointer must be allocated where that pointer can reach The points-to set of a pointer must be allocated where that pointer can reach
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiment & Results Experiment & Results Conclusion Conclusion
Features in Embedded Processors On-chip memory : efficiently On-chip memory : efficiently Zero page : use smaller pointer types, fast, compact code Zero page : use smaller pointer types, fast, compact code Harvard architectures : different native pointer representations and addressing mode are used for program and data address space Harvard architectures : different native pointer representations and addressing mode are used for program and data address space
AVR RAM Configuration
Separate VS Single address space (1) Separate address space Separate address space disjoint sets of pointer types know points-to sets to allocate data use separate address buses different memory access instructions use different pointer types as operands
Separate VS Single address space (2) Single address space Single address space A general native pointer type No restriction on allocation Try to use cheaper pointer type ( need to know the point-to set to decide if that pointer type can work)
Code Size Contrast
An Abstract Memory Model Memory Model : Memory Segments, Pointer Types, Relation Memory Model : Memory Segments, Pointer Types, Relation Memory Segments : subsets of the total memory space, uniform with properties( speed, addressing modes). M 1, M 2, M 3... M S Memory Segments : subsets of the total memory space, uniform with properties( speed, addressing modes). M 1, M 2, M 3... M S Pointer Types : P 1, P 2, P 3... P T Pointer Types : P 1, P 2, P 3... P T Relation : Pointer Type to Memory Segments Relation : Pointer Type to Memory Segments
Memory Model Size( M j ) : number of addressable units of storage in Memory Segment M j Size( M j ) : number of addressable units of storage in Memory Segment M j Relation function Relation function Mem( P k )={ j | P k can point to M j } Mem( P k )={ j | P k can point to M j } In which P k Pointer Type In which P k Pointer Type Memory Model can be expressed by a table Memory Model can be expressed by a table
Example: AVR
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiment & Results Experiment & Results Conclusion Conclusion
Two Kinds of Cost Cost : Static cost + Dynamic cost Cost : Static cost + Dynamic cost Static cost : the size of a program, expressed by the sum of all statements in instruction set STMTR Static cost : the size of a program, expressed by the sum of all statements in instruction set STMTR Dynamic cost : execution time, for each statement need to be scaled by the estimate execution frequency of the statement Dynamic cost : execution time, for each statement need to be scaled by the estimate execution frequency of the statement
Expressions V = {v 1, v 2... v N } : set of variables referred to in the source program V = {v 1, v 2... v N } : set of variables referred to in the source program Size(v) : number of allocation units required by v Size(v) : number of allocation units required by v STMTR : set of individual occurrences of instructions STMTR : set of individual occurrences of instructions E = {e 1, e 2... e M } : set of pointer expressions E = {e 1, e 2... e M } : set of pointer expressions
Functions PtrExp : STMTR E PtrExp : STMTR E allow the client of the storage allocator to modify the type of single pointer expressions independently allow the client of the storage allocator to modify the type of single pointer expressions independently Seg(v) : V M, the segment where a variable is allocated Seg(v) : V M, the segment where a variable is allocated PtrT(e) : E P, the pointer type a pointer expression is assigned PtrT(e) : E P, the pointer type a pointer expression is assigned
Cost of A Statement ( 1 ) For each statement in STMTR, we can have pointer cost and variable cost For each statement in STMTR, we can have pointer cost and variable cost ptrcost S ( e, t ) : pointer cost, the cost contribution of selecting “ t ” as the pointer type for pointer expression “ e ” ptrcost S ( e, t ) : pointer cost, the cost contribution of selecting “ t ” as the pointer type for pointer expression “ e ” varcost S ( v, m ) : variable cost, the cost contribution of variable “ v ” if it is located in memory segment “ m ” varcost S ( v, m ) : variable cost, the cost contribution of variable “ v ” if it is located in memory segment “ m ”
Cost of A Statement ( 2 ) The cost of a particular solution is defined in terms of the cost contribution of each statement in STMTR The cost of a particular solution is defined in terms of the cost contribution of each statement in STMTR Cost(S) = ∑ (e E) ptrcost S ( e, PtrT (e) ) Cost(S) = ∑ (e E) ptrcost S ( e, PtrT (e) ) + ∑ (v V) varcost S ( v, Seg(v) ) + ∑ (v V) varcost S ( v, Seg(v) )
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiment & Results Experiment & Results Conclusion Conclusion
BIP Formulation BIP : Binary Integer Program BIP : Binary Integer Program The storage allocation problem is formulated as a BIP The storage allocation problem is formulated as a BIP Based on the model of Memory Organization and the cost model Based on the model of Memory Organization and the cost model
Feasible Solution Two condition must be satisfied Two condition must be satisfied Total size of the variables in a single memory segment ≤ size of the segment Total size of the variables in a single memory segment ≤ size of the segment Pointer expression that may point to a variable must be assigned a type that can access the segment where that variable is Pointer expression that may point to a variable must be assigned a type that can access the segment where that variable is Find a feasible solution with minimal cost ! Find a feasible solution with minimal cost !
BIP ( 1 ) Each variable can be placed in exactly one memory segment Each variable can be placed in exactly one memory segment x i1 + x i x iS =1, 1 ≤ i ≤ N x i1 + x i x iS =1, 1 ≤ i ≤ N Total size of the variables in a segment can not be greater than the size of the segment Total size of the variables in a segment can not be greater than the size of the segment Size(v 1 ) x 1m + Size(v 2 ) x 2m Size(v N ) x Nm Size(v 1 ) x 1m + Size(v 2 ) x 2m Size(v N ) x Nm ≤ Size(M m ), 1 ≤ m ≤ S
BIP ( 2 ) Each pointer expression have a unique pointer type Each pointer expression have a unique pointer type y j1 + y j y jT = 1, 1 ≤ j≤ M y j1 + y j y jT = 1, 1 ≤ j≤ M Type of a pointer expression be general enough for all the variables in its points-to set Type of a pointer expression be general enough for all the variables in its points-to set Seg(v) Mem ( PtrT( e j ) ), v Pt ( e j ) Seg(v) Mem ( PtrT( e j ) ), v Pt ( e j )
BIP ( 3 ) Objective function is the cost of the solution Objective function is the cost of the solution
Discussion : Scalability Discussion : Scalability Number of variables and number of pointer expressions grow linearly in the size of the program Number of variables and number of pointer expressions grow linearly in the size of the program Number of memory segments and the number of pointer types depend on target architecture, so can be regarded as constants Number of memory segments and the number of pointer types depend on target architecture, so can be regarded as constants
Discussion : Accuracy Fundamental to the model : cost of a statement can be expressed as linear combination of cost from variables and pointer expressions ---- Not precise Fundamental to the model : cost of a statement can be expressed as linear combination of cost from variables and pointer expressions ---- Not precise Some architectures may represent pointers with multiple machine words, so loading a pointer need several instructions Some architectures may represent pointers with multiple machine words, so loading a pointer need several instructions
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiment & Results Experiment & Results Conclusion Conclusion
Compiler Framework
WPO Prototype Points-to analysis Points-to analysis estimate the points-to set of each pointer estimate the points-to set of each pointer ILP solver ILP solver Information about all global variables and pointers is put into the solver to get an allocation Information about all global variables and pointers is put into the solver to get an allocation
Modified ICCAVR Compile the C source code and use the allocation information by WPO to modify the memory attributes of variables Compile the C source code and use the allocation information by WPO to modify the memory attributes of variables Allocation information is used during the parsing stage to modify the memory attributes of variables Allocation information is used during the parsing stage to modify the memory attributes of variables
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiments & Results Experiments & Results Conclusion Conclusion
Experiments Measure the execution time and code size of 6 benchmarks Measure the execution time and code size of 6 benchmarks Results are compared with code compiled on the standard ICCAR compiler Results are compared with code compiled on the standard ICCAR compiler Different version of AVR microcontroller, internal memory vary from 0 to 4KB or more ( 64KB and 16KB) Different version of AVR microcontroller, internal memory vary from 0 to 4KB or more ( 64KB and 16KB)
Results Results vary a lot with benchmarks Results vary a lot with benchmarks CPU intensive or Memory intensive CPU intensive or Memory intensive Code size decreased slightly for most benchmarks ( Special : statemate ) Code size decreased slightly for most benchmarks ( Special : statemate ) Execution time improved for most benchmarks( also Statemate ) Execution time improved for most benchmarks( also Statemate )
Road Map Brief Background Brief Background Memory Organization & Model Memory Organization & Model Cost Model Cost Model Formulation & Discussion Formulation & Discussion Implementation Implementation Experiments & Results Experiments & Results Conclusion Conclusion
Conclusion Model of memory hierarchies for storage allocation for embedded system processors Model of memory hierarchies for storage allocation for embedded system processors Implement a memory allocator using ILP to get an optimal allocation under the memory model Implement a memory allocator using ILP to get an optimal allocation under the memory model Improve program size and speed Improve program size and speed Automatically done, portable, robust : no need to include target-specific information in source code Automatically done, portable, robust : no need to include target-specific information in source code
The End The End Thank You ! Thank You !