Skip to content

Commit

Permalink
Do a little function surgery
Browse files Browse the repository at this point in the history
split `Function` into `FunctionParameters` and `FunctionStencil`, so the
stencil can be compiled and cached, and compilation parameters can be
applied onto the result of compiling a FunctionStencil
(CompiledCodeBase<Stencil>) later on to become a consumable
CompiledCode.
  • Loading branch information
bnjbvr committed Aug 9, 2022
1 parent 3e95b3f commit 7a3d562
Show file tree
Hide file tree
Showing 82 changed files with 1,504 additions and 1,216 deletions.
6 changes: 5 additions & 1 deletion cranelift/codegen/src/cfg_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ impl<'a> CFGPrinter<'a> {
}

fn header(&self, w: &mut dyn Write) -> Result {
writeln!(w, "digraph \"{}\" {{", self.func.name)?;
writeln!(
w,
"digraph \"{}\" {{",
self.func.params.name().display(Some(&self.func.params))
)?;
if let Some(entry) = self.func.layout.entry_block() {
writeln!(w, " {{rank=min; {}}}", entry)?;
}
Expand Down
29 changes: 18 additions & 11 deletions cranelift/codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::isa::TargetIsa;
use crate::legalizer::simple_legalize;
use crate::licm::do_licm;
use crate::loop_analysis::LoopAnalysis;
use crate::machinst::CompiledCode;
use crate::machinst::{CompiledCode, CompiledCodeBase, Stencil};
use crate::nan_canonicalization::do_nan_canonicalization;
use crate::remove_constant_phis::do_remove_constant_phis;
use crate::result::{CodegenResult, CompileResult};
Expand Down Expand Up @@ -126,7 +126,15 @@ impl Context {
Ok(compiled_code)
}

pub(crate) fn compile_inner(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> {
/// Internally compiles the function into a stencil.
///
/// Public only for testing and fuzzing purposes.
pub fn compile_stencil(
&mut self,
isa: &dyn TargetIsa,
) -> CodegenResult<CompiledCodeBase<Stencil>> {
let _tt = timing::compile();

self.verify_if(isa)?;

let opt_level = isa.flags().opt_level();
Expand Down Expand Up @@ -165,9 +173,7 @@ impl Context {
self.simple_gvn(isa)?;
}

let result = isa.compile_function(&self.func, self.want_disasm)?;
self.compiled_code = Some(result);
Ok(())
isa.compile_function(&self.func, self.want_disasm)
}

/// Compile the function.
Expand All @@ -179,12 +185,13 @@ impl Context {
/// Returns information about the function's code and read-only data.
pub fn compile(&mut self, isa: &dyn TargetIsa) -> CompileResult<&CompiledCode> {
let _tt = timing::compile();
self.compile_inner(isa)
.map(|_| self.compiled_code.as_ref().unwrap())
.map_err(|error| CompileError {
inner: error,
func: &self.func,
})
let stencil = self.compile_stencil(isa).map_err(|error| CompileError {
inner: error,
func: &self.func,
})?;
Ok(self
.compiled_code
.insert(stencil.apply_params(&self.func.params)))
}

/// If available, return information about the code layout in the
Expand Down
5 changes: 3 additions & 2 deletions cranelift/codegen/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl<'f> FuncCursor<'f> {

/// Use the source location of `inst` for future instructions.
pub fn use_srcloc(&mut self, inst: ir::Inst) {
self.srcloc = self.func.srclocs[inst];
self.srcloc = self.func.srcloc(inst);
}

/// Create an instruction builder that inserts an instruction at the current position.
Expand All @@ -612,6 +612,7 @@ impl<'f> Cursor for FuncCursor<'f> {
}

fn set_srcloc(&mut self, srcloc: ir::SourceLoc) {
self.func.params.ensure_base_srcloc(srcloc);
self.srcloc = srcloc;
}

Expand Down Expand Up @@ -658,7 +659,7 @@ impl<'c, 'f> ir::InstInserterBase<'c> for &'c mut FuncCursor<'f> {
}
self.insert_inst(inst);
if !self.srcloc.is_default() {
self.func.srclocs[inst] = self.srcloc;
self.func.set_srcloc(inst, self.srcloc);
}
&mut self.func.dfg
}
Expand Down
Loading

0 comments on commit 7a3d562

Please sign in to comment.