Skip to content

Commit

Permalink
Limit number of matches consumed from ISLE
Browse files Browse the repository at this point in the history
We generally want to clamp down and avoid runaway behavior here.

But there also seems to be some sort of rustc/llvm bug on Rust 1.71 that is
causing iteration to wild here. This commit avoids that bug.
  • Loading branch information
fitzgen committed Nov 3, 2023
1 parent 000125a commit 77ed10a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cranelift/codegen/src/egraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,35 @@ where
return orig_value;
}
isle_ctx.ctx.rewrite_depth += 1;
trace!(
"Incrementing rewrite depth; now {}",
isle_ctx.ctx.rewrite_depth
);

// Invoke the ISLE toplevel constructor, getting all new
// values produced as equivalents to this value.
trace!("Calling into ISLE with original value {}", orig_value);
isle_ctx.ctx.stats.rewrite_rule_invoked += 1;
let mut optimized_values =
crate::opts::generated_code::constructor_simplify(&mut isle_ctx, orig_value);
trace!(
" -> returned from ISLE, optimized values's size hint = {:?}",
optimized_values.size_hint()
);

// Create a union of all new values with the original (or
// maybe just one new value marked as "subsuming" the
// original, if present.)
let mut i = 0;
let mut union_value = orig_value;
while let Some(optimized_value) = optimized_values.next(&mut isle_ctx) {
i += 1;
const MATCHES_LIMIT: u32 = 5;
if i > MATCHES_LIMIT {
trace!("Reached maximum matches limit; too many optimized values, ignoring rest.");
break;
}

trace!(
"Returned from ISLE for {}, got {:?}",
orig_value,
Expand Down
4 changes: 4 additions & 0 deletions cranelift/isle/isle/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl<'a> Codegen<'a> {
type Context;
type Output;
fn next(&mut self, ctx: &mut Self::Context) -> Option<Self::Output>;
fn size_hint(&self) -> (usize, Option<usize>) {{ (0, None) }}
}}
pub struct ContextIterWrapper<Item, I: Iterator < Item = Item>, C: Context> {{
Expand All @@ -238,6 +239,9 @@ impl<'a> Codegen<'a> {
fn next(&mut self, _ctx: &mut Self::Context) -> Option<Self::Output> {{
self.iter.next()
}}
fn size_hint(&self) -> (usize, Option<usize>) {{
self.iter.size_hint()
}}
}}
"#,
)
Expand Down

0 comments on commit 77ed10a

Please sign in to comment.