Skip to content

Commit 468dbd4

Browse files
committed
Add xdsl constant folding workload
1 parent 29cd683 commit 468dbd4

File tree

5 files changed

+363
-0
lines changed

5 files changed

+363
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ unpack_sequence <local>
9797
unpickle <local:pickle>
9898
unpickle_list <local:pickle>
9999
unpickle_pure_python <local:pickle>
100+
xdsl <local>
100101
xml_etree <local>
101102

102103

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_xdsl"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf", "xdsl"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "xdsl"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xdsl==0.46.0
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
Benchmark constant folding over a module of integer addition of constants using
3+
xDSL's pattern rewriting machinery. The workload module is generated by the
4+
``get_constant_folding_module()`` function, which is then transformed by the
5+
``xdsl.transforms.test_constant_folding.TestConstantFoldingPass`` xDSL
6+
transformation pass.
7+
"""
8+
9+
import random
10+
11+
import pyperf
12+
13+
from xdsl.context import Context
14+
from xdsl.dialects.arith import AddiOp, Arith, ConstantOp
15+
from xdsl.dialects.builtin import (
16+
Builtin,
17+
IntegerAttr,
18+
ModuleOp,
19+
i32,
20+
)
21+
from xdsl.dialects.test import TestOp
22+
from xdsl.ir import Operation
23+
from xdsl.transforms.test_constant_folding import TestConstantFoldingPass
24+
25+
26+
RANDOM_SEED = 0
27+
28+
29+
def get_constant_folding_module(size: int = 100) -> ModuleOp:
30+
"""Generate an integer addition constant folding workload of a given size.
31+
32+
The output of running the command
33+
`print(WorkloadBuilder().constant_folding_module(size=5))` is shown
34+
below:
35+
36+
```mlir
37+
"builtin.module"() ({
38+
%0 = "arith.constant"() {"value" = 865 : i32} : () -> i32
39+
%1 = "arith.constant"() {"value" = 395 : i32} : () -> i32
40+
%2 = "arith.addi"(%1, %0) : (i32, i32) -> i32
41+
%3 = "arith.constant"() {"value" = 777 : i32} : () -> i32
42+
%4 = "arith.addi"(%3, %2) : (i32, i32) -> i32
43+
%5 = "arith.constant"() {"value" = 912 : i32} : () -> i32
44+
"test.op"(%4) : (i32) -> ()
45+
}) : () -> ()
46+
```
47+
"""
48+
assert size >= 0
49+
random.seed(RANDOM_SEED)
50+
ops: list[Operation] = []
51+
ops.append(ConstantOp(IntegerAttr(random.randint(1, 1000), i32)))
52+
for i in range(1, size + 1):
53+
if i % 2 == 0:
54+
ops.append(AddiOp(ops[i - 1], ops[i - 2]))
55+
else:
56+
ops.append(ConstantOp(IntegerAttr(random.randint(1, 1000), i32)))
57+
ops.append(TestOp([ops[(size // 2) * 2]]))
58+
return ModuleOp(ops)
59+
60+
61+
def bench_xdsl_constant_folding(loops: int, size: int) -> float:
62+
"""Benchmark constant folding integer additions with xDSL."""
63+
ctx = Context(allow_unregistered=True)
64+
ctx.load_dialect(Arith)
65+
ctx.load_dialect(Builtin)
66+
constant_folding_pass = TestConstantFoldingPass()
67+
constant_folding_workload = get_constant_folding_module(size)
68+
69+
range_it = range(loops)
70+
t0 = pyperf.perf_counter()
71+
for _ in range_it:
72+
constant_folding_workload_mutable = constant_folding_workload.clone()
73+
constant_folding_pass.apply(ctx, constant_folding_workload_mutable)
74+
return pyperf.perf_counter() - t0
75+
76+
77+
def add_cmdline_args(cmd, args):
78+
"""Add command line arguments to a pyperf runner"""
79+
cmd.extend(("--size", str(args.size)))
80+
81+
82+
if __name__ == "__main__":
83+
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
84+
runner.metadata['description'] = "Benchmark xDSL constant folding integer addition"
85+
runner.argparser.add_argument("--size", type=int, default=1000,
86+
help="Number of integer additions (default: 100)")
87+
args = runner.parse_args()
88+
runner.bench_time_func('xdsl_constant_fold', bench_xdsl_constant_folding, args.size)

0 commit comments

Comments
 (0)