Building latest TVM on Windows


Although Windows is not the ideal platform for seriously running TVM, it is still handy for quick experiments. However, TVM currently does not build on Windows. I spent some time to make it work and hopefully this will help other people who face the same issue.

Errors when building current TVM

When building on Windows (using command cmake -G "Visual Studio 15 2017 Win64" for VS 2017 or cmake -G "Visual Studio 16 2019" for VS 2019), you will very likely to see the following errors:

tvm-master\include\tvm/relay/pass.h(456,14): error C2375:  'tvm::relay::Bind': redefinition; different linkage (compiling source file tvm-master\src\relay\backend\
tvm-master\include\tvm/relay/expr_functor.h(249): message :  see declaration of 'tvm::relay::Bind' (compiling source file tvm-master\src\relay\backend\
tvm-master\src\runtime\vm\,14): error C2065:  'uint': undeclared identifier
tvm-master\3rdparty\compiler-rt\builtin_fp16.h(76): error C2059: syntax error: '.' (compiling source file tvm-master\src\relay\pass\
tvm-master\src\relay\pass\pattern_util.h(210): note: see reference to function template instantiation 'DST_T __truncXfYf2__<float,uint32_t,23,uint16_t,uint16_t,10>(SRC_T)' being compiled
        ] (compiling source file tvm-master\src\relay\pass\
tvm-master\src\relay\pass\ note: see reference to function template instantiation 'tvm::relay::Constant tvm::relay::MakeConstantScalar<int>(tvm::relay::DataType,T)' being compiled


For the first two, you can simply add TVM_DLL to Bind in expr_functor.h and change uint to uint32_t.

However, the last error requires a newer compiler (this error is probably introduced by PR 3260). Therefore, VS2007 will not work - it simply throws syntax error even if you choose to use latest C++ standard.

VS 2009 appears to work - it prompts you to turn on std:c++latest. By turning on this option (in project property page), TVM does compile without any problem (after fixing the first two issues). However, you will very likely see weird runtime errors in the Python layer when you try to compile some schedules.

I tried to trace down these runtime errors and it appears to me that some exported packed funcs get messed up, e.g. calls to _Array end up with _raw_pointer. When compile an earlier version not requiring the latest C++ standard with std:c++latest, the same happens. I assume something went wrong in the compiler. I would very much like to know what is going on if someone finds it out.

The last option to make things work Windows is to use Clang. Visual Studio 2017 has a toolset v141_clang_c2. Unfortunately this does not work out of the box, plus this is Clang 3.8.0 (current release is 8.0.0).

VS 2019 comes with its own Clang (probably 7.0). This only works with CMake - first open the root CMakeLists.txt with VS 2019, then following these instructions to add x86-Clang-* config. When you do this, CMake configuration page appears and you will see all configurable variables (as in config.cmake). Repeat here whatever change you made in config.cmake then hit “build all” in build menu. TVM builds good and Clang allows discrepancy in DLL interface qualifier (a warning is displayed).

You can also install LLVM extension for VS 2019 to use a separate Clang. However, the building process is much slower than using VS 2019’s bundled Clang.

In short, install VS 2019, open CMakeLists.txt directly, add x86-Clang-Debug/x86-Clang-Release config, set CMake options properly, fix the uint and build (also set output dir if needed).