[TIR] Negative or zero extent in for loop

I’m thinking of implementing loop peeling in schedule. Some loops that we’re working with contain conditional code which is true/false for all iterations except the last. Separating (peeling) the last iteration from the loop would simplify the code and allow better codegen for the remaining loop.

Peeling would reduce the loop extent by the number of peeled iterations, which leads to the question:

Question Should negative or zero extent be allowed in For statement in TIR? If allowed, such loops would simply execute zero times.

In the LLVM codegen we assume the iteration count is positive (i.e. we don’t insert a loop guard at the beginning). Other codegens may have similar assumptions. Either case should be fairly easy to modify.

Edit: Seems like positive loop extent is the standard. I can’t close this thread, but I think it’s pretty much settled.

1 Like