-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement constant propagation pass
This commit implements a pass which performs constant propagation on a Program, elimintating any variables which are constants by converting any references to constant variables into constant expressions. Likewise, expressions which are constant in all operands are folded into constant values and propagated as well. The resulting Program is simpler and easier to perform local analysis/optimizations on.
- Loading branch information
Showing
6 changed files
with
614 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use miden_diagnostics::SourceSpan; | ||
|
||
use pretty_assertions::assert_eq; | ||
|
||
use crate::{ast::*, transforms::ConstantPropagator}; | ||
|
||
use super::ParseTest; | ||
|
||
#[test] | ||
fn test_constant_propagation() { | ||
let root = r#" | ||
def root | ||
use lib::* | ||
trace_columns: | ||
main: [clk, a, b[2], c] | ||
public_inputs: | ||
inputs: [0] | ||
const A = [2, 4, 6, 8] | ||
const B = [[1, 1], [2, 2]] | ||
integrity_constraints: | ||
enf test_constraint(b) | ||
let x = 2^EXP | ||
let y = A[0..2] | ||
enf a + y[1] = c + (x + 1) | ||
boundary_constraints: | ||
let x = B[0] | ||
enf a.first = x[0] | ||
"#; | ||
let lib = r#" | ||
mod lib | ||
const EXP = 2 | ||
ev test_constraint([b0, b1]): | ||
let x = EXP | ||
let y = 2^x | ||
enf b0 + x = b1 + y | ||
"#; | ||
|
||
let test = ParseTest::new(); | ||
let path = std::env::current_dir().unwrap().join("lib.air"); | ||
test.add_virtual_file(path, lib.to_string()); | ||
|
||
let mut program = match test.parse_program(root) { | ||
Err(err) => { | ||
test.diagnostics.emit(err); | ||
panic!("expected parsing to succeed, see diagnostics for details"); | ||
} | ||
Ok(ast) => ast, | ||
}; | ||
|
||
let pass = ConstantPropagator::new(); | ||
pass.run(&mut program).unwrap(); | ||
|
||
let mut expected = Program::new(ident!(root)); | ||
expected.trace_columns.push(trace_segment!( | ||
0, | ||
"$main", | ||
[(clk, 1), (a, 1), (b, 2), (c, 1)] | ||
)); | ||
expected.public_inputs.insert( | ||
ident!(inputs), | ||
PublicInput::new(SourceSpan::UNKNOWN, ident!(inputs), 0), | ||
); | ||
expected | ||
.constants | ||
.insert(ident!(root, A), constant!(A = [2, 4, 6, 8])); | ||
expected | ||
.constants | ||
.insert(ident!(root, B), constant!(B = [[1, 1], [2, 2]])); | ||
expected | ||
.constants | ||
.insert(ident!(lib, EXP), constant!(EXP = 2)); | ||
// When constant propagation is done, the boundary constraints should look like: | ||
// enf a.first = 1 | ||
expected.boundary_constraints.push(enforce!(eq!( | ||
bounded_access!(a, Boundary::First, Type::Felt), | ||
int!(1) | ||
))); | ||
// When constant propagation is done, the integrity constraints should look like: | ||
// enf test_constraint(b) | ||
// enf a + 4 = c + 5 | ||
expected | ||
.integrity_constraints | ||
.push(enforce!(call!(lib::test_constraint( | ||
access!(b, Type::Vector(2)).into() | ||
)))); | ||
expected.integrity_constraints.push(enforce!(eq!( | ||
add!(access!(a, Type::Felt), int!(4)), | ||
add!(access!(c, Type::Felt), int!(5)) | ||
))); | ||
// The test_constraint function should look like: | ||
// enf b0 + 2 = b1 + 4 | ||
let body = vec![enforce!(eq!( | ||
add!(access!(b0, Type::Felt), int!(2)), | ||
add!(access!(b1, Type::Felt), int!(4)) | ||
))]; | ||
expected.evaluators.insert( | ||
function_ident!(lib, test_constraint), | ||
EvaluatorFunction::new( | ||
SourceSpan::UNKNOWN, | ||
ident!(test_constraint), | ||
vec![trace_segment!(0, "%0", [(b0, 1), (b1, 1)])], | ||
body, | ||
), | ||
); | ||
|
||
assert_eq!(program, expected); | ||
} |
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
Oops, something went wrong.