-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement remove_dynamic_checks on ullbc
- Loading branch information
Showing
16 changed files
with
269 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//! # Micro-pass: After the `remove_dynamic_checks` pass, the addition, subtraction and | ||
//! multiplication operations can look like (depending on release mode ornot): | ||
//! tuple.0 := copy x + copy y | ||
//! assert(..) | ||
//! result := move (tuple.0) | ||
//! Moreover we know that this tuple isn't used anywhere else. We simplify this to: | ||
//! result := copy x + copy y | ||
//! This is the only remaining use of `assert` in ullbc. | ||
use std::mem; | ||
|
||
use crate::formatter::{Formatter, IntoFormatter}; | ||
use crate::llbc_ast::*; | ||
use crate::translate_ctx::{error_assert_then, error_or_panic, TransCtx}; | ||
use crate::types::*; | ||
use take_mut::take; | ||
|
||
struct RemoveOverflowTuples<'tcx, 'ctx, 'a> { | ||
/// We use the context for error reporting | ||
ctx: &'a mut TransCtx<'tcx, 'ctx>, | ||
} | ||
|
||
impl<'tcx, 'ctx, 'a> MutTypeVisitor for RemoveOverflowTuples<'tcx, 'ctx, 'a> {} | ||
impl<'tcx, 'ctx, 'a> MutExprVisitor for RemoveOverflowTuples<'tcx, 'ctx, 'a> {} | ||
|
||
impl<'tcx, 'ctx, 'a> RemoveOverflowTuples<'tcx, 'ctx, 'a> { | ||
fn simplify(&mut self, s: &mut Statement) { | ||
let RawStatement::Sequence(s0, s1) = &mut s.content else { return }; | ||
let RawStatement::Sequence(s1, s2) = &mut s1.content else { return }; | ||
let s2_inner = if let RawStatement::Sequence(s2, _) = &mut s2.content { | ||
s2 | ||
} else { | ||
s2 | ||
}; | ||
|
||
let RawStatement::Assert(..) = &s1.content else { return }; | ||
|
||
// The `assert` must be preceded by: | ||
// tuple.0 := copy x + copy y | ||
// and followed by: | ||
// result := move (tuple.0) | ||
if let RawStatement::Assign(tuple_field, Rvalue::BinaryOp(BinOp::Add | BinOp::Sub | BinOp::Mul, _, _),) = &mut s0.content | ||
&& let RawStatement::Assign(result, Rvalue::Use(Operand::Move(assign_from))) = &mut s2_inner.content | ||
&& tuple_field == assign_from | ||
&& let [ProjectionElem::Field(FieldProjKind::Tuple(2), p_id)] = tuple_field.projection.as_slice() | ||
&& p_id.index() == 0 | ||
{ | ||
// Set `s0` to `result := copy x + copy y`. | ||
mem::swap(tuple_field, result); | ||
// Set `s1_inner` to `result := copy x + copy y`. | ||
mem::swap(s0, s2_inner); | ||
// Discard `s0` and `s1`. | ||
take(s, |s| { | ||
let (_s0, s1) = s.content.to_sequence(); | ||
let (_s1, s2) = s1.content.to_sequence(); | ||
*s2 | ||
}); | ||
} else { | ||
// The only possible use of `assert` is this. | ||
let span = s0.meta.span; | ||
let fmt_ctx = self.ctx.into_fmt(); | ||
let msg = format!("Overflowing binary operation generated unexpected code:\n{}", s.fmt_with_ctx("", &fmt_ctx)); | ||
error_or_panic!( | ||
self.ctx, | ||
span, | ||
msg, | ||
return | ||
); | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx, 'ctx, 'a> MutAstVisitor for RemoveOverflowTuples<'tcx, 'ctx, 'a> { | ||
fn spawn(&mut self, visitor: &mut dyn FnMut(&mut Self)) { | ||
visitor(self) | ||
} | ||
|
||
fn merge(&mut self) {} | ||
|
||
fn visit_statement(&mut self, s: &mut Statement) { | ||
// These should have been removed in the `remove_dynamic_checks` pass. | ||
error_assert_then!( | ||
self.ctx, | ||
s.meta.span, | ||
!s.content.is_assert(), | ||
return, | ||
"Found an assert which was not simplified" | ||
); | ||
if s.content.is_assign() { | ||
let (_, rv) = s.content.as_assign(); | ||
error_assert_then!( | ||
self.ctx, | ||
s.meta.span, | ||
!rv.is_len(), | ||
return, | ||
"Found an occurrence of Len which was not simplified" | ||
); | ||
} | ||
|
||
// Simplify this statement. | ||
self.simplify(s); | ||
// Continue simplifying. | ||
self.default_visit_raw_statement(&mut s.content); | ||
} | ||
} | ||
|
||
pub fn transform(ctx: &mut TransCtx, funs: &mut FunDecls, globals: &mut GlobalDecls) { | ||
ctx.iter_bodies(funs, globals, |ctx, name, b| { | ||
let fmt_ctx = ctx.into_fmt(); | ||
trace!( | ||
"# About to remove the overflow tuples: {}:\n{}", | ||
name.fmt_with_ctx(&fmt_ctx), | ||
fmt_ctx.format_object(&*b) | ||
); | ||
let mut visitor = RemoveOverflowTuples { ctx }; | ||
visitor.visit_statement(&mut b.body); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters