stanford-ppl / spatial

Spatial: "Specify Parameterized Accelerators Through Inordinately Abstract Language"
https://spatial.stanford.edu
MIT License
271 stars 33 forks source link

Broadcast + CSE = Malformed Memory #274

Open mattfel1 opened 4 years ago

mattfel1 commented 4 years ago

Related to issue #273 , this is an attempt to rewrite the code to make 2 duplicates of the memory. The problem is that the mux(slice == i...) gets CSE'ed during unrolling, and the SRAMBankedRead of the second mem is a broadcast read whose source SRAMBankedRead node was deleted. If broadcasting were the correct thing to do here (which its not anyway, see #273), there should be a cleanup pass to make sure that if the original broadcaster node is deleted, the broadcast receivers are updated so one of them becomes the new broadcaster.

@spatial class hotfix_opt2 extends SpatialTest {
 def main(args: Array[String]): Unit = {
     type T = Int
     val N = ArgIn[Int]
     val np = 2
     val ep = 4
     val func: Idx => Int = {x => mux(random[Bit], 5.to[T], 10.to[T])}
     setArg(N, 888)
     Accel {
        val mem = List.tabulate(np){i => SRAM[T](32,ep).hierarchical}
        Foreach(N by 1 par 1) { i =>
            Foreach(ep by 1 par 1) { j =>
                mem.map{_(i,j) = j}
            }
        }
        Foreach(N by 1 par np) { i =>
           val a = func(i)
           val b = func(i)
           val slice = i % np
           Foreach(a by b par ep) { j =>
               val c = func(j)
               val read = mem.zipWithIndex.map{case (m,i) => mux(slice == i, m(c,j), 0.to[T])}.reduce{_|_}
               println(r"${read}")
           }
        }
     }

     assert(true)
  }
}