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 FileCheck for unit testing #3410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add FileCheck for unit testing #3410

wants to merge 1 commit into from

Conversation

jackkoenig
Copy link
Contributor

@jackkoenig jackkoenig commented Jul 6, 2023

Draft to see if the process works end-to-end

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

  • Internal or build-related (includes code refactoring/cleanup)

Desired Merge Strategy

  • Squash

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 jackkoenig added the Internal Internal change, does not affect users, will be included in release notes label Jul 6, 2023
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.

This looks amazing. The tests can really be bulletproof now.

Two thoughts:

  1. Is there any way to avoid needing to package FileCheck ourselves?
  2. This follows a pattern of FileCheck usage where the checks are all after the code. I tend to use this frequently. However, I will sometimes also write in-line tests. Is there a way that we could support this as well, e.g., something like:
val fileCheck = new FileCheck()

class Foo extends RawModule {
  fileCheck("CHECK-LABEL: module Foo")
  val a = IO(Input(Bool()))
  fileCheck("CHECK-NEXT: input a: UInt<1>")
}

fileCheck.check(ChiselStage.emitCHIRRTL(new Foo))

This can be handled in a follow-on.

.github/workflows/test.yml Outdated Show resolved Hide resolved
src/test/scala/chiselTests/ChiselSpec.scala Outdated Show resolved Hide resolved
|# Check for back-pressure (ready signal is driven in the opposite direction of bits + valid)
|CHECK: connect enq.ready, [[result]].ready
|CHECK: connect deq, [[result]]
|""".stripMargin)
Copy link
Member

Choose a reason for hiding this comment

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

This is sooooooooo much better. 💯

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 add something to the readme before this is merged with the link to primer/docs for those not familiar with file check syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mwachs5 I added instructions on how to install and a very brief description of FileCheck and where to look for more information. How much of a tutorial do you think we need to include?

@sequencer
Copy link
Member

Just wonder if possible to share the cases among firrtl, verilog.

@seldridge
Copy link
Member

Just wonder if possible to share the cases among firrtl, verilog.

FileCheck has support for checking multiple prefixes which is commonly used to share across separate invocations of some test run. This may work for sharing between firrtl and verilog, but is unlikely.

Comment on lines 220 to 231
/** Elaborate a Module to FIRRTL and check the FIRRTL with FileCheck */
def generateFirrtlAndFileCheck(t: => RawModule, fileCheckArgs: String*)(check: String): Unit = {
// Filecheck needs the thing to check in a file
os.write.over(checkFile.get, check)
val result = ChiselStage.emitCHIRRTL(t)
val extraArgs = os.Shellable(fileCheckArgs)
os.proc("FileCheck", checkFile.get, extraArgs).call(stdin = result)
}
Copy link
Member

Choose a reason for hiding this comment

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

For this and the other method, just a t: => String is probably more useful as I can see wanting to use this to check SystemVerilog emission. It isn't too onerous to have a user do fileCheck(ChiselStage.emitCHIRRTL(new Foo)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Capturing stdout and stderr is kind of annoying though, maybe I'll provide a 3rd method that just takes a String, and have the other 2 call it?

Copy link
Member

Choose a reason for hiding this comment

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

That works!

@jackkoenig jackkoenig enabled auto-merge (squash) September 29, 2023 22:52
Update SETUP.md including brief instructions on FileCheck.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internal Internal change, does not affect users, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants