Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ActualDirection calculation from SpecifiedDirection #4205

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions core/src/main/scala/chisel3/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1100,12 +1100,7 @@ abstract class Record extends Aggregate {
direction = ActualDirection.fromChildren(childDirections, resolvedDirection) match {
case Some(dir) => dir
case None =>
val resolvedDirection = SpecifiedDirection.fromParent(parentDirection, specifiedDirection)
resolvedDirection match {
case SpecifiedDirection.Unspecified => ActualDirection.Bidirectional(ActualDirection.Default)
case SpecifiedDirection.Flip => ActualDirection.Bidirectional(ActualDirection.Flipped)
case _ => ActualDirection.Bidirectional(ActualDirection.Default)
}
throwException(s"Internal Error! Unhandled directionality of children: $childDirections for $this!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to cover this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal error that isn't supposed to be reachable.

}
setElementRefs()

Expand Down
9 changes: 6 additions & 3 deletions core/src/main/scala/chisel3/Data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ object ActualDirection {
case _ => throwException(s"Unexpected ActualDirection value $b")
}

/** Converts a `SpecifiedDirection` to an `ActualDirection`
*
* Implements the Chisel convention that Flip is Input and unspecified is Output.
*/
def fromSpecified(direction: SpecifiedDirection): ActualDirection = direction match {
case SpecifiedDirection.Unspecified | SpecifiedDirection.Flip => ActualDirection.Unspecified
case SpecifiedDirection.Output => ActualDirection.Output
case SpecifiedDirection.Input => ActualDirection.Input
case SpecifiedDirection.Output | SpecifiedDirection.Unspecified => ActualDirection.Output
jackkoenig marked this conversation as resolved.
Show resolved Hide resolved
case SpecifiedDirection.Input | SpecifiedDirection.Flip => ActualDirection.Input
}

/** Determine the actual binding of a container given directions of its children.
Expand Down
74 changes: 61 additions & 13 deletions src/test/scala/chiselTests/Direction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
}

property("Directions should be preserved through cloning and binding of Bundles") {
ChiselStage.emitCHIRRTL(new Module {
class MyBundle extends Bundle {
val foo = Input(UInt(8.W))
val bar = Output(UInt(8.W))
}
class MyOuterBundle extends Bundle {
val fizz = new MyBundle
val buzz = Flipped(new MyBundle)
}
class MyBundle extends Bundle {
val foo = Input(UInt(8.W))
val bar = Output(UInt(8.W))
}
class MyOuterBundle extends Bundle {
val fizz = new MyBundle
val buzz = Flipped(new MyBundle)
}
class Top(hwop: MyOuterBundle => MyOuterBundle) extends Module {
val a = new MyOuterBundle
val b = IO(a)
val b = hwop(a)
val specifiedDirs = Seq(
a.fizz.foo -> SpecifiedDirection.Input,
a.fizz.bar -> SpecifiedDirection.Output,
Expand All @@ -193,11 +193,15 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
for ((data, dir) <- actualDirs) {
DataMirror.directionOf(data) shouldBe (dir)
}
}.asInstanceOf[Module]) // The cast works around weird reflection behavior (bug?)
}
val ops: Seq[MyOuterBundle => MyOuterBundle] = Seq(IO(_), Wire(_))
for (op <- ops) {
ChiselStage.emitCHIRRTL(new Top(op))
}
}

property("Directions should be preserved through cloning and binding of Vecs") {
ChiselStage.emitCHIRRTL(new Module {
class Top(hwop: Vec[Vec[UInt]] => Vec[Vec[UInt]]) extends Module {
val a = Vec(1, Input(UInt(8.W)))
val b = Vec(1, a)
val c = Vec(1, Flipped(a))
Expand Down Expand Up @@ -226,7 +230,11 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
for ((data, dir) <- actualDirs) {
DataMirror.directionOf(data) shouldBe (dir)
}
}.asInstanceOf[Module]) // The cast works around weird reflection behavior (bug?)
}
val ops: Seq[Vec[Vec[UInt]] => Vec[Vec[UInt]]] = Seq(IO(_), Wire(_))
for (op <- ops) {
ChiselStage.emitCHIRRTL(new Top(op))
}
}

property("Using Vec and Flipped together should calculate directions properly") {
Expand Down Expand Up @@ -493,6 +501,45 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
assert(emitted.contains("connect io.monitor.valid, io.driver.valid"))
assert(emitted.contains("connect io.monitor.ready, io.driver.ready"))
}

property("Output mixed with unspecified directions should report Output") {
class MyBundle extends Bundle {
val foo = UInt(8.W)
val bar = Output(UInt(8.W))
}
class MyModule extends RawModule {
val w = Wire(new MyBundle)
assert(DataMirror.specifiedDirectionOf(w) == SpecifiedDirection.Unspecified)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree with the tests tho

assert(DataMirror.specifiedDirectionOf(w.foo) == SpecifiedDirection.Unspecified)
assert(DataMirror.specifiedDirectionOf(w.bar) == SpecifiedDirection.Output)
assert(DataMirror.directionOf(w) == Direction.Output)
assert(DataMirror.directionOf(w.foo) == Direction.Output)
assert(DataMirror.directionOf(w.bar) == Direction.Output)

}
val chirrtl = ChiselStage.emitCHIRRTL(new MyModule)
assert(chirrtl.contains("wire w : { foo : UInt<8>, bar : UInt<8>}"))
}

property("Input mixed with Flipped directions should report Input") {
class MyBundle extends Bundle {
val foo = Flipped(UInt(8.W))
val bar = Input(UInt(8.W))
}
class MyModule extends RawModule {
val w = Wire(new MyBundle)
assert(DataMirror.specifiedDirectionOf(w) == SpecifiedDirection.Unspecified)
assert(DataMirror.specifiedDirectionOf(w.foo) == SpecifiedDirection.Flip)
assert(DataMirror.specifiedDirectionOf(w.bar) == SpecifiedDirection.Input)
assert(DataMirror.directionOf(w) == Direction.Input)
assert(DataMirror.directionOf(w.foo) == Direction.Input)
assert(DataMirror.directionOf(w.bar) == Direction.Input)

}
val chirrtl = ChiselStage.emitCHIRRTL(new MyModule)
assert(chirrtl.contains("wire w : { flip foo : UInt<8>, flip bar : UInt<8>}"))
}

jackkoenig marked this conversation as resolved.
Show resolved Hide resolved
property("Bugfix: marking Vec fields with mixed directionality as Output/Input clears inner directions") {
class Decoupled extends Bundle {
val bits = UInt(3.W)
Expand Down Expand Up @@ -559,4 +606,5 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
val emitted: String = ChiselStage.emitCHIRRTL(new MyModule)
assert(emitted.contains("Probe<const UInt<1>>"))
}

}