<commit_before>package spatial.codegen.pirgen

import argon.core._
import argon.nodes._
import spatial.aliases._
import spatial.metadata._
import spatial.nodes._
import spatial.utils._
import org.virtualized.SourceContext

import scala.collection.mutable

class PIRMemoryAnalyzer(implicit val codegen:PIRCodegen) extends PIRTraversal {
  override val name = "PIR Memory Analyzer"
  var IR = codegen.IR

  override def preprocess[S:Type](b: Block[S]): Block[S] = {
    super.preprocess(b)
  }

  override def postprocess[S:Type](b: Block[S]): Block[S] = {
    super.postprocess(b)
  }

  override protected def visit(lhs: Sym[_], rhs: Op[_]) = {
    rhs match {
      case ParLocalReader(reads)  =>
        val (mem, addrs, _) = reads.head
        addrs.foreach { addrs => 
          markInnerDim(mem, addrs.head)
        }

      case ParLocalWriter(writes)  => 
        val (mem, value, addrs, ens) = writes.head
        addrs.foreach { addrs => 
          markInnerDim(mem, addrs.head)
        }

      case _ => 
    }
    super.visit(lhs, rhs)
  }

  def containsInnerInd(ind:Expr):Boolean = {
    ind match {
      case _:Bound[_] => isInnerControl(ctrlOf(ind).get.node)
      case e:Sym[_] => e.dependents.exists(containsInnerInd)
      case e => false
    }
  }

  def markInnerDim(mem:Expr, inds:Seq[Expr]) = {
    inds.zipWithIndex.foreach { case (ind, dim) =>
      if (containsInnerInd(ind)) {
        innerDimOf(mem) = dim
        dbgs(s"${qdef(mem)}")
        dbgs(s"innerDimOf($mem) = $dim")
      }
    }
  }

}
<commit_msg>bug fix to analysis on sram dimension accessing inner dimension<commit_after>package spatial.codegen.pirgen

import argon.core._
import argon.nodes._
import spatial.aliases._
import spatial.metadata._
import spatial.nodes._
import spatial.utils._
import org.virtualized.SourceContext

import scala.collection.mutable

class PIRMemoryAnalyzer(implicit val codegen:PIRCodegen) extends PIRTraversal {
  override val name = "PIR Memory Analyzer"
  var IR = codegen.IR

  override def preprocess[S:Type](b: Block[S]): Block[S] = {
    super.preprocess(b)
  }

  override def postprocess[S:Type](b: Block[S]): Block[S] = {
    super.postprocess(b)
  }

  override protected def visit(lhs: Sym[_], rhs: Op[_]) = {
    rhs match {
      case ParLocalReader(reads)  =>
        val (mem, addrs, _) = reads.head
        addrs.foreach { addrs => 
          markInnerDim(mem, addrs.head)
        }

      case ParLocalWriter(writes)  => 
        val (mem, value, addrs, ens) = writes.head
        addrs.foreach { addrs => 
          markInnerDim(mem, addrs.head)
        }

      case _ => 
    }
    super.visit(lhs, rhs)
  }

  def containsInnerInd(ind:Expr):Boolean = {
    ind match {
      case b:Bound[_] => 
        val ctrl = ctrlOf(ind).get.node
        extractInnerBounds(ctrl).contains(b)
      case e:Sym[_] => e.dependents.exists(containsInnerInd)
      case e => false
    }
  }

  def extractInnerBounds(ctrl:Expr) = ctrl match {
      case ctrl if !isInnerControl(ctrl) => Nil
      case Def(UnrolledForeach(en, cchain, func, iters, valids)) => 
        iters.last
      case Def(UnrolledReduce(en, cchain, accum, func, iters, valids)) =>
        iters.last
      case _ => Nil
  }

  def markInnerDim(mem:Expr, inds:Seq[Expr]) = {
    inds.zipWithIndex.foreach { case (ind, dim) =>
      if (containsInnerInd(ind)) {
        innerDimOf(mem) = dim
        dbgs(s"${qdef(mem)}")
        dbgs(s"innerDimOf($mem) = $dim")
      }
    }
  }

}

{'fraction_non_alphanumeric': 0.12693854106835153, 'fraction_numerical': 0.0, 'mean_word_length': 2.7858695652173915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '5106533', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1170, 'n_words': 250}