Hi, I have a following IR with some control flow, which comes from translating a TorchScript RNN (a related discussion). My pytorch parser and the RNN test case is available here.
v0.0.4
def @main(%X: Tensor[(10, 10, 4), float32], %v26: Tensor[(4, 4), float32], %v25: Tensor[(4), float32]) -> Tensor[(10, 4), float32] {
%0 = full(0 /* ty=int32 */, shape=[10, 4], dtype="float32") /* ty=Tensor[(10, 4), float32] */;
%1 = full(0 /* ty=int32 */, shape=[10, 4], dtype="float32") /* ty=Tensor[(10, 4), float32] */;
%14 = (
let %while_loop: fn (int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) -> (int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) = fn (%i.1: int32, %y.5: Tensor[(10, 4), float32], %h.5: Tensor[(10, 4), float32]) -> (int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) {
%2 = less(%i.1, 10 /* ty=int32 */) /* ty=bool */;
if (%2) {
%3 = add(%i.1, 1 /* ty=int32 */) /* ty=int32 */;
%4 = take(%X, %i.1, axis=0) /* ty=Tensor[(10, 4), float32] */;
%5 = multiply(%4, 1f /* ty=float32 */) /* ty=Tensor[(10, 4), float32] */;
%6 = transpose(%v26, axes=[1, 0]) /* ty=Tensor[(4, 4), float32] */;
%7 = multiply(%6, 1f /* ty=float32 */) /* ty=Tensor[(4, 4), float32] */;
%8 = transpose(%7, axes=[1, 0]) /* ty=Tensor[(4, 4), float32] */;
%9 = nn.dense(%5, %8, units=4) /* ty=Tensor[(10, 4), float32] */;
%10 = nn.bias_add(%9, %v25) /* ty=Tensor[(10, 4), float32] */;
%11 = negative(%10) /* ty=Tensor[(10, 4), float32] */;
%12 = add(%11, %h.5) /* ty=Tensor[(10, 4), float32] */;
%13 = tanh(%12) /* ty=Tensor[(10, 4), float32] */;
%while_loop(%3, %13, %13) /* ty=(int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) */
} else {
(%i.1, %y.5, %h.5)
}
};
%while_loop
);
%15 = %14(0 /* ty=int32 */, %0, %1) /* ty=(int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) */;
%15.1
}
The IR looks reasonable to me, but it is rejected by the VM compiler:
File "/home/masa/projects/dev/tvm/src/relay/backend/vm/compiler.cc", line 643
TVMError: internal error: unreachable code,should be transformed away by previous passesfree_var %x: int32
free_var %x1: Tensor[(10, 4), float32]
free_var %X: Tensor[(10, 10, 4), float32]
free_var %v26: Tensor[(4, 4), float32]
free_var %v25: Tensor[(4), float32]
%0 = @lifted_name7090198356732118303(%X, %v26, %v25) /* ty=fn (int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) -> (int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) */;
%0(%x, %x1, %x1) /* ty=(int32, Tensor[(10, 4), float32], Tensor[(10, 4), float32]) */
I’m not completely sure what this means, but I guess it is something about accessing variables that are not bound in the while loop. But they are bound as arguments to the function, so I’d hope that there would be no problem compiling this IR.
Is this error expected? In that case I need to add all free vars to while_loop’s vars and rename appropriately, is that right?