Cross-compilation for Windows?

Dear community people,

I have no problems with TVM running natively on Linux (and it works great!), but now I’m forced to use TVM for delpoyment on Windows. It looks like I’m missing something in my appempt to cross-compile, and therefore kindly asking for a help please.

I’ve built llvm/clang both native and cross-compilation toolchains (x86_64-unknown-linux-gnu and x86_64-w64-mingw32) using the following guide: https://github.com/mstorsjo/llvm-mingw. I defined both ‘target’ and ‘target_host’ as ‘llvm -mtriple=x86_64-w64-mingw32’. But when it comes to

lib.export_library ('/tmp/deploy_lib.dll', cc.cross_compiler ("x86_64-w64-mingw32-clang++",output_format="dll"))

the compilation command

x86_64-w64-mingw-clang++ --shared -fPIC -o /tmp/deploy_lib.dll /tmp/tmpxxx/lib0.o

fails because the lib0.o object file is in Linux ELF format. This lib0.o file is created by llvm_module.cc ‘SaveToFile’ method which runs a llvm pass. What I can’t understand is how can I force it to create the object file in the PE format I need for cross-compiling for the exported dll?

I’d greatly appreciate any help.

Sincerely, Lev.

2 Likes

Solved: I was confused by LLVM’s triple vs target – ther’re not the same (though, sometimes they are :-)). For example:

$ clang -target x86_64-w64-mingw32 -v 2>&1 | grep Target

Target: x86_64-w64-windows-gnu

Therefore, the correct “triple” in my case should have been: x86_64-w64-windows-gnu (and not x86_64-w64-mingw32)

2 Likes

Could you give the steps of cross-compilation? Thanks very much!

I used an excelent LLVM MinGW recipe for building a LLVM/Clang/LLD based mingw-w64 toolchain for cross-compilation. It is available at GitHub - mstorsjo/llvm-mingw: An LLVM/Clang/LLD based mingw-w64 toolchain Having built it you can easily create your Windows dll under Linux as:

graph, lib, params = relay.build (mod, target = target, target_host = target_host, params = params)
cross_compiler_path = "~/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang++"
lib.export_library (path_lib, tvm.contrib.cross_compiler (cross_compiler_path, output_format = "dll"))

Hope this helps.

1 Like

Thank you so much for your reply, this is very useful~