Skip to content

Commit

Permalink
Added: Tests for PGO with Cross Compilation
Browse files Browse the repository at this point in the history
And various corrections in terms of how PGO works.
  • Loading branch information
Sewer56 committed Jun 17, 2024
1 parent c59962b commit 1b07b3d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test-build-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ jobs:
build-library: true
upload-artifacts: false

# This tests gathering PGO data on a host, and building for a different target.
test-pgo-with-cross-compile:
runs-on: ubuntu-latest
steps:
- name: Checkout Test Repository
uses: actions/checkout@v4
with:
repository: Sewer56/prs-rs
ref: d08599ed5473616f57d57a0966939e1a5dbda9b4
- name: Test PGO with Cross-Compilation (Binary)
uses: Reloaded-Project/devops-rust-lightweight-binary@v1
with:
rust-project-path: tools/cli/
pgo-project-path: .
crate-name: prs-rs-cli
target: aarch64-unknown-linux-gnu # x64 host to aarch64 simulated guest
use-pgo: true
use-cross: true
pgo-benchmark-name: my_benchmark
upload-artifacts: false
- name: Test PGO with Cross-Compilation (Library)
uses: Reloaded-Project/devops-rust-lightweight-binary@v1
with:
crate-name: prs-rs
target: aarch64-unknown-linux-gnu # x64 host to to aarch64 simulated guest
use-pgo: true
use-cross: true
pgo-benchmark-name: my_benchmark
upload-artifacts: false
build-library: true

test-library-feature-build:
runs-on: ubuntu-latest
steps:
Expand Down
15 changes: 11 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,17 @@ to keep the benchmark as close to the final usage of your application as possibl

Then said profile is used to build the final version of your application.

Setting `use-pgo` should work for any target technically speaking, as the profiling data is collected
on the host machine. However, I personally am not sure if it's a valid use case to use PGO data
when cross compiling code to a different CPU architecture. I haven't ran into issues trying, but
just be careful.
For PGO to work, the `target` platform must be the same as the host that generated the profiling
data. Sometimes you can get away with using `cross` to achieve this, for example:

```yaml
# Host is x86-linux but this seems to work just a-ok!
target: aarch64-unknown-linux-gnu # x64 host to to aarch64 simulated guest
use-pgo: true
use-cross: true
```

If the process fails, your CI will fail, so do experiment.

## Building Libraries

Expand Down
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ runs:
fi
# Generate PGO profiling data
RUSTFLAGS="$RUSTFLAGS -Cprofile-generate=${GITHUB_WORKSPACE}/${{ inputs.rust-project-path }}/target/pgo-profiles" cargo +nightly test --bench ${{ inputs.pgo-benchmark-name }} --release --target ${{ inputs.target }} --features $PGO_FEATURES_ARGS
RUSTFLAGS_BACKUP="$RUSTFLAGS"
RUSTFLAGS="$RUSTFLAGS -Cprofile-generate=${GITHUB_WORKSPACE}/${{ inputs.rust-project-path }}/target/pgo-profiles" $BUILD_CMD +nightly test --bench ${{ inputs.pgo-benchmark-name }} --release --target ${{ inputs.target }} --features $PGO_FEATURES_ARGS
# Merge the PGO profiles
# This is tricky, llvm-profdata isn't in $PATH, so we need to find it ourselves.
Expand All @@ -156,7 +157,7 @@ runs:
sanitized_path="${sanitized_path%/}"
# Use the generated PGO profiling data to perform an optimized build
RUSTFLAGS="$RUSTFLAGS -C llvm-args=-pgo-warn-missing-function -C profile-use=${sanitized_path}/target/pgo-profiles/merged.profdata"
RUSTFLAGS="$RUSTFLAGS_BACKUP -C llvm-args=-pgo-warn-missing-function -C profile-use=${sanitized_path}/target/pgo-profiles/merged.profdata"
fi
# Build the project
Expand Down

0 comments on commit 1b07b3d

Please sign in to comment.