calyxir / calyx

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

Hoist redundant parallel actions #1661

Open anshumanmohan opened 9 months ago

anshumanmohan commented 9 months ago

When working on the new slew of queues (FIFO, PIFO, PIFO Tree), I found myself doing the following refactoring by hand:

par (
  ( 
    if_case_1: 
      action; 
      additional_action_for_cases_1_and_2 
  ), 
  ( 
    if_case_2: 
      action; 
      additional_action_for_cases_1_and_2 
  ), 
  ( 
    if_case_3: 
      action; 
  )
) # par ends 

turned into:

action;
if_case_1_or_2: additional_action_for_cases_1_and_2

And I think that's simpler.

Basically the action was the same in all the parallel branches, so I lifted it. Then the additional_action_for_cases_1_and_2 is run after that. These cannot themselves be run in parallel: the latter step depends on some state change triggered by the former.

Just making a quick issue in case Calyx would like to perform such hoisting automatically.

_Originally posted by @anshumanmohan in https://github.com/cucapra/calyx/pull/1657#discussion_r1294609470_

rachitnigam commented 9 months ago

Thanks for creating this issue! One can imagine a similar pass that hoists groups out of a while loops but that would be more complicated because you'd be implementing loop invariant code motion.

rachitnigam commented 5 months ago

An obvious but important property of this rewrite should be that action cannot affect the value of the conditions if_case_*. If it does, we cannot move it outside the if.