exo-lang/exo

Reversing the iteration dimensions

Opened this issue · 2 comments

In some problems, you want to reverse the direction in which you iterate over a dimension.

For example, the natural starting algorithm of an algorithm that operate on the upper triangular part of a matrix is to iterate in the following way:

---------
| ----->|
|  ---->|
|   --->|
|    -->|
|     ->|
--------

However, as you schedule you want the tail to be at the diagonal rather than at the right side of the matrix; I can provide more context why is that if necessary. So, you will want to reverse the direction of the iteration dimensions over the rows:

---------
| <-----|
|  <----|
|   <---|
|    <--|
|     <-|
--------

Supporting that will require a new primitive that performs the following transformation:

@sched_op([ForCursorA])
def reverse_loop(proc, loop):
     """
     for i in seq(lo, hi):
          s
          ->
          s [i -> hi - (i - lo) - 1]
     """

As a sanity check:
revese_loop(reverse_loop(proc, loop), loop) would give us hi - ((hi - (i - lo) - 1) - lo) - 1 = hi - hi + i - lo + 1 + lo - 1 = i

There is a question of what analysis might be necessary to support this. A simple over-approximation would be to reuse the same analysis for parallelizing a loop then write a more specific analysis later on when the analysis gets revamped.

Loop carry dependency analysis will be necessary to support this, this is a good motivating example for the value sensitive analysis