Skip to content

Commit

Permalink
[MERGE]
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 26, 2024
2 parents b115eba + 600d2bf commit 5b26599
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/IR/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ impl Module {
/// Runs the pass manager over all functions
pub fn runPassMngr(&mut self, mngr: PassManager) {
for (_, func) in &mut self.funcs {
func.runPassMngr(&mngr)
func.runPassMngr(&mngr);

for opt in &mngr.passes {
opt.run_func(func);
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/IR/nodes/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ impl Ir for BrCond<Var, BlockId, BlockId> {
compiler.compile_br_cond(&self, &block, module)
}

fn maybe_inline(&self, _: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
None
fn maybe_inline(&self, vars: &HashMap<String, Type>) -> Option<Box<dyn Ir>> {
if let Some(check) = vars.get(&self.inner1.name) {
let value = check.val() as i64;

if value == 0 {
Some(Br::new(self.inner3.to_owned()))
} else {
Some(Br::new(self.inner2.to_owned()))
}
} else { None }
}

fn eval(&self) -> Option<Box<dyn Ir>> {
Expand Down
6 changes: 3 additions & 3 deletions src/Obj/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ impl ObjectBuilder {

for link in &self.links {
let (_, off, _, _, _, _) = syms.get(&link.from).expect("expectd valid link source");
let (_, _, to_sym, ty, _, _) = syms.get(&link.to).expect("expected valid link destination");
let (_, _, to_sym, decl, _, _) = syms.get(&link.to).expect("expected valid link destination");

let addend = match ty {
Decl::Function => 1,
let addend = match decl {
Decl::Function => 4,
_ => 0,
};
let offset = -3;
Expand Down
45 changes: 45 additions & 0 deletions src/Optimizations/Passes/DeadBlockElimination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::Optimizations::Pass;
use crate::IR::{ir::Br, ir::BrCond, BlockId, Var};

/// ## Pass DeadBlockElimination <br>
/// deletes unused blocks
pub(crate) struct DeadBlockElimination {
}

/// Creates a new DeadBlockElimination pass which is heap allocated
pub fn DeadBlockElimination() -> Box<dyn Pass> {
Box::from( DeadBlockElimination {} )
}

impl Pass for DeadBlockElimination {
fn run_func(&self, func: &mut crate::prelude::Function) {
let mut used_blocks = Vec::new();

// CHECK FOR ALL USED BLOCKS

for block in &func.blocks {
for node in &block.nodes {
if let Some(br) = node.as_any().downcast_ref::<Br<BlockId>>() {
used_blocks.push(br.inner1.name.to_owned());
}

if let Some(br) = node.as_any().downcast_ref::<BrCond<Var, BlockId, BlockId>>() {
used_blocks.push(br.inner2.name.to_owned());
used_blocks.push(br.inner3.name.to_owned());
}
}
}

// REMOVE UNUSED BLOCKS

let mut index = 0;

for block in func.blocks.clone() {
if !used_blocks.contains(&block.name) && index != 0 { // do not remove first block
func.blocks.remove(index);
}

index += 1;
}
}
}
5 changes: 4 additions & 1 deletion src/Optimizations/Passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
mod ConstantEvaluation;
#[allow(hidden_glob_reexports)]
mod DeadNodeElimination;
#[allow(hidden_glob_reexports)]
mod DeadBlockElimination;

pub use ConstantEvaluation::*;
pub use DeadNodeElimination::*;
pub use DeadNodeElimination::*;
pub use DeadBlockElimination::*;
2 changes: 1 addition & 1 deletion src/Optimizations/mngr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Pass;

/// The manager of all passes (PassManager)
pub struct PassManager {
passes: VecDeque<Box<dyn Pass>>,
pub(crate) passes: VecDeque<Box<dyn Pass>>,
}

impl PassManager {
Expand Down
9 changes: 6 additions & 3 deletions src/Optimizations/template.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::IR::Block;
use crate::IR::{Block, Function};

/// The trait all Passes need to implement
pub trait Pass {
/// Returns the pass
fn run(&self, block: &mut Block);
/// Runs the pass on a block
fn run(&self, _block: &mut Block) {}

/// Runs the pass on the entire function
fn run_func(&self, _func: &mut Function) {}
}
2 changes: 2 additions & 0 deletions src/Target/x64/abs_jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl AbsSymDealer for X64AbsSymDealer {
for fix in fixup {
code.insert(pos - bytes_to_remove, fix);
}

todo!("unsupported");
}

fn dbg(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
28 changes: 28 additions & 0 deletions tests/Optimizations/const_eval/br1.yl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# RUN:
cargo run -p ylc -- -in=%s -O -fmt -passes=cp
# IN:

define i32 @main() {
entry:
%0 = i32 1
br cond %0 yes, no

yes:
ret i32 0

no:
ret i32 0
}

# STDERR:
define i32 @main() {
entry:
%0 = i32 1
br yes

yes:
ret i32 0

no:
ret i32 0
}
23 changes: 23 additions & 0 deletions tests/Optimizations/dbe/dbe0.yl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RUN:
cargo run -p ylc -- -in=%s -passes=dbe -fmt
# IN:

define i32 @main() {
entry:
br ret

ret:
ret i32 5

should_be_removed:
ret i32 1
}

# STDOUT:
define i32 @main() {
entry:
br ret

ret:
ret i32 5
}
5 changes: 4 additions & 1 deletion tests/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub fn call() -> Result<(), Box<dyn Error>> {

Ok(())
}
/*
// NOT YET SUPPORTED
#[no_mangle]
extern "C" fn custom_func(ls: i32, rs: i32) -> i32 {
Expand Down Expand Up @@ -105,4 +108,4 @@ pub fn extern_symbol() -> Result<(), Box<dyn Error>> {
}
Ok(())
}
}*/
2 changes: 2 additions & 0 deletions tools/ylc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let pass = match pass.to_lowercase().as_str() {
"cp" | "const_eval" | "const_evaluation" | "const-eval" | "const-evaluation" => Some( Passes::ConstantEvaluation() ),
"dne" | "dead_node" | "dead_node_elim" | "dead-node" | "dead-node-elimination" => Some( Passes::DeadNodeElimination() ),
"dbe" | "dead_block" | "dead_block_elim" | "dead-block" | "dead-block-elimination" => Some( Passes::DeadBlockElimination() ),
_ => {eprintln!("unkown pass: {}", pass); None },
};

Expand All @@ -183,6 +184,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut opts = PassManager::new();

opts.add( Passes::ConstantEvaluation() );
opts.add( Passes::DeadBlockElimination() );
opts.add( Passes::DeadNodeElimination() );

module.runPassMngr(opts);
Expand Down

0 comments on commit 5b26599

Please sign in to comment.