Add lto support to clang-cl and lld-link

Enables lto & thinlto support for clang-cl when used with lld-link.
pull/13622/head
Elliot 3 months ago committed by Dylan Baker
parent 19847ba24a
commit 18427adbf2
  1. 29
      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

Loading…
Cancel
Save