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

Merged
merged 5 commits into from
Aug 1, 2024

Conversation

adkian-sifive
Copy link
Contributor

@adkian-sifive adkian-sifive commented Jun 27, 2024

Fixes #4223. Straighten out the reversing of bundle elements by removing sorting. Add reversals to fix ordering and fix tests

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

  • API Modification

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.

@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.

@adkian-sifive adkian-sifive added API Modification Internal Internal change, does not affect users, will be included in release notes and removed API Modification labels Jul 24, 2024
@jackkoenig jackkoenig added API Modification and removed Internal Internal change, does not affect users, will be included in release notes labels Jul 24, 2024
Add reverse to correct order
Comment on lines +516 to +525
"Bundle elements should be ordered in order of declaration" in {
val chirrtl = ChiselStage.emitCHIRRTL(new SimpleBundleElemOrder)
chirrtl should include("input in1 : { a : UInt<1>, b : UInt<8>}")
chirrtl should include("input in2 : { a : UInt<1>, b : UInt<8>}")

val chirrtl2 = ChiselStage.emitCHIRRTL(new SimpleBundleElemOrderByName)
chirrtl2 should include("input in0 : { a : UInt<8>, b : UInt<8>, c : UInt<8>}")
chirrtl2 should include("input in1 : { a : UInt<8>, b : UInt<8>, c : UInt<8>}")
}

Copy link
Contributor

Choose a reason for hiding this comment

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

These are good, but it would also be nice to do some assertElementsMatchExpected as that's the thing that changed (i.e. that you fixed) in this PR.

@jackkoenig jackkoenig added this to the 7.0 milestone Aug 1, 2024
@jackkoenig jackkoenig merged commit d80affa into main Aug 1, 2024
15 checks passed
@jackkoenig jackkoenig deleted the adkian-sifive/bundle-elements-ordering-fix branch August 1, 2024 20:34
SpriteOvO pushed a commit that referenced this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

asUInt packing inconsistent for Bundles with type parameters
3 participants