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

$scopeinfo support #4084

Merged
merged 14 commits into from
Feb 12, 2024
Merged

$scopeinfo support #4084

merged 14 commits into from
Feb 12, 2024

Conversation

jix
Copy link
Member

@jix jix commented Dec 19, 2023

This PR adds $scopeinfo cells to preserve information about the hierarchy during flattening, in particular attributes including src.

This is still missing a kernel header containing utilities for easily querying $scopeinfo data.

log(" -noscopeinfo\n");
log(" Do not create '$scopeinfo' cells that preserve attributes of cells and\n");
log(" modules that were removed during flattening. With this option the\n");
log(" 'src' attributes is handled like it was before '$scopeinfo' was\n");
Copy link
Member

Choose a reason for hiding this comment

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

Nit: typos here, either "attribute is handled" or "attributes are handled", "separated"

log(" 'src' attributes is handled like it was before '$scopeinfo' was\n");
log(" introduced, i.e. multiple 'src' locations are combined separted by\n");
log(" whitespace. Without this option these 'src' locations can be found\n");
log(" via the 'cell_src' and 'module_src' attribute of '$scopeinfo' cells.\n");
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest not referring to how this was handled in the past, just explain source locations would be collected from different points on the hierarchy and combined without distinction on the cell post flattening.

Copy link
Member

Choose a reason for hiding this comment

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

(Also FYI strpool uses | not whitespace as separator).

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I must have been thinking of hdlname, I'll update the description.

kernel/rtlil.cc Show resolved Hide resolved
{
// The $scopeinfo's name will be changed below after removing the flattened cell
scopeinfo = module->addCell(NEW_ID, ID($scopeinfo));
scopeinfo->setParam(ID::TYPE, RTLIL::Const("cell"));
Copy link
Member

Choose a reason for hiding this comment

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

You have probably thought this out already and I am curious to hear it. Should this be cell or module?

Copy link
Member

Choose a reason for hiding this comment

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

(Also sorry for bikeshedding but the only other occurences of enums on cell parameters within Yosys I have seen were on some vendor cells, where the values were fully uppercase. Also e.g. in select syntax we would have the parameter name and its value next to each other. For reasons of consistence we could say the values for an enum-style parameter should be uppercase.)

Copy link
Member

Choose a reason for hiding this comment

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

You have probably thought this out already and I am curious to hear it. Should this be cell or module?

It refers to a module instantiation (a module was written by the designer at some point) so I would say "module" and not "cell".

(Also sorry for bikeshedding but the only other occurences of enums on cell parameters within Yosys I have seen were on some vendor cells, where the values were fully uppercase. Also e.g. in select syntax we would have the parameter name and its value next to each other. For reasons of consistence we could say the values for an enum-style parameter should be uppercase.)

I think it's consistent enough with lowercase. In Verilog and RTLIL both the module is spelled as module, and when extending this I think we might have other scopes such as begin or maybe struct. We aren't writing ALGOL to have BEGIN :)

Copy link
Member

Choose a reason for hiding this comment

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

I think it's consistent enough with lowercase. In Verilog and RTLIL both the module is spelled as module, and when extending this I think we might have other scopes such as begin or maybe struct. We aren't writing ALGOL to have BEGIN :)

No contest

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't decide between "cell" and "module" as which one is more appropriate depends on whether you want to indicate what caused the presence of the scope i.e. the entity that has the same hierarchical name as the $scopeinfo cell, in which case it would be "cell" (or "instance" if we're using verilog not RTLIL terminology). If we're indicating what defines the contents of the scope then "module" is clearly the correct choice.

Since both approaches make sense to me and changing it is trivial at this point, I went with an arbitrary choice.

Given the feedback so far, I'd change this to "module".

Copy link
Member

Choose a reason for hiding this comment

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

I am not opposed to instance, though I feel it might be a little too ambiguous.

Copy link
Member

@povik povik left a comment

Choose a reason for hiding this comment

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

Looks good to me

@nakengelhardt
Copy link
Member

Currently opt_merge considers all $scopeinfo cells identical and will remove all but one of them. Should we special-case $scopeinfo there too, or should we perhaps have a global ignorelist in yosys_celltypes that all optimization passes can check against so as to not touch those cells?

@whitequark
Copy link
Member

or should we perhaps have a global ignorelist in yosys_celltypes that all optimization passes can check against so as to not touch those cells?

I do want opt_merge to merge the $debugprobe cells (using a special treatment) so this would not benefit that effort.

@povik
Copy link
Member

povik commented Dec 20, 2023 via email

@jix
Copy link
Member Author

jix commented Dec 20, 2023

or should we perhaps have a global ignorelist in yosys_celltypes that all optimization passes can check against so as to not touch those cells?

I think that makes sense if we think that list will grow or can already start out containing other cells too. If it's only going to be $scopeinfo I'd say we add code specific for that and can move to an ignore list later when there are more cells that should be treated the same way.

@jix
Copy link
Member Author

jix commented Jan 11, 2024

I'm currently working on getting this PR ready (as wall as a followup PR with code to build a $scopeinfo aware hierarchical index for use in yosys passes) and I'm not sure how the various backend passes should treat $scopeinfo. In particular backend passes that are used to transfer synthesis results to other tools.

I'm assuming that the current state of this PR is likely to break existing synthesis setups with other tools potentially failing on unexpected $scopeinfo cells. I can also imagine the $scopeinfo data could eventually be of use for some of the tools that would currently fail due to its presence.

Since I use yosys almost exclusively for FV, I could use some input on how to handle this, though.

@whitequark
Copy link
Member

Personally, I would be OK with every backend except for CXXRTL and JSON ignoring $scopeinfo entirely, since it is a Yosys internal cell that will definitely break such other tools. I don't know, however, how to deal with people who do want e.g. $scopeinfo in their structural Verilog output.

Maybe we could handle that when we encounter it?

@jix
Copy link
Member Author

jix commented Jan 11, 2024

That also makes sense to me, but for JSON there is the issue that on one hand the JSON backend is intended to include all of the RTLIL netlist but also that nextpnr which reads that JSON output will fail with ERROR: Unable to place cell 'soc.cpu', no BELs remaining to implement cell type '$scopeinfo' with this PR.

A way to work around this would be to have the corresponding synth passes explicitly delete $scopeinfo cells when using the -json option until nextpnr at least knows to ignore those cells. This would still break synth_…; write_json output.json when used with nextpnr, but in that case you can manually insert a delete */t:$scopeinfo between synth_… and write_json and it seems like all the nextpnr documentation and examples use -json anyway.

Does that sound acceptable?

Alternatives would be to get nextpnr to ignore these cells before merging this PR or to ignore $scopeinfo by default also for the JSON backend.

@whitequark
Copy link
Member

Alternatives would be to get nextpnr to ignore these cells before merging this PR

I think we can do this.

@gatecat
Copy link
Member

gatecat commented Jan 11, 2024

I am happy to add this if we really want to avoid removing these cells in Yosys - however, despite the lack of any firm compatibility guarantees between Yosys and nextpnr, I do try hard to avoid situations like this (not least because they make bisecting more annoying) and it's been some time since we last had to introduce one.

@whitequark
Copy link
Member

I think we can just not emit the $scopeinfo cells anywhere but in CXXRTL and RTLIL then.

@povik
Copy link
Member

povik commented Jan 11, 2024

How about both deleting the $scopeinfo in the synth_ passes ahead of JSON export, and also teaching nextpnr to ignore those? That would address the potential incompatibility (at least for the usual way of invoking the flow), while keeping the JSON backend on par with RTLIL.

@povik
Copy link
Member

povik commented Jan 11, 2024

Now that I think of it, we want $scopeinfo as part of the JSON output for more informed logging on part of nextpnr! E.g. when explaining the critical path.

@povik
Copy link
Member

povik commented Jan 11, 2024

Well, at least in the future, when the synthesis flow will stop losing source attribution because of a roundtrip through ABC...

@whitequark
Copy link
Member

Now that I think of it, we want $scopeinfo as part of the JSON output for more informed logging on part of nextpnr! E.g. when explaining the critical path.

Yes, which is why I proposed to have it in JSON initially.

@whitequark
Copy link
Member

whitequark commented Jan 11, 2024

We will have the exact same problem with $probe cells, so the solution for $scopeinfo should be extensible to that cell at least.

Similarly with any $check cells that end up in a synthesis flow. (It might be meaningful to execute those in hardware--eventually!)

@povik
Copy link
Member

povik commented Jan 11, 2024

We will have the exact same problem with $probe cells, so the solution for $scopeinfo should be extensible to that cell at least.

Similarly with any $check cells that end up in a synthesis flow. (It might be meaningful to synthesize those--eventually!)

As I see it, $probe and $check can simply be stripped at some point (as would $print), this is brought up in #4122, but $scopeinfo we want to get to the P&R tool for logging.

@whitequark
Copy link
Member

Right, even if I do want $print in hardware I don't want nextpnr to have the logic for it.

@gatecat
Copy link
Member

gatecat commented Jan 11, 2024

Either way, I'll update nextpnr to ignore $scopeinfo because that is never going to hurt (and in the future, extend the frontend to populate nextpnr's own hierarchy structures with the info from it, although these haven't been touched for a while and might need some changes too).

@jix
Copy link
Member Author

jix commented Jan 11, 2024

I think $probe and $scopeinfo are different from $check in that they would start appearing without any changes in the input source, so failing when they are present breaks existing flows with existing inputs whereas when anything that doesn't already support $assert fails on $check that would not be a regression. (And all the supported existing FV use cases would automatically support $check when clk2fflogic and async2sync do.) For this PR I'm more concerned about regressions, and think improvements on how $assert or $check are handled can be done separately.

My current plan for $scopeinfo would be: I add checks to ignore it to all backends besides RTLIL, CXXRTL and JSON. For now I'd explicitly check for $scopeinfo, but it will be easy to extend it with $probe or to eventually move it to some better cell-classification infrastructure should that appear as part of #4122 or otherwise. IMO the important part for now is identifying all the places where such checks are needed. Changing the exact check used later on when required for new cells or to support new use cases is not a big deal, IMO.

I don't have any opinion, though, on whether to add something temporary and/or optional to the JSON backend and/or to the relevant synth passes to ignore $scopeinfo for now, to keep compatibility with older nextpnr versions. I also don't think I'm in the best position to make a decision on that, but I can implement whatever consensus there is as part of this PR. (But I'm not sure there is one at this point?)

@gatecat
Copy link
Member

gatecat commented Jan 11, 2024

My suggestion is:

  • in the short term, we strip $scopeinfo from the JSON
  • in a couple of months, once there's been a nextpnr release that's at least able to ignore it, and support in nextpnr that actually makes use of it, we can consider to start outputting it.

@whitequark
Copy link
Member

My suggestion is:

  • in the short term, we strip $scopeinfo from the JSON
  • in a couple of months, once there's been a nextpnr release that's at least able to ignore it, and support in nextpnr that actually makes use of it, we can consider to start outputting it.

SGTM

@jix jix marked this pull request as ready for review January 16, 2024 17:15
jix added 12 commits February 6, 2024 17:51
Only declares the cell interface, doesn't make anything use or
understand $scopeinfo yet.
While SBY's aiger flow already removes non-assertion driving logic,
there are some uses of write_aiger outside of SBY that could end up with
$scopeinfo cells, so we explicitly ignore them.

The write_btor backend works differently and due to the way it
recursively visits cells, it would never reach isolated cells like
$scopeinfo.
@whitequark
Copy link
Member

I've been looking into integrating this with CXXRTL. I've actually hit a small amount of trouble since I'm not entirely sure how to expose both module and cell attributes, but this is CXXRTL-side. I will probably be done with it by the end of next week, but I do not mind having this PR merged before then.

@mmicko mmicko merged commit edb95c6 into YosysHQ:master Feb 12, 2024
17 checks passed
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.

6 participants