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

[WIP] Fix PIL optimizer #2054

Closed
wants to merge 3 commits 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
15 changes: 14 additions & 1 deletion pilopt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,23 @@ fn collect_required_symbols<'a, T: FieldElement>(
.values()
.map(|p| SymbolReference::from(&p.polynomial.name)),
);

let poly_ref_to_id = pil_file
.definitions
.values()
.filter_map(|(symbol, _)| matches!(symbol.kind, SymbolKind::Poly(_)).then_some(symbol))
.flat_map(|symbol| symbol.array_elements())
.collect::<BTreeMap<_, _>>();

for fun in &pil_file.prover_functions {
for e in fun.all_children() {
if let Expression::Reference(_, Reference::Poly(poly_ref)) = e {
required_names.insert(SymbolReference::from(poly_ref));
let symbol_ref = match poly_ref_to_id.get(&poly_ref.name) {
Some(poly_id) => poly_id_to_definition_name[poly_id].into(),
None => SymbolReference::from(poly_ref),
};

required_names.insert(symbol_ref);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions pilopt/tests/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ fn enum_ref_by_trait() {
}

#[test]
#[should_panic = "Symbol not found: N::x[0]"]
fn handle_array_references_in_prover_functions() {
// Reproduces https://github.com/powdr-labs/powdr/issues/2051
let input = r#"namespace N(8);
col witness x[1];

Expand All @@ -300,5 +298,16 @@ fn handle_array_references_in_prover_functions() {
}
};
"#;
optimize(analyze_string::<GoldilocksField>(input).unwrap()).to_string();
let expectation = r#"namespace N(8);
col witness x[1];
N::x[0]' = N::x[0] + 1;
{
let intermediate = N::x[0] + 1_expr;
query |i| {
let _: expr = intermediate;
}
};
"#;
let optimized = optimize(analyze_string::<GoldilocksField>(input).unwrap()).to_string();
assert_eq!(optimized, expectation);
}