Skip to content

Commit

Permalink
fix: conditions not flattening in non-generated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hoffman committed Oct 9, 2023
1 parent 5353e2f commit 2d17366
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
1 change: 0 additions & 1 deletion circom/tests/loops/inner_conditional_10.circom
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pragma circom 2.0.0;
// REQUIRES: circom
// RUN: rm -rf %t && mkdir %t && %circom --llvm -o %t %s | sed -n 's/.*Written successfully:.* \(.*\)/\1/p' | xargs cat | FileCheck %s --enable-var-scope
// XFAIL:.* // TODO: in addition to not extracting to a new function, branch conditions are not flattened for some reason which causes a panic in StoreBucket

template Sigma() {
signal input inp;
Expand Down
37 changes: 23 additions & 14 deletions circuit_passes/src/passes/conditional_flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ impl<'d> ConditionalFlatteningPass<'d> {
caller_context: RefCell::new(None),
}
}

fn get_known_condition(&self, bucket_id: &BucketId) -> Option<bool> {
// Get from the current 'caller_context' or lookup via None key in 'evaluated_conditions'
let ec = self.evaluated_conditions.borrow();
if let Some(bv) = self.caller_context.borrow().as_ref().or_else(|| ec.get(&None)) {
if let Some(Some(side)) = bv.get(bucket_id) {
return Some(*side);
}
}
None
}
}

impl InterpreterObserver for ConditionalFlatteningPass<'_> {
Expand Down Expand Up @@ -248,20 +259,18 @@ impl CircuitTransformationPass for ConditionalFlatteningPass<'_> {
}

fn transform_branch_bucket(&self, bucket: &BranchBucket) -> InstructionPointer {
if let Some(bv) = self.caller_context.borrow().as_ref() {
if let Some(Some(side)) = bv.get(&bucket.id) {
let code = if *side { &bucket.if_branch } else { &bucket.else_branch };
let block = BlockBucket {
id: new_id(),
source_file_id: bucket.source_file_id,
line: bucket.line,
message_id: bucket.message_id,
body: code.clone(),
n_iters: 1,
label: format!("fold_{}", side),
};
return self.transform_block_bucket(&block);
}
if let Some(side) = self.get_known_condition(&bucket.id) {
let code = if side { &bucket.if_branch } else { &bucket.else_branch };
let block = BlockBucket {
id: new_id(),
source_file_id: bucket.source_file_id,
line: bucket.line,
message_id: bucket.message_id,
body: code.clone(),
n_iters: 1,
label: format!("fold_{}", side),
};
return self.transform_block_bucket(&block);
}
// Default case: no change
BranchBucket {
Expand Down

0 comments on commit 2d17366

Please sign in to comment.