Skip to content

Commit 4901fa0

Browse files
committed
use existing algexpr->expr conversion, adjust tests
1 parent ee1fe14 commit 4901fa0

File tree

6 files changed

+22
-33
lines changed

6 files changed

+22
-33
lines changed

ast/src/parsed/mod.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ use std::{
1515

1616
use auto_enums::auto_enum;
1717
use derive_more::Display;
18-
use powdr_number::{BigInt, BigUint, DegreeType, FieldElement};
18+
use powdr_number::{BigInt, BigUint, DegreeType};
1919
use schemars::JsonSchema;
2020
use serde::{Deserialize, Serialize};
2121

2222
use powdr_parser_util::SourceRef;
2323

24-
use crate::analyzed::{AlgebraicExpression, PolynomialReference, Reference};
24+
use crate::analyzed::Reference;
2525

2626
use self::{
2727
asm::{Part, SymbolPath},
@@ -739,29 +739,6 @@ impl<Ref> From<u32> for Expression<Ref> {
739739
}
740740
}
741741

742-
impl<T: FieldElement> From<AlgebraicExpression<T>> for Expression<Reference> {
743-
fn from(e: AlgebraicExpression<T>) -> Self {
744-
match e {
745-
AlgebraicExpression::Reference(algebraic_reference) => Expression::Reference(
746-
SourceRef::unknown(),
747-
Reference::Poly(PolynomialReference {
748-
name: algebraic_reference.name,
749-
type_args: None,
750-
}),
751-
),
752-
AlgebraicExpression::PublicReference(_) => todo!(),
753-
AlgebraicExpression::Challenge(_) => todo!(),
754-
AlgebraicExpression::Number(n) => Number {
755-
value: n.to_arbitrary_integer(),
756-
type_: Some(Type::Expr),
757-
}
758-
.into(),
759-
AlgebraicExpression::BinaryOperation(_) => todo!(),
760-
AlgebraicExpression::UnaryOperation(_) => todo!(),
761-
}
762-
}
763-
}
764-
765742
pub type ExpressionPrecedence = u64;
766743

767744
impl<Ref> Expression<Ref> {

backend/src/plonky3/stark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ mod tests {
474474
let beta: expr = std::prelude::challenge(0, 42);
475475
col witness x;
476476
col witness stage(1) y;
477-
x = y + beta * alpha;
477+
y = x * x + beta * alpha;
478478
"#;
479479
run_test(content);
480480
}
@@ -498,7 +498,7 @@ mod tests {
498498

499499
#[test]
500500
fn polynomial_identity() {
501-
let content = "namespace Global(8); pol fixed z = [1, 2]*; pol witness a; a = z + 1;";
501+
let content = "namespace Global(8); pol fixed z = [1, 2]*; pol witness a; a = z * z + 1;";
502502
run_test(content);
503503
}
504504

pil-analyzer/src/expressionizer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ pub fn try_value_to_expression<T: FieldElement>(
5858
Expressionizer { poly_id_to_name }.try_value_to_expression(value)
5959
}
6060

61+
/// Tries to convert an algebraic expression to an expression.
62+
pub fn try_algebraic_expression_to_expression<T: FieldElement>(
63+
poly_id_to_name: &BTreeMap<(PolynomialType, u64), String>,
64+
e: &AlgebraicExpression<T>,
65+
) -> Result<Expression, EvalError> {
66+
Expressionizer { poly_id_to_name }.try_algebraic_expression_to_expression(e)
67+
}
68+
6169
struct Expressionizer<'a> {
6270
/// Maps polynomial IDs to their names.
6371
/// For arrays, this does not include the array elements, just the ID

pil-analyzer/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use powdr_ast::{
2626

2727
pub use pil_analyzer::{analyze_ast, analyze_file, analyze_string};
2828

29+
pub use expressionizer::try_algebraic_expression_to_expression;
30+
2931
pub trait AnalysisDriver: Clone + Copy {
3032
/// Turns a declaration into an absolute name.
3133
fn resolve_decl(&self, name: &String) -> String {

pilopt/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ repository = { workspace = true }
1010
[dependencies]
1111
powdr-ast.workspace = true
1212
powdr-number.workspace = true
13+
powdr-pil-analyzer.workspace = true
1314

1415
log = "0.4.17"
1516
pretty_assertions = "1.4.0"
1617
itertools = "0.13.0"
1718

18-
[dev-dependencies]
19-
powdr-pil-analyzer.workspace = true
20-
2119
[lints]
2220
workspace = true
2321

pilopt/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use powdr_number::{BigUint, FieldElement};
1919

2020
pub mod referenced_symbols;
2121

22+
use powdr_pil_analyzer::try_algebraic_expression_to_expression;
2223
use referenced_symbols::{ReferencedSymbols, SymbolReference};
2324

2425
pub fn optimize<T: FieldElement>(mut pil_file: Analyzed<T>) -> Analyzed<T> {
@@ -30,9 +31,7 @@ pub fn optimize<T: FieldElement>(mut pil_file: Analyzed<T>) -> Analyzed<T> {
3031
deduplicate_fixed_columns(&mut pil_file);
3132
simplify_identities(&mut pil_file);
3233
extract_constant_lookups(&mut pil_file);
33-
println!("before {pil_file}");
3434
replace_linear_witness_columns(&mut pil_file);
35-
println!("after {pil_file}");
3635
remove_constant_witness_columns(&mut pil_file);
3736
remove_constant_intermediate_columns(&mut pil_file);
3837
simplify_identities(&mut pil_file);
@@ -545,6 +544,7 @@ fn replace_linear_witness_columns<T: FieldElement>(pil_file: &mut Analyzed<T>) {
545544
..*poly_id
546545
};
547546
new_symbol.kind = SymbolKind::Poly(PolynomialType::Intermediate);
547+
new_symbol.stage = None;
548548
// Add the definition to the intermediate columns
549549
pil_file
550550
.intermediate_columns
@@ -635,6 +635,10 @@ fn substitute_polynomial_references<T: FieldElement>(
635635
pil_file: &mut Analyzed<T>,
636636
substitutions: Vec<((String, PolyID), AlgebraicExpression<T>)>,
637637
) {
638+
let poly_id_to_name = substitutions
639+
.iter()
640+
.map(|((name, id), _)| ((id.ptype, id.id), name.clone()))
641+
.collect();
638642
let substitutions_by_id = substitutions
639643
.iter()
640644
.map(|((_, id), value)| (*id, value.clone()))
@@ -650,7 +654,7 @@ fn substitute_polynomial_references<T: FieldElement>(
650654
) = e
651655
{
652656
if let Some(value) = substitutions_by_name.get(name) {
653-
*e = value.clone().into();
657+
*e = try_algebraic_expression_to_expression(&poly_id_to_name, value).unwrap();
654658
}
655659
}
656660
});

0 commit comments

Comments
 (0)