Skip to content

Commit

Permalink
add O0 O1 O2 O3 support for llvm
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Aug 6, 2024
1 parent 49c505e commit 853f33c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 105 deletions.
4 changes: 3 additions & 1 deletion infra/nightly-resources/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const COLORS = {
"rvsdg-round-trip-to-executable": "red",
"cranelift-O3": "blue",
"llvm-O0": "purple",
"llvm-O0-eggcc": "pink",
"llvm-O1": "green",
"llvm-O2": "orange",
"llvm-O3": "gray",
"llvm-O0-eggcc": "pink",
"llvm-O3-eggcc": "brown",
};

Expand Down
2 changes: 2 additions & 0 deletions infra/nightly-resources/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function shouldHaveLlvm(runMethod) {
return [
"rvsdg-round-trip-to-executable",
"llvm-O0",
"llvm-O1",
"llvm-O2",
"llvm-O0-eggcc",
"llvm-O3",
"llvm-O3-eggcc",
Expand Down
2 changes: 2 additions & 0 deletions infra/nightly-resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const treatments = [
"rvsdg-round-trip-to-executable",
"cranelift-O3",
"llvm-O0",
"llvm-O1",
"llvm-O2",
"llvm-O0-eggcc",
"llvm-O3",
"llvm-O3-eggcc",
Expand Down
14 changes: 10 additions & 4 deletions infra/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"rvsdg-round-trip-to-executable",
"cranelift-O3",
"llvm-O0",
"llvm-O1",
"llvm-O2",
"llvm-O0-eggcc",
"llvm-O3",
"llvm-O3-eggcc",
Expand All @@ -32,13 +34,17 @@ def get_eggcc_options(run_mode, benchmark):
case "cranelift-O3":
return f'--run-mode cranelift --optimize-egglog false --optimize-brilift true'
case "llvm-O0":
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm false --llvm-output-dir {llvm_out_dir}'
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm O0 --llvm-output-dir {llvm_out_dir}'
case "llvm-O1":
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm O1 --llvm-output-dir {llvm_out_dir}'
case "llvm-O2":
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm O2 --llvm-output-dir {llvm_out_dir}'
case "llvm-O3":
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm true --llvm-output-dir {llvm_out_dir}'
return f'--run-mode llvm --optimize-egglog false --optimize-bril-llvm O3 --llvm-output-dir {llvm_out_dir}'
case "llvm-O0-eggcc":
return f'--run-mode llvm --optimize-egglog true --optimize-bril-llvm false --llvm-output-dir {llvm_out_dir}'
return f'--run-mode llvm --optimize-egglog true --optimize-bril-llvm O0 --llvm-output-dir {llvm_out_dir}'
case "llvm-O3-eggcc":
return f'--run-mode llvm --optimize-egglog true --optimize-bril-llvm true --llvm-output-dir {llvm_out_dir}'
return f'--run-mode llvm --optimize-egglog true --optimize-bril-llvm O3 --llvm-output-dir {llvm_out_dir}'
case _:
raise Exception("Unexpected run mode: " + run_mode)

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use eggcc::util::{visualize, InterpMode, Run, RunMode, TestProgram};
use eggcc::util::{visualize, InterpMode, LLVMOptLevel, Run, RunMode, TestProgram};
use std::{ffi::OsStr, path::PathBuf};

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -44,7 +44,7 @@ struct Args {
optimize_brilift: Option<bool>,
/// For the LLVM run mode, choose between O0 and O3.
#[clap(long)]
optimize_bril_llvm: Option<bool>,
optimize_bril_llvm: Option<LLVMOptLevel>,
}

fn main() {
Expand Down
149 changes: 69 additions & 80 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ fn get_eggcc_root() -> String {
std::env::var("EGGCC_ROOT").unwrap_or(".".to_string())
}

#[derive(Debug, Clone, ValueEnum, Copy)]
#[clap(rename_all = "verbatim")]
pub enum LLVMOptLevel {
O0,
O1,
O2,
O3,
}

impl Display for LLVMOptLevel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_possible_value().unwrap().get_name())
}
}

/// Different ways to run eggcc
#[derive(Clone, Copy, PartialEq, Eq, Hash, ValueEnum, Debug)]
pub enum RunMode {
Expand Down Expand Up @@ -331,7 +346,7 @@ pub struct Run {
pub output_path: Option<String>,
pub optimize_egglog: Option<bool>,
pub optimize_brilift: Option<bool>,
pub optimize_bril_llvm: Option<bool>,
pub optimize_bril_llvm: Option<LLVMOptLevel>,
}

/// an enum of IRs that can be interpreted
Expand Down Expand Up @@ -457,7 +472,7 @@ impl Run {
#[cfg(feature = "llvm")]
{
for optimize_egglog in [true, false] {
for optimize_brillvm in [true, false] {
for optimize_llvm in [LLVMOptLevel::O0, LLVMOptLevel::O3] {
res.push(Run {
test_type: RunMode::LLVM,
interp: InterpMode::Interp,
Expand All @@ -467,7 +482,7 @@ impl Run {
llvm_output_dir: None,
optimize_egglog: Some(optimize_egglog),
optimize_brilift: None,
optimize_bril_llvm: Some(optimize_brillvm),
optimize_bril_llvm: Some(optimize_llvm),
});
}
}
Expand All @@ -486,15 +501,12 @@ impl Run {
};
}
if self.test_type == RunMode::LLVM {
name += match (
self.optimize_egglog.unwrap(),
self.optimize_bril_llvm.unwrap(),
) {
(false, false) => "-O0",
(true, false) => "-O0-eggcc",
(false, true) => "-O3",
(true, true) => "-O3-eggcc",
let end = match self.optimize_egglog.unwrap() {
false => format!("-{}", self.optimize_bril_llvm.as_ref().unwrap()),
true => format!("-{}-eggcc", self.optimize_bril_llvm.as_ref().unwrap()),
};

name += &end;
}

name
Expand Down Expand Up @@ -574,7 +586,7 @@ impl Run {
let rvsdg = Optimizer::program_to_rvsdg(&self.prog_with_args.program)?;
let cfg = rvsdg.to_cfg();
let bril = cfg.to_bril();
let interpretable = self.run_bril_llvm(bril, false, false)?;
let interpretable = self.run_bril_llvm(bril, false, LLVMOptLevel::O0)?;
(vec![], Some(interpretable))
}
RunMode::DagToRvsdg => {
Expand Down Expand Up @@ -795,7 +807,7 @@ impl Run {
self.prog_with_args.program.clone()
};

for optimize_llvm in [true, false] {
for optimize_llvm in [LLVMOptLevel::O0, LLVMOptLevel::O3] {
let interpretable =
self.run_bril_llvm(resulting_bril.clone(), false, optimize_llvm)?;
let new_interpreted = Optimizer::interp(
Expand Down Expand Up @@ -918,11 +930,11 @@ impl Run {
&self,
input_prog: Program,
optimize_egglog: bool,
optimize_llvm: bool,
llvm_level: LLVMOptLevel,
) -> Result<Interpretable, EggCCError> {
// Make a unique name for this test running bril llvm
// so we don't have conflicts in /tmp
let unique_name = format!("{}_{}_{}", self.name(), optimize_egglog, optimize_llvm);
let unique_name = format!("{}_{}_{}", self.name(), optimize_egglog, llvm_level);

let program = if optimize_egglog {
Run::optimize_bril(&input_prog)?
Expand Down Expand Up @@ -963,80 +975,57 @@ impl Run {
.status()
.unwrap();
}
if optimize_llvm {
expect_command_success(
Command::new("clang-18")
.arg(file_path.clone())
.arg("-O3")
.arg("-fno-vectorize")
.arg("-fno-slp-vectorize")
.arg("-o")
.arg(executable.clone()),
"failed to compile llvm ir",
);

if let Some(output_dir) = &self.llvm_output_dir {
// Emit optimized LLVM IR
expect_command_success(
Command::new("clang-18")
.current_dir(output_dir)
.arg(file_path.clone())
.arg("-O3")
.arg("-fno-vectorize")
.arg("-fno-slp-vectorize")
.arg("-emit-llvm")
.arg("-S")
.arg("-o")
.arg(format!("{}.ll", self.name())),
"failed to compile llvm ir and emit llvm ir",
);
}
let processed = dir.path().join("postprocessed.ll");
// HACK: check if opt-18 exists
// otherwise use opt
// On Linux, sometimes it's called opt-18, while on mac it seems to be just opt
// Also, on some machines, just running `opt-18` hangs, so we pass the version flag
let opt_cmd = if Command::new("opt-18").arg("--version").status().is_ok() {
"opt-18"
} else {
let processed = dir.path().join("postprocessed.ll");
// HACK: check if opt-18 exists
// otherwise use opt
// On Linux, sometimes it's called opt-18, while on mac it seems to be just opt
// Also, on some machines, just running `opt-18` hangs, so we pass the version flag
let opt_cmd = if Command::new("opt-18").arg("--version").status().is_ok() {
"opt-18"
} else {
"opt"
};
"opt"
};

let res = Command::new(opt_cmd)
.arg("-passes=sroa")
.arg("-S")
.arg(file_path.clone())
.arg("-o")
let res = Command::new(opt_cmd)
.arg("-passes=sroa")
.arg("-S")
.arg(file_path.clone())
.arg("-o")
.arg(processed.clone())
.status()
.unwrap();
if !res.success() {
let p1_string = std::fs::read_to_string(file_path.clone()).unwrap();
panic!("Opt failed on following input:\n{p1_string}");
}

expect_command_success(
Command::new("clang-18")
.arg(processed.clone())
.status()
.unwrap();
if !res.success() {
let p1_string = std::fs::read_to_string(file_path.clone()).unwrap();
panic!("Opt failed on following input:\n{p1_string}");
}
.arg(format!("-{}", llvm_level))
.arg("-fno-vectorize")
.arg("-fno-slp-vectorize")
.arg("-o")
.arg(executable.clone()),
"failed to compile llvm ir",
);
if let Some(output_dir) = &self.llvm_output_dir {
expect_command_success(
Command::new("clang-18")
.arg(processed.clone())
.arg("-O0")
.current_dir(output_dir)
.arg(processed)
.arg(format!("-{}", llvm_level))
.arg("-fno-vectorize")
.arg("-fno-slp-vectorize")
.arg("-emit-llvm")
.arg("-S")
.arg("-o")
.arg(executable.clone()),
"failed to compile llvm ir",
.arg(format!("{}.ll", self.name())),
"failed to copy unoptimized llvm ir",
);
if let Some(output_dir) = &self.llvm_output_dir {
expect_command_success(
Command::new("clang-18")
.current_dir(output_dir)
.arg(processed)
.arg("-O0")
.arg("-emit-llvm")
.arg("-S")
.arg("-o")
.arg(format!("{}.ll", self.name())),
"failed to copy unoptimized llvm ir",
);
}
}

let _ = std::fs::write(
executable.clone() + "-args",
self.prog_with_args.args.join(" "),
Expand Down
26 changes: 13 additions & 13 deletions tests/snapshots/files__block-diamond-optimize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ expression: visualization.result
---
@main(v0: int) {
.b1_:
c2_: int = const 2;
v3_: bool = lt v0 c2_;
v4_: bool = not v3_;
c5_: int = const 0;
c6_: int = const 1;
c2_: int = const 1;
c3_: int = const 2;
v4_: bool = lt v0 c3_;
v5_: bool = not v4_;
c6_: int = const 0;
c7_: int = const 5;
v8_: int = id c6_;
v9_: int = id c6_;
v10_: int = id c2_;
br v3_ .b11_ .b12_;
v8_: int = id c2_;
v9_: int = id c2_;
v10_: int = id c3_;
br v4_ .b11_ .b12_;
.b11_:
c13_: int = const 4;
v8_: int = id c13_;
v9_: int = id c6_;
v10_: int = id c2_;
v9_: int = id c2_;
v10_: int = id c3_;
.b12_:
v14_: int = id v8_;
v15_: int = id v9_;
br v4_ .b16_ .b17_;
br v5_ .b16_ .b17_;
.b16_:
v18_: int = add v10_ v8_;
v14_: int = id v18_;
v15_: int = id v9_;
.b17_:
v19_: int = add c6_ v14_;
v19_: int = add c2_ v14_;
print v19_;
}
2 changes: 1 addition & 1 deletion tests/snapshots/files__gamma_condition_and-optimize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: visualization.result
.b1_:
c2_: int = const 0;
v3_: bool = lt v0 c2_;
v4_: bool = lt c2_ v0;
v4_: bool = gt v0 c2_;
c5_: int = const 1;
c6_: int = const 3;
v7_: int = id c6_;
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/files__if_dead_code_nested-optimize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ expression: visualization.result
print v18_;
ret;
.b8_:
v20_: bool = lt c6_ v0;
v20_: bool = gt v0 c6_;
c21_: bool = const false;
c22_: int = const 2;
v23_: int = id c22_;
v24_: bool = id c21_;
v25_: int = id c4_;
br v20_ .b26_ .b27_;
.b26_:
v28_: bool = gt v0 c5_;
v28_: bool = lt c5_ v0;
c29_: int = const 4;
v30_: int = id c29_;
v31_: bool = id c21_;
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/files__sqrt-optimize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ expression: visualization.result
v28_: float = fadd v21_ v27_;
v29_: float = fdiv v28_ v24_;
v30_: float = fdiv v29_ v21_;
v31_: bool = fle v30_ v22_;
v32_: bool = fge v30_ v23_;
v31_: bool = fge v30_ v23_;
v32_: bool = fle v30_ v22_;
v33_: bool = and v31_ v32_;
v34_: bool = not v33_;
v20_: float = id v20_;
Expand Down

0 comments on commit 853f33c

Please sign in to comment.