calyxir / calyx

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

Queues: queues with lengths as powers of 2 #2028

Closed anshumanmohan closed 1 week ago

anshumanmohan commented 3 weeks ago

Trying out a silly thought re: our queues' max length.

At present, the FIFO is internally just a 1-d memory that I treat as a circular buffer. Two registers, read and write, mark which cell of the memory should next be read from or written to. The registers are incremented as needed, and, when they equal the maximum length of the queue, they are zeroed out to simulate a wraparound.

This PR requires the queue's maximum length to be a power of 2. We are now able to simply keep incrementing these registers forever, trusting actual overflow to do the zeroing-out for us. We save a couple of equality-checking cells and the wiring needed to run those checks and potentially zero the registers.

The PR also goes ahead and pipes this change to PIFOs, and therefore to PIFO trees. The user used to set a constant indicating a queue's maximum length. They now set a "queue-len-factor", and the actual maximum length is determined as $2^{\text{queue-len-factor}}$.

Please ignore the massive diffs in the .data and .expect files. The max queue length was originally 10, and I needed to re-run the oracles with the new queue length of 16 so that my change would be implementable.

Having set the length to 16, I compared both versions:

Sadly I didn't note register counts, or numbers for the other queues that build atop of FIFOs. One imagines magnifying gains.

The catch, of course, is that the max queue length is determined in a slightly roundabout way... someone who wants a max queue length of 64 can get exactly that, but someone who needs a max queue length of 70 will be forced to go get 128.

anshumanmohan commented 1 week ago

This whole idea is really quite silly and was never discussed in an issue beforehand, so I was halfway to deleting it. However, the conversation about FSMs at today's meeting helped me realize that other people on the team are also thinking about counters that count efficiently to arbitrary values of n (not just powers of two) and wrap around reliably.

@calebmkim, @parthsarkar17, @rachitnigam let me know if this sounds up your alley at all.