From 8ef6f9dd7bbb1611dd7166c4e14034e53df4a8b6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 31 Oct 2021 00:11:38 +0100 Subject: Compile `wasm64`; Add `lb_run_remove_unused_function_pass` --- src/llvm_backend_opt.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/llvm_backend_opt.cpp') diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index d5ea90aea..25e290d70 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -355,3 +355,52 @@ void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedure *p) { // are not removed lb_run_remove_dead_instruction_pass(p); } + + +void lb_run_remove_unused_function_pass(LLVMModuleRef mod) { + isize removal_count = 0; + isize pass_count = 0; + isize const max_pass_count = 10; + // Custom remove dead function pass + for (; pass_count < max_pass_count; pass_count++) { + bool was_dead_function = false; + for (LLVMValueRef func = LLVMGetFirstFunction(mod); + func != nullptr; + /**/ + ) { + LLVMValueRef curr_func = func; + func = LLVMGetNextFunction(func); + + LLVMUseRef first_use = LLVMGetFirstUse(curr_func); + if (first_use != nullptr) { + continue; + } + String name = {}; + name.text = cast(u8 *)LLVMGetValueName2(curr_func, cast(size_t *)&name.len); + + if (LLVMIsDeclaration(curr_func)) { + // Ignore for the time being + continue; + } + + + LLVMLinkage linkage = LLVMGetLinkage(curr_func); + + switch (linkage) { + case LLVMExternalLinkage: + case LLVMDLLImportLinkage: + case LLVMDLLExportLinkage: + default: + continue; + case LLVMInternalLinkage: + break; + } + LLVMDeleteFunction(curr_func); + was_dead_function = true; + removal_count += 1; + } + if (!was_dead_function) { + break; + } + } +} -- cgit v1.2.3 From 2a5b8f53fe7cb7a4261b765020f9342005046b63 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 31 Oct 2021 12:47:50 +0000 Subject: Add `memmove` and `memset` support for `wasm` --- core/runtime/procs.odin | 41 ++++++++++++++++++++++++++++++++++------- src/build_settings.cpp | 5 +++-- src/llvm_backend.cpp | 12 +++++++++--- src/llvm_backend_opt.cpp | 5 +++++ 4 files changed, 51 insertions(+), 12 deletions(-) (limited to 'src/llvm_backend_opt.cpp') diff --git a/core/runtime/procs.odin b/core/runtime/procs.odin index d97a3fadf..09ad25379 100644 --- a/core/runtime/procs.odin +++ b/core/runtime/procs.odin @@ -1,12 +1,39 @@ package runtime -memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr { - if ptr != nil && len != 0 { - b := byte(val) - p := ([^]byte)(ptr) - for i in 0.. rawptr { + if ptr != nil && len != 0 { + b := byte(val) + p := ([^]byte)(ptr) + for i in 0.. rawptr { + if dst != src { + d, s := ([^]byte)(dst), ([^]byte)(src) + d_end, s_end := d[len:], s[len:] + for i := len-1; i >= 0; i -= 1 { + d[i] = s[i] + } + } + return dst + + } +} else { + memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr { + if ptr != nil && len != 0 { + b := byte(val) + p := ([^]byte)(ptr) + for i in 0..metrics.arch == TargetArch_wasm64) { link_flags = gb_string_appendc(link_flags, "-mwas64 "); diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 27914efb2..d21ff8e5a 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -771,6 +771,8 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) Type *results = alloc_type_tuple(); Type *t_ptr_cstring = alloc_type_pointer(t_cstring); + + bool call_cleanup = true; bool has_args = false; bool is_dll_main = false; @@ -782,10 +784,12 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("hinstDLL"), t_rawptr, false, true); params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("fdwReason"), t_u32, false, true); params->Tuple.variables[2] = alloc_entity_param(nullptr, make_token_ident("lpReserved"), t_rawptr, false, true); + call_cleanup = false; } else if (build_context.metrics.os == TargetOs_windows && build_context.metrics.arch == TargetArch_386) { name = str_lit("mainCRTStartup"); } else if (build_context.metrics.os == TargetOs_wasi) { name = str_lit("_start"); + call_cleanup = false; } else { has_args = true; slice_init(¶ms->Tuple.variables, permanent_allocator(), 2); @@ -876,8 +880,10 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) } - lbValue cleanup_runtime_value = lb_find_runtime_value(m, str_lit("_cleanup_runtime")); - lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none, false); + if (call_cleanup) { + lbValue cleanup_runtime_value = lb_find_runtime_value(m, str_lit("_cleanup_runtime")); + lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none, false); + } if (is_dll_main) { @@ -890,7 +896,7 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) if (build_context.metrics.os == TargetOs_wasi) { - LLVMSetLinkage(p->value, LLVMDLLExportLinkage); + LLVMSetLinkage(p->value, LLVMDLLExportLinkage); } else { LLVMSetLinkage(p->value, LLVMExternalLinkage); } diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 25e290d70..8ddd3360d 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -383,6 +383,11 @@ void lb_run_remove_unused_function_pass(LLVMModuleRef mod) { continue; } + if (name == "memset" || + name == "memmove" || + name == "memcpy") { + continue; + } LLVMLinkage linkage = LLVMGetLinkage(curr_func); -- cgit v1.2.3 From 5f51337a01fa4a1e7a461604d564fa64601727cf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 31 Oct 2021 19:00:01 +0000 Subject: Add procs for wasm32 --- core/runtime/procs_wasm32.odin | 7 +++++++ src/checker.cpp | 3 +++ src/llvm_abi.cpp | 5 +++++ src/llvm_backend_opt.cpp | 6 ++++++ 4 files changed, 21 insertions(+) create mode 100644 core/runtime/procs_wasm32.odin (limited to 'src/llvm_backend_opt.cpp') diff --git a/core/runtime/procs_wasm32.odin b/core/runtime/procs_wasm32.odin new file mode 100644 index 000000000..dbc0dfcb7 --- /dev/null +++ b/core/runtime/procs_wasm32.odin @@ -0,0 +1,7 @@ +//+build wasm32 +package runtime + +@(link_name="__ashlti3") +__ashlti3 :: proc "c" (a: i64, b: i32) -> i64 { + return a +} \ No newline at end of file diff --git a/src/checker.cpp b/src/checker.cpp index 8db9e1bd6..f0f463816 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2011,6 +2011,9 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("gnu_h2f_ieee"), str_lit("gnu_f2h_ieee"), str_lit("extendhfsf2"), + + // WASM Specific + str_lit("__ashlti3"), }; for (isize i = 0; i < gb_count_of(required_runtime_entities); i++) { force_add_dependency_entity(c, c->info.runtime_package->scope, required_runtime_entities[i]); diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index ad8a45df7..9c7ced91e 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -1053,6 +1053,11 @@ namespace lbAbiWasm32 { } lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { + if (!is_return && type == LLVMIntTypeInContext(c, 128)) { + LLVMTypeRef cast_type = LLVMVectorType(LLVMInt64TypeInContext(c), 2); + return lb_arg_type_direct(type, cast_type, nullptr, nullptr); + } + if (!is_return && lb_sizeof(type) > 8) { return lb_arg_type_indirect(type, nullptr); } diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 8ddd3360d..75a377e5b 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -388,6 +388,12 @@ void lb_run_remove_unused_function_pass(LLVMModuleRef mod) { name == "memcpy") { continue; } + if (is_arch_wasm()) { + if (name == "__ashlti3") { + LLVMSetLinkage(curr_func, LLVMExternalLinkage); + continue; + } + } LLVMLinkage linkage = LLVMGetLinkage(curr_func); -- cgit v1.2.3