Harvest left-hand side superoptimization candidates.
Given a clif function, harvest all its integer subexpressions, so that they can be fed into [Souper](https://github.com/google/souper) as candidates for superoptimization. For some of these candidates, Souper will successfully synthesize a right-hand side that is equivalent but has lower cost than the left-hand side. Then, we can combine these left- and right-hand sides into a complete optimization, and add it to our peephole passes. To harvest the expression that produced a given value `x`, we do a post-order traversal of the dataflow graph starting from `x`. As we do this traversal, we maintain a map from clif values to their translated Souper values. We stop traversing when we reach anything that can't be translated into Souper IR: a memory load, a float-to-int conversion, a block parameter, etc. For values produced by these instructions, we create a Souper `var`, which is an input variable to the optimization. For instructions that have a direct mapping into Souper IR, we get the Souper version of each of its operands and then create the Souper version of the instruction itself. It should now be clear why we do a post-order traversal: we need an instruction's translated operands in order to translate the instruction itself. Once this instruction is translated, we update the clif-to-souper map with this new translation so that any other instruction that uses this result as an operand has access to the translated value. When the traversal is complete we return the translation of `x` as the root of left-hand side candidate.
This commit is contained in:
27
cranelift/src/clif-util.rs
Normal file → Executable file
27
cranelift/src/clif-util.rs
Normal file → Executable file
@@ -27,6 +27,8 @@ mod disasm;
|
||||
mod interpret;
|
||||
mod print_cfg;
|
||||
mod run;
|
||||
#[cfg(feature = "souper-harvest")]
|
||||
mod souper_harvest;
|
||||
mod utils;
|
||||
|
||||
#[cfg(feature = "peepmatic-souper")]
|
||||
@@ -265,6 +267,13 @@ fn main() {
|
||||
.about("Convert Souper optimizations into Peepmatic DSL.")
|
||||
.arg(add_single_input_file_arg())
|
||||
.arg(add_output_arg()),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("souper-harvest")
|
||||
.arg(add_single_input_file_arg())
|
||||
.arg(add_output_arg())
|
||||
.arg(add_target_flag())
|
||||
.arg(add_set_flag()),
|
||||
);
|
||||
|
||||
let res_util = match app_cmds.get_matches().subcommand() {
|
||||
@@ -392,12 +401,28 @@ fn main() {
|
||||
#[cfg(not(feature = "peepmatic-souper"))]
|
||||
{
|
||||
Err(
|
||||
"Error: clif-util was compiled without suport for the `souper-to-peepmatic` \
|
||||
"Error: clif-util was compiled without support for the `souper-to-peepmatic` \
|
||||
subcommand"
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
("souper-harvest", Some(rest_cmd)) => {
|
||||
#[cfg(feature = "souper-harvest")]
|
||||
{
|
||||
souper_harvest::run(
|
||||
rest_cmd.value_of("target").unwrap_or_default(),
|
||||
rest_cmd.value_of("single-file").unwrap(),
|
||||
rest_cmd.value_of("output").unwrap(),
|
||||
&get_vec(rest_cmd.values_of("set")),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "souper-harvest"))]
|
||||
{
|
||||
Err("clif-util was compiled without `souper-harvest` support".into())
|
||||
}
|
||||
}
|
||||
_ => Err("Invalid subcommand.".to_owned()),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user