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()),
|
||||
};
|
||||
|
||||
|
||||
87
cranelift/src/souper_harvest.rs
Normal file
87
cranelift/src/souper_harvest.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use crate::utils::parse_sets_and_triple;
|
||||
use cranelift_codegen::Context;
|
||||
use cranelift_wasm::{DummyEnvironment, ReturnMode};
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
use std::{fs, io};
|
||||
|
||||
static WASM_MAGIC: &[u8] = &[0x00, 0x61, 0x73, 0x6D];
|
||||
|
||||
pub fn run(target: &str, input: &str, output: &str, flag_set: &[String]) -> Result<(), String> {
|
||||
let parsed = parse_sets_and_triple(flag_set, target)?;
|
||||
let fisa = parsed.as_fisa();
|
||||
if fisa.isa.is_none() {
|
||||
return Err("`souper-harvest` requires a target isa".into());
|
||||
}
|
||||
|
||||
let stdin = io::stdin();
|
||||
let mut input: Box<dyn io::BufRead> = match input {
|
||||
"-" => Box::new(stdin.lock()),
|
||||
_ => Box::new(io::BufReader::new(
|
||||
fs::File::open(input).map_err(|e| format!("failed to open input file: {}", e))?,
|
||||
)),
|
||||
};
|
||||
|
||||
let mut output: Box<dyn io::Write + Send> = match output {
|
||||
"-" => Box::new(io::stdout()),
|
||||
_ => Box::new(io::BufWriter::new(
|
||||
fs::File::create(output).map_err(|e| format!("failed to create output file: {}", e))?,
|
||||
)),
|
||||
};
|
||||
|
||||
let mut contents = vec![];
|
||||
input
|
||||
.read_to_end(&mut contents)
|
||||
.map_err(|e| format!("failed to read from input file: {}", e))?;
|
||||
|
||||
let funcs = if &contents[..WASM_MAGIC.len()] == WASM_MAGIC {
|
||||
let mut dummy_environ = DummyEnvironment::new(
|
||||
fisa.isa.unwrap().frontend_config(),
|
||||
ReturnMode::NormalReturns,
|
||||
false,
|
||||
);
|
||||
cranelift_wasm::translate_module(&contents, &mut dummy_environ)
|
||||
.map_err(|e| format!("failed to translate Wasm module to clif: {}", e))?;
|
||||
dummy_environ
|
||||
.info
|
||||
.function_bodies
|
||||
.iter()
|
||||
.map(|(_, f)| f.clone())
|
||||
.collect()
|
||||
} else {
|
||||
let contents = String::from_utf8(contents)
|
||||
.map_err(|e| format!("input is not a UTF-8 string: {}", e))?;
|
||||
cranelift_reader::parse_functions(&contents)
|
||||
.map_err(|e| format!("failed to parse clif: {}", e))?
|
||||
};
|
||||
|
||||
let (send, recv) = std::sync::mpsc::channel::<String>();
|
||||
|
||||
let writing_thread = std::thread::spawn(move || -> Result<(), String> {
|
||||
for lhs in recv {
|
||||
output
|
||||
.write_all(lhs.as_bytes())
|
||||
.map_err(|e| format!("failed to write to output file: {}", e))?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
|
||||
funcs
|
||||
.into_par_iter()
|
||||
.map_with(send, move |send, func| {
|
||||
let mut ctx = Context::new();
|
||||
ctx.func = func;
|
||||
|
||||
ctx.souper_harvest(fisa.isa.unwrap(), send)
|
||||
.map_err(|e| format!("failed to run souper harvester: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<(), String>>()?;
|
||||
|
||||
match writing_thread.join() {
|
||||
Ok(result) => result?,
|
||||
Err(e) => std::panic::resume_unwind(e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user