Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/markdown/snippets/msvc_lto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## `-Db_lto` and `-Db_pgo` now supported for MSVC

`-Db_lto` is now supported for MSVC's `/LTCG`, as is `-Db_lto_mode=thin`
for `/LTCG:INCREMENTAL`. `-Db_pgo` is also supported, and should be used
alongside `-Db_lto=true`.
33 changes: 32 additions & 1 deletion mesonbuild/compilers/mixins/visualstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ... import mlog
from mesonbuild.compilers.compilers import CompileCheckMode
from ...options import OptionKey
from mesonbuild.linkers.linkers import ClangClDynamicLinker
from mesonbuild.linkers.linkers import ClangClDynamicLinker, MSVCDynamicLinker

if T.TYPE_CHECKING:
from ...build import BuildTarget
Expand Down Expand Up @@ -388,6 +388,8 @@ class MSVCCompiler(VisualStudioLikeCompiler):
def __init__(self, target: str):
super().__init__(target)

self.base_options.update({OptionKey('b_lto'), OptionKey('b_lto_mode'), OptionKey('b_pgo')})

# Visual Studio 2013 and earlier don't support the /utf-8 argument.
# We want to remove it. We also want to make an explicit copy so we
# don't mutate class constant state
Expand Down Expand Up @@ -427,6 +429,35 @@ def get_pch_base_name(self, header: str) -> str:
def should_link_pch_object(self) -> bool:
return True

def get_lto_compile_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0,
mode: str = 'default') -> T.List[str]:
args: T.List[str] = ['/GL']
if mode == 'thin':
args.append('/Gy')
return args

def get_lto_link_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0,
mode: str = 'default', thinlto_cache_dir: T.Optional[str] = None) -> T.List[str]:
args: T.List[str] = []
# LTO data generated by MSVC is only usable by link
if not isinstance(self.linker, MSVCDynamicLinker):
raise mesonlib.MesonException(f"MSVC's LTCG only works with link, not {self.linker.id}")
if mode == 'default':
args.append('/LTCG')
elif mode == 'thin':
args.append('/LTCG:INCREMENTAL')
return args

def get_profile_generate_args(self) -> T.List[str]:
if not isinstance(self.linker, MSVCDynamicLinker):
raise mesonlib.MesonException(f"MSVC's PGO only works with link, not {self.linker.id}")
return self.linker_to_compiler_args(['/GENPROFILE'])

def get_profile_use_args(self) -> T.List[str]:
if not isinstance(self.linker, MSVCDynamicLinker):
raise mesonlib.MesonException(f"MSVC's PGO only works with link, not {self.linker.id}")
return self.linker_to_compiler_args(['/USEPROFILE'])

class ClangClCompiler(VisualStudioLikeCompiler):

"""Specific to Clang-CL."""
Expand Down
3 changes: 3 additions & 0 deletions mesonbuild/linkers/linkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,9 @@ def get_win_subsystem_args(self, value: str) -> T.List[str]:
def fatal_warnings(self) -> T.List[str]:
return ['-WX']

def get_lto_args(self) -> T.List[str]:
return ['/LTCG']


class ClangClDynamicLinker(VisualStudioLikeLinkerMixin, DynamicLinker):

Expand Down
Loading