diff --git a/benchmarks/mem/two-sum.bril b/benchmarks/mem/two-sum.bril new file mode 100644 index 000000000..e39cc28e8 --- /dev/null +++ b/benchmarks/mem/two-sum.bril @@ -0,0 +1,127 @@ +# ARGS: 9 +@main(target: int) { + arr: ptr = call @initArr; + arr: ptr = id arr; + v45: ptr = id arr; + v46: int = const 4; + result: ptr = call @twoSum v45 v46 target; + result: ptr = id result; + v47: ptr = id arr; + free v47; + v48: int = const 0; + v49: ptr = id result; + v50: int = const 0; + v51: ptr = ptradd v49 v50; + v52: int = load v51; + print v52; + v53: int = const 0; + v54: ptr = id result; + v55: int = const 1; + v56: ptr = ptradd v54 v55; + v57: int = load v56; + print v57; + v58: int = const 0; + v59: ptr = id result; + free v59; + v60: int = const 0; +} +@initArr: ptr { + v0: int = const 4; + v1: ptr = alloc v0; + arr: ptr = id v1; + v2: ptr = id arr; + v3: int = const 0; + v4: ptr = ptradd v2 v3; + v5: int = const 2; + store v4 v5; + v6: int = const 0; + v7: ptr = id arr; + v8: int = const 1; + v9: ptr = ptradd v7 v8; + v10: int = const 7; + store v9 v10; + v11: int = const 0; + v12: ptr = id arr; + v13: int = const 2; + v14: ptr = ptradd v12 v13; + v15: int = const 11; + store v14 v15; + v16: int = const 0; + v17: ptr = id arr; + v18: int = const 3; + v19: ptr = ptradd v17 v18; + v20: int = const 15; + store v19 v20; + v21: int = const 0; + v22: ptr = id arr; + ret v22; +} +@twoSum(nums: ptr, size: int, target: int): ptr { + v0: int = const 2; + v1: ptr = alloc v0; + res: ptr = id v1; + v3: int = const 0; + i: int = id v3; +.for.cond.2: + v4: int = id i; + v5: int = id size; + v6: bool = lt v4 v5; + br v6 .for.body.2 .for.end.2; +.for.body.2: + v8: int = id i; + v9: int = const 1; + v10: int = add v8 v9; + j: int = id v10; +.for.cond.7: + v11: int = id j; + v12: int = id size; + v13: bool = lt v11 v12; + br v13 .for.body.7 .for.end.7; +.for.body.7: + v14: ptr = id nums; + v15: int = id i; + v16: ptr = ptradd v14 v15; + v17: int = load v16; + v18: ptr = id nums; + v19: int = id j; + v20: ptr = ptradd v18 v19; + v21: int = load v20; + v22: int = add v17 v21; + sum: int = id v22; + v24: int = id sum; + v25: int = id target; + v26: bool = eq v24 v25; + br v26 .then.23 .else.23; +.then.23: + v27: ptr = id res; + v28: int = const 0; + v29: ptr = ptradd v27 v28; + v30: int = id i; + store v29 v30; + v31: int = const 0; + v32: ptr = id res; + v33: int = const 1; + v34: ptr = ptradd v32 v33; + v35: int = id j; + store v34 v35; + v36: int = const 0; + v37: ptr = id res; + ret v37; + jmp .endif.23; +.else.23: +.endif.23: + v38: int = id j; + v39: int = const 1; + v40: int = add v38 v39; + j: int = id v40; + jmp .for.cond.7; +.for.end.7: + v41: int = id i; + v42: int = const 1; + v43: int = add v41 v42; + i: int = id v43; + jmp .for.cond.2; +.for.end.2: + v44: ptr = id res; + ret v44; +} diff --git a/benchmarks/mem/two-sum.out b/benchmarks/mem/two-sum.out new file mode 100644 index 000000000..0d66ea1ae --- /dev/null +++ b/benchmarks/mem/two-sum.out @@ -0,0 +1,2 @@ +0 +1 diff --git a/benchmarks/mem/two-sum.prof b/benchmarks/mem/two-sum.prof new file mode 100644 index 000000000..0b93cdb47 --- /dev/null +++ b/benchmarks/mem/two-sum.prof @@ -0,0 +1 @@ +total_dyn_inst: 98 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 690042e23..bbce89d1f 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -62,6 +62,7 @@ The current benchmarks are: * `sum-divisors`: Prints the positive integer divisors of the input integer, followed by the sum of the divisors. * `sum-sq-diff`: Output the difference between the sum of the squares of the first *n* natural numbers and the square of their sum. * `totient`: Computes [Euler's totient function][totient] on an input integer *n*. +* `two-sum`: Print the indices of two distinct elements in the list [2, 7, 11, 13] whose sum equals the input. * `up-arrow`: Computes [Knuth's up arrow][uparrow] notation, with the first argument being the number, the second argument being the number of Knuth's up arrows, and the third argument being the number of repeats. * `vsmul`: Multiplies a constant scalar to each element of a large array. Tests the performance of vectorization optimizations. * `reverse`: Compute number with reversed digits (e.g. 123 -> 321).