mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-30 18:24:00 +00:00
Merge pull request #6227 from JesseRMeyer/lto-support
Fix LTO on Windows
This commit is contained in:
@@ -331,7 +331,7 @@ try_cross_linking:;
|
||||
gbString lld_lto_flags = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(lld_lto_flags));
|
||||
if (build_context.lto_kind != LTO_None) {
|
||||
lld_lto_flags = gb_string_append_fmt(lld_lto_flags, "/lldltojobs:%d ", build_context.thread_count);
|
||||
lld_lto_flags = gb_string_append_fmt(lld_lto_flags, "/opt:lldltojobs=%d ", build_context.thread_count);
|
||||
}
|
||||
|
||||
switch (build_context.linker_choice) {
|
||||
|
||||
@@ -2454,14 +2454,21 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) {
|
||||
// tsan - Linux, Darwin
|
||||
// ubsan - Linux, Darwin, Windows (NOT SUPPORTED WITH LLVM C-API)
|
||||
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Address) {
|
||||
array_add(&passes, "asan");
|
||||
}
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
|
||||
array_add(&passes, "msan");
|
||||
}
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
|
||||
array_add(&passes, "tsan");
|
||||
// With LTO, sanitizer passes run at link time (via -fsanitize= linker flags)
|
||||
// where the linker has whole-program visibility. Running them here too would
|
||||
// double-instrument every module, producing "Redundant instrumentation" warnings.
|
||||
// Per-function sanitize attributes in the bitcode are preserved and respected
|
||||
// by the linker's sanitizer pass.
|
||||
if (build_context.lto_kind == LTO_None) {
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Address) {
|
||||
array_add(&passes, "asan");
|
||||
}
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
|
||||
array_add(&passes, "msan");
|
||||
}
|
||||
if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
|
||||
array_add(&passes, "tsan");
|
||||
}
|
||||
}
|
||||
|
||||
if (passes.count == 0) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -289,6 +289,19 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i
|
||||
|
||||
lb_set_linkage_from_entity_flags(p->module, p->value, entity->flags);
|
||||
|
||||
// With LTO on Windows, required procedures with external linkage need to be added to
|
||||
// llvm.used to survive linker-level dead code elimination. This is necessary because
|
||||
// LLVM may generate implicit calls to runtime builtins (e.g., __extendhfsf2 for f16
|
||||
// conversions) during instruction lowering, after the IR is finalized.
|
||||
if (build_context.lto_kind != LTO_None && build_context.metrics.os == TargetOs_windows) {
|
||||
if (entity->flags & EntityFlag_Require) {
|
||||
LLVMLinkage linkage = LLVMGetLinkage(p->value);
|
||||
if (linkage != LLVMInternalLinkage) {
|
||||
lb_append_to_used(m, p->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m->debug_builder) { // Debug Information
|
||||
Type *bt = base_type(p->type);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user