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

Conversation

jackkoenig
Copy link
Contributor

@jackkoenig jackkoenig commented Jun 21, 2024

In #2634, we unified the Chisel 2 and Chisel 3 direction semantics, making it legal to have Records where the directionality of elements is mixed (some may be specified, some may be not). The implementation is mostly right, but there was a corner case I came across in #4204.

Previously, ActualDirection.fromSpecified would return Unspecified for either Unspecified or Flipped input. This in turn resulted in Bundles mixing Unspecified and Outputs as being "bidirectional" despite the fact that they are actually unidirectional (or passive), and similarly for Bundles mixing Flipped with Input.

Now, Unspecified maps to Output and Flip maps to Input. This change makes the direction calculation more consistent albeit at the cost of marking the directions of unspecified Data as Output.

I'm backporting this to 6.x because #4204 will manifest there, but this is a pretty big change to the reported direction of Chisel Data so I don't think we should backport it to other branches.

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Bugfix

Desired Merge Strategy

  • Squash

Release Notes

Fixes #4204.

Unspecified direction maps to Output while Flip maps to Input. Previously, ActualDirection.fromSpecified would return Unspecified for either Unspecified or Flipped input. This in turn resulted in Bundles mixing Unspecified and Outputs as being "bidirectional" despite the fact that they are actually unidirectional (or passive).

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash), clean up the commit message, and label with Please Merge.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@jackkoenig jackkoenig added the Bugfix Fixes a bug, will be included in release notes label Jun 21, 2024
@jackkoenig jackkoenig added this to the 6.x milestone Jun 21, 2024
@jackkoenig jackkoenig marked this pull request as draft June 21, 2024 21:14
@jackkoenig
Copy link
Contributor Author

Converted to a draft to test on large designs internally as this may have unexpected effects.

@sequencer
Copy link
Member

Converted to a draft to test on large designs internally as this may have unexpected effects.

T1 passes. Waiting for SiFive's designs.

@jackkoenig jackkoenig marked this pull request as ready for review June 25, 2024 19:08
@jackkoenig
Copy link
Contributor Author

No longer draft, this is ready to go. The breakages I found were due to #4218.

@jackkoenig jackkoenig modified the milestones: 6.x, 7.0 Jun 25, 2024
@jackkoenig
Copy link
Contributor Author

I've switched this from 6.x to 7.0. This is a tough call but probably the right one.

The main reason to backport is that #4198 exposes a subtle, rare bug hit in the T1 (#4198 (comment)). It's also just generally a bug in our direction calculation behavior.

The reasons to not backport is that this is a change to a pretty core aspect of Chisel that easily could expose other bugs, whether in Chisel (which it does expose, see #4218), or in user code, especially if the user is using DataMirror to reflect on directions.

For that reason, I'm thinking we should save this for 7.0, even if it means that 6.5 may include rare breakages like the one experienced in T1. We can also try fixing that breakage in a much more focused way but I'm unsure how much effort we should put into it.

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.

}
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

Copy link
Member

@sequencer sequencer left a comment

Choose a reason for hiding this comment

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

It does resolve the issue.

Unspecified direction maps to Output while Flip maps to Input.
Previously, ActualDirection.fromSpecified would return Unspecified for
either Unspecified or Flipped input. This in turn resulted in Bundles
mixing Unspecified and Outputs as being "bidirectional" despite the fact
that they are actually unidirectional (or passive).

This change makes the direction calculation more consistent albeit at
the cost of marking the directions of unspecified Data as Output.
@sequencer
Copy link
Member

@jackkoenig @mwachs5 is there any plan to get this PR upstreamed?

sequencer added a commit to chipsalliance/t1 that referenced this pull request Jul 5, 2024
- circt is not released yet for DPI, use main branch instead.
- merge chipsalliance/chisel#4205 and chipsalliance/chisel#4205 in my
  chisel branch for T1.
sequencer added a commit to chipsalliance/t1 that referenced this pull request Jul 5, 2024
- circt is not released yet for DPI, use main branch instead.
- merge chipsalliance/chisel#4205 and chipsalliance/chisel#4205 in my
  chisel branch for T1.
sequencer added a commit to chipsalliance/t1 that referenced this pull request Jul 5, 2024
- circt is not released yet for DPI, use main branch instead.
- merge chipsalliance/chisel#4205 and chipsalliance/chisel#4205 in my
  chisel branch for T1.
Copy link
Member

@seldridge seldridge left a comment

Choose a reason for hiding this comment

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

LGTM

I was trying to think if there was a way that this could be done eagerly (a canonicalization) as opposed to via a function when you want it. However, I don't think that is possible given type cloning and whatever else could happen here.

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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix Fixes a bug, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect direction for mixed specified and unspecified directionality
4 participants