I am creating a new pass for restructuring multiplication with constants into shifts with addition/subtraction. $mul operations with constants will be implemented as shifts, which results in a smaller mapped area than implementing generic multipliers.
This will be useful in cases such as FIR filters, FFT with constants, MAC operations with constant weights etc.
Generally I think it’s probably helpful to have in the codebase. MCM is an NP-Hard problem and reduces to heuristic search, so I think it would be best if the implementation was as generic as possible over both it’s search algorithm and heuristic scoring measure.
I’ve been experimenting with DSP filters which have constant coefficients. And I have an idea to try with some designs that have constant weighted multipliers.
I agree with your idea about making it more generic. I will refactor it.
Even in the problem of a single constant multiplication Yosys main doesn’t seem to be doing anything. I expect your pass to help with that too, but also I’d like to be able to clearly verify from the code that it behaves nicely from a runtime perspective on designs without multiple constant multiplications done on a single value. Single constant multiplication optimization might or might not be best to rely on MCM code. Would be interested to hear more about the performance concerns of running your pass unconditionally if it’s heuristics applied to an NP-hard problem
Single constant multiplication optimization might or might not be best to rely on MCM code.
Agreed, SCM is only NP-Complete (https://doi.org/10.1109/TASSP.1984.1164433) and strictly easier than MCM. I would be interested in seeing the performance of the MCM implementation on an SCM problem vs a SAT solver.