Revert "Remove FunctionBuilderContext from API, and change FunctionBuilder API"

This reverts commit 39e638af99dbe6537bc935bfb1a74669b62877b3.
This commit is contained in:
Erin Power
2019-09-14 17:08:10 +02:00
committed by Benjamin Bouvier
parent 8d62d5f724
commit 5426e42a27
11 changed files with 172 additions and 162 deletions

View File

@@ -20,22 +20,22 @@ use std::vec::Vec;
/// In order to reduce memory reallocations when compiling multiple functions,
/// `FunctionBuilderContext` holds various data structures which are cleared between
/// functions, rather than dropped, preserving the underlying allocations.
struct FunctionBuilderContext {
pub struct FunctionBuilderContext {
ssa: SSABuilder,
ebbs: SecondaryMap<Ebb, EbbData>,
types: SecondaryMap<Variable, Type>,
}
/// Temporary object used to build a single Cranelift IR `Function`.
pub struct FunctionBuilder {
pub struct FunctionBuilder<'a> {
/// The function currently being built.
/// This field is public so the function can be re-borrowed.
pub func: Function,
pub func: &'a mut Function,
/// Source location to assign to all new instructions.
srcloc: ir::SourceLoc,
func_ctx: FunctionBuilderContext,
func_ctx: &'a mut FunctionBuilderContext,
position: Position,
}
@@ -75,9 +75,8 @@ impl Position {
}
impl FunctionBuilderContext {
/// Creates a FunctionBuilderContext structure. The structure is
/// automatically cleared after each `FunctionBuilder` completes translating
/// a function.
/// Creates a FunctionBuilderContext structure. The structure is automatically cleared after
/// each [`FunctionBuilder`](struct.FunctionBuilder.html) completes translating a function.
pub fn new() -> Self {
Self {
ssa: SSABuilder::new(),
@@ -91,22 +90,26 @@ impl FunctionBuilderContext {
self.ebbs.clear();
self.types.clear();
}
fn is_empty(&self) -> bool {
self.ssa.is_empty() && self.ebbs.is_empty() && self.types.is_empty()
}
}
/// Implementation of the [`InstBuilder`](cranelift_codegen::ir::InstBuilder) that has
/// one convenience method per Cranelift IR instruction.
pub struct FuncInstBuilder<'short> {
builder: &'short mut FunctionBuilder,
pub struct FuncInstBuilder<'short, 'long: 'short> {
builder: &'short mut FunctionBuilder<'long>,
ebb: Ebb,
}
impl<'short> FuncInstBuilder<'short> {
fn new(builder: &'short mut FunctionBuilder, ebb: Ebb) -> Self {
impl<'short, 'long> FuncInstBuilder<'short, 'long> {
fn new(builder: &'short mut FunctionBuilder<'long>, ebb: Ebb) -> Self {
Self { builder, ebb }
}
}
impl<'short> InstBuilderBase<'short> for FuncInstBuilder<'short> {
impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {
fn data_flow_graph(&self) -> &DataFlowGraph {
&self.builder.func.dfg
}
@@ -200,9 +203,12 @@ impl<'short> InstBuilderBase<'short> for FuncInstBuilder<'short> {
/// The first block for which you call `switch_to_block` will be assumed to be the beginning of
/// the function.
///
/// At creation, a `FunctionBuilder` instance borrows an already allocated
/// `Function`. The function passed in should be newly created with
/// [`Function::with_name_signature()`](Function::with_name_signature).
/// At creation, a `FunctionBuilder` instance borrows an already allocated `Function` which it
/// modifies with the information stored in the mutable borrowed
/// [`FunctionBuilderContext`](struct.FunctionBuilderContext.html). The function passed in
/// argument should be newly created with
/// [`Function::with_name_signature()`](Function::with_name_signature), whereas the
/// `FunctionBuilderContext` can be kept as is between two function translations.
///
/// # Errors
///
@@ -210,13 +216,15 @@ impl<'short> InstBuilderBase<'short> for FuncInstBuilder<'short> {
/// function in a way that violate the coherence of the code. For instance: switching to a new
/// `Ebb` when you haven't filled the current one with a terminator instruction, inserting a
/// return instruction with arguments that don't match the function's signature.
impl FunctionBuilder {
/// Creates a new FunctionBuilder structure that will operate on a `Function`.
pub fn new(func: Function) -> Self {
impl<'a> FunctionBuilder<'a> {
/// Creates a new FunctionBuilder structure that will operate on a `Function` using a
/// `FunctionBuilderContext`.
pub fn new(func: &'a mut Function, func_ctx: &'a mut FunctionBuilderContext) -> Self {
debug_assert!(func_ctx.is_empty());
Self {
func,
srcloc: Default::default(),
func_ctx: FunctionBuilderContext::new(),
func_ctx,
position: Position::default(),
}
}
@@ -271,7 +279,7 @@ impl FunctionBuilder {
/// created. Forgetting to call this method on every block will cause inconsistencies in the
/// produced functions.
pub fn seal_block(&mut self, ebb: Ebb) {
let side_effects = self.func_ctx.ssa.seal_ebb_header_block(ebb, &mut self.func);
let side_effects = self.func_ctx.ssa.seal_ebb_header_block(ebb, self.func);
self.handle_ssa_side_effects(side_effects);
}
@@ -282,7 +290,7 @@ impl FunctionBuilder {
/// function can be used at the end of translating all blocks to ensure
/// that everything is sealed.
pub fn seal_all_blocks(&mut self) {
let side_effects = self.func_ctx.ssa.seal_all_ebb_header_blocks(&mut self.func);
let side_effects = self.func_ctx.ssa.seal_all_ebb_header_blocks(self.func);
self.handle_ssa_side_effects(side_effects);
}
@@ -303,7 +311,7 @@ impl FunctionBuilder {
});
self.func_ctx
.ssa
.use_var(&mut self.func, var, ty, self.position.basic_block.unwrap())
.use_var(self.func, var, ty, self.position.basic_block.unwrap())
};
self.handle_ssa_side_effects(side_effects);
val
@@ -385,7 +393,7 @@ impl FunctionBuilder {
/// Returns an object with the [`InstBuilder`](cranelift_codegen::ir::InstBuilder)
/// trait that allows to conveniently append an instruction to the current `Ebb` being built.
pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short> {
pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short, 'a> {
let ebb = self
.position
.ebb
@@ -415,7 +423,7 @@ impl FunctionBuilder {
/// need to know about `FunctionBuilder` at all.
pub fn cursor(&mut self) -> FuncCursor {
self.ensure_inserted_ebb();
FuncCursor::new(&mut self.func)
FuncCursor::new(self.func)
.with_srcloc(self.srcloc)
.at_bottom(self.position.ebb.unwrap())
}
@@ -451,21 +459,10 @@ impl FunctionBuilder {
}
}
/// Clears the current state of the `FunctionBuilder` while preserving
/// allocations for reuse in translating another function.
pub fn clear(&mut self) {
// Clear the state (but preserve the allocated buffers) in preparation
// for translation another function.
self.func_ctx.clear();
// Reset srcloc and position to initial states.
self.srcloc = Default::default();
self.position = Position::default();
}
/// Declare that translation of the current function is complete, and return the completed
/// function. This resets the state of the `FunctionBuilder` in preparation to be used
/// Declare that translation of the current function is complete. This
/// resets the state of the `FunctionBuilder` in preparation to be used
/// for another function.
pub fn finalize(&mut self) -> ir::Function {
pub fn finalize(&mut self) {
// Check that all the `Ebb`s are filled and sealed.
debug_assert!(
self.func_ctx
@@ -495,8 +492,13 @@ impl FunctionBuilder {
}
}
self.clear();
std::mem::replace(&mut self.func, Function::new())
// Clear the state (but preserve the allocated buffers) in preparation
// for translation another function.
self.func_ctx.clear();
// Reset srcloc and position to initial states.
self.srcloc = Default::default();
self.position = Position::default();
}
}
@@ -505,7 +507,7 @@ impl FunctionBuilder {
/// performance of your translation perform more complex transformations to your Cranelift IR
/// function. The functions below help you inspect the function you're creating and modify it
/// in ways that can be unsafe if used incorrectly.
impl FunctionBuilder {
impl<'a> FunctionBuilder<'a> {
/// Retrieves all the parameters for an `Ebb` currently inferred from the jump instructions
/// inserted that target it and the SSA construction.
pub fn ebb_params(&self, ebb: Ebb) -> &[Value] {
@@ -594,7 +596,7 @@ impl FunctionBuilder {
}
/// Helper functions
impl FunctionBuilder {
impl<'a> FunctionBuilder<'a> {
/// Calls libc.memcpy
///
/// Copies the `size` bytes from `src` to `dest`, assumes that `src + size`
@@ -854,7 +856,7 @@ fn greatest_divisible_power_of_two(size: u64) -> u64 {
}
// Helper functions
impl FunctionBuilder {
impl<'a> FunctionBuilder<'a> {
fn move_to_next_basic_block(&mut self) {
self.position.basic_block = PackedOption::from(
self.func_ctx
@@ -889,7 +891,7 @@ impl FunctionBuilder {
#[cfg(test)]
mod tests {
use super::greatest_divisible_power_of_two;
use crate::frontend::FunctionBuilder;
use crate::frontend::{FunctionBuilder, FunctionBuilderContext};
use crate::Variable;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::types::*;
@@ -904,9 +906,10 @@ mod tests {
sig.returns.push(AbiParam::new(I32));
sig.params.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let block1 = builder.create_ebb();
@@ -988,8 +991,8 @@ mod tests {
builder.seal_all_blocks();
}
builder.finalize()
};
builder.finalize();
}
let flags = settings::Flags::new(settings::builder());
// println!("{}", func.display(None));
@@ -1026,9 +1029,10 @@ mod tests {
let mut sig = Signature::new(target.default_call_conv());
sig.returns.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let x = Variable::new(0);
@@ -1047,8 +1051,8 @@ mod tests {
builder.ins().return_(&[size]);
builder.seal_all_blocks();
builder.finalize()
};
builder.finalize();
}
assert_eq!(
func.display(None).to_string(),
@@ -1086,9 +1090,10 @@ ebb0:
let mut sig = Signature::new(target.default_call_conv());
sig.returns.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let x = Variable::new(0);
@@ -1105,8 +1110,8 @@ ebb0:
builder.ins().return_(&[dest]);
builder.seal_all_blocks();
builder.finalize()
};
builder.finalize();
}
assert_eq!(
func.display(None).to_string(),
@@ -1142,9 +1147,10 @@ ebb0:
let mut sig = Signature::new(target.default_call_conv());
sig.returns.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let x = Variable::new(0);
@@ -1161,8 +1167,8 @@ ebb0:
builder.ins().return_(&[dest]);
builder.seal_all_blocks();
builder.finalize()
};
builder.finalize();
}
assert_eq!(
func.display(None).to_string(),
@@ -1201,9 +1207,10 @@ ebb0:
let mut sig = Signature::new(target.default_call_conv());
sig.returns.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let y = Variable::new(16);
@@ -1217,8 +1224,8 @@ ebb0:
builder.ins().return_(&[dest]);
builder.seal_all_blocks();
builder.finalize()
};
builder.finalize();
}
assert_eq!(
func.display(None).to_string(),
@@ -1252,9 +1259,10 @@ ebb0:
let mut sig = Signature::new(target.default_call_conv());
sig.returns.push(AbiParam::new(I32));
let func = {
let func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
let mut builder = FunctionBuilder::new(func);
let mut fn_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
{
let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
let block0 = builder.create_ebb();
let y = Variable::new(16);
@@ -1268,8 +1276,8 @@ ebb0:
builder.ins().return_(&[dest]);
builder.seal_all_blocks();
builder.finalize()
};
builder.finalize();
}
assert_eq!(
func.display(None).to_string(),