build: add clang support for pgo on macOS/Linux · nodejs/node@2248869 · GitHub
Skip to content

Commit 2248869

Browse files
legendecasaduh95
authored andcommitted
build: add clang support for pgo on macOS/Linux
Signed-off-by: Chengzhong Wu <legendecas@gmail.com> PR-URL: #66136 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 2653b26 commit 2248869

6 files changed

Lines changed: 200 additions & 198 deletions

File tree

‎.gitignore‎

Lines changed: 5 additions & 0 deletions

‎common.gypi‎

Lines changed: 30 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+
'pgo_profile%': '',
1516
'clang_profile_lib%': '',
1617
'python%': 'python',
1718
'emulator%': [],
@@ -193,6 +194,7 @@
193194
}],
194195
['clang==1', {
195196
'lto': ' -flto ', # Clang
197+
'pgo_use': '-fprofile-use=<(pgo_profile)',
196198
}, {
197199
'lto': ' -flto=4 -ffat-lto-objects ', # GCC
198200
}],
@@ -247,6 +249,34 @@
247249
},],
248250
],
249251
},],
252+
['OS=="mac"', {
253+
'conditions': [
254+
['enable_pgo_generate=="true"', {
255+
'xcode_settings': {
256+
'OTHER_CFLAGS': ['<(pgo_generate)'],
257+
},
258+
'target_conditions': [
259+
['_type!="static_library"', {
260+
'xcode_settings': {
261+
'OTHER_LDFLAGS': ['<(pgo_generate)'],
262+
},
263+
}],
264+
],
265+
}],
266+
['enable_pgo_use=="true"', {
267+
'xcode_settings': {
268+
'OTHER_CFLAGS': ['<(pgo_use)'],
269+
},
270+
'target_conditions': [
271+
['_type!="static_library"', {
272+
'xcode_settings': {
273+
'OTHER_LDFLAGS': ['<(pgo_use)'],
274+
},
275+
}],
276+
],
277+
}],
278+
],
279+
}],
250280
['OS=="win"', {
251281
'conditions': [
252282
['enable_lto=="true"', {

‎configure.py‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@
208208
dest="enable_pgo_generate",
209209
default=None,
210210
help="Enable profiling with pgo of a binary. This feature is only available "
211-
"on linux with gcc and g++ 5.4.1 or newer and on windows.")
211+
"on linux with GCC or Clang, on macOS with Clang, and on windows.")
212212

213213
parser.add_argument("--enable-pgo-use",
214214
action="store_true",
215215
dest="enable_pgo_use",
216216
default=None,
217217
help="Enable use of the profile generated with --enable-pgo-generate. This "
218-
"feature is only available on linux with gcc and g++ 5.4.1 or newer and on windows.")
218+
"feature is only available on linux with GCC or Clang, on macOS "
219+
"with Clang, and on windows.")
219220

220221
parser.add_argument("--enable-lto",
221222
action="store_true",
@@ -2004,18 +2005,9 @@ def configure_node(o):
20042005
else:
20052006
o['variables']['node_enable_v8_vtunejit'] = 'false'
20062007

2007-
if (flavor != 'linux' and flavor != 'win') and (options.enable_pgo_generate or options.enable_pgo_use):
2008+
if flavor not in ('linux', 'mac', 'win') and (options.enable_pgo_generate or options.enable_pgo_use):
20082009
raise Exception(
2009-
'The pgo option is supported only on linux and windows.')
2010-
2011-
if flavor == 'linux':
2012-
if options.enable_pgo_generate or options.enable_pgo_use:
2013-
version_checked = (5, 4, 1)
2014-
if not gcc_version_ge(version_checked):
2015-
version_checked_str = ".".join(map(str, version_checked))
2016-
raise Exception(
2017-
'The options --enable-pgo-generate and --enable-pgo-use '
2018-
f'are supported for gcc and gxx {version_checked_str} or newer only.')
2010+
'The pgo option is supported only on linux, macOS, and windows.')
20192011

20202012
if options.enable_pgo_generate and options.enable_pgo_use:
20212013
raise Exception(
@@ -2024,6 +2016,24 @@ def configure_node(o):
20242016
'--enable-pgo-generate first, profile node, and then recompile '
20252017
'with --enable-pgo-use')
20262018

2019+
if flavor in ('linux', 'mac'):
2020+
if options.enable_pgo_generate or options.enable_pgo_use:
2021+
clang_compilers = [try_check_compiler(compiler, language)[1]
2022+
for compiler, language in ((CC, 'c'), (CXX, 'c++'))]
2023+
if all(clang_compilers):
2024+
profile = os.path.abspath('node.profdata')
2025+
if options.enable_pgo_use and not os.path.isfile(profile):
2026+
raise Exception(
2027+
f'PGO profile not found: {profile}. Run llvm-profdata merge first.')
2028+
o['variables']['pgo_profile'] = profile
2029+
elif flavor == 'mac' or any(clang_compilers):
2030+
raise Exception('PGO requires both CC and CXX to use Clang on macOS '
2031+
'or the same compiler family on linux.')
2032+
elif not gcc_version_ge((5, 4, 1)):
2033+
raise Exception(
2034+
'The options --enable-pgo-generate and --enable-pgo-use '
2035+
'require gcc and gxx 5.4.1 or newer.')
2036+
20272037
o['variables']['enable_pgo_generate'] = b(options.enable_pgo_generate)
20282038
o['variables']['enable_pgo_use'] = b(options.enable_pgo_use)
20292039

‎tools/pgo/README.md‎

Lines changed: 50 additions & 16 deletions

0 commit comments

Comments
 (0)