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

Add Map properties #3505

Merged
merged 6 commits into from
Aug 25, 2023
Merged

Add Map properties #3505

merged 6 commits into from
Aug 25, 2023

Conversation

albertchen-sifive
Copy link
Contributor

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

  • Feature (or new API)
  • API modification
    This PR changes the Property.apply from def apply[T: PropertyType](): Property[T] to def apply[T]()(implicit tpe: PropertyType[T]): Property[tpe.Type]. This lets implicit resolution resolve the result type, instead of having to add different apply methods for all the combinations of T and Property[T] alternatives - Seq[Property[T]]/Seq[T], Map[String, Property[T]]/Map[String, T], (Property[A], Property[B])/(A, B) etc.

Desired Merge Strategy

  • Squash: The PR will be squashed and merged (choose this if you have no preference).

Release Notes

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.5.x, 3.6.x, or 5.x depending on impact, API modification or big change: 6.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

I'm going to mark this as a Feature not-with-standing the binary incompatible change to Property.apply since these Property APIs have never been released (so from previous release 6.0.0-M2 this is just a new Feature).

@jackkoenig jackkoenig added the Feature New feature, will be included in release notes label Aug 24, 2023
value.map {
case (key, value) =>
key -> tpe.convert(value, ctx, info)
}.toSeq.sortBy(_._1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we actually want to be sorting here vs. just disallowing unordered maps? I think people generally want things to match the order they defined them rather than being sorted.

type Type = F[tpe.Type]
override def getPropertyType(value: Option[F[A]]): fir.PropertyType =
fir.MapPropertyType(implicitly[PropertyType[A]].getPropertyType(None))
type Underlying = Map[String, tpe.Underlying]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type Underlying = Map[String, tpe.Underlying]
type Underlying = VectorMap[String, tpe.Underlying]

We definitely should use an ordered map here.

Comment on lines 98 to 104
implicit def propertyTypeInstance[T](implicit pte: PropertyType[T]) = new PropertyType[Property[T]] {
type Type = T
override def getPropertyType(value: Option[Property[T]]): fir.PropertyType = pte.getPropertyType(None)
override def convert(value: Underlying, ctx: ir.Component, info: SourceInfo): fir.Expression =
ir.Converter.convert(value, ctx, info)
type Underlying = ir.Arg
override def convertUnderlying(value: Property[T]) = value.ref
Copy link
Contributor

@jackkoenig jackkoenig Aug 24, 2023

Choose a reason for hiding this comment

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

I get why this is here, but I think we should think about if this is the only way to accomplish the goal of making Property.apply work for the likes of both Property[Seq[BigInt]] and Seq[Property[BigInt]]. Is using overloading so bad since we only have the 2 collection types? I think we will just need 3 in that case.

What worries me about this is it makes it legal to write things like Property[Seq[Property[Property[Property[BigInt]]]]] and I'm not sure what that means.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should sandbag on this because overall this PR is a amazing and a huge step forward.
Please leave a comment explaining why this is here with maybe a TODO to see if there's either a way to remove the need for PropertyType[Property[T]] or at least disallow weird Property[Property[T]] or better yet disallow mutually recursive types like Property[Seq[Property[Seq[Property[T]]]]] (ideally disallow more than 1 Property[_] in the type if at all possible).

We can try to "fix" this later, let's definitely get this PR in.

}

implicit def mapPropertyTypeInstance[A, F[A] <: Map[String, A]](implicit tpe: PropertyType[A]) =
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
implicit def mapPropertyTypeInstance[A, F[A] <: Map[String, A]](implicit tpe: PropertyType[A]) =
implicit def mapPropertyTypeInstance[A, F[A] <: SeqMap[String, A]](implicit tpe: PropertyType[A]) =

Due to determinism issues, let's just not support Map and do SeqMap instead (using VectorMap as our concrete implementation)

Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

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

Please change from <: Map to <: SeqMap before merging, also take a look at removing the need (or otherwise mitigating) PropertyType[Property[T]], if it's too hard, go ahead and merge and we'll revisit that later.

Comment on lines 98 to 104
implicit def propertyTypeInstance[T](implicit pte: PropertyType[T]) = new PropertyType[Property[T]] {
type Type = T
override def getPropertyType(value: Option[Property[T]]): fir.PropertyType = pte.getPropertyType(None)
override def convert(value: Underlying, ctx: ir.Component, info: SourceInfo): fir.Expression =
ir.Converter.convert(value, ctx, info)
type Underlying = ir.Arg
override def convertUnderlying(value: Property[T]) = value.ref
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should sandbag on this because overall this PR is a amazing and a huge step forward.
Please leave a comment explaining why this is here with maybe a TODO to see if there's either a way to remove the need for PropertyType[Property[T]] or at least disallow weird Property[Property[T]] or better yet disallow mutually recursive types like Property[Seq[Property[Seq[Property[T]]]]] (ideally disallow more than 1 Property[_] in the type if at all possible).

We can try to "fix" this later, let's definitely get this PR in.

@@ -51,22 +51,63 @@ private[chisel3] trait PropertyType[T] {
def convertUnderlying(value: T): Underlying
}

private[chisel3] trait RecursivePropertyType[T] extends PropertyType[T]
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment describing what this is doing would be good.

Comment on lines 97 to 103
private[chisel3] trait LowPriorityPropertyTypeInstances {
implicit def sequencePropertyTypeInstance[A, F[A] <: Seq[A]](implicit tpe: RecursivePropertyType[A]) =
new SeqPropertyType[A, F, tpe.type](tpe) with RecursivePropertyType[F[A]]

implicit def mapPropertyTypeInstance[A, F[A] <: Map[String, A]](implicit tpe: RecursivePropertyType[A]) =
new MapPropertyType[A, F, tpe.type](tpe) with RecursivePropertyType[F[A]]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment describing why this is necessary would be helpful

@albertchen-sifive albertchen-sifive merged commit 5b823c8 into main Aug 25, 2023
@albertchen-sifive albertchen-sifive deleted the map-property branch August 25, 2023 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants