From 461e75bd5ffaf45a0f270998514d444463d11261 Mon Sep 17 00:00:00 2001 From: Tianqi Chen Date: Fri, 16 Oct 2020 14:24:23 -0700 Subject: [PATCH] [LLVM][WINDOWS] Recover windows support for the latest LLVM (#6698) Windows COFF requires comdat information to support weak-like linkage(via any). This patch fixes the windows LLVM support after LLVM-8. --- CMakeLists.txt | 2 ++ apps/cpp_rpc/rpc_env.cc | 11 +++++++++-- apps/cpp_rpc/win32_process.h | 6 +++++- src/target/llvm/codegen_cpu.cc | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0043abb172ef..e7e02b2d2973 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,8 @@ if(MSVC) add_definitions(-D_SCL_SECURE_NO_WARNINGS) add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE) add_definitions(-DNOMINMAX) + # regeneration does not work well with msbuild custom rules. + set(CMAKE_SUPPRESS_REGENERATION ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj") diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index b76a1abbdadd..967274fd88a2 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -40,7 +40,6 @@ int mkdir(const char* path, int /* ignored */) { return _mkdir(path); } #include #include -#include "../../src/runtime/file_utils.h" #include "../../src/support/utils.h" #include "rpc_env.h" @@ -115,7 +114,15 @@ RPCEnv::RPCEnv() { std::string file_name = this->GetPath(args[0]); file_name = BuildSharedLibrary(file_name); std::string bin; - LoadBinaryFromFile(file_name, &bin); + + std::ifstream fs(file_name, std::ios::in | std::ios::binary); + CHECK(!fs.fail()) << "Cannot open " << file_name; + fs.seekg(0, std::ios::end); + size_t size = static_cast(fs.tellg()); + fs.seekg(0, std::ios::beg); + bin.resize(size); + fs.read(dmlc::BeginPtr(bin), size); + TVMByteArray binarr; binarr.data = bin.data(); binarr.size = bin.length(); diff --git a/apps/cpp_rpc/win32_process.h b/apps/cpp_rpc/win32_process.h index 621444e18764..0f784681f209 100644 --- a/apps/cpp_rpc/win32_process.h +++ b/apps/cpp_rpc/win32_process.h @@ -23,8 +23,12 @@ */ #ifndef TVM_APPS_CPP_RPC_WIN32_PROCESS_H_ #define TVM_APPS_CPP_RPC_WIN32_PROCESS_H_ + #include #include + +#include "../../src/support/socket.h" + namespace tvm { namespace runtime { /*! @@ -41,4 +45,4 @@ void SpawnRPCChild(SOCKET fd, std::chrono::seconds timeout); void ChildProcSocketHandler(const std::string& mmap_path); } // namespace runtime } // namespace tvm -#endif // TVM_APPS_CPP_RPC_WIN32_PROCESS_H_ \ No newline at end of file +#endif // TVM_APPS_CPP_RPC_WIN32_PROCESS_H_ diff --git a/src/target/llvm/codegen_cpu.cc b/src/target/llvm/codegen_cpu.cc index 53104542417e..d15c6151edc5 100644 --- a/src/target/llvm/codegen_cpu.cc +++ b/src/target/llvm/codegen_cpu.cc @@ -233,6 +233,14 @@ void CodeGenCPU::AddMainFunction(const std::string& entry_func_name) { #else global->setAlignment(1); #endif + // comdat is needed for windows select any linking to work + // set comdat to Any(weak linking) + if (target_machine_->getTargetTriple().isOSWindows()) { + llvm::Comdat* comdat = module_->getOrInsertComdat(runtime::symbol::tvm_module_main); + comdat->setSelectionKind(llvm::Comdat::Any); + global->setComdat(comdat); + } + global->setInitializer(llvm::ConstantDataArray::getString(*ctx_, entry_func_name)); global->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); } @@ -358,6 +366,13 @@ llvm::GlobalVariable* CodeGenCPU::InitContextPtr(llvm::Type* p_type, std::string #endif gv->setInitializer(llvm::Constant::getNullValue(p_type)); gv->setDLLStorageClass(llvm::GlobalValue::DLLStorageClassTypes::DLLExportStorageClass); + // comdat is needed for windows select any linking to work + // set comdat to Any(weak linking) + if (target_machine_->getTargetTriple().isOSWindows()) { + llvm::Comdat* comdat = module_->getOrInsertComdat(name); + comdat->setSelectionKind(llvm::Comdat::Any); + gv->setComdat(comdat); + } return gv; }