yosys/tests/silimate/extract_reduce_gates.ys

950 lines
19 KiB
Plaintext

###################################################################
# Extract Reduce AND Gates Tests
###################################################################
log -header "Simple AND chain"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [3:0] a,
output wire x
);
assign x = a[0] & a[1] & a[2] & a[3];
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 1 t:$reduce_and
select -assert-count 1 t:$reduce_and r:A_WIDTH=4 %i
design -reset
log -pop
log -header "AND chain with constants"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [2:0] a,
output wire x
);
assign x = a[0] & a[1] & a[2] & 1'b1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 1 t:$reduce_and
select -assert-count 1 t:$reduce_and r:A_WIDTH=4 %i
design -reset
log -pop
log -header "AND chain with multiple branches"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [5:0] a,
output wire x
);
wire w0, w1, w2, w3;
assign w0 = a[0] & a[1];
assign w1 = a[2] & a[3];
assign w2 = a[4] & a[5];
assign w3 = w0 & w1;
assign x = w2 & w3;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 1 t:$reduce_and
select -assert-count 1 t:$reduce_and r:A_WIDTH=6 %i
design -reset
log -pop
log -header "No off-chain for AND"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] & a[1];
assign w1 = w0 & a[2];
assign w2 = w1 & a[3];
assign x = w2 & a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_and
select -assert-count 2 t:$reduce_and
# Check that both gates are 3 bits wide
select -assert-none t:$reduce_and r:A_WIDTH!=3 %i
design -reset
log -pop
log -header "Allow off-chain for AND"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] & a[1];
assign w1 = w0 & a[2];
assign w2 = w1 & a[3];
assign x = w2 & a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_and
# Check that only one gate has a width of 5 and one gate has a width of 3
select -assert-count 1 t:$reduce_and r:A_WIDTH=5 %i
select -assert-count 1 t:$reduce_and r:A_WIDTH=3 %i
design -reset
log -pop
log -header "No off-chain with branches for AND"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] & a[1] & a[2] & (a[3] & a[4]) & a[5];
assign x = w0 & a[6] & a[7] & (a[8] & a[9]) & a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_and
# Check that both gates are 6 bits wide
select -assert-none t:$reduce_and r:A_WIDTH!=6 %i
design -reset
log -pop
log -header "Allow off-chain with branches for AND"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] & a[1] & a[2] & (a[3] & a[4]) & a[5];
assign x = w0 & a[6] & a[7] & (a[8] & a[9]) & a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_and
# Check that only one gate has a width of 11 and one gate has a width of 6
select -assert-count 1 t:$reduce_and r:A_WIDTH=11 %i
select -assert-count 1 t:$reduce_and r:A_WIDTH=6 %i
design -reset
log -pop
###################################################################
# Extract Reduce OR Gates Tests
###################################################################
log -header "Simple OR chain"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [3:0] a,
output wire x
);
assign x = a[0] | a[1] | a[2] | a[3];
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$or
select -assert-count 1 t:$reduce_or
select -assert-count 1 t:$reduce_or r:A_WIDTH=4 %i
design -reset
log -pop
log -header "OR chain with constants"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [3:0] a,
output wire x
);
assign x = a[0] | a[1] | a[2] | 1'b1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$or
select -assert-count 1 t:$reduce_or
select -assert-count 1 t:$reduce_or r:A_WIDTH=4 %i
design -reset
log -pop
log -header "OR chain with multiple branches"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [5:0] a,
output wire x
);
wire w0, w1, w2, w3;
assign w0 = a[0] | a[1];
assign w1 = a[2] | a[3];
assign w2 = a[4] | a[5];
assign w3 = w0 | w1;
assign x = w2 | w3;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$or
select -assert-count 1 t:$reduce_or
select -assert-count 1 t:$reduce_or r:A_WIDTH=6 %i
design -reset
log -pop
log -header "No off-chain OR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] | a[1];
assign w1 = w0 | a[2];
assign w2 = w1 | a[3];
assign x = w2 | a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$or
select -assert-count 2 t:$reduce_or
# Check that both gates are 3 bits wide
select -assert-none t:$reduce_or r:A_WIDTH!=3 %i
design -reset
log -pop
log -header "Allow off-chain for OR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] | a[1];
assign w1 = w0 | a[2];
assign w2 = w1 | a[3];
assign x = w2 | a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$or
select -assert-count 2 t:$reduce_or
# Check that only one gate has a width of 5 and one gate has a width of 3
select -assert-count 1 t:$reduce_or r:A_WIDTH=5 %i
select -assert-count 1 t:$reduce_or r:A_WIDTH=3 %i
design -reset
log -pop
log -header "No off-chain with branches for OR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] | a[1] | a[2] | (a[3] | a[4]) | a[5];
assign x = w0 | a[6] | a[7] | (a[8] | a[9]) | a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_or
# Check that both gates are 6 bits wide
select -assert-none t:$reduce_or r:A_WIDTH!=6 %i
design -reset
log -pop
log -header "Allow off-chain with branches for OR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] | a[1] | a[2] | (a[3] | a[4]) | a[5];
assign x = w0 | a[6] | a[7] | (a[8] | a[9]) | a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$or
select -assert-count 2 t:$reduce_or
# Check that only one gate has a width of 11 and one gate has a width of 6
select -assert-count 1 t:$reduce_or r:A_WIDTH=11 %i
select -assert-count 1 t:$reduce_or r:A_WIDTH=6 %i
design -reset
log -pop
###################################################################
# Extract Reduce XOR Gates Tests
###################################################################
log -header "Simple XOR chain"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [3:0] a,
output wire x
);
assign x = a[0] ^ a[1] ^ a[2] ^ a[3];
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$xor
select -assert-count 1 t:$reduce_xor
select -assert-count 1 t:$reduce_xor r:A_WIDTH=4 %i
design -reset
log -pop
log -header "XOR chain with constants"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [3:0] a,
output wire x
);
assign x = a[0] ^ a[1] ^ a[2] ^ 1'b1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$xor
select -assert-count 1 t:$reduce_xor
select -assert-count 1 t:$reduce_xor r:A_WIDTH=4 %i
design -reset
log -pop
log -header "XOR chain with multiple branches"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [5:0] a,
output wire x
);
wire w0, w1, w2, w3;
assign w0 = a[0] ^ a[1];
assign w1 = a[2] ^ a[3];
assign w2 = a[4] ^ a[5];
assign w3 = w0 ^ w1;
assign x = w2 ^ w3;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$xor
select -assert-count 1 t:$reduce_xor
select -assert-count 1 t:$reduce_xor r:A_WIDTH=6 %i
design -reset
log -pop
log -header "No off-chain XOR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] ^ a[1];
assign w1 = w0 ^ a[2];
assign w2 = w1 ^ a[3];
assign x = w2 ^ a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$xor
select -assert-count 2 t:$reduce_xor
# Check that both gates are 3 bits wide
select -assert-none t:$reduce_xor r:A_WIDTH!=3 %i
design -reset
log -pop
log -header "Allow off-chain for XOR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [4:0] a,
output wire x,
output wire y
);
wire w0, w1, w2;
assign w0 = a[0] ^ a[1];
assign w1 = w0 ^ a[2];
assign w2 = w1 ^ a[3];
assign x = w2 ^ a[4];
// Off-chain use of w1
assign y = w1;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$xor
select -assert-count 2 t:$reduce_xor
# Check that only one gate has a width of 5 and one gate has a width of 3
select -assert-count 1 t:$reduce_xor r:A_WIDTH=5 %i
select -assert-count 1 t:$reduce_xor r:A_WIDTH=3 %i
design -reset
log -pop
log -header "No off-chain with branches for XOR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] ^ a[1] ^ a[2] ^ (a[3] ^ a[4]) ^ a[5];
assign x = w0 ^ a[6] ^ a[7] ^ (a[8] ^ a[9]) ^ a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$xor
select -assert-count 2 t:$reduce_xor
# Check that both gates are 6 bits wide
select -assert-none t:$reduce_xor r:A_WIDTH!=6 %i
design -reset
log -pop
log -header "Allow off-chain with branches for XOR"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [10:0] a,
output wire x,
output wire y
);
wire w0;
assign w0 = a[0] ^ a[1] ^ a[2] ^ (a[3] ^ a[4]) ^ a[5];
assign x = w0 ^ a[6] ^ a[7] ^ (a[8] ^ a[9]) ^ a[10];
// Off-chain use of w0
assign y = w0;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce -allow-off-chain
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates
select -assert-count 0 t:$xor
select -assert-count 2 t:$reduce_xor
# Check that only one gate has a width of 11 and one gate has a width of 6
select -assert-count 1 t:$reduce_xor r:A_WIDTH=11 %i
select -assert-count 1 t:$reduce_xor r:A_WIDTH=6 %i
design -reset
log -pop
###################################################################
# Edge Cases
###################################################################
log -header "Reconvergence"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [7:0] a,
output wire y
);
wire w0, w1, w2, w3, w4, w5, w6;
assign w0 = a[0] & a[1];
assign w1 = a[2] & a[3];
assign w2 = w0 & w1;
assign w3 = w2 & a[4];
assign w4 = w2 & a[5];
assign w5 = w3 & a[6];
assign w6 = w4 & a[7];
assign y = w5 & w6;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 2 t:$reduce_and
select -assert-count 1 t:$reduce_and r:A_WIDTH=4 %i
select -assert-count 1 t:$reduce_and r:A_WIDTH=6 %i
design -reset
log -pop
log -header "Stress test"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [7:0] a0,
input wire [7:0] a1,
input wire [7:0] a2,
output wire y
);
wire c0, c1, c2;
// Stage 0
wire s0_w0 = a0[0] & a0[1];
wire s0_w1 = a0[2] & a0[3];
wire s0_w2 = s0_w0 & s0_w1;
wire s0_w3 = s0_w2 & a0[4];
wire s0_w4 = a0[0] & a0[5];
wire s0_w5 = s0_w3 & a0[6];
wire s0_w6 = s0_w4 & a0[7];
assign c0 = s0_w5 & s0_w6;
// Stage 1
wire s1_w0 = a1[0] & a1[1];
wire s1_w1 = a1[2] & a1[3];
wire s1_w2 = s1_w0 & s1_w1;
wire s1_w3 = s1_w2 & a1[4];
wire s1_w4 = c0 & a1[5];
wire s1_w5 = s1_w3 & a1[6];
wire s1_w6 = s1_w4 & a1[7];
assign c1 = s1_w5 & s1_w6;
// Stage 2
wire s2_w0 = a2[0] & a2[1];
wire s2_w1 = a2[2] & a2[3];
wire s2_w2 = s2_w0 & s2_w1;
wire s2_w3 = s2_w2 & a2[4];
wire s2_w4 = c1 & a2[5];
wire s2_w5 = s2_w3 & a2[6];
wire s2_w6 = s2_w4 & a2[7];
assign c2 = s2_w5 & s2_w6;
assign y = c2;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
extract_reduce
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 1 t:$reduce_and
design -reset
log -pop
log -header "Stress test reconvergence"
log -push
design -reset
read_verilog <<EOF
module top (
input wire [7:0] a0,
input wire [7:0] a1,
input wire [7:0] a2,
output wire y
);
wire c0, c1, c2;
// Stage 0
wire s0_w0 = a0[0] & a0[1];
wire s0_w1 = a0[2] & a0[3];
wire s0_w2 = s0_w0 & s0_w1;
wire s0_w3 = s0_w2 & a0[4];
wire s0_w4 = s0_w2 & a0[5];
wire s0_w5 = s0_w3 & a0[6];
wire s0_w6 = s0_w4 & a0[7];
assign c0 = s0_w5 & s0_w6;
// Stage 1
wire s1_w0 = a1[0] & a1[1];
wire s1_w1 = a1[2] & a1[3];
wire s1_w2 = s1_w0 & s1_w1;
wire s1_w3 = s1_w2 & a1[4];
wire s1_w4 = s1_w2 & a1[5];
wire s1_w5 = s1_w3 & a1[6];
wire s1_w6 = s1_w4 & c0;
assign c1 = s1_w5 & s1_w6;
// Stage 2
wire s2_w0 = a2[0] & a2[1];
wire s2_w1 = a2[2] & a2[3];
wire s2_w2 = s2_w0 & s2_w1;
wire s2_w3 = s2_w2 & a2[4];
wire s2_w4 = s2_w2 & a2[5];
wire s2_w5 = s2_w3 & a2[6];
wire s2_w6 = s2_w4 & c1;
assign c2 = s2_w5 & s2_w6;
assign y = c2;
endmodule
EOF
check -assert
# Check equivalence after extract_reduce
equiv_opt -assert extract_reduce
# Load design and run opt_clean to remove unnecessary wires
design -load postopt
opt_clean
# Check final design has correct number of gates and inputs
select -assert-count 0 t:$and
select -assert-count 4 t:$reduce_and
design -reset
log -pop