cucapra/dahlia

Question about using functions in unrolled loop

Closed this issue · 1 comments

Dahlia currently does not allow functions to be present inside loops with unrolls.

While I understand that certain kinds of functions can cause an affine violation. I want to understand why Dahlia prevents it for all functions?

Possible use-cases

Simple functions defined on Records

record complex {
    real: float;
    imag: float
}

def complex_mul(a: complex, b: complex): complex = {
    let cr: float = (a.real * b.real) - (a.imag * b.imag);
    let ci: float = (a.real * b.imag) + (a.imag * b.real);
    let c: complex = { real=cr; imag=ci };
    return c;
}

let a: complex[1024 bank 4];
let b: complex[1024 bank 4];

let c: complex[1024 bank 4];

for (let i=0..1024) unroll 4 {
  c[i] := complex_mul(a[i], a[i]);
}

Unrolling for imported functions

import vivado("math.h") {
  def cosf(v: float): float;
  def sinf(v: float): float;
}

let a: float[1024 bank 4];
let b: float[1024 bank 4];

for (let i=0..1024) unroll 4 {
  b[i] := sinf(a[i]);
}

In this case for the vivado backend during synthesis Vitis HLS generates multiple sincos units. Something similar for the Calyx backend?

Hm, interesting! Maybe the simple-to-enforce constraint here can be that if a function only has scalar inputs (like sinf), we allow it to be called in a unrolled context? The implementation should be straightforward.