Fix Windows LTO: preserve required procedures with llvm.used

On Windows with LTO, required procedures with external linkage need to
be added to @llvm.used to survive linker-level dead code elimination.

LLVM may generate implicit calls to runtime builtins (e.g., __extendhfsf2
for f16 conversions) during instruction lowering, after the IR is
finalized. Without @llvm.used, the linker discards these procedures
before the implicit calls are generated.

This adds required procedures to @llvm.used at creation time. The fix
is Windows-specific; other platforms handle this correctly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jesse Meyer
2026-02-03 20:16:44 -05:00
parent a0562dfd6e
commit bd6148dd6b
2 changed files with 33 additions and 9 deletions

View File

@@ -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);