Skip to content

Commit

Permalink
apply auto-formatting (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hoffman authored Aug 22, 2023
1 parent 8f93b2e commit 58c3c94
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 235 deletions.
16 changes: 12 additions & 4 deletions circom/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ fn main() {
let path = entry.unwrap();
create_dir_all(Path::new(&out_dir).join(path.parent().unwrap())).unwrap();
copy(path.clone(), Path::new(&out_dir).join(path.clone())).unwrap();
let test_name = path.to_str().unwrap()
let test_name = path
.to_str()
.unwrap()
.replace("/", "_")
.replace(".circom", "")
.replace("-", "_")
.to_lowercase();

test_code = format!("
test_code = format!(
"
{}
#[test]
fn {}() -> LitResult<()> {{
lit_test(include_str!(\"{}\"), \"{}\")
}}", test_code, test_name, path.to_str().unwrap(), test_name);
}}",
test_code,
test_name,
path.to_str().unwrap(),
test_name
);
}

generate_file(&dest_path, test_code.as_bytes());
Expand All @@ -35,4 +43,4 @@ fn main() {
fn generate_file<P: AsRef<Path>>(path: P, text: &[u8]) {
let mut f = File::create(path).unwrap();
f.write_all(text).unwrap()
}
}
307 changes: 218 additions & 89 deletions circuit_passes/src/bucket_interpreter/mod.rs

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions circuit_passes/src/bucket_interpreter/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ pub fn compute_operation(bucket: &ComputeBucket, stack: &Vec<Value>, p: &BigInt)
OperatorType::BitOr => resolve_operation(value::bit_or_value, p, &stack),
OperatorType::BitAnd => resolve_operation(value::bit_and_value, p, &stack),
OperatorType::BitXor => resolve_operation(value::bit_xor_value, p, &stack),
OperatorType::PrefixSub => {
value::prefix_sub(&stack[0], p)
}
OperatorType::PrefixSub => value::prefix_sub(&stack[0], p),
OperatorType::BoolNot => KnownU32((!stack[0].to_bool(p)).into()),
OperatorType::Complement => {
value::complement(&stack[0], p)
}
OperatorType::Complement => value::complement(&stack[0], p),
OperatorType::ToAddress => value::to_address(&stack[0]),
OperatorType::MulAddress => stack.iter().fold(KnownU32(1), value::mul_address),
OperatorType::AddAddress => stack.iter().fold(KnownU32(0), value::add_address),
Expand All @@ -50,7 +46,7 @@ pub fn compute_offset(indexes: &Vec<usize>, lengths: &Vec<usize>) -> usize {
}
let mut total_offset = indexes.last().copied().expect("must contain some indexes!");
let mut size_multiplier = lengths.last().copied().expect("must contain some array lengths!");
for i in (0..lengths.len()-1).rev() {
for i in (0..lengths.len() - 1).rev() {
total_offset += indexes[i] * size_multiplier;
size_multiplier *= lengths[i];
}
Expand Down Expand Up @@ -84,11 +80,11 @@ mod test {
fn test_increments() {
let lengths = vec![5, 7];
for i in 0..lengths[0] {
for j in 0..lengths[1]-1 {
for j in 0..lengths[1] - 1 {
let offset = compute_offset(&vec![i, j], &lengths);
let next_offset = compute_offset(&vec![i, j + 1], &lengths);
assert_eq!(offset + 1, next_offset);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ impl InterpreterObserver for DeterministicSubCmpInvokePass {

fn on_call_bucket(&self, bucket: &CallBucket, env: &Env) -> bool {
match &bucket.return_info {
ReturnType::Intermediate {..} => true,
ReturnType::Intermediate { .. } => true,
ReturnType::Final(data) => {
self.try_resolve_input_status(&data.dest_address_type, env);
true
},
}
}
}

Expand Down
56 changes: 35 additions & 21 deletions circuit_passes/src/passes/mapped_to_indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ pub struct MappedToIndexedPass {

impl MappedToIndexedPass {
pub fn new(prime: &String) -> Self {
MappedToIndexedPass { memory: PassMemory::new_cell(prime, "".to_string(), Default::default()), replacements: Default::default() }
MappedToIndexedPass {
memory: PassMemory::new_cell(prime, "".to_string(), Default::default()),
replacements: Default::default(),
}
}

fn transform_mapped_loc_to_indexed_loc(&self,
cmp_address: &InstructionPointer, indexes: &Vec<InstructionPointer>, signal_code: usize, env: &Env) -> LocationRule {

fn transform_mapped_loc_to_indexed_loc(
&self,
cmp_address: &InstructionPointer,
indexes: &Vec<InstructionPointer>,
signal_code: usize,
env: &Env,
) -> LocationRule {
let mem = self.memory.borrow();
let interpreter = mem.build_interpreter(self);

let (resolved_addr, acc_env) = interpreter.execute_instruction(cmp_address, env.clone(), false);
let (resolved_addr, acc_env) =
interpreter.execute_instruction(cmp_address, env.clone(), false);

let resolved_addr = resolved_addr
.expect("cmp_address instruction in SubcmpSignal must produce a value!")
Expand Down Expand Up @@ -60,24 +68,30 @@ impl MappedToIndexedPass {
}
}

fn maybe_transform_location_rule(&self, address: &AddressType, location: &LocationRule, env: &Env) -> bool {
fn maybe_transform_location_rule(
&self,
address: &AddressType,
location: &LocationRule,
env: &Env,
) -> bool {
match address {
AddressType::Variable | AddressType::Signal => {
match location {
LocationRule::Indexed { .. } => true,
LocationRule::Mapped { .. } => unreachable!()
}
AddressType::Variable | AddressType::Signal => match location {
LocationRule::Indexed { .. } => true,
LocationRule::Mapped { .. } => unreachable!(),
},
AddressType::SubcmpSignal { cmp_address, .. } => {
match location {
LocationRule::Indexed { .. } => true,
LocationRule::Mapped { indexes, signal_code } => {
let indexed_rule = self.transform_mapped_loc_to_indexed_loc(cmp_address, indexes, *signal_code, env);
self.replacements.borrow_mut().insert(location.clone(), indexed_rule);
true
}
AddressType::SubcmpSignal { cmp_address, .. } => match location {
LocationRule::Indexed { .. } => true,
LocationRule::Mapped { indexes, signal_code } => {
let indexed_rule = self.transform_mapped_loc_to_indexed_loc(
cmp_address,
indexes,
*signal_code,
env,
);
self.replacements.borrow_mut().insert(location.clone(), indexed_rule);
true
}
}
},
}
}
}
Expand Down Expand Up @@ -177,7 +191,7 @@ impl CircuitTransformationPass for MappedToIndexedPass {
location: self.transform_instruction(location),
template_header: template_header.clone(),
},
LocationRule::Mapped { .. } => unreachable!()
LocationRule::Mapped { .. } => unreachable!(),
}
}

Expand Down
9 changes: 3 additions & 6 deletions code_producers/src/llvm_elements/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,7 @@ pub fn create_br<'a>(producer: &dyn LLVMIRProducer<'a>, bb: BasicBlock<'a>) -> A
producer.llvm().builder.build_unconditional_branch(bb).as_any_value_enum()
}

pub fn find_function<'a>(
producer: &dyn LLVMIRProducer<'a>,
name: &str,
) -> FunctionValue<'a> {
pub fn find_function<'a>(producer: &dyn LLVMIRProducer<'a>, name: &str) -> FunctionValue<'a> {
producer
.llvm()
.module
Expand Down Expand Up @@ -772,7 +769,7 @@ pub fn pointer_cast_with_name<'a>(
producer: &dyn LLVMIRProducer<'a>,
from: PointerValue<'a>,
to: PointerType<'a>,
name: &str
name: &str,
) -> PointerValue<'a> {
producer.builder().build_pointer_cast(from, to, name)
}
Expand All @@ -792,4 +789,4 @@ pub fn create_switch<'a>(
cases: &[(IntValue<'a>, BasicBlock<'a>)],
) -> InstructionValue<'a> {
producer.builder().build_switch(value, else_block, cases)
}
}
25 changes: 13 additions & 12 deletions code_producers/src/llvm_elements/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl<'a, 'b> LLVMIRProducer<'a> for TemplateLLVMIRProducer<'a, 'b> {
}

fn get_template_mem_arg(&self, run_fn: FunctionValue<'a>) -> ArrayValue<'a> {
run_fn.get_nth_param(self.template_ctx.signals_arg_offset as u32).unwrap().into_array_value()
run_fn
.get_nth_param(self.template_ctx.signals_arg_offset as u32)
.unwrap()
.into_array_value()
}
}

Expand Down Expand Up @@ -125,7 +128,7 @@ impl<'a> TemplateCtx<'a> {
pub fn load_subcmp(
&self,
producer: &dyn LLVMIRProducer<'a>,
id: AnyValueEnum<'a>
id: AnyValueEnum<'a>,
) -> PointerValue<'a> {
create_gep(producer, self.subcmps, &[zero(producer), id.into_int_value()])
.into_pointer_value()
Expand All @@ -137,8 +140,12 @@ impl<'a> TemplateCtx<'a> {
producer: &dyn LLVMIRProducer<'a>,
id: AnyValueEnum<'a>,
) -> PointerValue<'a> {
let signals = create_gep(producer, self.subcmps, &[zero(producer), id.into_int_value(), zero(producer)])
.into_pointer_value();
let signals = create_gep(
producer,
self.subcmps,
&[zero(producer), id.into_int_value(), zero(producer)],
)
.into_pointer_value();
create_load(producer, signals).into_pointer_value()
}

Expand Down Expand Up @@ -167,10 +174,7 @@ impl<'a> TemplateCtx<'a> {
}

/// Returns a pointer to the signal array
pub fn get_signal_array(
&self,
_producer: &dyn LLVMIRProducer<'a>,
) -> AnyValueEnum<'a> {
pub fn get_signal_array(&self, _producer: &dyn LLVMIRProducer<'a>) -> AnyValueEnum<'a> {
let signals = self.current_function.get_nth_param(self.signals_arg_offset as u32).unwrap();
signals.into_pointer_value().into()
}
Expand All @@ -186,10 +190,7 @@ impl<'a> BodyCtx<'a> for TemplateCtx<'a> {
create_gep(producer, self.stack, &[zero(producer), index])
}

fn get_variable_array(
&self,
_producer: &dyn LLVMIRProducer<'a>,
) -> AnyValueEnum<'a> {
fn get_variable_array(&self, _producer: &dyn LLVMIRProducer<'a>) -> AnyValueEnum<'a> {
self.stack.into()
}
}
Expand Down
20 changes: 14 additions & 6 deletions compiler/src/intermediate_representation/block_bucket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use code_producers::llvm_elements::{LLVMInstruction, LLVMIRProducer};
use crate::intermediate_representation::{BucketId, Instruction, InstructionList, InstructionPointer, new_id, SExp, ToSExp, UpdateId};
use crate::intermediate_representation::{
BucketId, Instruction, InstructionList, InstructionPointer, new_id, SExp, ToSExp, UpdateId,
};
use crate::intermediate_representation::ir_interface::{Allocate, IntoInstruction, ObtainMeta};
use crate::translating_traits::WriteLLVMIR;

Expand All @@ -10,7 +12,7 @@ pub struct BlockBucket {
pub line: usize,
pub message_id: usize,
pub body: InstructionList,
pub n_iters: usize
pub n_iters: usize,
}

impl IntoInstruction for BlockBucket {
Expand Down Expand Up @@ -46,7 +48,10 @@ impl ToString for BlockBucket {
body = format!("{} - {};\n", body, i.to_string());
}
body = format!("{}]", body);
format!("BLOCK(line:{},template_id:{},n_iterations:{},body:{})", line, template_id, self.n_iters, body)
format!(
"BLOCK(line:{},template_id:{},n_iterations:{},body:{})",
line, template_id, self.n_iters, body
)
}
}

Expand All @@ -57,7 +62,7 @@ impl ToSExp for BlockBucket {
SExp::Atom(format!("line:{}", self.line)),
SExp::Atom(format!("template_id:{}", self.message_id)),
SExp::Atom(format!("n_iterations:{}", self.n_iters)),
SExp::List(self.body.iter().map(|i| i.to_sexp()).collect())
SExp::List(self.body.iter().map(|i| i.to_sexp()).collect()),
])
}
}
Expand All @@ -72,7 +77,10 @@ impl UpdateId for BlockBucket {
}

impl WriteLLVMIR for BlockBucket {
fn produce_llvm_ir<'a, 'b>(&self, producer: &'b dyn LLVMIRProducer<'a>) -> Option<LLVMInstruction<'a>> {
fn produce_llvm_ir<'a, 'b>(
&self,
producer: &'b dyn LLVMIRProducer<'a>,
) -> Option<LLVMInstruction<'a>> {
Self::manage_debug_loc_from_curr(producer, self);

let mut last = None;
Expand All @@ -81,4 +89,4 @@ impl WriteLLVMIR for BlockBucket {
}
last
}
}
}
Loading

0 comments on commit 58c3c94

Please sign in to comment.