aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend_opt.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2026-02-04 08:59:23 +0000
committerGitHub <noreply@github.com>2026-02-04 08:59:23 +0000
commit61f3d45fa7cf3993a42ad122d450f5629d704720 (patch)
tree7b8783d43193c16e4ef393a175fede50a8fe52dd /src/llvm_backend_opt.cpp
parent270df36468df8f89e1ac944205272469142c7a65 (diff)
parentb8276065f9296754d1e76e25d6661b7b5567e3e1 (diff)
Merge pull request #6227 from JesseRMeyer/lto-support
Fix LTO on Windows
Diffstat (limited to 'src/llvm_backend_opt.cpp')
-rw-r--r--src/llvm_backend_opt.cpp29
1 files changed, 20 insertions, 9 deletions
diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp
index 4131f32bf..cb7fe1c75 100644
--- a/src/llvm_backend_opt.cpp
+++ b/src/llvm_backend_opt.cpp
@@ -512,8 +512,9 @@ gb_internal void llvm_delete_function(LLVMValueRef func) {
LLVMDeleteFunction(func);
}
-gb_internal void lb_append_to_compiler_used(lbModule *m, LLVMValueRef value) {
- LLVMValueRef global = LLVMGetNamedGlobal(m->mod, "llvm.compiler.used");
+// Helper to append a value to an llvm metadata array global (llvm.used or llvm.compiler.used)
+gb_internal void lb_append_to_llvm_used_list(lbModule *m, LLVMValueRef value, char const *list_name) {
+ LLVMValueRef global = LLVMGetNamedGlobal(m->mod, list_name);
LLVMValueRef *constants;
int operands = 1;
@@ -543,33 +544,43 @@ gb_internal void lb_append_to_compiler_used(lbModule *m, LLVMValueRef value) {
constants[operands - 1] = LLVMConstBitCast(value, Int8PtrTy);
LLVMValueRef initializer = LLVMConstArray(Int8PtrTy, constants, operands);
- global = LLVMAddGlobal(m->mod, ATy, "llvm.compiler.used");
+ global = LLVMAddGlobal(m->mod, ATy, list_name);
LLVMSetLinkage(global, LLVMAppendingLinkage);
LLVMSetSection(global, "llvm.metadata");
LLVMSetInitializer(global, initializer);
}
+gb_internal void lb_append_to_compiler_used(lbModule *m, LLVMValueRef value) {
+ lb_append_to_llvm_used_list(m, value, "llvm.compiler.used");
+}
+
+// llvm.used survives LTO linker optimizations (unlike llvm.compiler.used)
+gb_internal void lb_append_to_used(lbModule *m, LLVMValueRef value) {
+ lb_append_to_llvm_used_list(m, value, "llvm.used");
+}
+
gb_internal void lb_run_remove_unused_function_pass(lbModule *m) {
isize removal_count = 0;
isize pass_count = 0;
isize const max_pass_count = 10;
- // Custom remove dead function pass
+
+ // Custom remove dead function pass (for internal linkage functions)
for (; pass_count < max_pass_count; pass_count++) {
- bool was_dead = false;
+ bool was_dead = false;
for (LLVMValueRef func = LLVMGetFirstFunction(m->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;
@@ -578,7 +589,7 @@ gb_internal void lb_run_remove_unused_function_pass(lbModule *m) {
if (linkage != LLVMInternalLinkage) {
continue;
}
-
+
Entity **found = map_get(&m->procedure_values, curr_func);
if (found && *found) {
Entity *e = *found;
@@ -588,7 +599,7 @@ gb_internal void lb_run_remove_unused_function_pass(lbModule *m) {
continue;
}
}
-
+
llvm_delete_function(curr_func);
was_dead = true;
removal_count += 1;