Download Data Prefetch - Advanced Compiling Techniques - Lecture Slides and more Slides Compilers in PDF only on Docsity!
Data Prefetch and Software
Pipelining
Agenda
- Data Prefetch
- Software Pipelining
Data Prefetching
- What is it?
- Request for a future data need is initiated
- Useful execution continues during access
- Data moves from slow/far memory to fast/near cache
- Data ready in cache when needed (load/store)
Data Prefetching
- When can it be used?
- Future data needs are (somewhat) predictable
- How is it implemented?
- in hardware: history based prediction of future access
- in software: compiler inserted prefetch instructions
Software Data Prefetching Use
- Mostly in Scientific codes
- Vectorizable loops accessing arrays deterministically - Data access pattern is predictable - Prefetch scheduling easy (far in time, near in code)
- Large working data sets consumed
- Even large caches unable to capture access locality
- Sometimes in Integer codes
- Loops with pointer de-references
Selective Data Prefetch
do j = 1, n do i = 1, m A(i,j) = B(1,i) + B(1,i+1) enddo enddo
- E.g. A(i,j) has spatial locality, therefore only one prefetch is required for every cache line.
A
j
i
B 1 1 m
Prefetch Predicates
- If an access has spatial locality, only the first access to the same cache line will incur a miss.
- For temporal locality, only the first access will incur a cache miss
- If an access has group locality, only the leading reference incurs cache miss.
- If an access has no locality, it will miss in every iteration.
Example Code with Prefetches
do j = 1, n do i = 1, m A(i,j) = B(1,i) + B(1,i+1) if (iand(i,7) == 0) prefetch (A(i+k,j)) if (j == 1) prefetch (B(1,i+t)) enddo enddo
A
j
i
B 1 1 m
Assumed CLS = 64 bytes and data size = 8 bytes
k and t are prefetch distance values
Example Code with Spreading
do j = 1, n do i = 1, m C(i,j) = D(i-1,j) + D(i+1,j) if (iand(i,7) == 0) prefetch (C(i+k,j)) if (iand(i,7) == 1) prefetch (D(i+k+1,j)) enddo enddo
C, D
j
i
Assumed CLS = 64 bytes and data size = 8 bytes k is the prefetch distance value
Prefetch Strategy - Conditional
Example loop L:Load A(I) Load B(I)... IBr L, = I + 1if I<n
Conditional Prefetching L:Load A(I) Load B(I)Cmp pA=(I mod 8 == 0) if(pA) prefetchA(I+X) Cmp pB=(I mod 8 == 1)If(pB) prefetch B(I+X)... Code for condition I = I + 1Br L, if I<n generation
Prefetches occupy issue slots
Software Data Prefetching Cost
- Requires memory instruction resources
- A prefetch instruction for each access stream
- Issues every iteration, but needed less often
- If branched around, inefficient execution results
- If conditionally executed, more instruction overhead results
- If loop is unrolled, code bloat results
Software Data Prefetching Cost
- Redundant prefetches get in the way
- Resources consumed until prefetches discarded!
- Non redundant need careful scheduling
- Resources overwhelmed when many issued & miss
Rotating Registers
- Register rotation provides an automatic renaming mechanism.
- Instructions contain a “virtual” register number
Iteration 1 r32r33r34r35r36r37r38r
r33r34r35r36r37r38r39r
r34r35r36r37r38r39r32r
Iteration 2
Iteration 3
Rotating Reg Prefetch Illustrated
Example loop Orig_loop:Load A(I) Load B(I)Load C(I) Load D(I)Load E(I) ...I = I + 1 Br Orig_loop,if I<n
Rotating Register Prefetching r33 = address of E(1+X) r34 = address of D(1+X) r35 = address of C(1+X) r36 = address of B(1+X) r37 = address of A(1+X) Method1Loop: prefetch [r37] r32 = r37 + INCR
... load A(I) load B(I) load C(I) load D(I) load E(I) ... I = I + 1 Br Method1Loop, if I<n
Single prefetch inst No loop unrolling At most 1 miss/iter