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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -1088,12 +1088,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.

}
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 @@ -103,10 +103,13 @@ object ActualDirection {

case class Bidirectional(dir: BidirectionalDirection) extends ActualDirection

/** 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
Copy link
Contributor

@mwachs5 mwachs5 Jun 26, 2024

Choose a reason for hiding this comment

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

is this actually sound ...? Output is coercing all leaf members while Unspecified is not. I dont think this is equivalent

Copy link
Contributor Author

@jackkoenig jackkoenig Jun 26, 2024

Choose a reason for hiding this comment

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

It's unstated in the ScalaDoc but this function is only ever used to determine the ActualDirection for Elements. It wouldn't be correct for any Aggregate because it would need to handle ActualDirection.Bidirectional(_). As for coercion, chisel3.Output the operator coerces, but the ActualDirection it just reporting the final resolved direction for something after all flips and coercions from above have been applied.

case SpecifiedDirection.Input | SpecifiedDirection.Flip => ActualDirection.Input
}

/** Determine the actual binding of a container given directions of its children.
Expand Down
40 changes: 40 additions & 0 deletions src/test/scala/chiselTests/Direction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,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>}"))
}

Copy link
Member

Choose a reason for hiding this comment

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

What is directionOf a wire that has input and output? Is that already tested in this file as bidirectional? Is that missing a test?

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 +598,5 @@ class DirectionSpec extends ChiselPropSpec with Matchers with Utils {
val emitted: String = ChiselStage.emitCHIRRTL(new MyModule)
assert(emitted.contains("Probe<const UInt<1>>"))
}

}