Merge pull request #1343 from rtfeldman/cfold-rc-improvements

Cfold rc improvements
This commit is contained in:
Richard Feldman 2021-05-26 20:48:46 -04:00 committed by GitHub
commit a1fccdc740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 15 deletions

View File

@ -1101,7 +1101,13 @@ fn test_to_equality<'a>(
cond_layout: &Layout<'a>,
path: &[PathInstruction],
test: Test<'a>,
) -> (StoresVec<'a>, Symbol, Symbol, Layout<'a>) {
) -> (
StoresVec<'a>,
Symbol,
Symbol,
Layout<'a>,
Option<ConstructorKnown<'a>>,
) {
let (rhs_symbol, mut stores, _layout) =
path_to_expr_help(env, cond_symbol, &path, *cond_layout);
@ -1148,6 +1154,11 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Int64),
Some(ConstructorKnown::OnlyPass {
scrutinee: path_symbol,
layout: *cond_layout,
tag_id,
}),
)
}
Test::IsInt(test_int) => {
@ -1162,6 +1173,7 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Int64),
None,
)
}
@ -1177,6 +1189,7 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Float64),
None,
)
}
@ -1192,6 +1205,7 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Int8),
None,
)
}
@ -1205,6 +1219,7 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Int1),
None,
)
}
@ -1219,6 +1234,7 @@ fn test_to_equality<'a>(
lhs_symbol,
rhs_symbol,
Layout::Builtin(Builtin::Str),
None,
)
}
@ -1231,6 +1247,7 @@ type Tests<'a> = std::vec::Vec<(
Symbol,
Symbol,
Layout<'a>,
Option<ConstructorKnown<'a>>,
)>;
fn stores_and_condition<'a>(
@ -1239,7 +1256,7 @@ fn stores_and_condition<'a>(
cond_layout: &Layout<'a>,
test_chain: Vec<(Vec<PathInstruction>, Test<'a>)>,
) -> (Tests<'a>, Option<(Symbol, JoinPointId, Stmt<'a>)>) {
let mut tests = Vec::with_capacity(test_chain.len());
let mut tests: Tests = Vec::with_capacity(test_chain.len());
let mut guard = None;
@ -1444,12 +1461,20 @@ fn compile_tests<'a>(
cond = compile_guard(env, ret_layout, id, arena.alloc(stmt), fail, cond);
}
for (new_stores, lhs, rhs, _layout) in tests.into_iter() {
cond = compile_test(env, ret_layout, new_stores, lhs, rhs, fail, cond);
for (new_stores, lhs, rhs, _layout, opt_constructor_info) in tests.into_iter() {
match opt_constructor_info {
None => {
cond = compile_test(env, ret_layout, new_stores, lhs, rhs, fail, cond);
}
Some(cinfo) => {
cond = compile_test_help(env, cinfo, ret_layout, new_stores, lhs, rhs, fail, cond);
}
}
}
cond
}
#[derive(Debug)]
enum ConstructorKnown<'a> {
Both {
scrutinee: Symbol,
@ -1571,7 +1596,7 @@ fn decide_to_branching<'a>(
// use knowledge about constructors for optimization
debug_assert_eq!(tests.len(), 1);
let (new_stores, lhs, rhs, _layout) = tests.into_iter().next().unwrap();
let (new_stores, lhs, rhs, _layout, _cinfo) = tests.into_iter().next().unwrap();
compile_test_help(
env,
@ -1681,12 +1706,33 @@ fn decide_to_branching<'a>(
// We have learned more about the exact layout of the cond (based on the path)
// but tests are still relative to the original cond symbol
let mut switch = Stmt::Switch {
cond_layout: inner_cond_layout,
cond_symbol: inner_cond_symbol,
branches: branches.into_bump_slice(),
default_branch: (default_branch_info, env.arena.alloc(default_branch)),
ret_layout,
let mut switch = if let Layout::Union(_) = inner_cond_layout {
let tag_id_symbol = env.unique_symbol();
let temp = Stmt::Switch {
cond_layout: Layout::TAG_SIZE,
cond_symbol: tag_id_symbol,
branches: branches.into_bump_slice(),
default_branch: (default_branch_info, env.arena.alloc(default_branch)),
ret_layout,
};
let expr = Expr::AccessAtIndex {
index: 0,
field_layouts: &[Layout::TAG_SIZE],
structure: inner_cond_symbol,
wrapped: Wrapped::MultiTagUnion,
};
Stmt::Let(tag_id_symbol, expr, Layout::TAG_SIZE, env.arena.alloc(temp))
} else {
Stmt::Switch {
cond_layout: inner_cond_layout,
cond_symbol: inner_cond_symbol,
branches: branches.into_bump_slice(),
default_branch: (default_branch_info, env.arena.alloc(default_branch)),
ret_layout,
}
};
for (symbol, layout, expr) in cond_stores_vec.into_iter().rev() {

View File

@ -916,7 +916,14 @@ impl<'a> BranchInfo<'a> {
.append(", tag_id: ")
.append(format!("{}", tag_id))
.append("} "),
_ => alloc.text(""),
_ => {
if PRETTY_PRINT_IR_SYMBOLS {
alloc.text(" <no branch info>")
} else {
alloc.text("")
}
}
}
}
}

View File

@ -16,6 +16,10 @@ const GENERATE_NULLABLE: bool = true;
const DEFAULT_NUM_BUILTIN: Builtin<'_> = Builtin::Int64;
pub const TAG_SIZE: Builtin<'_> = Builtin::Int64;
impl Layout<'_> {
pub const TAG_SIZE: Layout<'static> = Layout::Builtin(Builtin::Int64);
}
#[derive(Copy, Clone)]
#[repr(u8)]
pub enum InPlace {

View File

@ -81,9 +81,9 @@ constFolding = \e ->
when Pair x1 x2 is
Pair (Val a) (Val b) -> Val (a+b)
# Pair (Val a) (Add (Val b) x) -> Add (Val (a+b)) x
Pair (Val a) (Add (Val b) x) -> Add (Val (a+b)) x
Pair (Val a) (Add x (Val b)) -> Add (Val (a+b)) x
Pair _ _ -> Add x1 x2
Pair y1 y2 -> Add y1 y2
Mul e1 e2 ->
x1 = constFolding e1
@ -93,7 +93,7 @@ constFolding = \e ->
Pair (Val a) (Val b) -> Val (a*b)
Pair (Val a) (Mul (Val b) x) -> Mul (Val (a*b)) x
Pair (Val a) (Mul x (Val b)) -> Mul (Val (a*b)) x
Pair _ _ -> Mul x1 x2
Pair y1 y2 -> Add y1 y2
_ -> e