spatial-lang icon indicating copy to clipboard operation
spatial-lang copied to clipboard

Reduction with 0 loop iterations is not well defined

Open dkoeplin opened this issue 6 years ago • 5 comments

val reg = Reg[Int]
Reduce(reg)(0 by 1){i => ... }{ ... }

Case 1: The contents of reg after the loop is effectively unchanged. Is this the desired behavior?

val out = Reduce(0)(0 by 1){i => ... }{ ... }

Case 2: [Needs to be confirmed] The contents of out is still not well defined. Should be 0?

dkoeplin avatar Apr 17 '18 21:04 dkoeplin

I think this is the desired behavior. A loop that runs for 0 iters is a noop.

In codegen there is a ctr_trivial signal that is used to mask out any memory enable signals in that controller's subtree, so the register here should just hold its init value or whatever value was written in it already.

mattfel1 avatar Apr 17 '18 22:04 mattfel1

I could see the argument for the second case writing 0 to the output register though, since that's considered to be the zero value for the operation and you would probably expect a sum of an empty vector to be zero, for example, instead of undefined.

dkoeplin avatar Apr 17 '18 23:04 dkoeplin

In the second case, does that zero correspond only to the zero value of the reduce, or does it also set the init value of the Reg that is created? If the Reg takes the default zero value and not the value provided to the Reduce, then I agree we need to do a write. But if we can use it for the init value of the Reg as well then we don't need to do anything

mattfel1 avatar Apr 17 '18 23:04 mattfel1

It also sets the initial value of the accumulator. Does the accumulator get reset when the Reduce has 0 iterations?

dkoeplin avatar Apr 18 '18 00:04 dkoeplin

I don't think it gets reset but it's easy to make that the case if that's how we want it. I think I've seen this case in some apps and I put an explicit .reset to be sure.

mattfel1 avatar Apr 18 '18 16:04 mattfel1