Skip to content

Commit

Permalink
rename llvm_jit_vm to llvm_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 19, 2024
1 parent c7961f9 commit f849c9d
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 49 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ set(UBPF_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/vm/ubpf-vm/ubpf)
message(STATUS " Adding libraries to single static archive file")
bpftime_add_static_lib_component_command(bpftime_vm)
if(${BPFTIME_LLVM_JIT})
bpftime_add_static_lib_component_command(bpftime_llvm_jit_vm)
bpftime_add_static_lib_component_command(bpftime_llvm_vm)
else()
bpftime_add_static_lib_component_command(bpftime_ubpf_vm)
bpftime_add_libs_component_command(${UBPF_BUILD_DIR}/lib/libubpf.a)
Expand Down
4 changes: 2 additions & 2 deletions tools/aot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ target_include_directories(bpftime-aot-cli PRIVATE
../../runtime/include/
../../runtime/src/
${LIBBPF_INCLUDE_DIRS})
target_link_libraries(bpftime-aot-cli PRIVATE spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_jit_vm runtime ${LIBBPF_LIBRARIES} elf z)
target_link_libraries(bpftime-aot-cli PRIVATE spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_vm runtime ${LIBBPF_LIBRARIES} elf z)
set_property(TARGET bpftime-aot-cli PROPERTY CXX_STANDARD 20)

target_compile_definitions(bpftime-aot-cli PRIVATE _GNU_SOURCE)

add_dependencies(bpftime-aot-cli spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_jit_vm libbpf)
add_dependencies(bpftime-aot-cli spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_vm libbpf)

install(TARGETS bpftime-aot-cli CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
6 changes: 3 additions & 3 deletions tools/aot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static int build_ebpf_program(const std::string &ebpf_elf,
helper_group.add_helper_group_to_prog(&bpftime_prog);
bpftime_prog.bpftime_prog_load(true);
llvm_bpf_jit_context ctx(
dynamic_cast<bpftime::vm::llvm::bpftime_llvm_jit_vm *>(
dynamic_cast<bpftime::vm::llvm::bpftime_llvm_vm *>(
bpftime_prog.get_vm()->vm_instance.get()));
auto result = ctx.do_aot_compile(emit_llvm_ir);
auto out_path = output / (std::string(name) + ".o");
Expand Down Expand Up @@ -129,7 +129,7 @@ static int compile_ebpf_program(const std::filesystem::path &output)
new_prog.bpftime_prog_load(true);
llvm_bpf_jit_context ctx(
dynamic_cast<
bpftime::vm::llvm::bpftime_llvm_jit_vm *>(
bpftime::vm::llvm::bpftime_llvm_vm *>(
new_prog.get_vm()->vm_instance.get()));
auto result = ctx.do_aot_compile(emit_llvm_ir);
auto out_path = output /
Expand Down Expand Up @@ -230,7 +230,7 @@ static int run_ebpf_program(const std::filesystem::path &elf,
bpftime::bpftime_helper_group::get_shm_maps_helper_group()
.add_helper_group_to_prog(&bpftime_prog);
auto vm = bpftime_prog.get_vm();
auto &jit_ctx = *dynamic_cast<bpftime::vm::llvm::bpftime_llvm_jit_vm &>(
auto &jit_ctx = *dynamic_cast<bpftime::vm::llvm::bpftime_llvm_vm &>(
*vm->vm_instance)
.get_jit_context();
jit_ctx.load_aot_object(file_buf);
Expand Down
9 changes: 0 additions & 9 deletions vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ if(${BPFTIME_BUILD_STANDALONE_VM})
add_subdirectory(../third_party/argparse ${CMAKE_CURRENT_BINARY_DIR}/argparse)
endif()

if(${BPFTIME_LLVM_JIT})
message(STATUS "Using llvm-jit")
add_subdirectory(llvm-jit)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_llvm_jit_vm")
else()
message(STATUS "Not using llvm-jit")
add_subdirectory(ubpf-vm)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_ubpf_vm")
endif()
add_subdirectory(compat)
add_subdirectory(vm-core)

Expand Down
4 changes: 2 additions & 2 deletions vm/compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ target_include_directories(bpftime_vm_compat INTERFACE ${CMAKE_CURRENT_SOURCE_DI
if(${BPFTIME_LLVM_JIT})
message(STATUS "Using llvm-jit")
add_subdirectory(llvm-vm)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_llvm_jit_vm")
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_llvm_vm")
else()
message(STATUS "Using ubpf-jit")
add_subdirectory(ubpf-vm)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_ubpf_vm")
endif()
endif()
10 changes: 9 additions & 1 deletion vm/compat/llvm-vm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
add_subdirectory(../../llvm-jit)
add_library(bpftime_llvm_vm STATIC ./compat_llvm.cpp)

add_dependencies(bpftime_llvm_vm llvmbpf_vm spdlog::spdlog bpftime_vm_compat)

target_link_libraries(bpftime_llvm_vm PUBLIC bpftime_vm_compat spdlog::spdlog)

target_include_directories(bpftime_llvm_vm PRIVATE ../../llvm-jit/inc ${CMAKE_CURRENT_SOURCE_DIR} ${SPDLOG_INCLUDE})

add_subdirectory(../../llvm-jit ${CMAKE_CURRENT_BINARY_DIR}/llvm-jit)

2 changes: 2 additions & 0 deletions vm/compat/llvm-vm/compat_llvm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <compat_llvm.hpp>

21 changes: 21 additions & 0 deletions vm/compat/llvm-vm/compat_llvm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _BPFTIME_VM_COMPAT_LLVM_HPP
#define _BPFTIME_VM_COMPAT_LLVM_HPP
#include <bpftime_vm_compat.hpp>
#include <memory>
#include <optional>
#include <vector>
#include <ebpf_inst.h>
#include <llvmbpf.hpp>

namespace bpftime::vm::llvm
{

class bpftime_llvm_vm : public bpftime::vm::compat::bpftime_vm_impl,
public bpftime::llvmbpf_vm {
bpftime_llvm_vm() : bpftime::llvmbpf_vm()
{
}
};
} // namespace bpftime::vm::llvm

#endif
5 changes: 5 additions & 0 deletions vm/compat/ubpf-vm/compat_ubpf.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef BPFTIME_UBPF_COMPACT_H
#define BPFTIME_UBPF_COMPACT_H

#include <bpftime_vm_compat.hpp>
#include <map>
#include <string>
Expand Down Expand Up @@ -40,3 +43,5 @@ class bpftime_ubpf_vm : public compat::bpftime_vm_impl {
size_t next_helper_id = 1;
};
} // namespace bpftime::vm::ubpf

#endif // BPFTIME_UBPF_COMPACT_H
10 changes: 5 additions & 5 deletions vm/llvm-jit-old/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function(bpftime_add_library target)
bpftime_setup_target(${target})
endfunction()

bpftime_add_library(bpftime_llvm_jit_vm
bpftime_add_library(bpftime_llvm_vm
src/llvm/llvm_jit_context.cpp
src/llvm/compiler.cpp
src/llvm/compiler_utils.cpp
src/compat/compat_llvm.cpp
)

set_target_properties(bpftime_llvm_jit_vm PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../")
set_target_properties(bpftime_llvm_vm PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../")

find_package(LLVM REQUIRED CONFIG)

Expand Down Expand Up @@ -79,11 +79,11 @@ endif()
message(STATUS "LLVM_LIBS=${LLVM_LIBS}")
find_package(Boost REQUIRED)

target_link_libraries(bpftime_llvm_jit_vm PUBLIC ${LLVM_LIBS} bpftime_vm_compat PRIVATE spdlog::spdlog)
target_include_directories(bpftime_llvm_jit_vm
target_link_libraries(bpftime_llvm_vm PUBLIC ${LLVM_LIBS} bpftime_vm_compat PRIVATE spdlog::spdlog)
target_include_directories(bpftime_llvm_vm
PUBLIC ${LLVM_INCLUDE_DIRS} ${SPDLOG_INCLUDE} ${Boost_INCLUDE} ../include include #LLVM jit also used these headers
)
add_dependencies(bpftime_llvm_jit_vm bpftime_vm_compat spdlog::spdlog)
add_dependencies(bpftime_llvm_vm bpftime_vm_compat spdlog::spdlog)

if(BPFTIME_ENABLE_UNIT_TESTING)
message(STATUS "Build unit tests for the project. Tests should always be found in the test folder\n")
Expand Down
4 changes: 2 additions & 2 deletions vm/llvm-jit-old/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ target_include_directories(bpftime-vm-cli
${CMAKE_CURRENT_SOURCE_DIR}/../include
${LIBBPF_INCLUDE_DIRS}
)
target_link_libraries(bpftime-vm-cli PRIVATE spdlog::spdlog argparse bpftime_llvm_jit_vm ${LIBBPF_LIBRARIES} elf z)
target_link_libraries(bpftime-vm-cli PRIVATE spdlog::spdlog argparse bpftime_llvm_vm ${LIBBPF_LIBRARIES} elf z)
set_property(TARGET bpftime-vm-cli PROPERTY CXX_STANDARD 20)

target_compile_definitions(bpftime-vm-cli PRIVATE _GNU_SOURCE)

add_dependencies(bpftime-vm-cli spdlog::spdlog argparse bpftime_llvm_jit_vm libbpf)
add_dependencies(bpftime-vm-cli spdlog::spdlog argparse bpftime_llvm_vm libbpf)

install(TARGETS bpftime-vm-cli CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
2 changes: 1 addition & 1 deletion vm/llvm-jit-old/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static int build_ebpf_program(const std::string &ebpf_elf,
{
auto name = bpf_program__name(prog);
SPDLOG_INFO("Processing program {}", name);
bpftime::vm::llvm::bpftime_llvm_jit_vm vm;
bpftime::vm::llvm::bpftime_llvm_vm vm;

if (vm.load_code((const void *)bpf_program__insns(prog),
(uint32_t)bpf_program__insn_cnt(prog) * 8) <
Expand Down
4 changes: 2 additions & 2 deletions vm/llvm-jit-old/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ bpftime_add_executable(vm-llvm-example
./main.cpp
)

add_dependencies(vm-llvm-example bpftime_llvm_jit_vm spdlog::spdlog)
target_link_libraries(vm-llvm-example bpftime_llvm_jit_vm)
add_dependencies(vm-llvm-example bpftime_llvm_vm spdlog::spdlog)
target_link_libraries(vm-llvm-example bpftime_llvm_vm)

2 changes: 1 addition & 1 deletion vm/llvm-jit-old/example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, char *argv[])

cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT");

bpftime::vm::llvm::bpftime_llvm_jit_vm vm;
bpftime::vm::llvm::bpftime_llvm_vm vm;

res = vm.load_code(TEST_BPF_CODE, TEST_BPF_SIZE);
if (res) {
Expand Down
4 changes: 2 additions & 2 deletions vm/llvm-jit-old/include/compat_llvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ struct external_function {
void *fn;
};

class bpftime_llvm_jit_vm : public bpftime::vm::compat::bpftime_vm_impl {
class bpftime_llvm_vm : public bpftime::vm::compat::bpftime_vm_impl {
public:
bpftime_llvm_jit_vm();
bpftime_llvm_vm();
/* override */
std::string get_error_message() override;
int register_external_function(size_t index, const std::string &name,
Expand Down
22 changes: 11 additions & 11 deletions vm/llvm-jit-old/src/compat/compat_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ namespace bpftime::vm::compat

std::unique_ptr<bpftime_vm_impl> create_vm_instance()
{
return std::make_unique<llvm::bpftime_llvm_jit_vm>();
return std::make_unique<llvm::bpftime_llvm_vm>();
}

} // namespace bpftime::vm::compat

using namespace bpftime::vm::llvm;

bpftime_llvm_jit_vm::bpftime_llvm_jit_vm() : ext_funcs(MAX_EXT_FUNCS)
bpftime_llvm_vm::bpftime_llvm_vm() : ext_funcs(MAX_EXT_FUNCS)

{
this->jit_ctx = std::make_unique<llvm_bpf_jit_context>(this);
}

std::string bpftime_llvm_jit_vm::get_error_message()
std::string bpftime_llvm_vm::get_error_message()
{
return error_msg;
}

int bpftime_llvm_jit_vm::register_external_function(size_t index,
int bpftime_llvm_vm::register_external_function(size_t index,
const std::string &name,
void *fn)
{
Expand All @@ -46,7 +46,7 @@ int bpftime_llvm_jit_vm::register_external_function(size_t index,
return 0;
}

int bpftime_llvm_jit_vm::load_code(const void *code, size_t code_len)
int bpftime_llvm_vm::load_code(const void *code, size_t code_len)
{
if (code_len % 8 != 0) {
error_msg = "Code len must be a multiple of 8";
Expand All @@ -57,12 +57,12 @@ int bpftime_llvm_jit_vm::load_code(const void *code, size_t code_len)
return 0;
}

void bpftime_llvm_jit_vm::unload_code()
void bpftime_llvm_vm::unload_code()
{
instructions.clear();
}

int bpftime_llvm_jit_vm::exec(void *mem, size_t mem_len,
int bpftime_llvm_vm::exec(void *mem, size_t mem_len,
uint64_t &bpf_return_value)
{
if (jitted_function) {
Expand All @@ -87,14 +87,14 @@ int bpftime_llvm_jit_vm::exec(void *mem, size_t mem_len,
}

std::optional<bpftime::vm::compat::precompiled_ebpf_function>
bpftime_llvm_jit_vm::compile()
bpftime_llvm_vm::compile()
{
auto func = jit_ctx->compile();
jitted_function = func;
return func;
}

void bpftime_llvm_jit_vm::set_lddw_helpers(uint64_t (*map_by_fd)(uint32_t),
void bpftime_llvm_vm::set_lddw_helpers(uint64_t (*map_by_fd)(uint32_t),
uint64_t (*map_by_idx)(uint32_t),
uint64_t (*map_val)(uint64_t),
uint64_t (*var_addr)(uint32_t),
Expand All @@ -107,13 +107,13 @@ void bpftime_llvm_jit_vm::set_lddw_helpers(uint64_t (*map_by_fd)(uint32_t),
this->code_addr = code_addr;
}

std::vector<uint8_t> bpftime_llvm_jit_vm::do_aot_compile(bool print_ir)
std::vector<uint8_t> bpftime_llvm_vm::do_aot_compile(bool print_ir)
{
return this->jit_ctx->do_aot_compile(print_ir);
}

std::optional<bpftime::vm::compat::precompiled_ebpf_function>
bpftime_llvm_jit_vm::load_aot_object(const std::vector<uint8_t> &object)
bpftime_llvm_vm::load_aot_object(const std::vector<uint8_t> &object)
{
if (jitted_function) {
error_msg = "Already compiled";
Expand Down
2 changes: 1 addition & 1 deletion vm/llvm-jit-old/src/llvm/llvm_jit_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ extern "C" void __aeabi_unwind_cpp_pr1();
static int llvm_initialized = 0;

llvm_bpf_jit_context::llvm_bpf_jit_context(
class bpftime::vm::llvm::bpftime_llvm_jit_vm *vm)
class bpftime::vm::llvm::bpftime_llvm_vm *vm)
: vm(vm)
{
using namespace llvm;
Expand Down
6 changes: 3 additions & 3 deletions vm/llvm-jit-old/src/llvm/llvm_jit_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <bpftime_vm_compat.hpp>
namespace bpftime::vm::llvm
{
class bpftime_llvm_jit_vm;
class bpftime_llvm_vm;
}

const static char *LDDW_HELPER_MAP_BY_FD = "__lddw_helper_map_by_fd";
Expand All @@ -28,7 +28,7 @@ const static char *LDDW_HELPER_CODE_ADDR = "__lddw_helper_code_addr";
#define IS_ALIGNED(x, a) (((uintptr_t)(x) & ((a)-1)) == 0)

class llvm_bpf_jit_context {
class bpftime::vm::llvm::bpftime_llvm_jit_vm *vm;
class bpftime::vm::llvm::bpftime_llvm_vm *vm;
std::optional<std::unique_ptr<llvm::orc::LLJIT> > jit;
std::unique_ptr<pthread_spinlock_t> compiling;
llvm::Expected<llvm::orc::ThreadSafeModule>
Expand All @@ -46,7 +46,7 @@ class llvm_bpf_jit_context {

public:
void do_jit_compile();
llvm_bpf_jit_context(class bpftime::vm::llvm::bpftime_llvm_jit_vm *vm);
llvm_bpf_jit_context(class bpftime::vm::llvm::bpftime_llvm_vm *vm);
virtual ~llvm_bpf_jit_context();
bpftime::vm::compat::precompiled_ebpf_function compile();
bpftime::vm::compat::precompiled_ebpf_function get_entry_address();
Expand Down
4 changes: 2 additions & 2 deletions vm/llvm-jit-old/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(TEST_SOURCES
)
add_executable(llvm_jit_tests ${TEST_SOURCES})
set_property(TARGET llvm_jit_tests PROPERTY CXX_STANDARD 20)
add_dependencies(llvm_jit_tests bpftime_llvm_jit_vm libbpf_with_headers)
target_link_libraries(llvm_jit_tests PRIVATE bpftime_llvm_jit_vm Catch2::Catch2WithMain ${LIBBPF_LIBRARIES})
add_dependencies(llvm_jit_tests bpftime_llvm_vm libbpf_with_headers)
target_link_libraries(llvm_jit_tests PRIVATE bpftime_llvm_vm Catch2::Catch2WithMain ${LIBBPF_LIBRARIES})
target_include_directories(llvm_jit_tests PRIVATE ${Catch2_INCLUDE} ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../src ${LIBBPF_INCLUDE_DIRS} ${LIBBPF_INCLUDE_DIRS}/uapi)
add_test(NAME llvm_jit_tests COMMAND llvm_jit_tests)
2 changes: 1 addition & 1 deletion vm/llvm-jit-old/unit-test/llvm-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" uint64_t add_func(uint64_t a, uint64_t b, uint64_t, uint64_t,

TEST_CASE("Test aot compilation")
{
bpftime::vm::llvm::bpftime_llvm_jit_vm vm;
bpftime::vm::llvm::bpftime_llvm_vm vm;
REQUIRE(vm.register_external_function(1, "add", (void *)add_func) == 0);

struct bpf_insn insns[] = { BPF_MOV64_IMM(1, 123),
Expand Down

0 comments on commit f849c9d

Please sign in to comment.