Profile Guided Optimization (PGO)

This topic provides information on PGO and the same being used in IBM Open SDK for Rust on AIX.

Profile Guided Optimization (PGO), also known as profile-directed feedback (PDF), is a compiler optimization technique in computer programming that uses profiling to improve program runtime performance. PGO is supported in IBM Open SDK for Rust on AIX 1.96 through the -Cprofile-generate and -Cprofile-use flags. Using these flags, profiles generated using either rustc or ibm-clang are compatible, allowing the merging of profiling data across programs written in Rust, C, C++, and Fortran. The PGO tool llvm-profdata required for PGO is shipped with IBM Open SDK for Rust on AIX 1.96.

Note: The current release is compatible only with the profiles generated by IBM Open XL C/C++ for AIX 17.1.4.
Refer to complete cargo PGO workflows in the following resources:

Examples

Creating the program to be profiled

cargo new pgo-example && cd pro-example
// src/main.rs
fn main() {
    let stdin = io::stdin();

    let mut line_count = 0;
    let mut word_count = 0;
    let mut byte_count = 0;

    for line in stdin.lines() {
        match line {
            Ok(line) => {
                line_count += 1;
                word_count += line.split_whitespace().count();
                byte_count += line.len();
            }
            Err(err) => {
                eprintln!("Error reading input: {}", err);
                std::process::exit(1);
            }
        }
    }

    println!("{} {} {}", line_count, word_count, byte_count);
}

Compile the program with profile-generate

Tell rustc to embed the profile runtime and to instrument the binaries:
RUSTFLAGS="-Cprofile-generate=./pgo_data" cargo build -r

Run the program with a training workload

The RUSTFLAGS environment variable tells the compiler to instrument the binary and include the -Cprofile-generate flag. The generated data *.profraw is stored in the pgo_data directory. Run your program with the following input data:
cat test.txt | target/release/pgo-example
This command runs the compiled program (target/release/pgo-example) with input test data from the test.txt file, to get an estimate for the actual workloads it would encounter.

Merge the profiles generated from training

Merge the profile dates together into a single .profdata that can be used.
<install path>/lib/rustlib/powerpc64-ibm-aix/bin/llvm-profdata merge pgo_data/*.profdata -o pgo_data/merged.profdata

Compile the final program with profile-use

Use the merged PGO data during compilation for the final binary with the -Cprofile-use:
RUSTFLAGS="-Cprofile-use=./pgo-data/merged.profdata" cargo build -r