calyxir / calyx

Intermediate Language (IL) for Hardware Accelerator Generators
https://calyxir.org
MIT License
453 stars 45 forks source link

[Calyx-FIRRTL] Script for generating FIRRTL primitives #1863

Closed ayakayorihiro closed 4 months ago

ayakayorihiro commented 4 months ago

As discussed briefly in #1860 , FIRRTL modules are monomorphic; i.e. you cannot pass parameters into modules in the same fashion that you can pass parameters to Calyx primitives. Therefore, we need to generate specific FIRRTL modules that represent a Calyx primitive usage that we later instantiate in our code. The first step towards this was writing a backend (primitive-uses) that generates a JSON file that contains all of the primitive uses in a particular Calyx program (#1860).

This PR contains a Python script (generate-firrtl-primitives.py) that uses the primitive usage information (in the JSON file) to produce FIRRTL modules for all the primitives that the associated Calyx program needs. It uses templated FIRRTL primitive files (in tools/firrtl/templates) and m4 to fill in the templates.

Please let me know if there's anything in tools/firrtl/generate-firrtl-primitives.py that I can fix! (I currently have a small hack in the script that I'm not sure how to bypass...) Or if my approach of adding 20 template FIRRTL files is not ideal.

I will be testing (once I get a test harness working... still working on it) the FIRRTL primitive templates, so I expect at least some of them to need fixes later. I'm also currently working on E2E fud2 FIRRTL support that pipelines the whole process together.

Usage

First, use the primitive-uses backend to produce a JSON file. ex)

fud2 examples/tutorial/language-tutorial-mem.futil --to primitive-uses-json -o language-tutorial-mem-primitive-uses.json

Then, run the script. It takes in a single argument (the location of the previously generated JSON file) and prints out the FIRRTL primitives: ex (continuing from above))

python3  tools/firrtl/generate-firrtl-primitives.py 
ayakayorihiro commented 4 months ago

I'm going to close this PR since I later realized that I should link it with #1864 ! Sorry for the confusion.