diff --git a/calyx-py/calyx/queue_call.py b/calyx-py/calyx/queue_call.py index e10d6539a7..7089807604 100644 --- a/calyx-py/calyx/queue_call.py +++ b/calyx-py/calyx/queue_call.py @@ -70,38 +70,34 @@ def insert_main(prog, queue): i_lt_max_cmds = main.lt_use(i.out, queue_util.MAX_CMDS) not_err = main.not_use(err.out) - main.control += [ - cb.while_with( - i_lt_max_cmds, # Run while i < MAX_CMDS - [ - read_cmd, - write_cmd_to_reg, # `cmd := commands[i]` - read_value, - write_value_to_reg, # `value := values[i]` - cb.invoke( # Invoke the queue. - queue, - in_cmd=cmd.out, - in_value=value.out, - ref_ans=ans, - ref_err=err, - ), + main.control += cb.while_with( + i_lt_max_cmds, # Run while i < MAX_CMDS + [ + read_cmd, + write_cmd_to_reg, # `cmd := commands[i]` + read_value, + write_value_to_reg, # `value := values[i]` + cb.invoke( # Invoke the queue. + queue, + in_cmd=cmd.out, + in_value=value.out, + ref_ans=ans, + ref_err=err, + ), + cb.if_with( + not_err, cb.if_with( - not_err, + cmd_le_1, # If the command was a pop or peek, [ - cb.if_with( - cmd_le_1, # If the command was a pop or peek, - [ - write_ans, # Write the answer to the answer list - incr_j, # And increment the answer index. - ], - ), + write_ans, # Write the answer to the answer list + incr_j, # And increment the answer index. ], ), - lower_err, # Lower the error flag - incr_i, # Increment the command index - ], - ), - ] + ), + lower_err, # Lower the error flag + incr_i, # Increment the command index + ], + ) return main @@ -206,8 +202,8 @@ def insert_runner(prog, queue, name, stats_component): [ cb.if_with( cmd_le_1, # If the command was a pop or peek - [raise_has_ans], # then raise the `has_ans` flag - [lower_has_ans], # else lower the `has_ans` flag + raise_has_ans, # then raise the `has_ans` flag + lower_has_ans, # else lower the `has_ans` flag ), ], ), diff --git a/calyx-py/test/correctness/fifo.py b/calyx-py/test/correctness/fifo.py index 5df6616acd..29c7d87269 100644 --- a/calyx-py/test/correctness/fifo.py +++ b/calyx-py/test/correctness/fifo.py @@ -64,63 +64,61 @@ def insert_fifo(prog, name): mem, ans, "read_payload_from_mem_phase2" ) - fifo.control += [ - cb.par( - # Was it a pop or a push? We can do both cases in parallel. + fifo.control += cb.par( + # Was it a pop or a push? We can do both cases in parallel. + cb.if_with( + # Did the user call pop? + cmd_eq_0, cb.if_with( - # Did the user call pop? - cmd_eq_0, - cb.if_with( - # Yes, the user called pop. But is the queue empty? - len_eq_0, - [raise_err, flash_ans], # The queue is empty: underflow. - [ # The queue is not empty. Proceed. - read_from_mem, # Read from the queue. - write_to_ans, # Write the answer to the answer register. - read_incr, # Increment the read pointer. - cb.if_with( - # Wrap around if necessary. - read_eq_max_queue_len, - flash_read, - ), - len_decr, # Decrement the length. - ], - ), + # Yes, the user called pop. But is the queue empty? + len_eq_0, + cb.par(raise_err, flash_ans), # The queue is empty: underflow. + [ # The queue is not empty. Proceed. + read_from_mem, # Read from the queue. + write_to_ans, # Write the answer to the answer register. + read_incr, # Increment the read pointer. + cb.if_with( + # Wrap around if necessary. + read_eq_max_queue_len, + flash_read, + ), + len_decr, # Decrement the length. + ], ), - cb.if_with( - # Did the user call peek? - cmd_eq_1, - cb.if_with( # Yes, the user called peek. But is the queue empty? - len_eq_0, - [raise_err, flash_ans], # The queue is empty: underflow. - [ # The queue is not empty. Proceed. - read_from_mem, # Read from the queue. - write_to_ans, # Write the answer to the answer register. - # But don't increment the read pointer or change the length. - ], - ), + ), + cb.if_with( + # Did the user call peek? + cmd_eq_1, + cb.if_with( # Yes, the user called peek. But is the queue empty? + len_eq_0, + cb.par(raise_err, flash_ans), # The queue is empty: underflow. + [ # The queue is not empty. Proceed. + read_from_mem, # Read from the queue. + write_to_ans, # Write the answer to the answer register. + # But don't increment the read pointer or change the length. + ], ), + ), + cb.if_with( + # Did the user call push? + cmd_eq_2, cb.if_with( - # Did the user call push? - cmd_eq_2, - cb.if_with( - # Yes, the user called push. But is the queue full? - len_eq_max_queue_len, - [raise_err, flash_ans], # The queue is full: overflow. - [ # The queue is not full. Proceed. - write_to_mem, # Write `value` to the queue. - write_incr, # Increment the write pointer. - cb.if_with( - # Wrap around if necessary. - write_eq_max_queue_len, - flash_write, - ), - len_incr, # Increment the length. - ], - ), + # Yes, the user called push. But is the queue full? + len_eq_max_queue_len, + cb.par(raise_err, flash_ans), # The queue is empty: underflow. + [ # The queue is not full. Proceed. + write_to_mem, # Write `value` to the queue. + write_incr, # Increment the write pointer. + cb.if_with( + # Wrap around if necessary. + write_eq_max_queue_len, + flash_write, + ), + len_incr, # Increment the length. + ], ), ), - ] + ) return fifo diff --git a/calyx-py/test/correctness/pifo.py b/calyx-py/test/correctness/pifo.py index ae868d1ac7..aabcae0b93 100644 --- a/calyx-py/test/correctness/pifo.py +++ b/calyx-py/test/correctness/pifo.py @@ -117,9 +117,6 @@ def insert_pifo(prog, name, queue_l, queue_r, boundary, stats=None, static=False # Some equality checks. hot_eq_0 = pifo.eq_use(hot.out, 0) - hot_eq_1 = pifo.eq_use(hot.out, 1) - flow_eq_0 = pifo.eq_use(flow.out, 0) - flow_eq_1 = pifo.eq_use(flow.out, 1) len_eq_0 = pifo.eq_use(len.out, 0) len_eq_max_queue_len = pifo.eq_use(len.out, MAX_QUEUE_LEN) cmd_eq_0 = pifo.eq_use(cmd, 0) @@ -137,182 +134,144 @@ def insert_pifo(prog, name, queue_l, queue_r, boundary, stats=None, static=False len_decr = pifo.decr(len) # len-- # The main logic. - pifo.control += [ - cb.par( - # Was it a pop, peek, or a push? We can do all cases in parallel. + pifo.control += cb.par( + # Was it a pop, peek, or a push? We can do all cases in parallel. + cb.if_with( + # Did the user call pop? + cmd_eq_0, cb.if_with( - # Did the user call pop? - cmd_eq_0, - cb.if_with( - # Yes, the user called pop. But is the queue empty? - len_eq_0, - [raise_err, flash_ans], # The queue is empty: underflow. - [ # The queue is not empty. Proceed. - # We must check if `hot` is 0 or 1. - lower_err, - cb.par( # We'll check both cases in parallel. + # Yes, the user called pop. But is the queue empty? + len_eq_0, + cb.par(raise_err, flash_ans), # The queue is empty: underflow. + [ # The queue is not empty. Proceed. + # We must check if `hot` is 0 or 1. + lower_err, + cb.if_with( + # Check if `hot` is 0. + hot_eq_0, + [ # `hot` is 0. We'll invoke `pop` on `queue_l`. + invoke_subqueue(queue_l, cmd, value, ans, err), + # Our next step depends on whether `queue_l` + # raised the error flag. + # We can check these cases in parallel. cb.if_with( - # Check if `hot` is 0. - hot_eq_0, - [ # `hot` is 0. We'll invoke `pop` on `queue_l`. - invoke_subqueue(queue_l, cmd, value, ans, err), - # Our next step depends on whether `queue_l` - # raised the error flag. - # We can check these cases in parallel. - cb.par( - cb.if_with( - err_neq_0, - [ # `queue_l` raised an error. - # We'll try to pop from `queue_r`. - # We'll pass it a lowered err - lower_err, - invoke_subqueue( - queue_r, cmd, value, ans, err - ), - ], - ), - cb.if_with( - err_eq_0, - [ # `queue_l` succeeded. - # Its answer is our answer. - flip_hot - # We'll just make `hot` point - # to the other sub-queue. - ], - ), - ), + err_neq_0, + [ # `queue_l` raised an error. + # We'll try to pop from `queue_r`. + # We'll pass it a lowered err + lower_err, + invoke_subqueue(queue_r, cmd, value, ans, err), ], + # `queue_l` succeeded. + # Its answer is our answer. + flip_hot + # We'll just make `hot` point + # to the other sub-queue. ), - # If `hot` is 1, we proceed symmetrically. + ], + [ # Else: `hot` is 1. Proceed symmetrically. + invoke_subqueue(queue_r, cmd, value, ans, err), cb.if_with( - hot_eq_1, + err_neq_0, [ - invoke_subqueue(queue_r, cmd, value, ans, err), - cb.par( - cb.if_with( - err_neq_0, - [ - lower_err, - invoke_subqueue( - queue_l, cmd, value, ans, err - ), - ], - ), - cb.if_with( - err_eq_0, - [flip_hot], - ), - ), + lower_err, + invoke_subqueue(queue_l, cmd, value, ans, err), ], + flip_hot, ), - ), - len_decr, # Decrement the length. - # It is possible that an irrecoverable error was raised above, - # in which case the length should _not_ in fact be decremented. - # However, in that case the PIFO's `err` flag would also - # have been raised, and no one will check this length anyway. - ], - ), + ], + ), + len_decr, # Decrement the length. + # It is possible that an irrecoverable error was raised above, + # in which case the length should _not_ in fact be decremented. + # However, in that case the PIFO's `err` flag would also + # have been raised, and no one will check this length anyway. + ], ), + ), + cb.if_with( + # Did the user call peek? + cmd_eq_1, cb.if_with( - # Did the user call peek? - cmd_eq_1, - cb.if_with( - # Yes, the user called peek. But is the queue empty? - len_eq_0, - [raise_err, flash_ans], # The queue is empty: underflow. - [ # The queue is not empty. Proceed. - # We must check if `hot` is 0 or 1. - lower_err, - cb.par( # We'll check both cases in parallel. + # Yes, the user called peek. But is the queue empty? + len_eq_0, + cb.par(raise_err, flash_ans), # The queue is empty: underflow. + [ # The queue is not empty. Proceed. + # We must check if `hot` is 0 or 1. + lower_err, + cb.if_with( + # Check if `hot` is 0. + hot_eq_0, + [ # `hot` is 0. We'll invoke `peek` on `queue_l`. + invoke_subqueue(queue_l, cmd, value, ans, err), + # Our next step depends on whether `queue_l` + # raised the error flag. cb.if_with( - # Check if `hot` is 0. - hot_eq_0, - [ # `hot` is 0. We'll invoke `peek` on `queue_l`. - invoke_subqueue(queue_l, cmd, value, ans, err), - # Our next step depends on whether `queue_l` - # raised the error flag. - cb.if_with( - err_neq_0, - [ # `queue_l` raised an error. - # We'll try to peek from `queue_r`. - # We'll pass it a lowered `err`. - lower_err, - invoke_subqueue( - queue_r, cmd, value, ans, err - ), - ], - ), - # Peeking does not affect `hot`. - # Peeking does not affect the length. + err_neq_0, + [ # `queue_l` raised an error. + # We'll try to peek from `queue_r`. + # We'll pass it a lowered `err`. + lower_err, + invoke_subqueue(queue_r, cmd, value, ans, err), ], ), - # If `hot` is 1, we proceed symmetrically. + # Peeking does not affect `hot`. + # Peeking does not affect the length. + ], + [ + invoke_subqueue(queue_r, cmd, value, ans, err), cb.if_with( - hot_eq_1, + err_neq_0, [ - invoke_subqueue(queue_r, cmd, value, ans, err), - cb.if_with( - err_neq_0, - [ - lower_err, - invoke_subqueue( - queue_l, cmd, value, ans, err - ), - ], - ), + lower_err, + invoke_subqueue(queue_l, cmd, value, ans, err), ], ), - ), - ], - ), + ], + ), + ], ), + ), + cb.if_with( + # Did the user call push? + cmd_eq_2, cb.if_with( - # Did the user call push? - cmd_eq_2, - cb.if_with( - # Yes, the user called push. But is the queue full? - len_eq_max_queue_len, - [raise_err, flash_ans], # The queue is full: overflow. - [ # The queue is not full. Proceed. - lower_err, - # We need to check which flow this value should be pushed to. - infer_flow, # Infer the flow and write it to `fifo_{flow}`. - cb.par( - cb.if_with( - flow_eq_0, - # This value should be pushed to queue_l. - invoke_subqueue(queue_l, cmd, value, ans, err), - ), - cb.if_with( - flow_eq_1, - # This value should be pushed to queue_r. - invoke_subqueue(queue_r, cmd, value, ans, err), + # Yes, the user called push. But is the queue full? + len_eq_max_queue_len, + cb.par(raise_err, flash_ans), # The queue is full: overflow. + [ # The queue is not full. Proceed. + lower_err, + # We need to check which flow this value should be pushed to. + infer_flow, # Infer the flow and write it to `flow`. + cb.if_( + flow.out, + # If flow = 1, value should be pushed to queue_r. + invoke_subqueue(queue_r, cmd, value, ans, err), + # If flow = 0, value should be pushed to queue_l. + invoke_subqueue(queue_l, cmd, value, ans, err), + ), + cb.if_with( + err_eq_0, + # If no stats component is provided, + # just increment the length. + len_incr + if not stats + else cb.par( + # If a stats component is provided, + # Increment the length and also + # tell the stats component what flow we pushed. + len_incr, + ( + cb.static_invoke(stats, in_flow=flow.out) + if static + else cb.invoke(stats, in_flow=flow.out) ), ), - cb.if_with( - err_eq_0, - # If no stats component is provided, - # just increment the length. - [len_incr] - if not stats - else [ - # If a stats component is provided, - # Increment the length and also - # tell the stats component what flow we pushed. - len_incr, - ( - cb.static_invoke(stats, in_flow=flow.out) - if static - else cb.invoke(stats, in_flow=flow.out) - ), - ], - ), - ], - ), + ), + ], ), ), - ] + ) return pifo diff --git a/calyx-py/test/correctness/sdn.py b/calyx-py/test/correctness/sdn.py index f87f950793..89aecd7214 100644 --- a/calyx-py/test/correctness/sdn.py +++ b/calyx-py/test/correctness/sdn.py @@ -87,10 +87,8 @@ def insert_controller(prog, name, stats_component): get_data_locally.done = (count_0.done & count_1.done) @ 1 # The main logic. - controller.control += [ - get_data_locally, - # Great, now I have the data around locally. - ] + controller.control += get_data_locally + # Great, now I have the data around locally. return controller @@ -124,36 +122,34 @@ def insert_main(prog, dataplane, controller, stats_component): not_err = main.not_use(dataplane_err.out) - main.control += [ + main.control += cb.while_with( # We will run the dataplane and controller components in parallel, # in a while loop. The loop will terminate when the dataplane component # raises `dataplane_err`. - cb.while_with( - not_err, # While the dataplane component has not errored out. - [ - lower_has_ans, # Lower the has-ans flag. - cb.invoke( # Invoke the dataplane component. - dataplane, - ref_commands=commands, - ref_values=values, - ref_has_ans=has_ans, - ref_component_ans=dataplane_ans, - ref_component_err=dataplane_err, - ref_stats_runner=stats, - ), - # If the dataplane component has a nonzero answer, - # write it to the answer-list and increment the index `j`. - cb.if_( - has_ans.out, - cb.if_with(ans_neq_0, [write_ans, incr_j]), - ), - cb.invoke( # Invoke the controller component. - controller, - ref_stats_controller=stats, - ), - ], - ) - ] + not_err, # While the dataplane component has not errored out. + [ + lower_has_ans, # Lower the has-ans flag. + cb.invoke( # Invoke the dataplane component. + dataplane, + ref_commands=commands, + ref_values=values, + ref_has_ans=has_ans, + ref_component_ans=dataplane_ans, + ref_component_err=dataplane_err, + ref_stats_runner=stats, + ), + # If the dataplane component has a nonzero answer, + # write it to the answer-list and increment the index `j`. + cb.if_( + has_ans.out, + cb.if_with(ans_neq_0, [write_ans, incr_j]), + ), + cb.invoke( # Invoke the controller component. + controller, + ref_stats_controller=stats, + ), + ], + ) def build(static=False):