diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-07-28 16:01:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-28 16:01:18 +0200 |
| commit | 674ebe395f1feba70becaac75c4fb9451f74f84c (patch) | |
| tree | 78589612bd458269904d53de46d0accf4f58a7af /src/llvm_backend_opt.cpp | |
| parent | 9746e25784c208cf8a7883bcb2e1ac2317117aeb (diff) | |
| parent | 96eecaab54cea02499e9cf418aa39eb5a9c9ec75 (diff) | |
Merge branch 'master' into master
Diffstat (limited to 'src/llvm_backend_opt.cpp')
| -rw-r--r-- | src/llvm_backend_opt.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 8f1c7ad59..6b80b21d6 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -56,7 +56,7 @@ LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data #endif void lb_basic_populate_function_pass_manager(LLVMPassManagerRef fpm, i32 optimization_level) { - if (optimization_level == 0 && build_context.ODIN_DEBUG) { + if (false && optimization_level == 0 && build_context.ODIN_DEBUG) { LLVMAddMergedLoadStoreMotionPass(fpm); } else { LLVMAddPromoteMemoryToRegisterPass(fpm); @@ -380,6 +380,43 @@ void llvm_delete_function(LLVMValueRef func) { LLVMDeleteFunction(func); } +void lb_append_to_compiler_used(lbModule *m, LLVMValueRef func) { + LLVMValueRef global = LLVMGetNamedGlobal(m->mod, "llvm.compiler.used"); + + LLVMValueRef *constants; + int operands = 1; + + if (global != NULL) { + GB_ASSERT(LLVMIsAGlobalVariable(global)); + LLVMValueRef initializer = LLVMGetInitializer(global); + + GB_ASSERT(LLVMIsAConstantArray(initializer)); + operands = LLVMGetNumOperands(initializer) + 1; + constants = gb_alloc_array(temporary_allocator(), LLVMValueRef, operands); + + for (int i = 0; i < operands - 1; i++) { + LLVMValueRef operand = LLVMGetOperand(initializer, i); + GB_ASSERT(LLVMIsAConstant(operand)); + constants[i] = operand; + } + + LLVMDeleteGlobal(global); + } else { + constants = gb_alloc_array(temporary_allocator(), LLVMValueRef, 1); + } + + LLVMTypeRef Int8PtrTy = LLVMPointerType(LLVMInt8TypeInContext(m->ctx), 0); + LLVMTypeRef ATy = LLVMArrayType(Int8PtrTy, operands); + + constants[operands - 1] = LLVMConstBitCast(func, Int8PtrTy); + LLVMValueRef initializer = LLVMConstArray(Int8PtrTy, constants, operands); + + global = LLVMAddGlobal(m->mod, ATy, "llvm.compiler.used"); + LLVMSetLinkage(global, LLVMAppendingLinkage); + LLVMSetSection(global, "llvm.metadata"); + LLVMSetInitializer(global, initializer); +} + void lb_run_remove_unused_function_pass(lbModule *m) { isize removal_count = 0; isize pass_count = 0; @@ -415,6 +452,7 @@ void lb_run_remove_unused_function_pass(lbModule *m) { Entity *e = *found; bool is_required = (e->flags & EntityFlag_Require) == EntityFlag_Require; if (is_required) { + lb_append_to_compiler_used(m, curr_func); continue; } } |