Cretonne IL frontend: ILBuilder (#97)

* API and data structures proposal for the SSA construction module

* Polished API and implemented trivial functions

* API more explicit, Variable now struct parameter

* Sample test written to see how the API could be used

* Implemented local value numbering for SSABuilder

* Implemented SSA within a single Ebb

* Unfinished unoptimized implementation for recursive use and seal

* Working global value numbering
The SSABuilder now create ebb args and modifies jump instructions accordingly

* Updated doc and improved branch argument modifying.
Removed instructions::branch_arguments and instructions::branch_argument_mut

* SSA building: bugfix, asserts and new test case
Missing a key optimization to remove cycles of Phi

* SSA Building: small changes after code review
Created helper function for seal_block (which now contains sanity checks)

* Optimization: removed useless phis (ebb arguments)
Using pessimistic assumption that when using a non-def variable in an unsealed block we create an ebb argument which is removed when sealing if we detect it as useless
Using aliases to avoid rewriting variables

* Changed the semantics of remove_ebb_arg and turned it into a proper API method

* Adapted ssa branch to changes in the DFG API

* Abandonned SparseMaps for EntityMaps, added named structure for headr block data.

* Created skeletton for a Cretonne IL builder frontend

* Frontend IL builder: first draft of implementation with example of instruction methods

* Working basic implementation of the frontend
Missing handling of function arguments and return values

* Interaction with function signature, sample test, more checks

* Test with function verifier, seal and fill sanity check

* Implemented python script to generate ILBuilder methods

* Added support for jump tables and stack slot

* Major API overhaul
* No longer generating rust through Python but implements InstBuilder
* No longer parametrized by user's blocks but use regular `Ebb`
* Reuse of allocated memory via distinction between ILBuilder and FunctionBuilder

* Integrate changes from StackSlot

* Improved error message

* Added support for jump arguments supplied by the user

* Added an ebb_args proxy method needed

* Adapted to Entity_ref splitted into a new module

* Better error messages and fixed tests

* Added method to change jump destination

* We whould be able to add unreachable code

* Added inst_result proxy to frontend

* Import support

* Added optimization for SSA construction:
If multiple predecessors but agree on value don't create EBB argument

* Move unsafe and not write-only funcs apart, improved doc

* Added proxy function for append_ebb_arg

* Support for unreachable code and better layout of the Ebbs

* Fixed a bug yielding an infinite loop in SSA construction

* SSA predecessors lookup code refactoring

* Fixed bug in unreachable definition

* New sanity check and display debug function

* Fixed bug in verifier and added is_pristine ;ethod for frontend

* Extended set of characters printable in function names
To be able to print names of functions in test suite

* Fixes and improvements of SSA construction after code review

* Bugfixes for frontend code simplification

* On-the-fly critical edge splitting in case of br_table with jump arguments

* No more dangling undefined values, now attached as EBB args

* Bugfix: only split corresponding edges on demand, not all br_table edges

* Added signature retrieval method

* Bugfix for critical edge splitting not sealing the ebbs it created

* Proper handling of SSA side effects by the frontend

* Code refactoring: moving frontend and SSA to new crate

* Frontend: small changes and bugfixes after code review
This commit is contained in:
Denis Merigoux
2017-07-11 15:08:57 -07:00
committed by Jakob Stoklund Olesen
parent a4a8c83aab
commit 962c945a3c
10 changed files with 2162 additions and 2 deletions

152
lib/frontend/src/lib.rs Normal file
View File

@@ -0,0 +1,152 @@
//! Cretonne IL builder library.
//!
//! Provides a straightforward way to create a Cretonne IL function and fill it with instructions
//! translated from another language. Contains a SSA construction module that lets you translate
//! your non-SSA variables into SSA Cretonne IL values via `use_var` and `def_var` calls.
//!
//! To get started, create an [`IlBuilder`](struct.ILBuilder.html) and pass it as an argument
//! to a [`FunctionBuilder`](struct.FunctionBuilder.html).
//!
//! # Example
//!
//! Here is a pseudo-program we want to transform into Cretonne IL:
//!
//! ```cton
//! function(x) {
//! x, y, z : i32
//! block0:
//! y = 2;
//! z = x + y;
//! jump block1
//! block1:
//! z = z + y;
//! brnz y, block2;
//! z = z - x;
//! return y
//! block2:
//! y = y - x
//! jump block1
//! }
//! ```
//!
//! Here is how you build the corresponding Cretonne IL function using `ILBuilder`:
//!
//! ```rust
//! extern crate cretonne;
//! extern crate cton_frontend;
//!
//! use cretonne::entity_ref::EntityRef;
//! use cretonne::ir::{FunctionName, Function, Signature, ArgumentType, InstBuilder};
//! use cretonne::ir::types::*;
//! use cton_frontend::{ILBuilder, FunctionBuilder};
//! use cretonne::verifier::verify_function;
//! use std::u32;
//!
//! // An opaque reference to variable.
//! #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
//! pub struct Variable(u32);
//! impl EntityRef for Variable {
//! fn new(index: usize) -> Self {
//! assert!(index < (u32::MAX as usize));
//! Variable(index as u32)
//! }
//!
//! fn index(self) -> usize {
//! self.0 as usize
//! }
//! }
//! impl Default for Variable {
//! fn default() -> Variable {
//! Variable(u32::MAX)
//! }
//! }
//!
//! fn main() {
//! let mut sig = Signature::new();
//! sig.return_types.push(ArgumentType::new(I32));
//! sig.argument_types.push(ArgumentType::new(I32));
//! let mut il_builder = ILBuilder::<Variable>::new();
//! let mut func = Function::with_name_signature(FunctionName::new("sample_function"), sig);
//! {
//! let mut builder = FunctionBuilder::<Variable>::new(&mut func, &mut il_builder);
//!
//! let block0 = builder.create_ebb();
//! let block1 = builder.create_ebb();
//! let block2 = builder.create_ebb();
//! let x = Variable(0);
//! let y = Variable(1);
//! let z = Variable(2);
//! builder.declare_var(x, I32);
//! builder.declare_var(y, I32);
//! builder.declare_var(z, I32);
//!
//! builder.switch_to_block(block0, &[]);
//! builder.seal_block(block0);
//! {
//! let tmp = builder.arg_value(0);
//! builder.def_var(x, tmp);
//! }
//! {
//! let tmp = builder.ins().iconst(I32, 2);
//! builder.def_var(y, tmp);
//! }
//! {
//! let arg1 = builder.use_var(x);
//! let arg2 = builder.use_var(y);
//! let tmp = builder.ins().iadd(arg1, arg2);
//! builder.def_var(z, tmp);
//! }
//! builder.ins().jump(block1, &[]);
//!
//! builder.switch_to_block(block1, &[]);
//! {
//! let arg1 = builder.use_var(y);
//! let arg2 = builder.use_var(z);
//! let tmp = builder.ins().iadd(arg1, arg2);
//! builder.def_var(z, tmp);
//! }
//! {
//! let arg = builder.use_var(y);
//! builder.ins().brnz(arg, block2, &[]);
//! }
//! {
//! let arg1 = builder.use_var(z);
//! let arg2 = builder.use_var(x);
//! let tmp = builder.ins().isub(arg1, arg2);
//! builder.def_var(z, tmp);
//! }
//! {
//! let arg = builder.use_var(y);
//! builder.ins().return_(&[arg]);
//! }
//!
//! builder.switch_to_block(block2, &[]);
//! builder.seal_block(block2);
//!
//! {
//! let arg1 = builder.use_var(y);
//! let arg2 = builder.use_var(x);
//! let tmp = builder.ins().isub(arg1, arg2);
//! builder.def_var(y, tmp);
//! }
//! builder.ins().jump(block1, &[]);
//! builder.seal_block(block1);
//! }
//!
//! let res = verify_function(&func, None);
//! println!("{}", func.display(None));
//! match res {
//! Ok(_) => {}
//! Err(err) => panic!("{}", err),
//! }
//! }
//! ```
#![deny(missing_docs)]
extern crate cretonne;
pub use frontend::{ILBuilder, FunctionBuilder};
mod frontend;
mod ssa;