Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Rutefig/274 using m type to keep debugsym in expr #297

Closed
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
47 changes: 25 additions & 22 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use num_bigint::BigInt;

use crate::{
field::Field,
frontend::dsl::trace::DSLTraceGenerator,
interpreter::InterpreterTraceGenerator,
parser::{
ast::{
Expand Down Expand Up @@ -269,6 +270,8 @@ impl<F: Field + Hash> Compiler<F> {
sbpir.machines.insert(machine_name.clone(), sbpir_machine);
}

let sbpir = sbpir.transform_metadata(|_| ());

sbpir.without_trace()
}

Expand All @@ -279,7 +282,7 @@ impl<F: Field + Hash> Compiler<F> {
setup: &MachineSetup<F>,
machine_name: &str,
state_id: &str,
) -> Vec<Constraint<F, ()>> {
) -> Vec<Constraint<F, DebugSymRef>> {
let exprs = setup.get_poly_constraints(state_id).unwrap();

exprs
Expand All @@ -300,40 +303,40 @@ impl<F: Field + Hash> Compiler<F> {
symbols: &SymTable,
machine_name: &str,
state_id: &str,
expr: &Expr<F, Identifier, ()>,
) -> Expr<F, Queriable<F>, ()> {
expr: &Expr<F, Identifier, DebugSymRef>,
) -> Expr<F, Queriable<F>, DebugSymRef> {
use Expr::*;
match expr {
Const(v, _) => Const(*v, ()),
Sum(ses, _) => Sum(
Const(v, dsym) => Const(*v, dsym.clone()),
Sum(ses, dsym) => Sum(
ses.iter()
.map(|se| self.translate_queries_expr(symbols, machine_name, state_id, se))
.collect(),
(),
dsym.clone(),
),
Mul(ses, _) => Mul(
Mul(ses, dsym) => Mul(
ses.iter()
.map(|se| self.translate_queries_expr(symbols, machine_name, state_id, se))
.collect(),
(),
dsym.clone(),
),
Neg(se, _) => Neg(
Neg(se, dsym) => Neg(
Box::new(self.translate_queries_expr(symbols, machine_name, state_id, se.as_ref())),
(),
dsym.clone(),
),
Pow(se, exp, _) => Pow(
Pow(se, exp, dsym) => Pow(
Box::new(self.translate_queries_expr(symbols, machine_name, state_id, se.as_ref())),
*exp,
(),
dsym.clone(),
),
MI(se, _) => MI(
MI(se, dsym) => MI(
Box::new(self.translate_queries_expr(symbols, machine_name, state_id, se.as_ref())),
(),
dsym.clone(),
),
Halo2Expr(se, _) => Halo2Expr(se.clone(), ()),
Query(id, _) => Query(
Halo2Expr(se, dsym) => Halo2Expr(se.clone(), dsym.clone()),
Query(id, dsym) => Query(
self.translate_query(symbols, machine_name, state_id, id),
(),
dsym.clone(),
),
}
}
Expand Down Expand Up @@ -428,10 +431,10 @@ impl<F: Field + Hash> Compiler<F> {
machine_name: &str,
machine_setup: &MachineSetup<F>,
state_id: &str,
) -> StepType<F, ()> {
) -> StepType<F, DebugSymRef> {
let handler = self.mapping.get_step_type_handler(machine_name, state_id);

let mut step_type: StepType<F, ()> =
let mut step_type: StepType<F, DebugSymRef> =
StepType::new(handler.uuid(), handler.annotation.to_string());

self.add_internal_signals(symbols, machine_name, &mut step_type, state_id);
Expand All @@ -448,7 +451,7 @@ impl<F: Field + Hash> Compiler<F> {
&mut self,
symbols: &SymTable,
machine_name: &str,
step_type: &mut StepType<F, ()>,
step_type: &mut StepType<F, DebugSymRef>,
state_id: &str,
) {
let internal_ids = self.get_all_internals(symbols, machine_name, state_id);
Expand All @@ -467,7 +470,7 @@ impl<F: Field + Hash> Compiler<F> {

fn add_step_type_handlers(
&mut self,
machine: &mut SBPIRMachine<F>,
machine: &mut SBPIRMachine<F, DSLTraceGenerator<F>, DebugSymRef>,
symbols: &SymTable,
machine_name: &str,
) {
Expand Down Expand Up @@ -497,7 +500,7 @@ impl<F: Field + Hash> Compiler<F> {

fn add_forward_signals(
&mut self,
machine: &mut SBPIRMachine<F>,
machine: &mut SBPIRMachine<F, DSLTraceGenerator<F>, DebugSymRef>,
symbols: &SymTable,
machine_name: &str,
) {
Expand Down
78 changes: 45 additions & 33 deletions src/compiler/compiler_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,16 @@ impl<F: Field + Hash> CompilerLegacy<F> {
setup
.iter()
.map(|(machine_id, machine)| {
let poly_constraints: HashMap<String, Vec<Expr<F, Identifier, ()>>> = machine
.poly_constraints_iter()
.map(|(step_id, step)| {
let new_step: Vec<Expr<F, Identifier, ()>> =
step.iter().map(|pi| Self::map_pi_consts(pi)).collect();
let poly_constraints: HashMap<String, Vec<Expr<F, Identifier, DebugSymRef>>> =
machine
.poly_constraints_iter()
.map(|(step_id, step)| {
let new_step: Vec<Expr<F, Identifier, DebugSymRef>> =
step.iter().map(|pi| Self::map_pi_consts(pi)).collect();

(step_id.clone(), new_step)
})
.collect();
(step_id.clone(), new_step)
})
.collect();

let new_machine: MachineSetup<F> =
machine.replace_poly_constraints(poly_constraints);
Expand All @@ -276,17 +277,25 @@ impl<F: Field + Hash> CompilerLegacy<F> {
.collect()
}

fn map_pi_consts(expr: &Expr<BigInt, Identifier, ()>) -> Expr<F, Identifier, ()> {
fn map_pi_consts(
expr: &Expr<BigInt, Identifier, DebugSymRef>,
) -> Expr<F, Identifier, DebugSymRef> {
use Expr::*;
match expr {
Const(v, _) => Const(F::from_big_int(v), ()),
Sum(ses, _) => Sum(ses.iter().map(|se| Self::map_pi_consts(se)).collect(), ()),
Mul(ses, _) => Mul(ses.iter().map(|se| Self::map_pi_consts(se)).collect(), ()),
Neg(se, _) => Neg(Box::new(Self::map_pi_consts(se)), ()),
Pow(se, exp, _) => Pow(Box::new(Self::map_pi_consts(se)), *exp, ()),
Query(q, _) => Query(q.clone(), ()),
Const(v, dsym) => Const(F::from_big_int(v), dsym.clone()),
Sum(ses, dsym) => Sum(
ses.iter().map(|se| Self::map_pi_consts(se)).collect(),
dsym.clone(),
),
Mul(ses, dsym) => Mul(
ses.iter().map(|se| Self::map_pi_consts(se)).collect(),
dsym.clone(),
),
Neg(se, dsym) => Neg(Box::new(Self::map_pi_consts(se)), dsym.clone()),
Pow(se, exp, dsym) => Pow(Box::new(Self::map_pi_consts(se)), *exp, dsym.clone()),
Query(q, dsym) => Query(q.clone(), dsym.clone()),
Halo2Expr(_, _) => todo!(),
MI(se, _) => MI(Box::new(Self::map_pi_consts(se)), ()),
MI(se, dsym) => MI(Box::new(Self::map_pi_consts(se)), dsym.clone()),
}
}

Expand Down Expand Up @@ -316,7 +325,7 @@ impl<F: Field + Hash> CompilerLegacy<F> {
poly_constraints.iter().for_each(|poly| {
let constraint = Constraint {
annotation: format!("{:?}", poly),
expr: poly.clone(),
expr: poly.transform_meta(|_| ()),
typing: Typing::AntiBooly,
};
ctx.constr(constraint);
Expand Down Expand Up @@ -372,7 +381,7 @@ impl<F: Field + Hash> CompilerLegacy<F> {
setup: &Setup<F>,
machine_id: &str,
state_id: &str,
) -> Vec<Expr<F, Queriable<F>, ()>> {
) -> Vec<Expr<F, Queriable<F>, DebugSymRef>> {
let exprs = setup
.get(machine_id)
.unwrap()
Expand All @@ -390,38 +399,41 @@ impl<F: Field + Hash> CompilerLegacy<F> {
symbols: &SymTable,
machine_id: &str,
state_id: &str,
expr: &Expr<F, Identifier, ()>,
) -> Expr<F, Queriable<F>, ()> {
expr: &Expr<F, Identifier, DebugSymRef>,
) -> Expr<F, Queriable<F>, DebugSymRef> {
use Expr::*;
match expr {
Const(v, _) => Const(*v, ()),
Sum(ses, _) => Sum(
Const(v, dsym) => Const(*v, dsym.clone()),
Sum(ses, dsym) => Sum(
ses.iter()
.map(|se| self.translate_queries_expr(symbols, machine_id, state_id, se))
.collect(),
(),
dsym.clone(),
),
Mul(ses, _) => Mul(
Mul(ses, dsym) => Mul(
ses.iter()
.map(|se| self.translate_queries_expr(symbols, machine_id, state_id, se))
.collect(),
(),
dsym.clone(),
),
Neg(se, _) => Neg(
Neg(se, dsym) => Neg(
Box::new(self.translate_queries_expr(symbols, machine_id, state_id, se.as_ref())),
(),
dsym.clone(),
),
Pow(se, exp, _) => Pow(
Pow(se, exp, dsym) => Pow(
Box::new(self.translate_queries_expr(symbols, machine_id, state_id, se.as_ref())),
*exp,
(),
dsym.clone(),
),
MI(se, _) => MI(
MI(se, dsym) => MI(
Box::new(self.translate_queries_expr(symbols, machine_id, state_id, se.as_ref())),
(),
dsym.clone(),
),
Halo2Expr(se, dsym) => Halo2Expr(se.clone(), dsym.clone()),
Query(id, dsym) => Query(
self.translate_query(symbols, machine_id, state_id, id),
dsym.clone(),
),
Halo2Expr(se, _) => Halo2Expr(se.clone(), ()),
Query(id, _) => Query(self.translate_query(symbols, machine_id, state_id, id), ()),
}
}

Expand Down
49 changes: 29 additions & 20 deletions src/compiler/setup_inter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(super) fn interpret(ast: &[TLDecl<BigInt, Identifier>], _symbols: &SymTable)
pub(super) type Setup<F> = HashMap<String, MachineSetup<F>>;

pub(super) struct MachineSetup<F> {
poly_constraints: HashMap<String, Vec<Expr<F, Identifier, ()>>>,
poly_constraints: HashMap<String, Vec<Expr<F, Identifier, DebugSymRef>>>,

input_signals: Vec<TypedIdDecl<Identifier>>,
output_signals: Vec<TypedIdDecl<Identifier>>,
Expand All @@ -49,10 +49,10 @@ impl<F> Default for MachineSetup<F> {
}
impl MachineSetup<BigInt> {
pub(crate) fn map_consts<F: Field>(&self) -> MachineSetup<F> {
let poly_constraints: HashMap<String, Vec<Expr<F, Identifier, ()>>> = self
let poly_constraints: HashMap<String, Vec<Expr<F, Identifier, DebugSymRef>>> = self
.poly_constraints_iter()
.map(|(step_id, step)| {
let new_step: Vec<Expr<F, Identifier, ()>> = step
let new_step: Vec<Expr<F, Identifier, DebugSymRef>> = step
.iter()
.map(|pi| Self::convert_const_to_field(pi))
.collect();
Expand All @@ -66,28 +66,32 @@ impl MachineSetup<BigInt> {
}

fn convert_const_to_field<F: Field>(
expr: &Expr<BigInt, Identifier, ()>,
) -> Expr<F, Identifier, ()> {
expr: &Expr<BigInt, Identifier, DebugSymRef>,
) -> Expr<F, Identifier, DebugSymRef> {
use Expr::*;
match expr {
Const(v, _) => Const(F::from_big_int(v), ()),
Sum(ses, _) => Sum(
Const(v, dsym) => Const(F::from_big_int(v), dsym.clone()),
Sum(ses, dsym) => Sum(
ses.iter()
.map(|se| Self::convert_const_to_field(se))
.collect(),
(),
dsym.clone(),
),
Mul(ses, _) => Mul(
Mul(ses, dsym) => Mul(
ses.iter()
.map(|se| Self::convert_const_to_field(se))
.collect(),
(),
dsym.clone(),
),
Neg(se, _) => Neg(Box::new(Self::convert_const_to_field(se)), ()),
Pow(se, exp, _) => Pow(Box::new(Self::convert_const_to_field(se)), *exp, ()),
Query(q, _) => Query(q.clone(), ()),
Neg(se, dsym) => Neg(Box::new(Self::convert_const_to_field(se)), dsym.clone()),
Pow(se, exp, dsym) => Pow(
Box::new(Self::convert_const_to_field(se)),
*exp,
dsym.clone(),
),
Query(q, dsym) => Query(q.clone(), dsym.clone()),
Halo2Expr(_, _) => todo!(),
MI(se, _) => MI(Box::new(Self::convert_const_to_field(se)), ()),
MI(se, dsym) => MI(Box::new(Self::convert_const_to_field(se)), dsym.clone()),
}
}
}
Expand Down Expand Up @@ -125,7 +129,7 @@ impl<F: Clone> MachineSetup<F> {
fn add_poly_constraints<S: Into<String>>(
&mut self,
state: S,
poly_constraints: Vec<Expr<F, Identifier, ()>>,
poly_constraints: Vec<Expr<F, Identifier, DebugSymRef>>,
) {
self.poly_constraints
.get_mut(&state.into())
Expand All @@ -135,13 +139,13 @@ impl<F: Clone> MachineSetup<F> {

pub(super) fn poly_constraints_iter(
&self,
) -> std::collections::hash_map::Iter<String, Vec<Expr<F, Identifier, ()>>> {
) -> std::collections::hash_map::Iter<String, Vec<Expr<F, Identifier, DebugSymRef>>> {
self.poly_constraints.iter()
}

pub(super) fn replace_poly_constraints<F2>(
&self,
poly_constraints: HashMap<String, Vec<Expr<F2, Identifier, ()>>>,
poly_constraints: HashMap<String, Vec<Expr<F2, Identifier, DebugSymRef>>>,
) -> MachineSetup<F2> {
MachineSetup {
poly_constraints,
Expand All @@ -157,7 +161,7 @@ impl<F: Clone> MachineSetup<F> {
pub(super) fn get_poly_constraints<S: Into<String>>(
&self,
state: S,
) -> Option<&Vec<Expr<F, Identifier, ()>>> {
) -> Option<&Vec<Expr<F, Identifier, DebugSymRef>>> {
self.poly_constraints.get(&state.into())
}
}
Expand Down Expand Up @@ -253,10 +257,15 @@ impl SetupInterpreter {
HyperTransition(_, _, _, _) => todo!("Implement compilation for hyper transitions"),
};

self.add_poly_constraints(result.into_iter().map(|cr| cr.anti_booly).collect());
self.add_poly_constraints(
result
.into_iter()
.map(|cr| cr.anti_booly.transform_meta(|_| cr.dsym.clone()))
.collect(),
);
}

fn add_poly_constraints(&mut self, pis: Vec<Expr<BigInt, Identifier, ()>>) {
fn add_poly_constraints(&mut self, pis: Vec<Expr<BigInt, Identifier, DebugSymRef>>) {
self.setup
.get_mut(&self.current_machine)
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/poly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ impl<F: Clone, V: Clone, M> Expr<F, V, M> {
}
}

impl<F: Field + Hash, V: Debug + Clone + Eq + Hash, M: Clone> Expr<F, V, M> {
impl<F: Clone, V: Debug + Clone + Eq, M: Clone> Expr<F, V, M> {
pub fn transform_meta<N: Clone, ApplyMetaFn>(&self, apply_meta: ApplyMetaFn) -> Expr<F, V, N>
where
ApplyMetaFn: Fn(&Expr<F, V, M>) -> N + Clone,
{
let new_meta = apply_meta(self);
match self {
Expr::Const(v, _) => Expr::Const(*v, new_meta),
Expr::Const(v, _) => Expr::Const(v.clone(), new_meta),
Expr::Sum(ses, _) => Expr::Sum(
ses.iter()
.map(|e| e.transform_meta(apply_meta.clone()))
Expand Down
Loading
Loading