Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Let firrtl based applications run despite loading unknown annotations #2387

Merged
merged 14 commits into from
Nov 12, 2021

Conversation

chick
Copy link
Contributor

@chick chick commented Oct 13, 2021

An application like barstools may contain a main that loads an annotations file containing
annotation classes that are not on it's classpath. This change allows unknown annotations
to be preserved by wrapping them in a UnrecognizedAnnotation.
This feature can be enabled via an AllowUnrecognizedAnnotations

Addresses issue #2384

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • [NA] Did you update the FIRRTL spec to include every new feature/behavior?
  • 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 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 with a minor API addition

API Impact

This new feature is opt in. It adds an Annotation and a CLI flag. Allows Stage subClasses to load annotation files with unrecognized Annotations.
Error messages produced when unrecognized class occur and this feature is not enabled have changed slightly and show class names for all unrecognized annotations. These messages appear via Error level logging.

Backend Code Generation Impact

No change to backend code generation

Desired Merge Strategy

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

Release Notes

It is possible to disable errors caused by JSON annotation files containing unrecognized annotations. This situation
can occur when, for example applications like barstools with no code level dependency on rocket chip encounters
a rocket chip annotations in an annotation file. This change carries the annotations forward while ignoring their content.

Reviewer Checklist (only modified by reviewer)

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

An application like barstools may contain a main that loads an annotations file containing
annotation classes that are not on it's classpath. This change allows unknown annotations
to be preserved by wrapping them in a UnrecognizedAnnotation. If annotations are then output
to a file, they will be unwrapped during serialization
This feature can be enabled via an AllowUnrecognizedAnnotations annotation
@chick chick force-pushed the allow-unrecognized-annotations branch from d32b384 to 3cd4288 Compare October 14, 2021 17:38
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.

This is badly needed, just need to make sure it works with CLI

src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
chick and others added 7 commits November 1, 2021 13:25
- Added `UnrecogizedAnnotationsException` for internal communications of errors during annotation deserialization
- Made tracking of Unrecognized annotation types more robust, by extracting class with regex
- fixed AllowUnrecognizedAnnotations to stop it from eating following annotation
- change to use mutable package inline with ArrayBuffer
- removed intermediate result values used in debugging JsonProtocol
- added handler in `stage.view` annotation handler
- Added new tests to show behavior when run from command line
- text has been simplified when unrecognized errors occur
- finally got the flag passing down into the deserialization right in
  - JsonProtocol
  - GetIncludes
- Rearranged allowing unrecognized tests
- Adding checking for annotation having made it through to the output annotations
@@ -165,3 +166,5 @@ object Annotation
case class DeletedAnnotation(xFormName: String, anno: Annotation) extends NoTargetAnnotation {
override def serialize: String = s"""DELETED by $xFormName\n${anno.serialize}"""
}

Copy link
Contributor

Choose a reason for hiding this comment

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

can this have scaladoc added?

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 but shouldn't an unrelated tests like this go in a separate PR?

Copy link
Contributor

@jackkoenig jackkoenig Nov 11, 2021

Choose a reason for hiding this comment

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

@chick I think you may have responded to the wrong comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jackkoenig @mwachs5 I meant to say, adding scaladoc to pre-existing code that is unrelated to this PR should go on a separate PR. But I'm happy to add some. Any thoughts on what it should say, I feel like DeletedAnnotations are heading towards deprecation and using them, particularly looking inside them, is an anti-pattern

src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
src/main/scala/firrtl/annotations/JsonProtocol.scala Outdated Show resolved Hide resolved
@@ -564,7 +564,7 @@ class JsonAnnotationTests extends AnnotationTests {
val manager = setupManager(Some(anno))

the[Exception] thrownBy Driver.execute(manager) should matchPattern {
case InvalidAnnotationFileException(_, _: AnnotationClassNotFoundException) =>
case InvalidAnnotationFileException(_, _: UnrecogizedAnnotationsException) =>
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect, but am not certain, that this change will change the error you receive if you have an unserializable annotation (eg. an annotation that just wraps a random non-case class). Can you add a test here showing what happens in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the second to last test in AnnotationTests demonstrate this?

chick and others added 2 commits November 4, 2021 17:16
  - Fixed up white space explosion
  - UnrecognizedAnnotationSerializerSerializer => UnrecognizedAnnotationSerializer
  - canonicalized getAnnotationNameFromMappingException
  - var => val
- UnrecognizedAnnotationSpec
  - simplified sample annotation data, much smaller now
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.

A couple of minor requests but otherwise this looks good to :shipit:

chick and others added 2 commits November 12, 2021 10:43
  - Made GetClassPattern private
  - Switched to flatMap to compute `val loaded = ...`
@jackkoenig jackkoenig enabled auto-merge (squash) November 12, 2021 20:16
@jackkoenig jackkoenig added the Please Merge Accepted PRs that are ready to be merged. Useful when waiting on CI. label Nov 12, 2021
@jackkoenig jackkoenig merged commit c6093cb into master Nov 12, 2021
@jackkoenig jackkoenig deleted the allow-unrecognized-annotations branch November 12, 2021 20:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Addition (no impact on existing code) Please Merge Accepted PRs that are ready to be merged. Useful when waiting on CI.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants