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

Backend for listing instantiations of primitives #1860

Merged
merged 11 commits into from
Jan 23, 2024

Conversation

ayakayorihiro
Copy link
Contributor

@ayakayorihiro ayakayorihiro commented Jan 20, 2024

This PR contains a new backend that lists all of the unique instantiations of primitives within a program into a JSON file. This is necessary for my Calyx-FIRRTL efforts because FIRRTL does not support parameterization of modules, and therefore I have to generate individual FIRRTL modules that each correspond to a specific primitive instantiation in Calyx. This backend is the first step, and the produced JSON file will be processed by a script that I will write next. I'll also add fud2 support for this backend soon.
As a side note, this backend would also be similarly useful for the BTOR2-Cider efforts as described in #1845 under Generation Scaffolding.

This backend borrowed a lot of code from the Resource Estimation backend, which I'm very thankful for 🙂

Please let me know if there's any points I can improve here!! Also, I'm personally not a fan of the namings I used (primitive-inst, primtives.rs, etc) but didn't have any better ideas, so I'd really appreciate any suggestions on that front 🙂

Usage

You can run the backend by using the -b primitive-inst flag, and the optional -o <OUTPUT_FILE> flag to write the JSON to a file.

ex) Running with language-tutorial-mem:

cargo run -- examples/tutorial/language-tutorial-mem.futil -b primitive-inst -o p.json

we get in p.json...

[
  {
    "name": "std_mem_d1",
    "params": [
      {
        "param_name": "WIDTH",
        "param_value": 32
      },
      {
        "param_name": "SIZE",
        "param_value": 1
      },
      {
        "param_name": "IDX_SIZE",
        "param_value": 1
      }
    ]
  }
]

Copy link
Contributor

@sampsyo sampsyo left a comment

Choose a reason for hiding this comment

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

Heck yes!!! This seems perfect! Wahoo! Just have a couple of code-level suggestions within.

As for the name, maybe something like primitive-uses or primitives-used something would be nice and clear? We could even call the source file primitive_uses.rs.

calyx-backend/src/primitives.rs Outdated Show resolved Hide resolved
Comment on lines 80 to 87
let mut curr_params = Vec::new();
for (param_name, param_size) in param_binding.iter() {
let param = PrimitiveParam {
param_name: param_name.to_string(),
param_value: *param_size,
};
curr_params.push(param);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not that this matters very much, but this is probably possible to write in a functional way (and eliminate mut, in favor of collect). I think it would look something like this (although I haven't tested it):

let curr_params: Vec<_> = param_binding.iter().map(|param_name, param_size| {
  PrimitiveParam {
    // unchanged
  }
}).collect();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is super cool! Thanks for the suggestion :)

@sampsyo
Copy link
Contributor

sampsyo commented Jan 22, 2024

Wonderful; looks great! Merge at will, I say. 😃

Copy link
Collaborator

@EclecticGriffin EclecticGriffin 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, left a few minor notes that you can take or leave

Comment on lines 37 to 41
let main_comp = ctx
.components
.iter()
.find(|comp| comp.name == ctx.entrypoint)
.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a method on Context called entrypoint which does this exact thing.
It's functionally the same but might as well make use of it

Comment on lines 44 to 46

gen_primitive_set(ctx, main_comp, primitive_set);

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a more idiomatic way of writing it. Better when you might want to use
something as more than a mutable reference down the line

Suggested change
gen_primitive_set(ctx, main_comp, primitive_set);
let mut primitive_set: HashSet<PrimitiveUse> = HashSet::new();
gen_primitive_set(ctx, main_comp, &mut primitive_set);

@ayakayorihiro ayakayorihiro enabled auto-merge (squash) January 23, 2024 04:26
@ayakayorihiro ayakayorihiro merged commit 39a527d into main Jan 23, 2024
7 checks passed
@ayakayorihiro ayakayorihiro deleted the primitive-instantiations-backend branch January 23, 2024 04:36
rachitnigam pushed a commit that referenced this pull request Feb 16, 2024
* First pass for backend to produce json file containing primitive instantiation info

* pretty print the json

* Map from param names to values

* Fix clippy error

* usage message

* Fix doc comments

* Change JSON format for parameter names and values

* Fix formatting error

* Changed naming and fixed unchanged name

* Make parameter collection functional

* Make fixes from PR comments
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.

3 participants