calyx icon indicating copy to clipboard operation
calyx copied to clipboard

`static-inference` doesn't kick in for `ref`-ed memories

Open ayakayorihiro opened this issue 7 months ago • 1 comments
trafficstars

For example, consider the tiny Calyx program, where main is a wrapper around the comp component:

component main(@go go: 1) -> (@done done: 1) {
  cells {
    @external(1) @data mem = seq_mem_d1(32, 1, 1);
    @data my_comp = comp();
  }
  wires { }
  control { invoke my_comp[my_mem = mem]()(); }
}
component comp(@go go: 1) -> (@done done: 1) {
  cells { ref my_mem = seq_mem_d1(32, 1, 1); }
  wires {
      group write {
      	my_mem.addr0 = 1'b0;
      	my_mem.write_data = 32'd42;
      	my_mem.write_en = 1'b1;
        my_mem.content_en = 1'b0;
      	write[done] = my_mem.done;
      }
  }
  control { write; }
}

The write group in comp should have the annotation <"promotable"=0> after the static-inference pass. However, it doesn't because the ports of the my_mem are converted into ports for the comp component, obfuscating the fact that a memory cell is being acted on here.

Specifically, the comp component looks like this immediately before static inference (compile-invoke is the previous pass, which generates the ports):

component comp<"state_share"=1>(@go go: 1, @clk clk: 1, @reset reset: 1, my_mem_read_data: 32, my_mem_done: 1) -> (@done done: 1, @data my_mem_addr0: 1, my_mem_content_en: 1, my_mem_write_en: 1, @data my_mem_write_data: 32) {
  cells {
  }
  wires {
    group write {
      my_mem_content_en = 1'd0;
      my_mem_write_en = 1'd1;
      my_mem_write_data = 32'd42;
      my_mem_addr0 = 1'd0;
      write[done] = my_mem_done;
    }
  }
  control {
    @NODE_ID(0) write;
  }
}

and static-inference makes no changes to the IR in this case. However, if we converted the program so that comp is the main component (and therefore there is no wrapper), static-inference will kick in and add the <"promotable"=0> annotation to write.

ayakayorihiro avatar Apr 25 '25 21:04 ayakayorihiro

Also, just swapping the ordering of compile-invoke and static-inference/static-promotion will patch this one toy program, but generate errors for others.

For example, when I ran the Calyx compiler on calyx/tests/correctness/new_fsm.futil after swapping the order in calyx/opt/src/default_passes.rs, I got the below error:

thread 'main' panicked at calyx/opt/src/analysis/promotion_analysis.rs:95:9:
Shouldn't Promote to Static if there is a Comb Group

ayakayorihiro avatar Apr 25 '25 21:04 ayakayorihiro