<commit_before>package spatial.analysis

import argon.core._
import spatial.aliases._
import spatial.metadata._
import spatial.nodes._
import spatial.utils._

trait UnrolledControlAnalyzer extends ControlSignalAnalyzer {
  override val name = "Unrolled Control Analyzer"

  var memStreams = Set[(Exp[_], Int, Int)]()
  var genericStreams = Set[(Exp[_], String)]()
  var argPorts = Set[(Exp[_], String)]()

  private def visitUnrolled(e: Exp[_])(blk: => Unit) = visitBlk(e)(blk)

  override protected def preprocess[S:Type](block: Block[S]) = {
    memStreams = Set[(Exp[_], Int, Int)]()
    genericStreams = Set[(Exp[_], String)]()
    argPorts = Set[(Exp[_], String)]()
    super.preprocess(block)
  }

  override def addCommonControlData(lhs: Sym[_], rhs: Op[_]) = {
    rhs match {
      case DRAMNew(dims,zero) =>
        memStreams += ((lhs, 0, 0))
      case FringeDenseLoad(dram,_,_) =>
        val prevLoads = memStreams.toList.filter{_._1 == dram}.head._2
        val prevStores = memStreams.toList.filter{_._1 == dram}.head._3
        memStreams -= ((dram, prevLoads, prevStores))<commit_msg>Added dead dispatch removal in UnrolledControlAnalyzer<commit_after>package spatial.analysis

import argon.core._
import spatial.aliases._
import spatial.metadata._
import spatial.nodes._
import spatial.utils._

trait UnrolledControlAnalyzer extends ControlSignalAnalyzer {
  override val name = "Unrolled Control Analyzer"

  var memStreams = Set[(Exp[_], Int, Int)]()
  var genericStreams = Set[(Exp[_], String)]()
  var argPorts = Set[(Exp[_], String)]()

  private def visitUnrolled(e: Exp[_])(blk: => Unit) = visitBlk(e)(blk)

  override protected def preprocess[S:Type](block: Block[S]): Block[S] = {
    memStreams = Set[(Exp[_], Int, Int)]()
    genericStreams = Set[(Exp[_], String)]()
    argPorts = Set[(Exp[_], String)]()
    super.preprocess(block)
  }

  override protected def postprocess[S:Type](block: Block[S]): Block[S] = {
    // Eliminate duplicates which no longer have readers as long as at least one duplicate is retained
    localMems.foreach{mem =>
      val orig = duplicatesOf(mem)
      val inds: Set[Int] = orig.indices.toSet
      val readers = readersOf(mem)
      val writers = writersOf(mem)

      val readDuplicates = readers.flatMap{read => dispatchOf(read,mem) }.toSet
      val unreadDuplicates = inds diff readDuplicates

      var removedDuplicates = unreadDuplicates
      writers.foreach{write =>
        val dispatch = dispatchOf(write, mem)
        val remaining = dispatch diff removedDuplicates
        if (remaining.isEmpty) {
          removedDuplicates = removedDuplicates - dispatch.head
        }
      }
      if (removedDuplicates.nonEmpty) {
        dbgs(u"Memory $mem: ")
        dbgs(c"  ${str(mem)}")
        dbgs("  Removing dead duplicates: " + removedDuplicates.mkString(", "))

        val accesses = readers ++ writers
        val duplicates = orig.zipWithIndex.filter{case (dup,i) => !removedDuplicates.contains(i) }
        val mapping = duplicates.map(_._2).zipWithIndex.toMap
        duplicatesOf(mem) = duplicates.map(_._1)

        accesses.foreach{access =>
          dispatchOf(access,mem) = dispatchOf(access,mem).flatMap{o => mapping.get(o) }
          portsOf.set(access.node,mem, {
            portsOf(access,mem).flatMap{case (i,ps) => mapping.get(i).map{i2 => i2 -> ps}}
          })
        }
      }
    }
    super.postprocess(block)
  }

  override def addCommonControlData(lhs: Sym[_], rhs: Op[_]) = {
    rhs match {
      case DRAMNew(dims,zero) =>
        memStreams += ((lhs, 0, 0))
      case FringeDenseLoad(dram,_,_) =>
        val prevLoads = memStreams.toList.filter{_._1 == dram}.head._2
        val prevStores = memStreams.toList.filter{_._1 == dram}.head._3
        memStreams -= ((dram, prevLoads, prevStores))
{'fraction_non_alphanumeric': 0.13659049120042027, 'fraction_numerical': 0.0042027843446283165, 'mean_word_length': 3.392156862745098, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815503', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1206, 'n_words': 275}