GeezerVison is showing github.com — build: add clang support for pgo on macOS/Linux · nodejs/node@96b6ea8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,11 @@ _UpgradeReport_Files/ | 103 | 103 | tools/*/*.i | 104 | 104 | tools/*/*.i.tmp | 105 | 105 |
| | 106 | +# === Rules for pgo artifacts === | | 107 | +*.profdata | | 108 | +*.profraw | | 109 | +*.gcda | | 110 | + | 106 | 111 | # === Rules for release artifacts === | 107 | 112 | /*.tar.* | 108 | 113 | /*.pkg | |
Collapse file Copy file name to clipboardExpand all lines: common.gypi +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | 'msvs_multi_core_compile': '0', # we do enable multicore compiles, but not using the V8 way | 13 | 13 | 'enable_pgo_generate%': '0', | 14 | 14 | 'enable_pgo_use%': '0', | | 15 | + 'pgo_profile%': '', | 15 | 16 | 'clang_profile_lib%': '', | 16 | 17 | 'python%': 'python', | 17 | 18 | 'emulator%': [], | | 198 | 199 | }], | 199 | 200 | ['clang==1', { | 200 | 201 | 'lto': ' -flto ', # Clang | | 202 | + 'pgo_use': '-fprofile-use=<(pgo_profile)', | 201 | 203 | }, { | 202 | 204 | 'lto': ' -flto=4 -ffat-lto-objects ', # GCC | 203 | 205 | }], | | 252 | 254 | },], | 253 | 255 | ], | 254 | 256 | },], | | 257 | + ['OS=="mac"', { | | 258 | + 'conditions': [ | | 259 | + ['enable_pgo_generate=="true"', { | | 260 | + 'xcode_settings': { | | 261 | + 'OTHER_CFLAGS': ['<(pgo_generate)'], | | 262 | + }, | | 263 | + 'target_conditions': [ | | 264 | + ['_type!="static_library"', { | | 265 | + 'xcode_settings': { | | 266 | + 'OTHER_LDFLAGS': ['<(pgo_generate)'], | | 267 | + }, | | 268 | + }], | | 269 | + ], | | 270 | + }], | | 271 | + ['enable_pgo_use=="true"', { | | 272 | + 'xcode_settings': { | | 273 | + 'OTHER_CFLAGS': ['<(pgo_use)'], | | 274 | + }, | | 275 | + 'target_conditions': [ | | 276 | + ['_type!="static_library"', { | | 277 | + 'xcode_settings': { | | 278 | + 'OTHER_LDFLAGS': ['<(pgo_use)'], | | 279 | + }, | | 280 | + }], | | 281 | + ], | | 282 | + }], | | 283 | + ], | | 284 | + }], | 255 | 285 | ['OS=="win"', { | 256 | 286 | 'conditions': [ | 257 | 287 | ['enable_lto=="true"', { | |
Collapse file Copy file name to clipboardExpand all lines: configure.py +23-13Lines changed: 23 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 208 | 208 | dest="enable_pgo_generate", | 209 | 209 | default=None, | 210 | 210 | 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.") | 212 | 212 |
| 213 | 213 | parser.add_argument("--enable-pgo-use", | 214 | 214 | action="store_true", | 215 | 215 | dest="enable_pgo_use", | 216 | 216 | default=None, | 217 | 217 | 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.") | 219 | 220 |
| 220 | 221 | parser.add_argument("--enable-lto", | 221 | 222 | action="store_true", | @@ -1720,18 +1721,9 @@ def configure_node(o): | 1720 | 1721 | else: | 1721 | 1722 | o['variables']['node_enable_v8_vtunejit'] = 'false' | 1722 | 1723 |
| 1723 | | - if (flavor != 'linux' and flavor != 'win') and (options.enable_pgo_generate or options.enable_pgo_use): | | 1724 | + if flavor not in ('linux', 'mac', 'win') and (options.enable_pgo_generate or options.enable_pgo_use): | 1724 | 1725 | raise Exception( | 1725 | | - 'The pgo option is supported only on linux and windows.') | 1726 | | - | 1727 | | - if flavor == 'linux': | 1728 | | - if options.enable_pgo_generate or options.enable_pgo_use: | 1729 | | - version_checked = (5, 4, 1) | 1730 | | - if not gcc_version_ge(version_checked): | 1731 | | - version_checked_str = ".".join(map(str, version_checked)) | 1732 | | - raise Exception( | 1733 | | - 'The options --enable-pgo-generate and --enable-pgo-use ' | 1734 | | - f'are supported for gcc and gxx {version_checked_str} or newer only.') | | 1726 | + 'The pgo option is supported only on linux, macOS, and windows.') | 1735 | 1727 |
| 1736 | 1728 | if options.enable_pgo_generate and options.enable_pgo_use: | 1737 | 1729 | raise Exception( | @@ -1740,6 +1732,24 @@ def configure_node(o): | 1740 | 1732 | '--enable-pgo-generate first, profile node, and then recompile ' | 1741 | 1733 | 'with --enable-pgo-use') | 1742 | 1734 |
| | 1735 | + if flavor in ('linux', 'mac'): | | 1736 | + if options.enable_pgo_generate or options.enable_pgo_use: | | 1737 | + clang_compilers = [try_check_compiler(compiler, language)[1] | | 1738 | + for compiler, language in ((CC, 'c'), (CXX, 'c++'))] | | 1739 | + if all(clang_compilers): | | 1740 | + profile = os.path.abspath('node.profdata') | | 1741 | + if options.enable_pgo_use and not os.path.isfile(profile): | | 1742 | + raise Exception( | | 1743 | + f'PGO profile not found: {profile}. Run llvm-profdata merge first.') | | 1744 | + o['variables']['pgo_profile'] = profile | | 1745 | + elif flavor == 'mac' or any(clang_compilers): | | 1746 | + raise Exception('PGO requires both CC and CXX to use Clang on macOS ' | | 1747 | + 'or the same compiler family on linux.') | | 1748 | + elif not gcc_version_ge((5, 4, 1)): | | 1749 | + raise Exception( | | 1750 | + 'The options --enable-pgo-generate and --enable-pgo-use ' | | 1751 | + 'require gcc and gxx 5.4.1 or newer.') | | 1752 | + | 1743 | 1753 | o['variables']['enable_pgo_generate'] = b(options.enable_pgo_generate) | 1744 | 1754 | o['variables']['enable_pgo_use'] = b(options.enable_pgo_use) | 1745 | 1755 |
| |
Collapse file Copy file name to clipboardExpand all lines: tools/pgo/README.md +50-16Lines changed: 50 additions & 16 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -15,20 +15,18 @@ The process has three phases: | 15 | 15 |
| 16 | 16 | ## Platform Support | 17 | 17 |
| 18 | | -| Platform | Supported toolchains | Driver | | 19 | | -| -------- | -------------------- | ------------------------- | | 20 | | -| Windows | Clang-CL | `vcbuild.bat` + `pgo.ps1` | | 21 | | -| Linux | GCC | `configure` + `make` | | 22 | | -| macOS | — | — | | | 18 | +| Platform | Supported toolchains | Driver | | | 19 | +| -------- | -------------------- | ------------------------------------- | | | 20 | +| Windows | Clang-CL | `vcbuild.bat` + `pgo.py` | | | 21 | +| Linux | GCC, Clang | `configure` + `make`, `pgo.py` for Clang | | | 22 | +| macOS | Clang | `configure` + `make` + `pgo.py` | | 23 | 23 |
| 24 | 24 | The two supported flows differ in how profile data is collected. Clang writes | 25 | 25 | one `.profraw` file per process, which must be merged into a single | 26 | 26 | `.profdata` before the optimize phase. GCC's libgcov instead merges counters | 27 | 27 | into `.gcda` files next to each object file as each process exits, so there is | 28 | 28 | no merge step. | 29 | 29 |
| 30 | | -Clang on Linux and macOS are not supported yet. | 31 | | - | 32 | 30 | ## Quick Start: Windows | 33 | 31 |
| 34 | 32 | From a VS Developer Command Prompt, at the repo root: | @@ -38,25 +36,25 @@ From a VS Developer Command Prompt, at the repo root: | 38 | 36 | vcbuild.bat pgo-generate | 39 | 37 |
| 40 | 38 | # Step 2: Run workloads to collect profile data | 41 | | -powershell -ExecutionPolicy Bypass -File .\tools\pgo\pgo.ps1 | | 39 | +python tools\pgo\pgo.py | 42 | 40 |
| 43 | 41 | # Step 3: Build the optimized binary | 44 | 42 | vcbuild.bat pgo-use | 45 | 43 | ``` | 46 | 44 |
| 47 | | -`pgo.ps1` expects the instrumented binary at `Release\node.exe` (produced by | | 45 | +`pgo.py` expects the instrumented binary at `Release\node.exe` (produced by | 48 | 46 | step 1) and writes `node.profdata` to the repo root (consumed by step 3). | 49 | 47 |
| 50 | | -The script is unsigned, so the default execution policy refuses to run it | 51 | | -without `-ExecutionPolicy Bypass`. Use `pwsh` in place of `powershell` on | 52 | | -PowerShell 7. | | 48 | +The script finds `llvm-profdata` in the Visual Studio LLVM toolset, then | | 49 | +`PATH`. Set `LLVM_PROFDATA` to the matching tool when using a different | | 50 | +Clang installation. | 53 | 51 |
| 54 | 52 | ```powershell | 55 | 53 | # Optionally set a longer training duration (default: 15s per script) | 56 | | -powershell -ExecutionPolicy Bypass -File .\tools\pgo\pgo.ps1 -Duration 30 | | 54 | +python tools\pgo\pgo.py --duration=30 | 57 | 55 | ``` | 58 | 56 |
| 59 | | -## Quick Start: Linux | | 57 | +## Quick Start: Linux with GCC | 60 | 58 |
| 61 | 59 | ```bash | 62 | 60 | # Step 1: Build the instrumented binary | @@ -88,6 +86,42 @@ updates from the worker threads and the libuv thread pool race with each | 88 | 86 | other, and GCC treats the resulting inconsistent profile as an error unless | 89 | 87 | told to smooth it out. | 90 | 88 |
| | 89 | +## Quick Start: Linux and macOS with Clang | | 90 | + | | 91 | +From the repo root: | | 92 | + | | 93 | +```bash | | 94 | +# Step 1: Build the instrumented binary | | 95 | +./configure --ninja --enable-pgo-generate | | 96 | +make | | 97 | + | | 98 | +# Step 2: Run workloads to collect profile data | | 99 | +python3 tools/pgo/pgo.py | | 100 | + | | 101 | +# Step 3: Build the optimized binary | | 102 | +./configure --ninja --enable-pgo-use | | 103 | +make | | 104 | +``` | | 105 | + | | 106 | +`pgo.py` expects the instrumented binary at `out/Release/node` (produced by | | 107 | +step 1) and writes `node.profdata` to the repo root (consumed by step 3). | | 108 | + | | 109 | +The script finds `llvm-profdata` through `xcrun` on macOS and `PATH` on Linux. | | 110 | +Set `LLVM_PROFDATA` to the matching tool when using a different Clang | | 111 | +installation. | | 112 | + | | 113 | +```bash | | 114 | +# Optionally set a longer training duration (default: 15s per script) | | 115 | +python3 tools/pgo/pgo.py --duration=30 | | 116 | +``` | | 117 | + | | 118 | +## Clang Profile Collection | | 119 | + | | 120 | +On all platforms, `pgo.py` collects raw profiles in a fresh directory and | | 121 | +replaces `node.profdata` after a successful merge. It removes raw profiles | | 122 | +after success and preserves them if training or merging fails. Training | | 123 | +failures stop the script so the workloads can be fixed before trying again. | | 124 | + | 91 | 125 | ## Training Scripts | 92 | 126 |
| 93 | 127 | All scripts use only Node.js built-in modules (no npm dependencies). | @@ -110,7 +144,7 @@ Each script is run as a separate process via `fork()`. | 110 | 144 | ### Running the Orchestrator Directly | 111 | 145 |
| 112 | 146 | The orchestrator can also be invoked directly (e.g. for testing individual | 113 | | -workloads). When used with `pgo.ps1`, this is handled automatically. | | 147 | +workloads). When used with `pgo.py`, this is handled automatically. | 114 | 148 |
| 115 | 149 | ```bash | 116 | 150 | # Run all scripts | @@ -131,7 +165,7 @@ automatically from the `--duration` flag (in seconds). | 131 | 165 |
| 132 | 166 | ``` | 133 | 167 | tools/pgo/ | 134 | | -├── pgo.ps1 # Windows training driver (collect + merge) | | 168 | +├── pgo.py # Clang training driver (collect + merge) | 135 | 169 | ├── pgo-run-all.js # Training orchestrator | 136 | 170 | ├── pgo-http-server.js # HTTP server + client workload | 137 | 171 | ├── pgo-json.js # JSON parse/stringify workload | |
|