Skip to content

Commit 262dee5

Browse files
build,win: enable x64 PGO
PR-URL: #62761 Refs: #61964 Reviewed-By: Xuguang Mei <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 1178926 commit 262dee5

9 files changed

Lines changed: 357 additions & 30 deletions

File tree

common.gypi

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'msvs_multi_core_compile': '0', # we do enable multicore compiles, but not using the V8 way
1313
'enable_pgo_generate%': '0',
1414
'enable_pgo_use%': '0',
15+
'clang_profile_lib%': '',
1516
'python%': 'python',
1617

1718
'node_shared%': 'false',
@@ -243,6 +244,65 @@
243244
},],
244245
],
245246
},],
247+
['OS=="win"', {
248+
'conditions': [
249+
['enable_lto=="true"', {
250+
'msvs_settings': {
251+
'VCCLCompilerTool': {
252+
'AdditionalOptions': ['-flto=full'],
253+
},
254+
'VCLibrarianTool': {
255+
'AdditionalOptions': ['-flto=full'],
256+
},
257+
'VCLinkerTool': {
258+
'AdditionalOptions': ['-flto=full'],
259+
},
260+
},
261+
},],
262+
['enable_thin_lto=="true"', {
263+
'msvs_settings': {
264+
'VCCLCompilerTool': {
265+
'AdditionalOptions': ['-flto=thin'],
266+
},
267+
'VCLibrarianTool': {
268+
'AdditionalOptions': ['-flto=thin'],
269+
},
270+
'VCLinkerTool': {
271+
'AdditionalOptions': ['-flto=thin'],
272+
},
273+
},
274+
},],
275+
],
276+
'target_conditions': [
277+
['_toolset=="target"', {
278+
'conditions': [
279+
['enable_pgo_generate=="true"', {
280+
'msvs_settings': {
281+
'VCCLCompilerTool': {
282+
'AdditionalOptions': ['-fprofile-generate'],
283+
},
284+
'VCLinkerTool': {
285+
'AdditionalOptions': [
286+
'/NODEFAULTLIB:clang_rt.profile.lib',
287+
'"<(clang_profile_lib)"',
288+
],
289+
},
290+
},
291+
},],
292+
['enable_pgo_use=="true"', {
293+
'msvs_settings': {
294+
'VCCLCompilerTool': {
295+
'AdditionalOptions': ['-fprofile-use=$(SolutionDir)node.profdata'],
296+
},
297+
'VCLinkerTool': {
298+
'AdditionalOptions': ['-fprofile-use=$(SolutionDir)node.profdata'],
299+
},
300+
},
301+
},],
302+
],
303+
},],
304+
],
305+
},],
246306
['OS == "android"', {
247307
'cflags': [ '-fPIC', '-I<(android_ndk_path)/sources/android/cpufeatures' ],
248308
'ldflags': [ '-fPIC' ]

configure.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@
202202
dest="enable_pgo_generate",
203203
default=None,
204204
help="Enable profiling with pgo of a binary. This feature is only available "
205-
"on linux with gcc and g++ 5.4.1 or newer.")
205+
"on linux with gcc and g++ 5.4.1 or newer and on windows.")
206206

207207
parser.add_argument("--enable-pgo-use",
208208
action="store_true",
209209
dest="enable_pgo_use",
210210
default=None,
211211
help="Enable use of the profile generated with --enable-pgo-generate. This "
212-
"feature is only available on linux with gcc and g++ 5.4.1 or newer.")
212+
"feature is only available on linux with gcc and g++ 5.4.1 or newer and on windows.")
213213

214214
parser.add_argument("--enable-lto",
215215
action="store_true",
@@ -218,6 +218,13 @@
218218
help="Enable compiling with lto of a binary. This feature is only available "
219219
"with gcc 5.4.1+ or clang 3.9.1+.")
220220

221+
parser.add_argument("--enable-thin-lto",
222+
action="store_true",
223+
dest="enable_thin_lto",
224+
default=None,
225+
help="Enable compiling with thin lto of a binary. This feature is only available "
226+
"on windows.")
227+
221228
parser.add_argument("--link-module",
222229
action="append",
223230
dest="linked_module",
@@ -925,7 +932,8 @@
925932
action='store_true',
926933
dest='with_ltcg',
927934
default=None,
928-
help='Use Link Time Code Generation. This feature is only available on Windows.')
935+
help='Use Thin LTO scoped to node.exe and libnode only. '
936+
'This feature is only available on Windows.')
929937

930938
parser.add_argument('--write-snapshot-as-array-literals',
931939
action='store_true',
@@ -1916,9 +1924,9 @@ def configure_node(o):
19161924
else:
19171925
o['variables']['node_enable_v8_vtunejit'] = 'false'
19181926

1919-
if flavor != 'linux' and (options.enable_pgo_generate or options.enable_pgo_use):
1927+
if (flavor != 'linux' and flavor != 'win') and (options.enable_pgo_generate or options.enable_pgo_use):
19201928
raise Exception(
1921-
'The pgo option is supported only on linux.')
1929+
'The pgo option is supported only on linux and windows.')
19221930

19231931
if flavor == 'linux':
19241932
if options.enable_pgo_generate or options.enable_pgo_use:
@@ -1929,21 +1937,55 @@ def configure_node(o):
19291937
'The options --enable-pgo-generate and --enable-pgo-use '
19301938
f'are supported for gcc and gxx {version_checked_str} or newer only.')
19311939

1932-
if options.enable_pgo_generate and options.enable_pgo_use:
1933-
raise Exception(
1934-
'Only one of the --enable-pgo-generate or --enable-pgo-use options '
1935-
'can be specified at a time. You would like to use '
1936-
'--enable-pgo-generate first, profile node, and then recompile '
1937-
'with --enable-pgo-use')
1940+
if options.enable_pgo_generate and options.enable_pgo_use:
1941+
raise Exception(
1942+
'Only one of the --enable-pgo-generate or --enable-pgo-use options '
1943+
'can be specified at a time. You would like to use '
1944+
'--enable-pgo-generate first, profile node, and then recompile '
1945+
'with --enable-pgo-use')
19381946

19391947
o['variables']['enable_pgo_generate'] = b(options.enable_pgo_generate)
19401948
o['variables']['enable_pgo_use'] = b(options.enable_pgo_use)
19411949

1942-
if flavor == 'win' and (options.enable_lto):
1950+
if flavor == 'win' and (options.enable_pgo_generate or options.enable_pgo_use):
1951+
lib_suffix = 'aarch64' if target_arch == 'arm64' else 'x86_64'
1952+
lib_name = f'clang_rt.profile-{lib_suffix}.lib'
1953+
msvc_dir = target_arch # 'x64' or 'arm64'
1954+
1955+
vc_tools_dir = os.environ.get('VCToolsInstallDir', '')
1956+
if vc_tools_dir:
1957+
clang_profile_lib = os.path.join(vc_tools_dir, 'lib', msvc_dir, lib_name)
1958+
if os.path.isfile(clang_profile_lib):
1959+
o['variables']['clang_profile_lib'] = clang_profile_lib
1960+
else:
1961+
raise Exception(
1962+
f'PGO profile runtime library not found at {clang_profile_lib}. '
1963+
'Ensure the ClangCL toolset is installed.')
1964+
else:
1965+
raise Exception(
1966+
'VCToolsInstallDir not set. Run from a Visual Studio command prompt.')
1967+
1968+
if flavor != 'win' and options.enable_thin_lto:
19431969
raise Exception(
1944-
'Use Link Time Code Generation instead.')
1970+
'Use --enable-lto instead.')
1971+
1972+
# LTO mutual exclusion
1973+
if flavor == 'win':
1974+
lto_options = []
1975+
if options.enable_lto:
1976+
lto_options.append('--enable-lto')
1977+
if options.enable_thin_lto:
1978+
lto_options.append('--enable-thin-lto')
1979+
if options.with_ltcg:
1980+
lto_options.append('--with-ltcg')
1981+
if len(lto_options) > 1:
1982+
raise Exception(
1983+
f'Only one LTO option can be specified at a time: {", ".join(lto_options)}. '
1984+
'Use --enable-lto for Full LTO (global), '
1985+
'--enable-thin-lto for Thin LTO (global), '
1986+
'or --with-ltcg for Thin LTO (scoped to node.exe and libnode).')
19451987

1946-
if options.enable_lto:
1988+
if options.enable_lto and flavor != 'win':
19471989
gcc_version_checked = (5, 4, 1)
19481990
clang_version_checked = (3, 9, 1)
19491991
if not gcc_version_ge(gcc_version_checked) and not clang_version_ge(clang_version_checked):
@@ -1954,6 +1996,7 @@ def configure_node(o):
19541996
f'or clang {clang_version_checked_str}+ only.')
19551997

19561998
o['variables']['enable_lto'] = b(options.enable_lto)
1999+
o['variables']['enable_thin_lto'] = b(options.enable_thin_lto)
19572000

19582001
if options.node_use_large_pages or options.node_use_large_pages_script_lld:
19592002
warn('''The `--use-largepages` and `--use-largepages-script-lld` options

deps/openssl/openssl-cli.gypi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,15 @@
2525
['enable_lto=="true"', {
2626
'ldflags': [ '-fno-lto' ],
2727
}],
28+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
29+
'msvs_settings': {
30+
'VCCLCompilerTool': {
31+
'AdditionalOptions': ['-fno-lto'],
32+
},
33+
'VCLinkerTool': {
34+
'AdditionalOptions': ['-fno-lto'],
35+
},
36+
},
37+
}],
2838
],
2939
}

deps/openssl/openssl.gyp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@
7777
['enable_lto=="true"', {
7878
'ldflags': [ '-fno-lto' ],
7979
}],
80+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
81+
'msvs_settings': {
82+
'VCCLCompilerTool': {
83+
'AdditionalOptions': ['-fno-lto'],
84+
},
85+
'VCLinkerTool': {
86+
'AdditionalOptions': ['-fno-lto'],
87+
},
88+
},
89+
}],
8090
]
8191
}, {
8292
# openssl-fipsmodule target

node.gyp

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -730,35 +730,34 @@
730730
'Ws2_32.lib',
731731
],
732732
}],
733+
# Thin LTO for node_main.cc and linker (scoped to node_exe)
733734
['node_with_ltcg=="true"', {
734735
'msvs_settings': {
735736
'VCCLCompilerTool': {
736-
'WholeProgramOptimization': 'true' # /GL, whole program optimization, needed for LTCG
737+
'AdditionalOptions': ['-flto=thin'],
737738
},
738-
'VCLibrarianTool': {
739-
'AdditionalOptions': [
740-
'/LTCG:INCREMENTAL', # link time code generation
741-
],
739+
'VCLinkerTool': {
740+
'AdditionalOptions': ['-flto=thin'],
742741
},
742+
},
743+
}],
744+
# Whole-program optimization: either Thin LTO or PGO
745+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true" or enable_pgo_generate=="true" or enable_pgo_use=="true"', {
746+
'msvs_settings': {
743747
'VCLinkerTool': {
744748
'OptimizeReferences': 2, # /OPT:REF
745749
'EnableCOMDATFolding': 2, # /OPT:ICF
746750
'LinkIncremental': 1, # disable incremental linking
747-
'AdditionalOptions': [
748-
'/LTCG:INCREMENTAL', # incremental link-time code generation
749-
],
750-
}
751-
}
751+
},
752+
},
752753
}, {
754+
# No whole-program optimization
753755
'msvs_settings': {
754-
'VCCLCompilerTool': {
755-
'WholeProgramOptimization': 'false'
756-
},
757756
'VCLinkerTool': {
758-
'LinkIncremental': 2 # enable incremental linking
757+
'LinkIncremental': 2, # enable incremental linking
759758
},
760759
},
761-
}],
760+
}],
762761
['node_use_node_snapshot=="true"', {
763762
'dependencies': [
764763
'node_mksnapshot',
@@ -1139,6 +1138,17 @@
11391138
[ 'debug_nghttp2==1', {
11401139
'defines': [ 'NODE_DEBUG_NGHTTP2=1' ]
11411140
}],
1141+
# Thin LTO for node sources (scoped to libnode, not global)
1142+
['node_with_ltcg=="true"', {
1143+
'msvs_settings': {
1144+
'VCCLCompilerTool': {
1145+
'AdditionalOptions': ['-flto=thin'],
1146+
},
1147+
'VCLibrarianTool': {
1148+
'AdditionalOptions': ['-flto=thin'],
1149+
},
1150+
},
1151+
}],
11421152
],
11431153
'actions': [
11441154
{
@@ -1439,6 +1449,16 @@
14391449
['enable_lto=="true"', {
14401450
'ldflags': [ '-fno-lto' ],
14411451
}],
1452+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
1453+
'msvs_settings': {
1454+
'VCCLCompilerTool': {
1455+
'AdditionalOptions': ['-fno-lto'],
1456+
},
1457+
'VCLinkerTool': {
1458+
'AdditionalOptions': ['-fno-lto'],
1459+
},
1460+
},
1461+
}],
14421462
],
14431463
}, # cctest
14441464

@@ -1503,6 +1523,16 @@
15031523
['enable_lto=="true"', {
15041524
'ldflags': [ '-fno-lto' ],
15051525
}],
1526+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
1527+
'msvs_settings': {
1528+
'VCCLCompilerTool': {
1529+
'AdditionalOptions': ['-fno-lto'],
1530+
},
1531+
'VCLinkerTool': {
1532+
'AdditionalOptions': ['-fno-lto'],
1533+
},
1534+
},
1535+
}],
15061536
],
15071537
}, # embedtest
15081538

@@ -1580,6 +1610,16 @@
15801610
['enable_lto=="true"', {
15811611
'ldflags': [ '-fno-lto' ],
15821612
}],
1613+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
1614+
'msvs_settings': {
1615+
'VCCLCompilerTool': {
1616+
'AdditionalOptions': ['-fno-lto'],
1617+
},
1618+
'VCLinkerTool': {
1619+
'AdditionalOptions': ['-fno-lto'],
1620+
},
1621+
},
1622+
}],
15831623
]
15841624
}, # overlapped-checker
15851625
{
@@ -1706,6 +1746,16 @@
17061746
['enable_lto=="true"', {
17071747
'ldflags': [ '-fno-lto' ],
17081748
}],
1749+
['node_with_ltcg=="true" or enable_lto=="true" or enable_thin_lto=="true"', {
1750+
'msvs_settings': {
1751+
'VCCLCompilerTool': {
1752+
'AdditionalOptions': ['-fno-lto'],
1753+
},
1754+
'VCLinkerTool': {
1755+
'AdditionalOptions': ['-fno-lto'],
1756+
},
1757+
},
1758+
}],
17091759
],
17101760
}, # node_mksnapshot
17111761
], # end targets

0 commit comments

Comments
 (0)