diff --git a/cmake/FindLLVM.cmake b/cmake/FindLLVM.cmake index 7fcbfb5a5..12fb65039 100644 --- a/cmake/FindLLVM.cmake +++ b/cmake/FindLLVM.cmake @@ -29,6 +29,10 @@ find_program(LLDB lldb PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) +find_program(LLVM_DIS llvm-dis + PATHS ${LLVM_TOOLS_BINARY_DIR} + NO_DEFAULT_PATH) + execute_process( COMMAND "${LLVM_TOOLS_BINARY_DIR}/llvm-config" "--libdir" OUTPUT_VARIABLE LLVM_LIBRARY_DIR @@ -41,3 +45,7 @@ endif() if(NOT LLC) message(FATAL_ERROR "Could not find an llc binary. Is llvm installed on your PATH?") endif() + +if(NOT LLVM_DIS) + message(FATAL_ERROR "Could not find an llvm-dis binary. Is llvm installed on your PATH?") +endif() diff --git a/include/kllvm/codegen/ApplyPasses.h b/include/kllvm/codegen/ApplyPasses.h index ece4d96d0..247e225af 100644 --- a/include/kllvm/codegen/ApplyPasses.h +++ b/include/kllvm/codegen/ApplyPasses.h @@ -6,6 +6,8 @@ namespace kllvm { +void do_bitcode_linking(llvm::Module &); + void apply_kllvm_opt_passes(llvm::Module &, bool hidden_visibility); void generate_object_file(llvm::Module &, llvm::raw_ostream &); diff --git a/lib/codegen/ApplyPasses.cpp b/lib/codegen/ApplyPasses.cpp index 5283c02ed..1fadc1109 100644 --- a/lib/codegen/ApplyPasses.cpp +++ b/lib/codegen/ApplyPasses.cpp @@ -2,6 +2,7 @@ #include #include +#include "alloc_cpp.h" #include "runtime/header.h" #if LLVM_VERSION_MAJOR >= 17 @@ -12,7 +13,10 @@ #include #endif +#include "llvm/IRReader/IRReader.h" +#include #include +#include #include #include #include @@ -145,4 +149,18 @@ void generate_object_file(llvm::Module &mod, llvm::raw_ostream &os) { pm.run(mod); } +void do_bitcode_linking(llvm::Module &mod) { + Linker linker(mod); + llvm::SMDiagnostic err; + auto alloc_cpp_mod = llvm::parseIR( + *llvm::MemoryBuffer::getMemBuffer( + std::string((char *)alloc_cpp_o_ll, alloc_cpp_o_ll_len)), + err, mod.getContext()); + bool error = linker.linkInModule(std::move(alloc_cpp_mod)); + if (error) { + throw std::runtime_error( + "Bitcode linking failed. Please report this as a bug."); + } +} + } // namespace kllvm diff --git a/lib/codegen/CMakeLists.txt b/lib/codegen/CMakeLists.txt index a70af8cad..d58de008c 100644 --- a/lib/codegen/CMakeLists.txt +++ b/lib/codegen/CMakeLists.txt @@ -15,3 +15,7 @@ add_library(Codegen target_link_libraries(Codegen PUBLIC AST fmt::fmt-header-only PRIVATE base64) + +target_include_directories(Codegen + PUBLIC ${CMAKE_BINARY_DIR}/runtime/lto +) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index df44dddb9..7cb635ccd 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -9,12 +9,13 @@ install( DESTINATION lib/kllvm/llvm/main ) -add_subdirectory(arithmetic) -add_subdirectory(util) -add_subdirectory(strings) -add_subdirectory(meta) add_subdirectory(alloc) +add_subdirectory(arithmetic) add_subdirectory(collect) -add_subdirectory(io) add_subdirectory(collections) +add_subdirectory(io) add_subdirectory(json) +add_subdirectory(lto) +add_subdirectory(meta) +add_subdirectory(strings) +add_subdirectory(util) diff --git a/runtime/alloc/CMakeLists.txt b/runtime/alloc/CMakeLists.txt index 3ae0b4cc1..beff00640 100644 --- a/runtime/alloc/CMakeLists.txt +++ b/runtime/alloc/CMakeLists.txt @@ -1,5 +1,4 @@ add_library(alloc STATIC - alloc.cpp arena.cpp register_gc_roots_enum.cpp ) diff --git a/runtime/lto/CMakeLists.txt b/runtime/lto/CMakeLists.txt new file mode 100644 index 000000000..61d7d2207 --- /dev/null +++ b/runtime/lto/CMakeLists.txt @@ -0,0 +1,24 @@ +add_library(lto STATIC + alloc.cpp +) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") + +add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/runtime/lto/alloc.cpp.o + COMMAND ${CMAKE_C_COMPILER_AR} x ${CMAKE_BINARY_DIR}/runtime/lto/liblto.a alloc.cpp.o + DEPENDS lto +) + +add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/runtime/lto/alloc.cpp.o.ll + COMMAND ${LLVM_DIS} ${CMAKE_BINARY_DIR}/runtime/lto/alloc.cpp.o + DEPENDS ${CMAKE_BINARY_DIR}/runtime/lto/alloc.cpp.o +) + +add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/runtime/lto/alloc_cpp.h + COMMAND xxd -i alloc.cpp.o.ll alloc_cpp.h + DEPENDS ${CMAKE_BINARY_DIR}/runtime/lto/alloc.cpp.o.ll + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/runtime/lto +) + +add_custom_target(skeleton ALL + DEPENDS "${CMAKE_BINARY_DIR}/runtime/lto/alloc_cpp.h") diff --git a/runtime/alloc/alloc.cpp b/runtime/lto/alloc.cpp similarity index 100% rename from runtime/alloc/alloc.cpp rename to runtime/lto/alloc.cpp diff --git a/tools/llvm-kompile-codegen/main.cpp b/tools/llvm-kompile-codegen/main.cpp index 99fabf3d4..9cc5c5b0c 100644 --- a/tools/llvm-kompile-codegen/main.cpp +++ b/tools/llvm-kompile-codegen/main.cpp @@ -236,6 +236,8 @@ int main(int argc, char **argv) { finalize_debug_info(); } + do_bitcode_linking(*mod); + if (!no_optimize) { apply_kllvm_opt_passes(*mod, hidden_visibility); }