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

[VAN-676][VAN-670] flatten branches in extracted function by duplicating the functions #55

Merged
merged 15 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
186 changes: 186 additions & 0 deletions circom/tests/loops/assign_in_loop.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
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:.* // panicked at 'not yet implemented', circuit_passes/src/passes/loop_unroll/loop_env_recorder.rs:149:44 (LocationRule::Mapped case)

template Inner(i) {
signal input in;
signal output out;

out <-- (in >> i) & 1;
}

template Num2Bits(n) {
signal input in;
signal output out[n];

component c[n];
for (var i = 0; i < n; i++) {
c[i] = Inner(i);
c[i].in <-- in;
out[i] <-- c[i].out;
}
}

component main = Num2Bits(3);

//CHECK-LABEL: define void @..generated..loop.body.
//CHECK-SAME: [[$F_ID_1:[0-9]+]]([0 x i256]* %lvars, [0 x i256]* %signals, i256* %subfix_[[X1:[0-9]+]], i256* %fix_[[X2:[0-9]+]], i256* %subfix_[[X3:[0-9]+]],
//CHECK-SAME: [0 x i256]* %sub_[[X1]], i256* %subc_[[X1]], [0 x i256]* %sub_[[X3]], i256* %subc_[[X3]]){{.*}} {
//CHECK-NEXT: ..generated..loop.body.[[$F_ID_1]]:
//CHECK-NEXT: br label %store1
//CHECK-EMPTY:
//CHECK-NEXT: store1:
//CHECK-NEXT: %0 = getelementptr [0 x i256], [0 x i256]* %signals, i32 0, i32 3
//CHECK-NEXT: %1 = load i256, i256* %0, align 4
//CHECK-NEXT: %2 = getelementptr i256, i256* %subfix_[[X1]], i32 0
//CHECK-NEXT: store i256 %1, i256* %2, align 4
//CHECK-NEXT: br label %store2
//CHECK-EMPTY:
//CHECK-NEXT: store2:
//CHECK-NEXT: %3 = getelementptr [0 x i256], [0 x i256]* %sub_[[X1]], i32 0
//CHECK-NEXT: call void @Inner_?_run([0 x i256]* %sub_[[X1]]) //TODO: which function to call depends on which iteration of the loop
//CHECK-NEXT: br label %store3
//CHECK-EMPTY:
//CHECK-NEXT: store3:
//CHECK-NEXT: %4 = getelementptr i256, i256* %subfix_[[X3]], i32 0
//CHECK-NEXT: %5 = load i256, i256* %4, align 4
//CHECK-NEXT: %6 = getelementptr i256, i256* %fix_[[X2]], i32 0
//CHECK-NEXT: store i256 %5, i256* %6, align 4
//CHECK-NEXT: br label %store4
//CHECK-EMPTY:
//CHECK-NEXT: store4:
//CHECK-NEXT: %7 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 1
//CHECK-NEXT: %8 = load i256, i256* %7, align 4
//CHECK-NEXT: %call.fr_add = call i256 @fr_add(i256 %8, i256 1)
//CHECK-NEXT: %9 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 1
//CHECK-NEXT: store i256 %call.fr_add, i256* %9, align 4
//CHECK-NEXT: br label %return5
//CHECK-EMPTY:
//CHECK-NEXT: return5:
//CHECK-NEXT: ret void
//CHECK-NEXT: }
//
//CHECK-LABEL: define void @Inner_0_build({ [0 x i256]*, i32 }* %0){{.*}} {
//
//CHECK-LABEL: define void @Inner_0_run([0 x i256]* %0){{.*}} {
//
//CHECK-LABEL: define void @Inner_1_build({ [0 x i256]*, i32 }* %0){{.*}} {
//
//CHECK-LABEL: define void @Inner_1_run([0 x i256]* %0){{.*}} {
//
//CHECK-LABEL: define void @Inner_2_build({ [0 x i256]*, i32 }* %0){{.*}} {
//
//CHECK-LABEL: define void @Inner_2_run([0 x i256]* %0){{.*}} {
//
//CHECK-LABEL: define void @Num2Bits_3_build({ [0 x i256]*, i32 }* %0){{.*}} {
//CHECK-NEXT: main:
//CHECK-NEXT: %1 = alloca [4 x i256], align 8
//CHECK-NEXT: %2 = getelementptr { [0 x i256]*, i32 }, { [0 x i256]*, i32 }* %0, i32 0, i32 1
//CHECK-NEXT: store i32 1, i32* %2, align 4
//CHECK-NEXT: %3 = getelementptr { [0 x i256]*, i32 }, { [0 x i256]*, i32 }* %0, i32 0, i32 0
//CHECK-NEXT: %4 = bitcast [4 x i256]* %1 to [0 x i256]*
//CHECK-NEXT: store [0 x i256]* %4, [0 x i256]** %3, align 8
//CHECK-NEXT: ret void
//CHECK-NEXT: }
//
//CHECK-LABEL: define void @Num2Bits_3_run([0 x i256]* %0){{.*}} {
//CHECK-NEXT: prelude:
//CHECK-NEXT: %lvars = alloca [2 x i256], align 8
//CHECK-NEXT: %subcmps = alloca [3 x { [0 x i256]*, i32 }], align 8
//CHECK-NEXT: br label %store1
//CHECK-EMPTY:
//CHECK-NEXT: store1:
//CHECK-NEXT: %1 = getelementptr [2 x i256], [2 x i256]* %lvars, i32 0, i32 0
//CHECK-NEXT: store i256 3, i256* %1, align 4
//CHECK-NEXT: br label %create_cmp2
//CHECK-EMPTY:
//CHECK-NEXT: create_cmp2:
//CHECK-NEXT: %2 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0
//CHECK-NEXT: call void @Inner_0_build({ [0 x i256]*, i32 }* %2)
//CHECK-NEXT: br label %create_cmp3
//CHECK-EMPTY:
//CHECK-NEXT: create_cmp3:
//CHECK-NEXT: %3 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1
//CHECK-NEXT: call void @Inner_1_build({ [0 x i256]*, i32 }* %3)
//CHECK-NEXT: br label %create_cmp4
//CHECK-EMPTY:
//CHECK-NEXT: create_cmp4:
//CHECK-NEXT: %4 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2
//CHECK-NEXT: call void @Inner_2_build({ [0 x i256]*, i32 }* %4)
//CHECK-NEXT: br label %store5
//CHECK-EMPTY:
//CHECK-NEXT: store5:
//CHECK-NEXT: %5 = getelementptr [2 x i256], [2 x i256]* %lvars, i32 0, i32 1
//CHECK-NEXT: store i256 0, i256* %5, align 4
//CHECK-NEXT: br label %unrolled_loop6
//CHECK-EMPTY:
//CHECK-NEXT: unrolled_loop6:
//CHECK-NEXT: %6 = bitcast [2 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: %7 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 0
//CHECK-NEXT: %8 = load [0 x i256]*, [0 x i256]** %7, align 8
//CHECK-NEXT: %9 = getelementptr [0 x i256], [0 x i256]* %8, i32 0
//CHECK-NEXT: %10 = getelementptr [0 x i256], [0 x i256]* %9, i32 0, i256 1
//CHECK-NEXT: %11 = getelementptr [0 x i256], [0 x i256]* %0, i32 0, i256 0
//CHECK-NEXT: %12 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 0
//CHECK-NEXT: %13 = load [0 x i256]*, [0 x i256]** %12, align 8
//CHECK-NEXT: %14 = getelementptr [0 x i256], [0 x i256]* %13, i32 0
//CHECK-NEXT: %15 = getelementptr [0 x i256], [0 x i256]* %14, i32 0, i256 0
//CHECK-NEXT: %16 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 0
//CHECK-NEXT: %17 = load [0 x i256]*, [0 x i256]** %16, align 8
//CHECK-NEXT: %18 = getelementptr [0 x i256], [0 x i256]* %17, i32 0
//CHECK-NEXT: %19 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 1
//CHECK-NEXT: %20 = bitcast i32* %19 to i256*
//CHECK-NEXT: %21 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 0
//CHECK-NEXT: %22 = load [0 x i256]*, [0 x i256]** %21, align 8
//CHECK-NEXT: %23 = getelementptr [0 x i256], [0 x i256]* %22, i32 0
//CHECK-NEXT: %24 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 0, i32 1
//CHECK-NEXT: %25 = bitcast i32* %24 to i256*
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_1]]([0 x i256]* %6, [0 x i256]* %0, i256* %10, i256* %11, i256* %15, [0 x i256]* %18, i256* %20, [0 x i256]* %23, i256* %25)
//CHECK-NEXT: %26 = bitcast [2 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: %27 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 0
//CHECK-NEXT: %28 = load [0 x i256]*, [0 x i256]** %27, align 8
//CHECK-NEXT: %29 = getelementptr [0 x i256], [0 x i256]* %28, i32 0
//CHECK-NEXT: %30 = getelementptr [0 x i256], [0 x i256]* %29, i32 0, i256 1
//CHECK-NEXT: %31 = getelementptr [0 x i256], [0 x i256]* %0, i32 0, i256 1
//CHECK-NEXT: %32 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 0
//CHECK-NEXT: %33 = load [0 x i256]*, [0 x i256]** %32, align 8
//CHECK-NEXT: %34 = getelementptr [0 x i256], [0 x i256]* %33, i32 0
//CHECK-NEXT: %35 = getelementptr [0 x i256], [0 x i256]* %34, i32 0, i256 0
//CHECK-NEXT: %36 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 0
//CHECK-NEXT: %37 = load [0 x i256]*, [0 x i256]** %36, align 8
//CHECK-NEXT: %38 = getelementptr [0 x i256], [0 x i256]* %37, i32 0
//CHECK-NEXT: %39 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 1
//CHECK-NEXT: %40 = bitcast i32* %39 to i256*
//CHECK-NEXT: %41 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 0
//CHECK-NEXT: %42 = load [0 x i256]*, [0 x i256]** %41, align 8
//CHECK-NEXT: %43 = getelementptr [0 x i256], [0 x i256]* %42, i32 0
//CHECK-NEXT: %44 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 1, i32 1
//CHECK-NEXT: %45 = bitcast i32* %44 to i256*
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_1]]([0 x i256]* %26, [0 x i256]* %0, i256* %30, i256* %31, i256* %35, [0 x i256]* %38, i256* %40, [0 x i256]* %43, i256* %45)
//CHECK-NEXT: %46 = bitcast [2 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: %47 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 0
//CHECK-NEXT: %48 = load [0 x i256]*, [0 x i256]** %47, align 8
//CHECK-NEXT: %49 = getelementptr [0 x i256], [0 x i256]* %48, i32 0
//CHECK-NEXT: %50 = getelementptr [0 x i256], [0 x i256]* %49, i32 0, i256 1
//CHECK-NEXT: %51 = getelementptr [0 x i256], [0 x i256]* %0, i32 0, i256 2
//CHECK-NEXT: %52 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 0
//CHECK-NEXT: %53 = load [0 x i256]*, [0 x i256]** %52, align 8
//CHECK-NEXT: %54 = getelementptr [0 x i256], [0 x i256]* %53, i32 0
//CHECK-NEXT: %55 = getelementptr [0 x i256], [0 x i256]* %54, i32 0, i256 0
//CHECK-NEXT: %56 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 0
//CHECK-NEXT: %57 = load [0 x i256]*, [0 x i256]** %56, align 8
//CHECK-NEXT: %58 = getelementptr [0 x i256], [0 x i256]* %57, i32 0
//CHECK-NEXT: %59 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 1
//CHECK-NEXT: %60 = bitcast i32* %59 to i256*
//CHECK-NEXT: %61 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 0
//CHECK-NEXT: %62 = load [0 x i256]*, [0 x i256]** %61, align 8
//CHECK-NEXT: %63 = getelementptr [0 x i256], [0 x i256]* %62, i32 0
//CHECK-NEXT: %64 = getelementptr [3 x { [0 x i256]*, i32 }], [3 x { [0 x i256]*, i32 }]* %subcmps, i32 0, i32 2, i32 1
//CHECK-NEXT: %65 = bitcast i32* %64 to i256*
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_1]]([0 x i256]* %46, [0 x i256]* %0, i256* %50, i256* %51, i256* %55, [0 x i256]* %58, i256* %60, [0 x i256]* %63, i256* %65)
//CHECK-NEXT: br label %prologue
//CHECK-EMPTY:
//CHECK-NEXT: prologue:
//CHECK-NEXT: ret void
//CHECK-NEXT: }
35 changes: 17 additions & 18 deletions circom/tests/loops/call_inside_loop.circom
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ component main = CallInLoop(2, 3);
//CHECK-NEXT: }
//
//CHECK-LABEL: define void @..generated..loop.body.
//CHECK-SAME: [[$F_ID_2:[0-9]+]]([0 x i256]* %lvars, [0 x i256]* %signals){{.*}} {
//CHECK-SAME: [[$F_ID_2:[0-9]+]]([0 x i256]* %lvars, [0 x i256]* %signals, i256* %fix_0){{.*}} {
//CHECK-NEXT: ..generated..loop.body.[[$F_ID_2]]:
//CHECK-NEXT: br label %call1
//CHECK-EMPTY:
Expand All @@ -95,21 +95,16 @@ component main = CallInLoop(2, 3);
//CHECK-NEXT: store i256 3, i256* %8, align 4
//CHECK-NEXT: %9 = bitcast [15 x i256]* %fun_0_arena to i256*
//CHECK-NEXT: %call.fun_0 = call i256 @fun_0(i256* %9)
//CHECK-NEXT: %10 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 6
//CHECK-NEXT: %11 = load i256, i256* %10, align 4
//CHECK-NEXT: %call.fr_cast_to_addr = call i32 @fr_cast_to_addr(i256 %11)
//CHECK-NEXT: %mul_addr = mul i32 1, %call.fr_cast_to_addr
//CHECK-NEXT: %add_addr = add i32 %mul_addr, 4
//CHECK-NEXT: %12 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 %add_addr
//CHECK-NEXT: store i256 %call.fun_0, i256* %12, align 4
//CHECK-NEXT: %10 = getelementptr i256, i256* %fix_0, i32 0
//CHECK-NEXT: store i256 %call.fun_0, i256* %10, align 4
//CHECK-NEXT: br label %store2
//CHECK-EMPTY:
//CHECK-NEXT: store2:
//CHECK-NEXT: %11 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 6
//CHECK-NEXT: %12 = load i256, i256* %11, align 4
//CHECK-NEXT: %call.fr_add = call i256 @fr_add(i256 %12, i256 1)
//CHECK-NEXT: %13 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 6
//CHECK-NEXT: %14 = load i256, i256* %13, align 4
//CHECK-NEXT: %call.fr_add = call i256 @fr_add(i256 %14, i256 1)
//CHECK-NEXT: %15 = getelementptr [0 x i256], [0 x i256]* %lvars, i32 0, i32 6
//CHECK-NEXT: store i256 %call.fr_add, i256* %15, align 4
//CHECK-NEXT: store i256 %call.fr_add, i256* %13, align 4
//CHECK-NEXT: br label %return3
//CHECK-EMPTY:
//CHECK-NEXT: return3:
Expand Down Expand Up @@ -175,16 +170,20 @@ component main = CallInLoop(2, 3);
//CHECK-EMPTY:
//CHECK-NEXT: unrolled_loop10:
//CHECK-NEXT: %15 = bitcast [7 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_2]]([0 x i256]* %15, [0 x i256]* %0)
//CHECK-NEXT: %16 = bitcast [7 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_2]]([0 x i256]* %16, [0 x i256]* %0)
//CHECK-NEXT: %17 = getelementptr [0 x i256], [0 x i256]* %16, i32 0, i256 4
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_2]]([0 x i256]* %15, [0 x i256]* %0, i256* %17)
//CHECK-NEXT: %18 = bitcast [7 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: %19 = bitcast [7 x i256]* %lvars to [0 x i256]*
//CHECK-NEXT: %20 = getelementptr [0 x i256], [0 x i256]* %19, i32 0, i256 5
//CHECK-NEXT: call void @..generated..loop.body.[[$F_ID_2]]([0 x i256]* %18, [0 x i256]* %0, i256* %20)
//CHECK-NEXT: br label %store11
//CHECK-EMPTY:
//CHECK-NEXT: store11:
//CHECK-NEXT: %17 = getelementptr [7 x i256], [7 x i256]* %lvars, i32 0, i32 4
//CHECK-NEXT: %18 = load i256, i256* %17, align 4
//CHECK-NEXT: %19 = getelementptr [0 x i256], [0 x i256]* %0, i32 0, i32 0
//CHECK-NEXT: store i256 %18, i256* %19, align 4
//CHECK-NEXT: %21 = getelementptr [7 x i256], [7 x i256]* %lvars, i32 0, i32 4
//CHECK-NEXT: %22 = load i256, i256* %21, align 4
//CHECK-NEXT: %23 = getelementptr [0 x i256], [0 x i256]* %0, i32 0, i32 0
//CHECK-NEXT: store i256 %22, i256* %23, align 4
//CHECK-NEXT: br label %prologue
//CHECK-EMPTY:
//CHECK-NEXT: prologue:
Expand Down
Loading
Loading