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 ChiselPhase, Stop writing files in ChiselStage$ methods, Expand ChiselStage$ helpers #1566

Merged
merged 7 commits into from
Aug 26, 2020

Conversation

seldridge
Copy link
Member

@seldridge seldridge commented Aug 25, 2020

Adds a new private[chisel3] phase called ChiselPhase which does the work of the one-off phase manager inside the existing ChiselStage. This is then used inside ChiselStage. ChiselStage companion object methods, elaborate and convert, are then switched to use ChsielStage so that they stop writing files.

This adds new APIs to the ChiselStage object that write no files:

  • emitChirrtl(gen: => RawModule): String
  • emitFirrtl(gen: => RawModule): String
  • emitVerilog(gen: => RawModule): String
  • emitSystemVerilog(gen: => RawModule): String

Tests are added, thanks to Java security manager magic, to assert that these methods do, in fact, write no files. Existing tests that use the ChiselStage class are modified to use the object if they are only checking a string output (and have no need to generate files).

Commentary

The new methods which run the FIRRTL compiler have to not use FirrtlStage (as that will write files), but this patch series shows how to use a PhaseManager to piece together a custom solution out of existing Phases. I think that this hints at an overall better API than the omnibus Stage which is opinionated in the fixed set of Phases that it wants to run before and after your code.

tl;dr: I think a flat structure is better than nesting of stages and it's worthwhile to think about how to refactor things to work this way.

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 state the API impact?
  • Did you specify the code generation impact?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • new feature/API

API Impact

None.

Backend Code Generation Impact

None.

Desired Merge Strategy

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

Release Notes

Add additional helper methods to ChiselStage object: emitChirrtl, emitFirrtl, emitVerilog, emitSystemVerilog. These new methods, as well as existing ChiselStage object methods, elaborate and convert, will no longer write files.

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels?
  • Did you mark the proper milestone (3.2.x, 3.3.x, 3.4.0, 3.5.0) ?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you mark as Please Merge?

@seldridge seldridge requested a review from a team as a code owner August 25, 2020 00:17
@seldridge seldridge requested review from albert-magyar and removed request for a team August 25, 2020 00:17
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.

Clean change, nice improvement!

@jackkoenig
Copy link
Contributor

This removes the ChiselStage.targets API

This is a backwards compatible change from 3.3.2, why are we removing it?

val targets: Seq[Dependency[Phase]] =
Seq( Dependency[chisel3.stage.phases.Checks],
Dependency[chisel3.stage.phases.AddImplicitOutputFile],
Dependency[chisel3.stage.phases.AddImplicitOutputAnnotationFile],
Dependency[chisel3.stage.phases.MaybeAspectPhase],
Dependency[chisel3.stage.phases.Convert],
Dependency[chisel3.stage.phases.MaybeFirrtlStage] )

final lazy val phaseManager = new PhaseManager(targets) {
override val wrappers = Seq( (a: Phase) => DeletedWrapper(a) )
}
final lazy val phaseManager = new ChiselPhase
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 remove this API without first deprecating. It is used by rocket-chip after all: https://github.com/chipsalliance/rocket-chip/blob/d71c5a674275676b9c870a20cb0a42cd039a4115/src/main/scala/system/RocketChipStageGenerator.scala#L15

I dunno how many other users there might be, but I'd say preserve it by keeping phaseManager the same and then implement targets as def targets: Seq[Dependency[Phase]] = (new ChiselPhase).targets, then it can still be overridden to affect phaseManager, feel free to deprecate it though, instead suggesting composition I assume?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Updated in the rebase. There's annoyingly no good Scala 2.11 compatible way to deprecate this. @deprecatedOverriding is what we want, but it's a 2.12 API addition. (And just using @deprecated produces no warnings if you override it.)

That said, I feel like there are some warts with the Stage API that can be better handled with a flat structure. So, I wouldn't sweat this and would rather put more thought into how to better express this flat structure (which would likely be a larger update to the Stage API.)

Signed-off-by: Schuyler Eldridge <[email protected]>
Switch from a one-off PhaseManager inside ChiselStage to actually
using the newly added ChiselPhase. This removes the targets
method (and API) from ChiselStage.

Signed-off-by: Schuyler Eldridge <[email protected]>
Change the ChiselStage companion object methods, elaborate and
convert, to not write files. Under the hood, these are switched from
using ChiselStage (which, like all phases, will write files) to using
ChiselPhase.

Signed-off-by: Schuyler Eldridge <[email protected]>
Modify existing ChiselStage object method tests to check that no files
are written.

Signed-off-by: Schuyler Eldridge <[email protected]>
This adds additional methods to the ChiselStage object for going
directly from a Chisel module to a string including: CHIRRTL, high
FIRRTL IR, Verilog, and SystemVerilog.

Differing from their ChiselStage class counterparts, these take no
arguments other than the module and write no files.

Signed-off-by: Schuyler Eldridge <[email protected]>
@seldridge seldridge changed the title Add ChiselPhase, Stop writing files in ChiselStage$ methods Add ChiselPhase, Stop writing files in ChiselStage$ methods, Expand ChiselStage$ helpers Aug 26, 2020
@seldridge seldridge requested a review from jackkoenig August 26, 2020 02:44
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.

LGTM! Thanks!

Signed-off-by: Schuyler Eldridge <[email protected]>
@seldridge
Copy link
Member Author

@jackkoenig: One minor update to this. I changed tests that were using the class methods to using the object methods to prevent unnecessary file writing. Still okay to merge?

@jackkoenig
Copy link
Contributor

Sorry for the delay, this looks great!

@jackkoenig jackkoenig merged commit b0389cc into master Aug 26, 2020
@jackkoenig jackkoenig deleted the driver-methods branch June 9, 2023 19:37
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.

2 participants