From 18427adbf21909f66a307a54ba4c47fd91e18fba Mon Sep 17 00:00:00 2001 From: Elliot <35050275+apache-hb@users.noreply.github.com> Date: Fri, 16 Aug 2024 11:28:48 +0100 Subject: [PATCH] Add lto support to clang-cl and lld-link Enables lto & thinlto support for clang-cl when used with lld-link. --- mesonbuild/compilers/mixins/visualstudio.py | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py index 78d62cb42..b4677f417 100644 --- a/mesonbuild/compilers/mixins/visualstudio.py +++ b/mesonbuild/compilers/mixins/visualstudio.py @@ -16,6 +16,7 @@ from ... import mesonlib from ... import mlog from mesonbuild.compilers.compilers import CompileCheckMode from ...options import OptionKey +from mesonbuild.linkers.linkers import ClangClDynamicLinker if T.TYPE_CHECKING: from ...environment import Environment @@ -443,6 +444,10 @@ class ClangClCompiler(VisualStudioLikeCompiler): def __init__(self, target: str): super().__init__(target) + self.base_options.update( + {OptionKey('b_lto_threads'), OptionKey('b_lto'), OptionKey('b_lto_mode'), OptionKey('b_thinlto_cache'), + OptionKey('b_thinlto_cache_dir')}) + # Assembly self.can_compile_suffixes.add('s') self.can_compile_suffixes.add('sx') @@ -494,3 +499,27 @@ class ClangClCompiler(VisualStudioLikeCompiler): if libs is None: raise mesonlib.MesonBugException('Could not find libomp') return super().openmp_link_flags(env) + libs + + def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: + args: T.List[str] = [] + if mode == 'thin': + # LTO data generated by clang-cl is only usable by lld-link + if not isinstance(self.linker, ClangClDynamicLinker): + raise mesonlib.MesonException(f"LLVM's ThinLTO only works with lld-link, not {self.linker.id}") + args.append(f'-flto={mode}') + else: + assert mode == 'default', 'someone forgot to wire something up' + args.extend(super().get_lto_compile_args(threads=threads)) + return args + + def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default', + thinlto_cache_dir: T.Optional[str] = None) -> T.List[str]: + args = [] + if mode == 'thin' and thinlto_cache_dir is not None: + args.extend(self.linker.get_thinlto_cache_args(thinlto_cache_dir)) + # lld-link /threads:N has the same behaviour as -flto-jobs=N in lld + if threads > 0: + # clang-cl was released after clang already had LTO support, so it + # is safe to assume that all versions of clang-cl support LTO + args.append(f'/threads:{threads}') + return args