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.
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
RUSTFLAGS="-Cprofile-generate=./pgo_data" cargo build -rRun the program with a training workload
cat test.txt | target/release/pgo-exampleThis 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
<install path>/lib/rustlib/powerpc64-ibm-aix/bin/llvm-profdata merge pgo_data/*.profdata -o pgo_data/merged.profdataCompile the final program with profile-use
RUSTFLAGS="-Cprofile-use=./pgo-data/merged.profdata" cargo build -r