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

Bundle elements ordering fix #4226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adkian-sifive
Copy link
Contributor

TODO: support backwards compatibility with old ordering, perhaps by new chiselOption

After this change, all Bundle elements will be ordered by the order of their declaration, including inheritance.

Fixes #4223. Straighten out the reversing of bundle elements by fixing the ordering during the compiler plugin. Remove runtime reversals and fix tests to reflect the new ordering.

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: The PR will be squashed and merged (choose this if you have no preference).

Release Notes

All Bundle elements will be ordered by the order of their declaration, including by inheritance.

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.

}

class Module1 extends Module {
val i = IO(Input(new Bundle1))
val m0 = Module(new Module0)
m0.i := i
m0.o := DontCare
assertElementsMatchExpected(i)("b" -> _.b, "a" -> _.a)
assertElementsMatchExpected(i)( "a" -> _.a, "b" -> _.b)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
assertElementsMatchExpected(i)( "a" -> _.a, "b" -> _.b)
assertElementsMatchExpected(i)("a" -> _.a, "b" -> _.b)

@jackkoenig
Copy link
Contributor

jackkoenig commented Jun 27, 2024

I want to clarify my statement that "The right order is the order of the fields in the definition of the Bundle". The order I'm talking about here is in the FIRRTL (and thus the Verilog). It is a well-known mild annoyance that the elements of a Bundle are stored in reverse order in the elements during elaboration. We should not change that, at least not lightly, as that is known and accounted for in lots of user code that iterates on elements of Bundles.

For fixing #4223 I think we should stick to just fixing the issue where object creation order sometimes violates field order.

Copy link
Contributor

@mwachs5 mwachs5 left a comment

Choose a reason for hiding this comment

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

make sure this cookbook is still valid whatever you do... my most bookmarked: https://www.chisel-lang.org/docs/cookbooks/cookbook#how-do-i-create-a-bundle-from-a-uint

)
}


Copy link
Contributor

Choose a reason for hiding this comment

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

seems like the cases we want to see are cross products of:
a. vals in the constructor args
b. vals in the body
c. vals in superclass/traits bodies

We should have a clear story on the order of these and the order within each of a, b, c and then show that as expected they go in order with a few test cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

before the PR, i think that b had expected behavior that we don't want to change in this PR except for weird interactions flagged by the bug

@@ -410,7 +410,7 @@ private[chisel3] object Converter {
fir.Field(getRef(elt, info).name, fir.Flip, extractType(elt, false, info, checkProbe, true, typeAliases))
}
if (!t._isOpaqueType)
fir.BundleType(t._elements.toIndexedSeq.reverse.map { case (_, e) => eltField(e) })
fir.BundleType(t._elements.toIndexedSeq.map { case (_, e) => eltField(e) })
Copy link
Contributor

Choose a reason for hiding this comment

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

you can't make this change here because it is affecting all Records, not just Bundles. So even if you undo the reversing for Bundles, this change is now messing with records so .asUint on a record would have different behavior.

YOu could show this by taking the cookbook here https://www.chisel-lang.org/docs/cookbooks/cookbook#how-do-i-create-a-bundle-from-a-uint and rewriting it with a record and convincing yourself it doesn't work the same after this change

@@ -44,27 +44,27 @@ class BpipDecoupled extends BpipOneField {
class HasDecoupledBundleByInheritance extends Module {
Copy link
Contributor

Choose a reason for hiding this comment

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

add more tests that mess with object creation order like have static objects, call-by-name, gens, etc

@mwachs5
Copy link
Contributor

mwachs5 commented Jun 28, 2024

it seems like part of the confusion here is that .asUInt is not actually documented in the firrtl spec in anyway how it is supposed to function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

asUInt packing inconsistent for Bundles with type parameters
3 participants