|
1 | 1 | --- |
2 | | -title: Incorporating PGO into a GitHub Actions workflow |
| 2 | +title: Using Profile Guided Optimization (Windows) |
3 | 3 | weight: 6 |
4 | 4 |
|
5 | 5 | ### FIXED, DO NOT MODIFY |
6 | 6 | layout: learningpathall |
7 | 7 | --- |
8 | 8 |
|
9 | | -### Build locally with make |
10 | | - |
11 | | -PGO can be integrated into a `Makefile` and continuous integration (CI) systems using simple command-line instructions, as shown in the sample `Makefile` below. |
12 | | - |
13 | | -{{% notice Caution %}} |
14 | | -PGO adds additional build steps which can increase compile time - especially for large code bases. As such, PGO is not suitable for all sections of code. You should PGO only for sections of code which are heavily influenced by run-time behavior and are performance critical. Therefore, PGO might not be ideal for early-stage development or for applications with highly variable or unpredictable usage patterns. |
15 | | -{{% /notice %}} |
16 | | - |
17 | | -Use a text editor to create a file named `Makefile` containing the following content: |
18 | | - |
19 | | -```makefile |
20 | | -# Simple Makefile for building and benchmarking div_bench with and without PGO |
21 | | - |
22 | | -# Compiler and flags |
23 | | -CXX := g++ |
24 | | -CXXFLAGS := -O3 -std=c++17 |
25 | | -LDLIBS := -lbenchmark -lpthread |
26 | | - |
27 | | -# Default target: build both binaries |
28 | | -.PHONY: all clean clean-gcda clean-perf run |
29 | | -all: div_bench.base div_bench.opt |
30 | | - |
31 | | -# Build the baseline binary (no PGO) |
32 | | -div_bench.base: div_bench.cpp |
33 | | - $(CXX) $(CXXFLAGS) $< $(LDLIBS) -o $@ |
34 | | - |
35 | | -# Build the PGO-optimized binary: |
36 | | -# Note: This target depends on the source file and cleans previous profile data first. |
37 | | -# It runs the instrumented binary to generate new profile data before the final compilation. |
38 | | -div_bench.opt: div_bench.cpp |
39 | | - $(MAKE) clean-gcda # Ensure no old profile data interferes |
40 | | - $(CXX) $(CXXFLAGS) -fprofile-generate $< $(LDLIBS) -o $@ |
41 | | - @echo "Running instrumented binary to gather profile data..." |
42 | | - ./div_bench.opt # Generate .gcda file |
43 | | - $(CXX) $(CXXFLAGS) -fprofile-use $< $(LDLIBS) -o $@ # Compile using the generated profile |
44 | | - $(MAKE) clean-perf # Optional: Clean perf data if generated elsewhere |
45 | | - |
46 | | -# Remove profile data files |
47 | | -clean-gcda: |
48 | | - rm -f ./*.gcda |
49 | | - |
50 | | -# Remove perf data files (if applicable) |
51 | | -clean-perf: |
52 | | - rm -f perf-division-* |
53 | | - |
54 | | -# Remove all generated files including binaries and profile data |
55 | | -clean: clean-gcda clean-perf |
56 | | - rm -f div_bench.base div_bench.opt |
57 | | - |
58 | | -# Run both benchmarks with informative headers |
59 | | -run: all # Ensure binaries are built before running |
60 | | - @echo "==================== Without Profile-Guided Optimization ====================" |
61 | | - ./div_bench.base |
62 | | - @echo "==================== With Profile-Guided Optimization ====================" |
63 | | - ./div_bench.opt |
| 9 | +### Build with PGO |
| 10 | + |
| 11 | +To generate a binary optimized using runtime profile data, first build an instrumented binary that records usage data. Before building, open the Arm dev shell so that the compiler is in your PATH: |
| 12 | + |
| 13 | +```bash |
| 14 | +& "C:\Program Files\Microsoft Visual Studio\18\Community\Common7\Tools\Launch-VsDevShell.ps1" -Arch arm64 |
| 15 | +``` |
| 16 | + |
| 17 | +(**note:** you may need to change the version number in your Visual Studio path, depending on which Visual Studio version you've installed.) |
| 18 | + |
| 19 | +Next, set an environment variable to refer to the installed packages directory: |
| 20 | + |
| 21 | +```bash |
| 22 | +$VCPKG="C:\git\vcpkg\installed\arm64-windows-static" |
64 | 23 | ``` |
65 | 24 |
|
66 | | -You can run the following commands in your terminal: |
| 25 | +Next, run the following command, which includes the `/GENPROFILE` flag, to build the instrumented binary: |
67 | 26 |
|
68 | | -* `make all` (or simply `make`): Compiles both `div_bench.base` (without PGO) and `div_bench.opt` (with PGO). This includes the steps of generating profile data for the optimized version. |
69 | | -* `make run`: Builds both binaries (if they don't exist) and then runs them, displaying the benchmark results for comparison. |
70 | | -* `make clean`: Removes the compiled binaries (`div_bench.base`, `div_bench.opt`) and any generated profile data files (`*.gcda`). |
| 27 | +```bash |
| 28 | +cl /O2 /GL /D BENCHMARK_STATIC_DEFINE /I "$VCPKG\include" /Fe:div_bench.exe div_bench.cpp /link /LTCG /GENPROFILE /PGD:div_bench.pgd /LIBPATH:"$VCPKG\lib" benchmark.lib benchmark_main.lib shlwapi.lib |
| 29 | +``` |
| 30 | + |
| 31 | +The compiler options used in this command are: |
| 32 | + |
| 33 | +* **/O2**: Creates [fast code](https://learn.microsoft.com/en-us/cpp/build/reference/o1-o2-minimize-size-maximize-speed?view=msvc-170) |
| 34 | +* **/GL**: Enables [whole program optimization](https://learn.microsoft.com/en-us/cpp/build/reference/gl-whole-program-optimization?view=msvc-170). |
| 35 | +* **/D**: Enables the Benchmark [static preprocessor definition](https://learn.microsoft.com/en-us/cpp/build/reference/d-preprocessor-definitions?view=msvc-170). |
| 36 | +* **/I**: Adds the arm64 includes to the [list of include directories](https://learn.microsoft.com/en-us/cpp/build/reference/i-additional-include-directories?view=msvc-170). |
| 37 | +* **/Fe**: Specifies a name for the [executable file output](https://learn.microsoft.com/en-us/cpp/build/reference/fe-name-exe-file?view=msvc-170). |
| 38 | +* **/link**: Specifies [options to pass to linker](https://learn.microsoft.com/en-us/cpp/build/reference/link-pass-options-to-linker?view=msvc-170). |
71 | 39 |
|
72 | | -### Build with GitHub Actions |
| 40 | +The linker options used in this command are: |
73 | 41 |
|
74 | | -Alternatively, you can integrate PGO into your Continuous Integration (CI) workflow using GitHub Actions. The YAML file below provides a basic example that compiles and runs the benchmark on a GitHub-hosted Ubuntu 24.04 Arm-based runner. This setup can be extended with automated tests to check for performance regressions. |
| 42 | +* **/LTCG**: Specifies [link time code generation](https://learn.microsoft.com/en-us/cpp/build/reference/ltcg-link-time-code-generation?view=msvc-170). |
| 43 | +* **/GENPROFILE**: Specifies [generation of a .pgd file for PGO](https://learn.microsoft.com/en-us/cpp/build/reference/genprofile-fastgenprofile-generate-profiling-instrumented-build?view=msvc-170). |
| 44 | +* **/PGD**: Specifies a [database for PGO](https://learn.microsoft.com/en-us/cpp/build/reference/pgd-specify-database-for-profile-guided-optimizations?view=msvc-170). |
| 45 | +* **/LIBPATH**: Specifies the [additional library path](https://learn.microsoft.com/en-us/cpp/build/reference/libpath-additional-libpath?view=msvc-170). |
75 | 46 |
|
76 | | -```yaml |
77 | | -name: PGO Benchmark |
| 47 | +Next, run the instrumented binary to generate the profile data: |
78 | 48 |
|
79 | | -on: |
80 | | - push: |
81 | | - branches: [ main ] |
| 49 | +```bash |
| 50 | +.\div_bench.exe |
| 51 | +``` |
82 | 52 |
|
83 | | -jobs: |
84 | | - build: |
85 | | - runs-on: ubuntu-24.04-arm |
| 53 | +This execution creates profile data files (typically with a `.pgc` extension) in the same directory. |
86 | 54 |
|
87 | | - steps: |
88 | | - - name: Check out source |
89 | | - uses: actions/checkout@v3 |
| 55 | +Now recompile the program using the `/USEPROFILE` flag to apply optimizations based on the collected data: |
90 | 56 |
|
91 | | - - name: Install dependencies |
92 | | - run: | |
93 | | - sudo apt-get update |
94 | | - sudo apt-get install -y libbenchmark-dev g++ |
| 57 | +```bash |
| 58 | +cl /O2 /GL /D BENCHMARK_STATIC_DEFINE /I "$VCPKG\include" /Fe:div_bench_opt.exe div_bench.cpp /link /LTCG:PGOptimize /USEPROFILE /PGD:div_bench.pgd /LIBPATH:"$VCPKG\lib" benchmark.lib benchmark_main.lib shlwapi.lib |
| 59 | +``` |
95 | 60 |
|
96 | | - - name: Clean previous profiling data |
97 | | - run: | |
98 | | - rm -rf ./*gcda |
99 | | - rm -f div_bench.base div_bench.opt |
| 61 | +In this command, the [USEPROFILE linker option](https://learn.microsoft.com/en-us/cpp/build/reference/useprofile?view=msvc-170) instructs the linker to enable PGO with the profile generated during the previous run of the executable. |
100 | 62 |
|
101 | | - - name: Compile base and instrumented binary |
102 | | - run: | |
103 | | - g++ -O3 -std=c++17 div_bench.cpp -lbenchmark -lpthread -o div_bench.base |
104 | | - g++ -O3 -std=c++17 -fprofile-generate div_bench.cpp -lbenchmark -lpthread -o div_bench.opt |
| 63 | +### Run the optimized binary |
105 | 64 |
|
106 | | - - name: Generate profile data and compile with PGO |
107 | | - run: | |
108 | | - ./div_bench.opt |
109 | | - g++ -O3 -std=c++17 -fprofile-use div_bench.cpp -lbenchmark -lpthread -o div_bench.opt |
| 65 | +Now run the optimized binary: |
110 | 66 |
|
111 | | - - name: Run benchmarks |
112 | | - run: | |
113 | | - echo "==================== Without Profile-Guided Optimization ====================" |
114 | | - ./div_bench.base |
115 | | - echo "==================== With Profile-Guided Optimization ====================" |
116 | | - ./div_bench.opt |
117 | | - echo "==================== Benchmarking complete ====================" |
| 67 | +```bash |
| 68 | +.\div_bench_opt.exe |
118 | 69 | ``` |
119 | 70 |
|
120 | | -To use this workflow, save the YAML content into a file named `pgo_benchmark.yml` (or any other `.yml` name) inside the `.github/workflows/` directory of your GitHub repository. Ensure your `div_bench.cpp` file is present in the repository root. When you push changes to the `main` branch, GitHub Actions will automatically detect this workflow file and execute the defined steps on an Arm-based runner, compiling both versions of the benchmark and running them. |
| 71 | +The following output shows the performance improvement: |
| 72 | + |
| 73 | +```output |
| 74 | +Running ./div_bench.opt |
| 75 | +Run on (4 X 2100 MHz CPU s) |
| 76 | +CPU Caches: |
| 77 | + L1 Data 64 KiB (x4) |
| 78 | + L1 Instruction 64 KiB (x4) |
| 79 | + L2 Unified 1024 KiB (x4) |
| 80 | + L3 Unified 32768 KiB (x1) |
| 81 | +Load Average: 0.10, 0.03, 0.01 |
| 82 | +***WARNING*** Library was built as DEBUG. Timings may be affected. |
| 83 | +------------------------------------------------------- |
| 84 | +Benchmark Time CPU Iterations |
| 85 | +------------------------------------------------------- |
| 86 | +baseDiv/1500 2.86 us 2.86 us 244429 |
| 87 | +``` |
121 | 88 |
|
| 89 | +As the terminal output above shows, the average execution time is reduced from 7.90 to 2.86 microseconds. This improvement occurs because the profile data informed the compiler that the input divisor was consistently 1500 during the profiled runs, allowing it to apply specific optimizations. |
0 commit comments