-
Notifications
You must be signed in to change notification settings - Fork 127
Add heuristic to order harness codegen #4257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexanderPortland
wants to merge
4
commits into
model-checking:main
Choose a base branch
from
AlexanderPortland:codegen-ordering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3899557
do reachability for all harnesses before codegen
AlexanderPortland 96e9c30
apply heuristics to order harness codegen
AlexanderPortland 7230032
remove accidental print statement
AlexanderPortland 697bd53
Merge branch 'main' into codegen-ordering
AlexanderPortland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! Utilities for optimizing the order of Kani codegen. | ||
//! | ||
//! When compiling with more than a single thread, the order in which we codegen harnesses can have | ||
//! an non-negligable impact on performance. Specifically, if we handle harness that will generate | ||
//! a lot of code near the end of compilation, the main compiler thread can get stuck waiting for | ||
//! worker threads to export that code, slowing down overall compilation. | ||
//! | ||
//! This module currently provides a simple [MostReachableItems] heuristic to combat that, but more | ||
//! complex heuristics might be able to improve on this or avoid other kinds of pitfalls. | ||
|
||
use crate::{ | ||
codegen_cprover_gotoc::HarnessWithReachable, kani_middle::transform::BodyTransformation, | ||
}; | ||
|
||
/// Orders harnesses within a [CodegenUnit](crate::kani_middle::codegen_units::CodegenUnit) based on | ||
/// **the raw number of items found during reachability analysis**, putting those with more first. | ||
/// | ||
/// The number of reachable items seems to be a good proxy for the amount of code we will generate and | ||
/// thus how long both codegen and the goto file exporting will take. Putting the harnesses that will take | ||
/// the longest first ensures that | ||
pub struct MostReachableItems; | ||
|
||
impl CodegenHeuristic for MostReachableItems { | ||
fn evaluate_harness(harness: &HarnessWithReachable) -> usize { | ||
harness.1.reachable.len() | ||
} | ||
} | ||
|
||
pub trait CodegenHeuristic { | ||
/// Evaluate and rate a harness based on the given heuristic (where *higher is better*). | ||
fn evaluate_harness(harness: &HarnessWithReachable) -> usize; | ||
} | ||
|
||
fn reorder_harnesses<'a, H: CodegenHeuristic>( | ||
harnesses: &mut Vec<(HarnessWithReachable<'a>, BodyTransformation)>, | ||
) { | ||
// Sort is ascending by default, so `usize::MAX - ...` ensures higher rated harnesses come first. | ||
// We don't care about stability, and for cheap heuristic fns like the one for `MostReachableItems`, | ||
// caching isn't likely to make a difference. | ||
harnesses.sort_unstable_by_key(|(harness, _)| usize::MAX - H::evaluate_harness(harness)); | ||
} | ||
|
||
/// Simple trait extender to allow us to call `.apply_...()` on the right kind of iterators. | ||
/// Could also just be implemented as a function, but this matches the iterator style now used | ||
/// for reachability in `codegen_crate`. | ||
pub trait HeuristicOrderable: Iterator { | ||
fn apply_ordering_heuristic<T: CodegenHeuristic>(self) -> impl Iterator<Item = Self::Item>; | ||
} | ||
|
||
impl<'a, I> HeuristicOrderable for I | ||
where | ||
I: Iterator<Item = Vec<(HarnessWithReachable<'a>, BodyTransformation)>>, | ||
{ | ||
/// Apply an codegen ordering heuristic to an iterator over codegen units. | ||
fn apply_ordering_heuristic<H: CodegenHeuristic>(self) -> impl Iterator<Item = I::Item> { | ||
// Reorder harnesses within each codegen unit according to `T`. | ||
self.map(|mut harnesses| { | ||
reorder_harnesses::<H>(&mut harnesses); | ||
harnesses | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not tell you why tbh, but testing is showing that forcing all of the ordering to happen up front with a collect rather than lazily with a map makes this change go from slightly worse to significantly better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to look into why this is, as collecting seems to sneakily guzzle memory (peak goes from normal ~2 GB to ~6 GB), I think bc it requires one transformer per codegen unit to be kept in memory? If this change is doable without that drawback, it may be worthwhile but, if not, it should totally be dropped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cause of the memory overhead is that, now that we are doing reachability in a single pass first, we're inadvertently storing the reachable call tree for every harness while we figure out which order to codegen them in. Definitely not ideal as this takes up a ton of space :(. I'll look into how to fix this and if other heuristics could help