Skip to content

Commit

Permalink
make alloc.cpp inlineable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwight Guth committed Jul 29, 2024
1 parent fea9590 commit 8c46527
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 6 deletions.
8 changes: 8 additions & 0 deletions cmake/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
2 changes: 2 additions & 0 deletions include/kllvm/codegen/ApplyPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);
Expand Down
18 changes: 18 additions & 0 deletions lib/codegen/ApplyPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <kllvm/codegen/Options.h>
#include <kllvm/codegen/SetVisibilityHidden.h>

#include "alloc_cpp.h"
#include "runtime/header.h"

#if LLVM_VERSION_MAJOR >= 17
Expand All @@ -12,7 +13,10 @@
#include <llvm/Support/Host.h>
#endif

#include "llvm/IRReader/IRReader.h"
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Linker/Linker.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Pass.h>
#include <llvm/Support/CommandLine.h>
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
11 changes: 6 additions & 5 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion runtime/alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(alloc STATIC
alloc.cpp
arena.cpp
register_gc_roots_enum.cpp
)
Expand Down
24 changes: 24 additions & 0 deletions runtime/lto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
File renamed without changes.
2 changes: 2 additions & 0 deletions tools/llvm-kompile-codegen/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8c46527

Please sign in to comment.