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 Property expression for integer multiplication. #3844

Merged
merged 2 commits into from
Feb 22, 2024
Merged
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
14 changes: 14 additions & 0 deletions core/src/main/scala/chisel3/properties/Property.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,15 @@ sealed trait Property[T] extends Element { self =>
}
}

/** Perform addition as defined by FIRRTL spec section Integer Add Operation.
*/
final def +(that: Property[T])(implicit ev: PropertyArithmeticOps[Property[T]], sourceInfo: SourceInfo): Property[T] =
mikeurbach marked this conversation as resolved.
Show resolved Hide resolved
ev.add(this, that)

/** Perform multiplication as defined by FIRRTL spec section Integer Multiply Operation.
*/
final def *(that: Property[T])(implicit ev: PropertyArithmeticOps[Property[T]], sourceInfo: SourceInfo): Property[T] =
ev.mul(this, that)
}

private[chisel3] sealed trait ClassTypeProvider[A] {
Expand All @@ -343,6 +350,7 @@ private[chisel3] object ClassTypeProvider {
@implicitNotFound("arithmetic operations are not supported on Property type ${T}")
sealed trait PropertyArithmeticOps[T] {
def add(lhs: T, rhs: T)(implicit sourceInfo: SourceInfo): T
def mul(lhs: T, rhs: T)(implicit sourceInfo: SourceInfo): T
}

object PropertyArithmeticOps {
Expand All @@ -351,18 +359,24 @@ object PropertyArithmeticOps {
new PropertyArithmeticOps[Property[Int]] {
def add(lhs: Property[Int], rhs: Property[Int])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerAddOp, lhs, rhs)
def mul(lhs: Property[Int], rhs: Property[Int])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerMulOp, lhs, rhs)
}

implicit val longArithmeticOps: PropertyArithmeticOps[Property[Long]] =
new PropertyArithmeticOps[Property[Long]] {
def add(lhs: Property[Long], rhs: Property[Long])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerAddOp, lhs, rhs)
def mul(lhs: Property[Long], rhs: Property[Long])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerMulOp, lhs, rhs)
}

implicit val bigIntArithmeticOps: PropertyArithmeticOps[Property[BigInt]] =
new PropertyArithmeticOps[Property[BigInt]] {
def add(lhs: Property[BigInt], rhs: Property[BigInt])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerAddOp, lhs, rhs)
def mul(lhs: Property[BigInt], rhs: Property[BigInt])(implicit sourceInfo: SourceInfo) =
binOp(sourceInfo, fir.IntegerMulOp, lhs, rhs)
}

// Helper function to create Property expression IR.
Expand Down
7 changes: 4 additions & 3 deletions docs/src/explanations/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class IntegerArithmeticExample extends RawModule {
The following table lists the possible arithmetic operators that are supported
on integral `Property` typed values.

| Operation | Description |
| --------- | ----------- |
| `+` | Perform addition as defined by FIRRTL spec section 25.1.1. |
| Operation | Description |
| --------- | ----------- |
| `+` | Perform addition as defined by FIRRTL spec section Integer Add Operation |
| `*` | Perform multiplication as defined by FIRRTL spec section Integer Multiply Operation |
1 change: 1 addition & 0 deletions firrtl/src/main/scala/firrtl/ir/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ sealed abstract class PropPrimOp(name: String) {
override def toString: String = name
}
case object IntegerAddOp extends PropPrimOp("integer_add")
case object IntegerMulOp extends PropPrimOp("integer_mul")

/** Property expressions.
*
Expand Down
15 changes: 15 additions & 0 deletions src/test/scala/chiselTests/properties/PropertySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -738,4 +738,19 @@ class PropertySpec extends ChiselFlatSpec with MatchesAndOmits {
"propassign c, _c_propExpr"
)()
}

it should "support multiplication" in {
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val a = IO(Input(Property[BigInt]()))
val b = IO(Input(Property[BigInt]()))
val c = IO(Output(Property[BigInt]()))
c := a * b
})

matchesAndOmits(chirrtl)(
"wire _c_propExpr : Integer",
"propassign _c_propExpr, integer_mul(a, b)",
"propassign c, _c_propExpr"
)()
}
}
Loading