Skip to content

Commit

Permalink
Provide an annotation mix-in that marks RTs as dontTouch (#1433)
Browse files Browse the repository at this point in the history
* Provide an annotation mix-in that marks RTs as dontTouch

* Update src/main/scala/firrtl/transforms/OptimizationAnnotations.scala

Co-Authored-By: Albert Magyar <[email protected]>

* Update src/test/scala/firrtlTests/DCETests.scala

Co-Authored-By: Albert Magyar <[email protected]>

* Update src/main/scala/firrtl/transforms/OptimizationAnnotations.scala

* Update OptimizationAnnotations.scala

Co-authored-by: Albert Magyar <[email protected]>
  • Loading branch information
davidbiancolin and albert-magyar authored Mar 10, 2020
1 parent 0c40f2b commit 113a4aa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/main/scala/firrtl/transforms/ConstantPropagation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,12 @@ class ConstantPropagation extends Transform with ResolvedAnnotationPaths {
}

def execute(state: CircuitState): CircuitState = {
val dontTouches: Seq[(OfModule, String)] = state.annotations.collect {
case DontTouchAnnotation(Target(_, Some(m), Seq(Ref(c)))) => m.OfModule -> c
val dontTouchRTs = state.annotations.flatMap {
case anno: HasDontTouches => anno.dontTouches
case o => Nil
}
val dontTouches: Seq[(OfModule, String)] = dontTouchRTs.map {
case Target(_, Some(m), Seq(Ref(c))) => m.OfModule -> c
}
// Map from module name to component names
val dontTouchMap: Map[OfModule, Set[String]] =
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/firrtl/transforms/DeadCodeElimination.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ class DeadCodeElimination extends Transform with ResolvedAnnotationPaths with Re
Seq(classOf[DontTouchAnnotation], classOf[OptimizableExtModuleAnnotation])

def execute(state: CircuitState): CircuitState = {
val dontTouches: Seq[LogicNode] = state.annotations.collect {
case DontTouchAnnotation(component: ReferenceTarget) if component.isLocal => LogicNode(component)
val dontTouches: Seq[LogicNode] = state.annotations.flatMap {
case anno: HasDontTouches => anno.dontTouches.filter(_.isLocal).map(LogicNode(_))
case o => Nil
}
val doTouchExtMods: Seq[String] = state.annotations.collect {
case OptimizableExtModuleAnnotation(ModuleName(name, _)) => name
Expand Down
24 changes: 23 additions & 1 deletion src/main/scala/firrtl/transforms/OptimizationAnnotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@ import firrtl.passes.PassException
/** Indicate that DCE should not be run */
case object NoDCEAnnotation extends NoTargetAnnotation

/** Lets an annotation mark its ReferenceTarget members as DontTouch
*
* This permits a transform to run and remove its associated annotations,
* thus making their ReferenceTargets new candidates for optimization. This
* removes the need for the pass writer to reason about pre-existing
* DontTouchAnnotations that may touch the same node.
*/
trait HasDontTouches { self: Annotation =>
def dontTouches: Iterable[ReferenceTarget]
}

/**
* A globalized form of HasDontTouches which applies to all ReferenceTargets
* provided with the annotation
*/
trait DontTouchAllTargets extends HasDontTouches { self: Annotation =>
def dontTouches: Iterable[ReferenceTarget] = getTargets.collect {
case rT: ReferenceTarget => rT
}
}

/** A component that should be preserved
*
* DCE treats the component as a top-level sink of the circuit
*/
case class DontTouchAnnotation(target: ReferenceTarget) extends SingleTargetAnnotation[ReferenceTarget] {
case class DontTouchAnnotation(target: ReferenceTarget)
extends SingleTargetAnnotation[ReferenceTarget] with DontTouchAllTargets {
def targets = Seq(target)
def duplicate(n: ReferenceTarget) = this.copy(n)
}
Expand Down
15 changes: 14 additions & 1 deletion src/test/scala/firrtlTests/ConstantPropagationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,20 @@ class ConstantPropagationIntegrationSpec extends LowTransformSpec {
execute(input, check, Seq(dontTouch("Top.z")))
}

"ConstProp" should "NOT optimize across dontTouch on registers" in {
it should "NOT optimize across nodes marked dontTouch by other annotations" in {
val input =
"""circuit Top :
| module Top :
| input x : UInt<1>
| output y : UInt<1>
| node z = x
| y <= z""".stripMargin
val check = input
val dontTouchRT = annotations.ModuleTarget("Top", "Top").ref("z")
execute(input, check, Seq(AnnotationWithDontTouches(dontTouchRT)))
}

it should "NOT optimize across dontTouch on registers" in {
val input =
"""circuit Top :
| module Top :
Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/firrtlTests/DCETests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import firrtl.passes.memlib.SimpleTransform
import java.io.File
import java.nio.file.Paths

case class AnnotationWithDontTouches(target: ReferenceTarget)
extends SingleTargetAnnotation[ReferenceTarget] with HasDontTouches {
def targets = Seq(target)
def duplicate(n: ReferenceTarget) = this.copy(n)
def dontTouches: Seq[ReferenceTarget] = targets
}

class DCETests extends FirrtlFlatSpec {
// Not using executeTest because it is for positive testing, we need to check that stuff got
// deleted
Expand Down Expand Up @@ -63,6 +70,24 @@ class DCETests extends FirrtlFlatSpec {
| z <= x""".stripMargin
exec(input, check, Seq(dontTouch("Top.a")))
}
"Unread wire marked dont touch by another annotation" should "NOT be deleted" in {
val input =
"""circuit Top :
| module Top :
| input x : UInt<1>
| output z : UInt<1>
| wire a : UInt<1>
| z <= x
| a <= x""".stripMargin
val check =
"""circuit Top :
| module Top :
| input x : UInt<1>
| output z : UInt<1>
| node a = x
| z <= x""".stripMargin
exec(input, check, Seq(AnnotationWithDontTouches(ModuleTarget("Top", "Top").ref("a"))))
}
"Unread register" should "be deleted" in {
val input =
"""circuit Top :
Expand Down

0 comments on commit 113a4aa

Please sign in to comment.