From 1a7f78b66578492103607628d2cd4a2de70b29ec Mon Sep 17 00:00:00 2001 From: Jesse Meyer Date: Sun, 1 Feb 2026 09:04:53 -0500 Subject: Add ThinLTO support via -lto:thin and -lto:thin-files flags - Add -lto:thin and -lto:thin-files CLI flags with validation - Emit LLVM bitcode (.bc) instead of object files when LTO is enabled - Pass -flto=thin and -flto-jobs to clang/lld linkers - Guard linkage corrections to skip declarations without definitions (required for LTO where declarations appear across modules) - Allow module-per-file with LTO even at higher optimization levels Co-Authored-By: Claude Opus 4.5 --- src/build_settings.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/build_settings.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 6b8fefa59..e0017baea 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -420,6 +420,12 @@ struct BuildCacheData { }; +enum LTOKind : i32 { + LTO_None, + LTO_Thin, + LTO_Thin_Files, +}; + enum LinkerChoice : i32 { Linker_Invalid = -1, Linker_Default = 0, @@ -560,6 +566,7 @@ struct BuildContext { bool use_single_module; bool use_separate_modules; + LTOKind lto_kind; bool module_per_file; bool cached; BuildCacheData build_cache_data; @@ -2030,6 +2037,29 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta bc->use_separate_modules = false; } + if (bc->lto_kind == LTO_Thin || bc->lto_kind == LTO_Thin_Files) { +#if LLVM_VERSION_MAJOR < 17 + gb_printf_err("-lto:thin requires LLVM 17 or later\n"); + gb_exit(1); +#endif + if (bc->build_mode == BuildMode_Assembly || bc->build_mode == BuildMode_LLVM_IR) { + gb_printf_err("-lto:thin is incompatible with -build-mode:asm and -build-mode:llvm-ir\n"); + gb_exit(1); + } +#if defined(GB_SYSTEM_WINDOWS) + if (bc->linker_choice != Linker_lld) { + gb_printf_err("-lto:thin on Windows requires -linker:lld\n"); + gb_exit(1); + } +#endif + if (bc->use_single_module) { + gb_printf_err("Warning: -lto:thin overrides -use-single-module; separate modules will be used\n"); + } + bc->use_separate_modules = true; + if (bc->lto_kind == LTO_Thin_Files) { + bc->module_per_file = true; + } + } bc->ODIN_VALGRIND_SUPPORT = false; if (build_context.metrics.os != TargetOs_windows) { -- cgit v1.2.3