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

Provide ImplicitClock and ImplicitReset #3714

Merged
merged 7 commits into from
Jan 27, 2024

Conversation

jackkoenig
Copy link
Contributor

@jackkoenig jackkoenig commented Jan 6, 2024

This also deleted a long-deprecated protected method that didn't do anything.

  • TODO add an example to the mdoc

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)

Desired Merge Strategy

  • Squash

Release Notes

These traits implement the functionality formerly only implemented in Module such that they can now be used by RawModules. They also define new protected virtual methods implicitClock and implicitReset that can be overridden within Module to change what values are used as the implicit clock and implicit reset respectively.

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 jackkoenig added the Feature New feature, will be included in release notes label Jan 6, 2024
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.

Would the following work (not sure why i would want to override the clock and reset when there is already a .clock and .reset, that seems like an anti-pattern?)

class MyRawModule extends RawModule with OverrideClock {

  val myClock = IO(Input(Clock))
  val myReset = IO(Input(Reset))
  internalClock := myClock
  withClockAndReset(Module.clock, myReset) {
    ???
  }
}

In above example I'm trying to show a) it works for a RawModule and b) now even a RawModule has a Module.clock

@jackkoenig
Copy link
Contributor Author

I agree with making this apply to RawModule, that requires a bit more generalization but I think it's a good improvement to the codebase and API. It will make it possible for Module to not be "special" in that a user could create their own thing just like Module except with a different name for clock and reset, or including clock and reset but also with an automatic clock gate on the clock.

@jackkoenig jackkoenig force-pushed the jackkoenig/override-clock-and-reset branch from 8704b92 to 39c7297 Compare January 18, 2024 23:26
@jackkoenig jackkoenig changed the title Provide OverrideClock and OverrideReset Provide ImplicitClock and ImplicitReset Jan 18, 2024
@jackkoenig jackkoenig added this to the 6.x milestone Jan 18, 2024
@jackkoenig jackkoenig force-pushed the jackkoenig/override-clock-and-reset branch from d64f37b to 88212bf Compare January 18, 2024 23:57
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.

Go. For it.

core/src/main/scala/chisel3/Module.scala Show resolved Hide resolved
@@ -11,6 +11,7 @@ import chisel3.internal.Builder.Prefix
import scala.util.Try
import scala.annotation.implicitNotFound
import scala.collection.mutable
import chisel3.ChiselException
Copy link
Member

Choose a reason for hiding this comment

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

Is this used?

val out = IO(Output(UInt(8.W)))
override protected val implicitClock = (!foo).asClock

val r = Reg(UInt(8.W))
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do a test that withClock(Module.clock) {...} still gives me the expected implicitClock

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant that comment to apply for a Module as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you basically asking that Module.clock returns implicitClock? I should even be able to just assert that

/** Provides an implicit Reset for use _within_ the [[RawModule]]
*
* Be careful to define the Reset value before trying to use it.
* Due to Scala initialization order, the actual val defining the Reset must occur before any
Copy link
Contributor

@mwachs5 mwachs5 Jan 19, 2024

Choose a reason for hiding this comment

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

nit: You say "the actual val" but then below you use a def. I can certainly imagine someone doing override protected def implicitClock = InstantiateAClockGate(...) and being very sad by all the clock gates getting put down. Should we use a val in the example body, or explicilty say "val or def defining the reset" in the code above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point I'm trying to illustrate is that the actual reset object needs to be a value, a val, but implicitReset can be a def. I could simplify the example a little bit by glossing over this subtly, but I was trying to show this point. What do you think I should do here? I could make this example very simple and put the more subtle example in a cookbook example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could also just add more text explaining this fact.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've decided to keep the def but add a clarifying comment

They were private and [deprecated] protected methods used by an already
removed API for setting the clock and reset of a Module from the
outside.
Mixing in these traits enables overriding the implicit clock or reset
from within a Module.
Module's logic that creates the implicit clock and implicit reset within
the Module's body is now implemented in new traits that users are free
to mix in to RawModule. There are also now virtual methods implicitClock
and implicitReset that the user can override to change what clock or
reset is used.
@jackkoenig jackkoenig force-pushed the jackkoenig/override-clock-and-reset branch from 88212bf to 62d0be0 Compare January 27, 2024 00:38
@jackkoenig jackkoenig enabled auto-merge (squash) January 27, 2024 00:39
@jackkoenig jackkoenig merged commit abc96e2 into main Jan 27, 2024
14 checks passed
@jackkoenig jackkoenig deleted the jackkoenig/override-clock-and-reset branch January 27, 2024 17:39
@mergify mergify bot added the Backported This PR has been backported label Jan 27, 2024
mergify bot pushed a commit that referenced this pull request Jan 27, 2024
Module's logic that creates the implicit clock and implicit reset within
the Module's body is now implemented in new traits that users are free
to mix in to RawModule. There are also now virtual methods implicitClock
and implicitReset that the user can override to change what clock or
reset is used.

(cherry picked from commit abc96e2)
mergify bot added a commit that referenced this pull request Jan 27, 2024
Module's logic that creates the implicit clock and implicit reset within
the Module's body is now implemented in new traits that users are free
to mix in to RawModule. There are also now virtual methods implicitClock
and implicitReset that the user can override to change what clock or
reset is used.

(cherry picked from commit abc96e2)

Co-authored-by: Jack Koenig <koenig@sifive.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported This PR has been backported Feature New feature, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants