Skip to content

Commit

Permalink
Add Run Passes (#474)
Browse files Browse the repository at this point in the history
Add a run_passes step to object persistence

This allows running the optimization from selected levels
  • Loading branch information
ghaith authored Mar 30, 2022
1 parent 89c5968 commit fd520d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::str::FromStr;
use clap::ArgEnum;
use codegen::CodeGen;
use glob::glob;
use inkwell::passes::PassBuilderOptions;
use std::path::Path;

use ast::{LinkageType, PouType, SourceRange};
Expand Down Expand Up @@ -130,6 +131,17 @@ impl From<OptimizationLevel> for inkwell::OptimizationLevel {
}
}

impl OptimizationLevel {
fn opt_params(&self) -> &str {
match self {
OptimizationLevel::None => "default<O0>",
OptimizationLevel::Less => "default<O1>",
OptimizationLevel::Default => "default<O2>",
OptimizationLevel::Aggressive => "default<O3>",
}
}
}

/// A struct representing the result of a compilation
#[derive(Default)]
pub struct CompileResult {
Expand Down Expand Up @@ -284,9 +296,16 @@ fn persist_to_obj(
Diagnostic::codegen_error("Cannot create target machine.", SourceRange::undefined())
});

////Run the passes
machine.and_then(|it| {
it.write_to_file(&codegen.module, FileType::Object, Path::new(output))
codegen
.module
.run_passes(optimization.opt_params(), &it, PassBuilderOptions::create())
.map_err(|it| Diagnostic::llvm_error(output, &it))
.and_then(|_| {
it.write_to_file(&codegen.module, FileType::Object, Path::new(output))
.map_err(|it| Diagnostic::llvm_error(output, &it))
})
})
}

Expand Down

0 comments on commit fd520d7

Please sign in to comment.