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

[codegen] Emit literal identifiers for numeric ids #3374

Merged
merged 3 commits into from Jun 27, 2023

Conversation

seldridge
Copy link
Member

@seldridge seldridge commented Jun 21, 2023

Change the FIRRTL serializer to use literal identifiers if any identifiers begin with a leading digit. This is a FIRRTL 3.0.0 feature that simplifies parsing.

This enables literal identifier emission anywhere. However, this code path is only reachable via MixedVec due to mangling of numeric names to add a leading underscore in all other circumstances. In the future, this commit will enable removing this restriction and switching to literal identifiers when this happens.

No changes are made to annotation targets. E.g., the literal identifier, "wire 42" in circuit "Foo" and module "Bar" is still referred to by a local target "~Foo|Bar>42". This is intentional (for now). Targets don't have parsing ambiguity like FIRRTL text and the name of this wire is still "42" not "42". It follows that the storage of this name in FIRRTL IR is not changed. This is the same way that this is handled in CIRCT.

Release Notes

Use FIRRTL 3.0.0 emission of literal identifiers when a Chisel name begins with a digit.

Notes

This PR is stacked on #3188 (and should be merged after it).

Example

import chisel3._
import chisel3.util.MixedVec
import circt.stage.ChiselStage

class `9000` extends RawModule {
  val `0` = IO(Input(MixedVec(Bool())))
  val `1` = IO(Output(MixedVec(Bool())))

  val `2` = Wire(MixedVec(Bool()))
  dontTouch(`2`(0))
  `2`(0) := `0`(0)

  `1` := `2`
}

println(ChiselStage.emitCHIRRTL(new `9000`))
println(ChiselStage.emitSystemVerilog(new `9000`))

With this commit, this will produce the following FIRRTL and Verilog:

FIRRTL version 3.0.0
circuit iw :%[[
  {
    "class":"firrtl.transforms.DontTouchAnnotation",
    "target":"~iw|iw>_2"
  }
]]
  module iw :
    input _0 : { `0` : UInt<1>}
    output _1 : { `0` : UInt<1>}

    wire _2 : { `0` : UInt<1>}
    connect _2.`0`, _0.`0`
    connect _1, _2
module iw(
  input  _0_0,
  output _1_0
);

  wire _2_0 = _0_0;
  assign _1_0 = _2_0;
endmodule

With further changes that go beyond this PR< this should emit as:

FIRRTL version 3.0.0
circuit iw :%[[
  {
    "class":"firrtl.transforms.DontTouchAnnotation",
    "target":"~iw|iw>2"
  }
]]
  module iw :
    input `0` : { `0` : UInt<1>}
    output `1` : { `0` : UInt<1>}

    wire `2` : { `0` : UInt<1>}
    connect `2`.`0`, `0`.`0`
    connect `1`,`2`

@seldridge seldridge added the Backend Code Generation Affects backend code generation, will be included in release notes label Jun 21, 2023
@seldridge seldridge force-pushed the dev/seldridge/emit-literal-identifiers branch from d9de057 to 16dcbd7 Compare June 21, 2023 22:17
@seldridge seldridge force-pushed the dev/seldridge/use-simpler-firrtl-syntax branch from b0a8977 to 31e9a23 Compare June 21, 2023 22:17
@seldridge seldridge force-pushed the dev/seldridge/emit-literal-identifiers branch from cb19f5e to f8c7fae Compare June 21, 2023 22:24
@seldridge seldridge force-pushed the dev/seldridge/use-simpler-firrtl-syntax branch from 31e9a23 to c228de4 Compare June 21, 2023 22:24
@jackkoenig jackkoenig added this to the 6.0 milestone Jun 21, 2023
@seldridge seldridge force-pushed the dev/seldridge/emit-literal-identifiers branch from f8c7fae to 72f4f26 Compare June 22, 2023 19:37
Base automatically changed from dev/seldridge/use-simpler-firrtl-syntax to main June 27, 2023 00:06
@jackkoenig jackkoenig force-pushed the dev/seldridge/emit-literal-identifiers branch from 88f5517 to f791e45 Compare June 27, 2023 00:09
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.

Generally LGTM but question about verification statements

readwriters.foreach { r => b ++= "readwriter => "; b ++= r; newLineAndIndent(1) }
readers.foreach { r => b ++= "reader => "; b ++= legalize(r); newLineAndIndent(1) }
writers.foreach { w => b ++= "writer => "; b ++= legalize(w); newLineAndIndent(1) }
readwriters.foreach { r => b ++= "readwriter => "; b ++= legalize(r); newLineAndIndent(1) }
b ++= "read-under-write => "; b ++= readUnderWrite.toString
case Attach(info, exprs) =>
// exprs should never be empty since the attach statement takes *at least* two signals according to the spec
Copy link
Contributor

Choose a reason for hiding this comment

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

What about verification Statements? They have labels which are names in the sense that they can be annotated and they should correspond to labels in the Verilog.

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. I had considered this and thought it wasn't necessary. However, it is. I added a commit that makes these literal identifiers and adds checks that it works here: 9587c6c

Change the FIRRTL serializer to use literal identifiers if any identifiers
begin with a leading digit.  This is a FIRRTL 3.0.0 feature that
simplifies parsing.

This enables literal identifier emission anywhere.  However, this code
path is only reachable via MixedVec due to mangling of numeric names to
add a leading underscore in all other circumstances.  In the future, this
commit will enable removing this restriction and switching to literal
identifiers when this happens.

No changes are made to annotation targets.  E.g., the literal identifier,
"wire `42`" in circuit "Foo" and module "Bar" is still referred to by a
local target "~Foo|Bar>42".  This is intentional (for now).  Targets don't
have parsing ambiguity like FIRRTL text and the name of this wire is still
"42" not "`42`".  It follows that the storage of this name in FIRRTL IR is
not changed.  This is the same way that this is handled in CIRCT.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
@seldridge seldridge force-pushed the dev/seldridge/emit-literal-identifiers branch from f791e45 to 9587c6c Compare June 27, 2023 03:37
@seldridge seldridge merged commit 385feff into main Jun 27, 2023
13 checks passed
@seldridge seldridge deleted the dev/seldridge/emit-literal-identifiers branch June 27, 2023 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend Code Generation Affects backend code generation, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants