Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Backport ResolveKinds improvements from #1475 to 1.2.x (#1623)
Browse files Browse the repository at this point in the history
* Use HashMap instead of LinkedHashMap in ResolveKinds

* Backport to 1.2.x

(cherry picked from commit 0f78e2d)

Co-authored-by: Albert Magyar <albert.magyar@gmail.com>

* Eliminate unnecessary traversals in ResolveKinds

* Modify for backporting

* Make find_port return Unit and use Foreachers in ResolveKinds

* Modify for backporting

Co-authored-by: Jack Koenig <koenig@sifive.com>
  • Loading branch information
albert-magyar and jackkoenig authored May 15, 2020
1 parent 3510590 commit a99469a
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/main/scala/firrtl/passes/ResolveKinds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,42 @@ package firrtl.passes
import firrtl._
import firrtl.ir._
import firrtl.Mappers._
import firrtl.traversals.Foreachers._
import firrtl.options.PreservesAll

object ResolveKinds extends Pass {

private def find_port(kinds: collection.mutable.HashMap[String, Kind])(p: Port): Unit = {
kinds(p.name) = PortKind
}

def resolve_expr(kinds: collection.mutable.HashMap[String, Kind])(e: Expression): Expression = e match {
case ex: WRef => ex copy (kind = kinds(ex.name))
case _ => e map resolve_expr(kinds)
}

def resolve_stmt(kinds: collection.mutable.HashMap[String, Kind])(s: Statement): Statement = {
s match {
case sx: DefWire => kinds(sx.name) = WireKind
case sx: DefNode => kinds(sx.name) = NodeKind
case sx: DefRegister => kinds(sx.name) = RegKind
case sx: WDefInstance => kinds(sx.name) = InstanceKind
case sx: DefMemory => kinds(sx.name) = MemKind
case _ =>
}
s.map(resolve_stmt(kinds))
.map(resolve_expr(kinds))
}

def resolve_kinds(m: DefModule): DefModule = {
val kinds = new collection.mutable.HashMap[String, Kind]
m.foreach(find_port(kinds))
m.map(resolve_stmt(kinds))
}

def run(c: Circuit): Circuit =
c copy (modules = c.modules map resolve_kinds)

type KindMap = collection.mutable.LinkedHashMap[String, Kind]

def find_port(kinds: KindMap)(p: Port): Port = {
Expand All @@ -32,14 +66,5 @@ object ResolveKinds extends Pass {

def resolve_stmt(kinds: KindMap)(s: Statement): Statement =
s map resolve_stmt(kinds) map resolve_expr(kinds)

def resolve_kinds(m: DefModule): DefModule = {
val kinds = new KindMap
(m map find_port(kinds)
map find_stmt(kinds)
map resolve_stmt(kinds))
}

def run(c: Circuit): Circuit =
c copy (modules = c.modules map resolve_kinds)
}

0 comments on commit a99469a

Please sign in to comment.