Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

min and max #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bril-rs/brillvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inkwell = { git = "https://github.com/TheDan64/inkwell.git", features = [
"llvm18-0",
], rev = "6c0fb56b3554e939f9ca61b465043d6a84fb7b95" }

bril-rs = { git = "https://github.com/uwplse/bril", features = ["float", "ssa", "memory"] }
bril-rs = { path = "../", features = ["float", "ssa", "memory"] }


# Need to set a default `main` to build `rt` bin
Expand Down
70 changes: 70 additions & 0 deletions bril-rs/brillvm/src/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,76 @@ fn build_instruction<'a, 'b>(
);
}

Instruction::Value {
args,
dest,
funcs: _,
labels: _,
op: ValueOps::Smax,
op_type: _,
} => {
let sel_name = fresh.fresh_var();
let ret_name= fresh.fresh_var();
build_op(
context,
builder,
heap,
fresh,
|v| {
let pred = builder.build_int_compare::<IntValue>(
IntPredicate::SGT,
v[0].try_into().unwrap(),
v[1].try_into().unwrap(),
&sel_name
).unwrap();
builder
.build_select::<BasicValueEnum, IntValue>(
pred,
v[0],
v[1],
&ret_name
).unwrap().into()
},
args,
dest
)
}

Instruction::Value {
args,
dest,
funcs: _,
labels: _,
op: ValueOps::Smin,
op_type: _,
} => {
let sel_name = fresh.fresh_var();
let ret_name= fresh.fresh_var();
build_op(
context,
builder,
heap,
fresh,
|v| {
let pred = builder.build_int_compare::<IntValue>(
IntPredicate::SLT,
v[0].try_into().unwrap(),
v[1].try_into().unwrap(),
&sel_name
).unwrap();
builder
.build_select::<BasicValueEnum, IntValue>(
pred,
v[0],
v[1],
&ret_name
).unwrap().into()
},
args,
dest
)
}

Instruction::Value {
args,
dest,
Expand Down
2 changes: 2 additions & 0 deletions bril-rs/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ impl TryFrom<AbstractInstruction> for Instruction {
"call" => ValueOps::Call,
"id" => ValueOps::Id,
"select" => ValueOps::Select,
"smax" => ValueOps::Smax,
"smin" => ValueOps::Smin,
"sub" => ValueOps::Sub,
#[cfg(feature = "ssa")]
"phi" => ValueOps::Phi,
Expand Down
6 changes: 6 additions & 0 deletions bril-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ pub enum ValueOps {
Id,
/// Select
Select,
/// Signed Max
Smax,
/// Signed Min
Smin,
/// <https://capra.cs.cornell.edu/bril/lang/ssa.html#operations>
#[cfg(feature = "ssa")]
Phi,
Expand Down Expand Up @@ -503,6 +507,8 @@ impl Display for ValueOps {
Self::Call => write!(f, "call"),
Self::Id => write!(f, "id"),
Self::Select => write!(f, "select"),
Self::Smax => write!(f, "smax"),
Self::Smin => write!(f, "smin"),
#[cfg(feature = "ssa")]
Self::Phi => write!(f, "phi"),
#[cfg(feature = "float")]
Expand Down
18 changes: 17 additions & 1 deletion brilift/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,24 @@ impl CompileEnv<'_> {
let thn = builder.use_var(self.vars[&args[1]]);
let els = builder.use_var(self.vars[&args[2]]);
let res = builder.ins().select(cond, thn, els);
builder.def_var(self.vars[dest], res);
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Smax => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().icmp(IntCC::SignedGreaterThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}

bril::ValueOps::Smin => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().icmp(IntCC::SignedLessThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}

bril::ValueOps::Lt
| bril::ValueOps::Le
| bril::ValueOps::Eq
Expand Down
19 changes: 18 additions & 1 deletion brilirs/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn type_check_instruction<'a>(
args,
funcs,
labels,
pos: _
pos: _,
} => {
check_num_args(3, args)?;
check_num_funcs(0, funcs)?;
Expand All @@ -193,6 +193,23 @@ fn type_check_instruction<'a>(
check_asmt_type(&op_type, get_type(env, 2, args)?)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Smax | ValueOps::Smin,
dest,
op_type,
args,
funcs,
labels,
pos: _,
} => {
check_num_args(2, args)?;
check_num_funcs(0, funcs)?;
check_num_labels(0, labels)?;
check_asmt_type(&Type::Int, get_type(env, 0, args)?)?;
check_asmt_type(&Type::Int, get_type(env, 1, args)?)?;
check_asmt_type(&Type::Int, op_type)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Fadd | ValueOps::Fsub | ValueOps::Fmul | ValueOps::Fdiv,
dest,
Expand Down
15 changes: 14 additions & 1 deletion brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ fn execute_value_op<T: std::io::Write>(
) -> Result<(), InterpError> {
use bril_rs::ValueOps::{
Add, Alloc, And, Call, Ceq, Cge, Cgt, Char2int, Cle, Clt, Div, Eq, Fadd, Fdiv, Feq, Fge, Fgt,
Fle, Flt, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi, PtrAdd, Sub, Select
Fle, Flt, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi, PtrAdd, Select,
Smax, Smin, Sub,
};
match op {
Add => {
Expand Down Expand Up @@ -401,6 +402,18 @@ fn execute_value_op<T: std::io::Write>(
let res = if arg0 { arg1 } else { arg2 };
state.env.set(dest, res);
}
Smax => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = if arg0 > arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Int(res));
}
Smin => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = if arg0 < arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Int(res));
}
Fadd => {
let arg0 = get_arg::<f64>(&state.env, 0, args);
let arg1 = get_arg::<f64>(&state.env, 1, args);
Expand Down
Loading