Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Windows support #403

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
429 changes: 263 additions & 166 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ configure_file("${PROJECT_SOURCE_DIR}/cmake/config.py.in"
option(CODON_GPU "build Codon GPU backend" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -pedantic -fvisibility-inlines-hidden -Wno-return-type-c-linkage -Wno-gnu-zero-variadic-macro-arguments -Wno-deprecated-declarations"
"${CMAKE_CXX_FLAGS} -Wno-return-type-c-linkage -Wno-gnu-zero-variadic-macro-arguments -Wno-deprecated-declarations"
)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-return-type")
Expand Down Expand Up @@ -49,7 +49,7 @@ set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
if(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path;@loader_path/../lib/codon")
set(STATIC_LIBCPP "")
else()
elseif(NOT WIN32)
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../lib/codon")
set(STATIC_LIBCPP "-static-libstdc++")
endif()
Expand Down Expand Up @@ -97,7 +97,7 @@ if(APPLE)
else()
target_link_libraries(
codonrt
PRIVATE -Wl,--whole-archive $<TARGET_FILE:zlibstatic> $<TARGET_FILE:gc>
PRIVATE $<TARGET_FILE:gc> -Wl,--whole-archive $<TARGET_FILE:zlibstatic>
$<TARGET_FILE:bz2> $<TARGET_FILE:liblzma> $<TARGET_FILE:re2>
-Wl,--no-whole-archive)
endif()
Expand Down Expand Up @@ -129,7 +129,8 @@ endif()

# Codon compiler library
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
separate_arguments(LLDEF NATIVE_COMMAND "${LLVM_DEFINITIONS}")
add_definitions(${LLDEF})
set(CODON_HPPFILES
codon/compiler/compiler.h
codon/compiler/debug_listener.h
Expand Down Expand Up @@ -370,6 +371,8 @@ llvm_map_components_to_libnames(
Passes)
if(APPLE)
target_link_libraries(codonc PRIVATE ${LLVM_LIBS} fmt dl codonrt)
elseif(WIN32)
target_link_libraries(codonc PRIVATE ${LLVM_LIBS} fmt codonrt)
else()
target_link_libraries(codonc PRIVATE ${STATIC_LIBCPP} ${LLVM_LIBS} fmt dl codonrt)
endif()
Expand Down
93 changes: 71 additions & 22 deletions cmake/deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
endif()
include(${CPM_DOWNLOAD_LOCATION})

# CPMAddPackage(
# NAME libunistd
# GITHUB_REPOSITORY "robinrowe/libunistd"
# VERSION master
# GIT_TAG ff791340466556f815e04a0280c31ad266a1d15d
# EXCLUDE_FROM_ALL YES
# OPTIONS "CMAKE_BUILD_TYPE Release")
# if(libunistd_ADDED)
# set_target_properties(libportable PROPERTIES CMAKE_CXX_STANDARD 17)
# set_target_properties(libsqlite PROPERTIES EXCLUDE_FROM_ALL ON)
# set_target_properties(libuuid PROPERTIES EXCLUDE_FROM_ALL ON)
# set_target_properties(libregex PROPERTIES EXCLUDE_FROM_ALL ON)
# set_target_properties(libxxhash PROPERTIES EXCLUDE_FROM_ALL ON)
# endif()

CPMAddPackage(
NAME peglib
GITHUB_REPOSITORY "exaloop/cpp-peglib"
Expand All @@ -32,11 +47,12 @@ CPMAddPackage(
CPMAddPackage(
NAME zlibng
GITHUB_REPOSITORY "zlib-ng/zlib-ng"
VERSION 2.0.5
GIT_TAG 2.0.5
VERSION 2.1.2
GIT_TAG 2.1.2
EXCLUDE_FROM_ALL YES
OPTIONS "HAVE_OFF64_T ON"
"ZLIB_COMPAT ON"
"WITH_GTEST OFF"
"ZLIB_ENABLE_TESTS OFF"
"CMAKE_POSITION_INDEPENDENT_CODE ON")
if(zlibng_ADDED)
Expand Down Expand Up @@ -75,26 +91,46 @@ if(bz2_ADDED)
POSITION_INDEPENDENT_CODE ON)
endif()

CPMAddPackage(
NAME bdwgc
GITHUB_REPOSITORY "ivmai/bdwgc"
VERSION 8.0.5
GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"BUILD_SHARED_LIBS OFF"
"enable_threads ON"
"enable_large_config ON"
"enable_thread_local_alloc ON"
"enable_handle_fork ON")
if (WIN32)
CPMAddPackage(
NAME bdwgc
GITHUB_REPOSITORY "ivmai/bdwgc"
VERSION 8.0.5
GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"BUILD_SHARED_LIBS OFF"
"enable_threads ON"
"enable_large_config ON"
"enable_thread_local_alloc ON"
"disable_handle_fork ON"
"enable_single_obj_compilation on")
else()
CPMAddPackage(
NAME bdwgc
GITHUB_REPOSITORY "ivmai/bdwgc"
VERSION 8.0.5
GIT_TAG d0ba209660ea8c663e06d9a68332ba5f42da54ba
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE ON"
"BUILD_SHARED_LIBS OFF"
"enable_threads ON"
"enable_large_config ON"
"enable_thread_local_alloc ON"
"enable_handle_fork ON")
endif()
if(bdwgc_ADDED)
set_target_properties(cord PROPERTIES EXCLUDE_FROM_ALL ON)
# if(WIN32 AND NOT EXISTS "${bdwgc_SOURCE_DIR}/libatomic_ops")
# file(COPY "${libatomic_ops_SOURCE_DIR}" DESTINATION "${bdwgc_SOURCE_DIR}/")
# file(RENAME "${bdwgc_SOURCE_DIR}/libatomic_ops-src" "${bdwgc_SOURCE_DIR}/libatomic_ops")
# endif()
endif()

CPMAddPackage(
NAME openmp
GITHUB_REPOSITORY "exaloop/openmp"
GIT_TAG 11daa2021c590dc74a0e734b4783570b619d88c9
GIT_TAG 376ec88480b9eeead8193a6bd4bb743efc6c5ea5
EXCLUDE_FROM_ALL YES
OPTIONS "CMAKE_BUILD_TYPE Release"
"OPENMP_ENABLE_LIBOMPTARGET OFF"
Expand All @@ -108,14 +144,10 @@ CPMAddPackage(
if(backtrace_ADDED)
set(backtrace_SOURCES
"${backtrace_SOURCE_DIR}/atomic.c"
"${backtrace_SOURCE_DIR}/backtrace.c"
"${backtrace_SOURCE_DIR}/dwarf.c"
"${backtrace_SOURCE_DIR}/fileline.c"
"${backtrace_SOURCE_DIR}/mmapio.c"
"${backtrace_SOURCE_DIR}/mmap.c"
"${backtrace_SOURCE_DIR}/posix.c"
"${backtrace_SOURCE_DIR}/print.c"
"${backtrace_SOURCE_DIR}/simple.c"
"${backtrace_SOURCE_DIR}/sort.c"
"${backtrace_SOURCE_DIR}/state.c")

Expand All @@ -129,10 +161,27 @@ if(backtrace_ADDED)
set(HAVE_SYNC_FUNCTIONS 1)
if(APPLE)
set(HAVE_MACH_O_DYLD_H 1)
list(APPEND backtrace_SOURCES "${backtrace_SOURCE_DIR}/macho.c")
list(APPEND backtrace_SOURCES
"${backtrace_SOURCE_DIR}/macho.c"
"${backtrace_SOURCE_DIR}/mmapio.c"
"${backtrace_SOURCE_DIR}/mmap.c"
"${backtrace_SOURCE_DIR}/simple.c"
"${backtrace_SOURCE_DIR}/backtrace.c")
elseif(WIN32)
set(HAVE_SYS_MMAN_H 0)
list(APPEND backtrace_SOURCES
"${backtrace_SOURCE_DIR}/pecoff.c"
"${backtrace_SOURCE_DIR}/alloc.c"
"${backtrace_SOURCE_DIR}/read.c"
"${backtrace_SOURCE_DIR}/nounwind.c")
else()
set(HAVE_MACH_O_DYLD_H 0)
list(APPEND backtrace_SOURCES "${backtrace_SOURCE_DIR}/elf.c")
list(APPEND backtrace_SOURCES
"${backtrace_SOURCE_DIR}/elf.c"
"${backtrace_SOURCE_DIR}/mmapio.c"
"${backtrace_SOURCE_DIR}/mmap.c"
"${backtrace_SOURCE_DIR}/simple.c"
"${backtrace_SOURCE_DIR}/backtrace.c")
endif()
# Generate backtrace-supported.h based on the above.
configure_file(
Expand All @@ -141,7 +190,7 @@ if(backtrace_ADDED)
configure_file(
${CMAKE_SOURCE_DIR}/cmake/backtrace-config.h.in
${backtrace_SOURCE_DIR}/config.h)
add_library(backtrace STATIC ${backtrace_SOURCES})
add_library(backtrace OBJECT ${backtrace_SOURCES})
target_include_directories(backtrace BEFORE PRIVATE "${backtrace_SOURCE_DIR}")
set_target_properties(backtrace PROPERTIES
COMPILE_FLAGS "-funwind-tables -D_GNU_SOURCE"
Expand Down
1 change: 1 addition & 0 deletions codon/cir/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
Expand Down
26 changes: 18 additions & 8 deletions codon/cir/llvm/llvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#include <cctype>
#include <cstdlib>
#include <fmt/args.h>
#include <sys/wait.h>
#include <unistd.h>
#include <utility>

#ifdef _WIN32
#else
#include <sys/wait.h>
#endif

#include "codon/cir/dsl/codegen.h"
#include "codon/cir/llvm/optimize.h"
#include "codon/cir/util/irtools.h"
Expand Down Expand Up @@ -80,22 +84,21 @@ LLVMVisitor::LLVMVisitor()
auto &registry = *llvm::PassRegistry::getPassRegistry();
llvm::initializeCore(registry);
llvm::initializeScalarOpts(registry);
llvm::initializeObjCARCOpts(registry);
llvm::initializeVectorization(registry);
llvm::initializeIPO(registry);
llvm::initializeAnalysis(registry);
llvm::initializeTransformUtils(registry);
llvm::initializeInstCombine(registry);
llvm::initializeAggressiveInstCombine(registry);
llvm::initializeInstrumentation(registry);
llvm::initializeTarget(registry);

llvm::initializeExpandLargeDivRemLegacyPassPass(registry);
llvm::initializeExpandLargeFpConvertLegacyPassPass(registry);
llvm::initializeExpandMemCmpPassPass(registry);
llvm::initializeScalarizeMaskedMemIntrinLegacyPassPass(registry);
llvm::initializeSelectOptimizePass(registry);
llvm::initializeCallBrPreparePass(registry);
llvm::initializeCodeGenPreparePass(registry);
llvm::initializeAtomicExpandPass(registry);
llvm::initializeRewriteSymbolsLegacyPassPass(registry);
llvm::initializeWinEHPreparePass(registry);
llvm::initializeDwarfEHPrepareLegacyPassPass(registry);
llvm::initializeSafeStackLegacyPassPass(registry);
Expand All @@ -110,8 +113,6 @@ LLVMVisitor::LLVMVisitor()
llvm::initializeExpandVectorPredicationPass(registry);
llvm::initializeWasmEHPreparePass(registry);
llvm::initializeWriteBitcodePassPass(registry);
llvm::initializeHardwareLoopsPass(registry);
llvm::initializeTypePromotionPass(registry);
llvm::initializeReplaceWithVeclibLegacyPass(registry);
llvm::initializeJMCInstrumenterPass(registry);
}
Expand Down Expand Up @@ -410,6 +411,9 @@ void executeCommand(const std::vector<std::string> &args) {
LOG_USER("Executing '{}'", fmt::join(cArgs, " "));
cArgs.push_back(nullptr);

#ifdef _WIN32
/// TODO WIN32
#else
if (fork() == 0) {
int status = execvp(cArgs[0], (char *const *)&cArgs[0]);
exit(status);
Expand All @@ -424,6 +428,7 @@ void executeCommand(const std::vector<std::string> &args) {
std::to_string(WEXITSTATUS(status)));
}
}
#endif
}
} // namespace

Expand Down Expand Up @@ -1243,16 +1248,21 @@ void LLVMVisitor::run(const std::vector<std::string> &args,
-> llvm::Expected<std::unique_ptr<llvm::orc::ObjectLayer>> {
auto L = std::make_unique<llvm::orc::ObjectLinkingLayer>(
es, llvm::cantFail(BoehmGCJITLinkMemoryManager::Create()));
/*
L->addPlugin(std::make_unique<llvm::orc::EHFrameRegistrationPlugin>(
es, llvm::cantFail(llvm::orc::EPCEHFrameRegistrar::Create(es))));
L->addPlugin(std::make_unique<llvm::orc::DebugObjectManagerPlugin>(
es, llvm::cantFail(llvm::orc::createJITLoaderGDBRegistrar(es))));
auto dbPlugin = std::make_unique<DebugPlugin>();
dbp = dbPlugin.get();
L->addPlugin(std::move(dbPlugin));
*/
return L;
});
builder.setJITTargetMachineBuilder(llvm::orc::JITTargetMachineBuilder(triple));
if (auto *orcRuntimeLibPath = std::getenv("CODON_ORC")) {
builder.setPlatformSetUp(llvm::orc::ExecutorNativePlatform(orcRuntimeLibPath));
}

auto jit = llvm::cantFail(builder.create());
jit->getMainJITDylib().addGenerator(
Expand Down Expand Up @@ -1855,7 +1865,7 @@ void LLVMVisitor::visit(const LLVMFunc *x) {
// set up debug info
// for now we just set all to func's source location
auto *srcInfo = getSrcInfo(x);
for (auto &block : func->getBasicBlockList()) {
for (auto &block : *func) {
for (auto &inst : block) {
if (!inst.getDebugLoc()) {
inst.setDebugLoc(llvm::DebugLoc(llvm::DILocation::get(
Expand Down
6 changes: 1 addition & 5 deletions codon/cir/llvm/llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/CaptureTracking.h"
Expand All @@ -26,7 +25,6 @@
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/ExecutionEngine/JITLink/JITLinkDylib.h"
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/JITLink/MemoryFlags.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
Expand Down Expand Up @@ -81,7 +79,6 @@
#include "llvm/LinkAllIR.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/Linker/Linker.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/Allocator.h"
Expand All @@ -92,7 +89,6 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Process.h"
Expand All @@ -105,11 +101,11 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/AlwaysInliner.h"
#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/Transforms/IPO/Internalize.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
#include "llvm/Transforms/IPO/StripSymbols.h"
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Expand Down
2 changes: 1 addition & 1 deletion codon/cir/llvm/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void applyDebugTransformations(llvm::Module *module, bool debug, bool jit) {
f.addFnAttr("no-frame-pointer-elim-non-leaf");
f.addFnAttr("no-jump-tables", "false");

for (auto &block : f.getBasicBlockList()) {
for (auto &block : f) {
for (auto &inst : block) {
if (auto *call = llvm::dyn_cast<llvm::CallInst>(&inst)) {
call->setTailCall(false);
Expand Down
6 changes: 4 additions & 2 deletions codon/compiler/debug_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ llvm::Error DebugPlugin::notifyFailed(llvm::orc::MaterializationResponsibility &
return llvm::Error::success();
}

llvm::Error DebugPlugin::notifyRemovingResources(llvm::orc::ResourceKey key) {
llvm::Error DebugPlugin::notifyRemovingResources(llvm::orc::JITDylib &jd,
llvm::orc::ResourceKey key) {
std::lock_guard<std::mutex> lock(pluginMutex);
registeredObjs.erase(key);
return llvm::Error::success();
}

void DebugPlugin::notifyTransferringResources(llvm::orc::ResourceKey dstKey,
void DebugPlugin::notifyTransferringResources(llvm::orc::JITDylib &jd,
llvm::orc::ResourceKey dstKey,
llvm::orc::ResourceKey srcKey) {
std::lock_guard<std::mutex> lock(pluginMutex);
auto it = registeredObjs.find(srcKey);
Expand Down
6 changes: 4 additions & 2 deletions codon/compiler/debug_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class DebugPlugin : public llvm::orc::ObjectLinkingLayer::Plugin {
llvm::MemoryBufferRef inputObject) override;
llvm::Error notifyEmitted(llvm::orc::MaterializationResponsibility &mr) override;
llvm::Error notifyFailed(llvm::orc::MaterializationResponsibility &mr) override;
llvm::Error notifyRemovingResources(llvm::orc::ResourceKey key) override;
void notifyTransferringResources(llvm::orc::ResourceKey dstKey,
llvm::Error notifyRemovingResources(llvm::orc::JITDylib &jd,
llvm::orc::ResourceKey key) override;
void notifyTransferringResources(llvm::orc::JITDylib &jd,
llvm::orc::ResourceKey dstKey,
llvm::orc::ResourceKey srcKey) override;
void modifyPassConfig(llvm::orc::MaterializationResponsibility &mr,
llvm::jitlink::LinkGraph &,
Expand Down
Loading