Skip to content

Commit

Permalink
[FIX] now all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Nov 2, 2024
1 parent 7189ed8 commit 53d2b28
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 570 deletions.
24 changes: 19 additions & 5 deletions src/IR/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct Module {

/// The number of current constants
pub(crate) const_index: usize,

pub(crate) debug_passes: bool,
}

impl Module {
Expand All @@ -25,6 +27,7 @@ impl Module {
consts: HashMap::new(),
dbg_registry: None,
const_index: 0,
debug_passes: false,
}
}

Expand All @@ -33,6 +36,11 @@ impl Module {
self.dbg_registry = Some(DebugRegistry::new(producer, lang, infile));
}

/// Makes, that debugging information is outputed from the passes
pub fn activate_pass_dbg(&mut self) {
self.debug_passes = true;
}

/// Adds a new function to the module
pub fn add(&mut self, name: &str, ty: &FunctionType) -> &mut Function {
self.funcs
Expand Down Expand Up @@ -155,11 +163,17 @@ 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);

for opt in &mngr.passes {
opt.run_func(func);
for pass in &mngr.passes {
if self.debug_passes {
eprintln!("Running pass: {}", pass.name());
}

for (_, func) in &mut self.funcs {
pass.run_func(func);

for block in &mut func.blocks {
pass.run(block);
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/IR/nodes/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ impl Ir for Cast<Var, TypeMetadata, Var> {
}

impl EvalOptVisitor for Cast<Var, TypeMetadata, Var> {
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(var) = vars.get(&self.inner1.name) {
Some(Assign::new(self.inner3.to_owned(), *var))
} else { return None; }
}

fn eval(&self) -> Option<Box<dyn Ir>> {
Expand Down
593 changes: 35 additions & 558 deletions src/IR/parser/semnatic.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Optimizations/Passes/ConstantEvaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub fn ConstantEvaluation() -> Box<dyn Pass> {
}

impl Pass for ConstantEvaluation {
fn name(&self) -> &'static str {
"ConstantEvaluation"
}

fn run(&self, block: &mut crate::prelude::Block) {
let mut const_values = HashMap::new();

Expand Down
4 changes: 4 additions & 0 deletions src/Optimizations/Passes/DeadBlockElimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub fn DeadBlockElimination() -> Box<dyn Pass> {
}

impl Pass for DeadBlockElimination {
fn name(&self) -> &'static str {
"DeadBlockElimination"
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/Optimizations/Passes/DeadNodeElimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub fn DeadNodeElimination() -> Box<dyn Pass> {
}

impl Pass for DeadNodeElimination_ {
fn name(&self) -> &'static str {
"DeadNodeElimination"
}

fn run_func(&self, func: &mut crate::prelude::Function) {
for _ in 0..2 { // iterate two times, cuz then we can remove dependants with a dept of 1
let mut used: Vec<String> = Vec::new();
Expand Down
8 changes: 6 additions & 2 deletions src/Optimizations/Passes/InstrCombine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub fn InstrCombine() -> Box<dyn Pass> {
}

impl Pass for InstrCombinePass {
fn name(&self) -> &'static str {
"InstrCombine"
}

fn run_func(&self, func: &mut crate::prelude::Function) {
InstrCombinePass::opt_func(func);

Expand Down Expand Up @@ -49,12 +53,12 @@ impl InstrCombinePass {
}

/// Optimizes the block by combining instructions
pub(crate) fn opt_block(block: &mut Block) {
pub(crate) fn opt_block(_block: &mut Block) {
// TODO
}

/// Optimizes the function by combining instructions
pub(crate) fn opt_func(func: &mut Function) {
pub(crate) fn opt_func(_func: &mut Function) {
// TODO
}
}
3 changes: 3 additions & 0 deletions src/Optimizations/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ pub trait Pass {

/// Runs the pass on the entire function
fn run_func(&self, _func: &mut Function) {}

/// Returns the name of the pass
fn name(&self) -> &'static str;
}
4 changes: 2 additions & 2 deletions tests/Optimizations/const_eval/cast0.yl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ define i32 @main() {
define i32 @main() {
entry:
%0 = i32 5
%1 = i32 %0
ret i32 %1
%1 = i32 5
ret i32 5
}
2 changes: 1 addition & 1 deletion tests/Optimizations/instcombine/select_into_cast.yl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define i32 @main() {

# STDOUT:

define void @main() {
define i32 @main() {
entry:
%1 = i8 1
%2 = cast i8 %1 to i32
Expand Down

0 comments on commit 53d2b28

Please sign in to comment.