Meet Check failed: checked_type_.defined() == false: internal error in TVM 0.8

This reproducible bug is triggered by running the code snippet below:

import tvm.relay as relay
import tvm.testing
import tvm.relay.transform
from tvm.tir.expr import *
import tvm
import pytest
from tvm.relay.dataflow_pattern import *
import tvm.relay.testing

target=tvm.target.Target('''llvm -device=arm_cpu -mtriple=aarch64-linux-gnu''')

def _get_mod(data_dtype, kernel_dtype):
    data_shape = (10, 3)
    kernel_shape = (20, 3)
    data = relay.var('data', shape=data_shape, dtype=data_dtype)
    kernel = relay.var('kernel', shape=kernel_shape, dtype=kernel_dtype)
    func = relay.qnn.op.dense(data, kernel, input_zero_point=relay.const(1, 'int32'), \
        kernel_zero_point=relay.const(1, 'int32'), \
            input_scale=relay.const(1, 'float32'), \
                kernel_scale=relay.const(1, 'float32'), units=kernel_shape[0], out_dtype='int32')
    mod = relay.Function(relay.analysis.free_vars(func), func)
    mod = tvm.IRModule.from_expr(mod)
    return mod
mod=_get_mod('''uint8''','''int8''')

with target:
    legalize=relay.qnn.transform.Legalize()
    # print('~~~' + str(A9qRA))
    res=legalize(mod)

During differential testing, I found it will crash if running with TVM-0.8 dev while nothing happened if running with TVM-0.7, which confused me a lot.

Below is crash message of TVM-0.8 dev

Traceback (most recent call last):
  File "/home/lisa/TVMSmith/buggyFile/2.py", line 30, in <module>
    res=legalize(mod)
  File "/home/lisa/tvm/python/tvm/ir/transform.py", line 127, in __call__
    return _ffi_transform_api.RunPass(self, mod)
  File "/home/lisa/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 237, in __call__
    raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
  [bt] (8) /home/lisa/tvm/build/libtvm.so(tvm::relay::ExprFunctor<tvm::RelayExpr (tvm::RelayExpr const&)>::InitVTable()::{lambda(tvm::runtime::ObjectRef const&, tvm::relay::ExprFunctor<tvm::RelayExpr (tvm::RelayExpr const&)>*)#5}::_FUN(tvm::runtime::ObjectRef const&, tvm::relay::ExprFunctor<tvm::RelayExpr (tvm::RelayExpr const&)>*)+0x2c) [0x7fbec62a039c]
  [bt] (7) /home/lisa/tvm/build/libtvm.so(tvm::relay::ExprMutator::VisitExpr_(tvm::relay::FunctionNode const*)+0x55f) [0x7fbec640b7ff]
  [bt] (6) /home/lisa/tvm/build/libtvm.so(tvm::relay::MixedModeMutator::VisitExpr(tvm::RelayExpr const&)+0x1b1) [0x7fbec640d211]
  [bt] (5) /home/lisa/tvm/build/libtvm.so(tvm::relay::MixedModeMutator::VisitLeaf(tvm::RelayExpr const&)+0x47) [0x7fbec640c447]
  [bt] (4) /home/lisa/tvm/build/libtvm.so(tvm::relay::PostOrderRewriter::DispatchVisitExpr(tvm::RelayExpr const&)+0xff) [0x7fbec6413d2f]
  [bt] (3) /home/lisa/tvm/build/libtvm.so(tvm::relay::ExprRewriter::InitVTable()::{lambda(tvm::runtime::ObjectRef const&, tvm::relay::ExprRewriter*, tvm::RelayExpr const&)#6}::_FUN(tvm::runtime::ObjectRef const&, tvm::relay::ExprRewriter*, tvm::RelayExpr const&)+0x2c) [0x7fbec615fbec]
  [bt] (2) /home/lisa/tvm/build/libtvm.so(tvm::relay::legalize::Legalizer::Rewrite_(tvm::relay::CallNode const*, tvm::RelayExpr const&)+0x7a0) [0x7fbec62abe60]
  [bt] (1) /home/lisa/tvm/build/libtvm.so(tvm::RelayExprNode::checked_type() const+0x157) [0x7fbec613f5e7]
  [bt] (0) /home/lisa/tvm/build/libtvm.so(+0x2702338) [0x7fbec613d338]
  File "/home/lisa/tvm/include/tvm/ir/expr.h", line 476
TVMError:
---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------
  Check failed: checked_type_.defined() == false: internal error: the type checker has not populated the checked_type field for Var(data, ty=TensorType([10, 3], uint8))

TVM v0.8 changed when type checking is done. You need to run the relay.transform.InferType pass before legalize.

That works for me. Thanks!