Skip to content

Commit

Permalink
[LLVM][WINDOWS] Recover windows support for the latest LLVM (#6698)
Browse files Browse the repository at this point in the history
Windows COFF requires comdat information to support weak-like linkage(via any).
This patch fixes the windows LLVM support after LLVM-8.
  • Loading branch information
tqchen authored Oct 16, 2020
1 parent e997185 commit 461e75b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 9 additions & 2 deletions apps/cpp_rpc/rpc_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ int mkdir(const char* path, int /* ignored */) { return _mkdir(path); }
#include <string>
#include <vector>

#include "../../src/runtime/file_utils.h"
#include "../../src/support/utils.h"
#include "rpc_env.h"

Expand Down Expand Up @@ -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<size_t>(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();
Expand Down
6 changes: 5 additions & 1 deletion apps/cpp_rpc/win32_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
*/
#ifndef TVM_APPS_CPP_RPC_WIN32_PROCESS_H_
#define TVM_APPS_CPP_RPC_WIN32_PROCESS_H_

#include <chrono>
#include <string>

#include "../../src/support/socket.h"

namespace tvm {
namespace runtime {
/*!
Expand All @@ -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_
#endif // TVM_APPS_CPP_RPC_WIN32_PROCESS_H_
15 changes: 15 additions & 0 deletions src/target/llvm/codegen_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 461e75b

Please sign in to comment.