-
Notifications
You must be signed in to change notification settings - Fork 70
/
test_memref.py
185 lines (143 loc) · 6.63 KB
/
test_memref.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from io import StringIO
from xdsl.ir import OpResult, Block
from xdsl.dialects.builtin import i32, IntegerType, IndexType
from xdsl.dialects.memref import (Alloc, Alloca, Dealloc, Dealloca, MemRefType,
Load, Store)
from xdsl.dialects import builtin, memref, func, arith, scf
from xdsl.printer import Printer
def test_memreftype():
mem1 = MemRefType.from_element_type_and_shape(i32, [1])
assert mem1.get_num_dims() == 1
assert mem1.get_shape() == [1]
assert mem1.element_type is i32
mem2 = MemRefType.from_element_type_and_shape(i32, [3, 3, 3])
assert mem2.get_num_dims() == 3
assert mem2.get_shape() == [3, 3, 3]
assert mem2.element_type is i32
my_i32 = IntegerType.from_width(32)
mem3 = MemRefType.from_params(my_i32)
assert mem3.get_num_dims() == 1
assert mem3.get_shape() == [1]
assert mem3.element_type is my_i32
def test_memref_load_i32():
i32_memref_type = MemRefType.from_element_type_and_shape(i32, [1])
memref_ssa_value = OpResult(i32_memref_type, [], [])
load = Load.get(memref_ssa_value, [])
assert load.memref is memref_ssa_value
assert load.indices == ()
assert load.res.typ is i32
def test_memref_load_i32_with_dimensions():
i32_memref_type = MemRefType.from_element_type_and_shape(i32, [2, 3])
memref_ssa_value = OpResult(i32_memref_type, [], [])
index1 = OpResult(IndexType, [], [])
index2 = OpResult(IndexType, [], [])
load = Load.get(memref_ssa_value, [index1, index2])
assert load.memref is memref_ssa_value
assert load.indices[0] is index1
assert load.indices[1] is index2
assert load.res.typ is i32
def test_memref_store_i32():
i32_memref_type = MemRefType.from_element_type_and_shape(i32, [1])
memref_ssa_value = OpResult(i32_memref_type, [], [])
i32_ssa_value = OpResult(i32, [], [])
store = Store.get(i32_ssa_value, memref_ssa_value, [])
assert store.memref is memref_ssa_value
assert store.indices == ()
assert store.value is i32_ssa_value
def test_memref_store_i32_with_dimensions():
i32_memref_type = MemRefType.from_element_type_and_shape(i32, [2, 3])
memref_ssa_value = OpResult(i32_memref_type, [], [])
i32_ssa_value = OpResult(i32, [], [])
index1 = OpResult(IndexType, [], [])
index2 = OpResult(IndexType, [], [])
store = Store.get(i32_ssa_value, memref_ssa_value, [index1, index2])
assert store.memref is memref_ssa_value
assert store.indices[0] is index1
assert store.indices[1] is index2
assert store.value is i32_ssa_value
def test_memref_alloc():
my_i32 = IntegerType.from_width(32)
alloc0 = Alloc.get(my_i32, 64, [3, 1, 2])
alloc1 = Alloc.get(my_i32, 64)
assert alloc0.dynamic_sizes == ()
assert type(alloc0.results[0]) is OpResult
assert alloc0.results[0].typ.get_shape() == [3, 1, 2]
assert type(alloc0.results[0].typ) is MemRefType
assert type(alloc1.results[0]) is OpResult
assert alloc1.results[0].typ.get_shape() == [1]
assert type(alloc1.results[0].typ) is MemRefType
def test_memref_alloca():
my_i32 = IntegerType.from_width(32)
alloc0 = Alloca.get(my_i32, 64, [3, 1, 2])
alloc1 = Alloca.get(my_i32, 64)
assert type(alloc0.results[0]) is OpResult
assert alloc0.results[0].typ.get_shape() == [3, 1, 2]
assert type(alloc0.results[0].typ) is MemRefType
assert type(alloc1.results[0]) is OpResult
assert alloc1.results[0].typ.get_shape() == [1]
assert type(alloc1.results[0].typ) is MemRefType
def test_memref_dealloc():
my_i32 = IntegerType.from_width(32)
alloc0 = Alloc.get(my_i32, 64, [3, 1, 2])
dealloc0 = Dealloc.get(alloc0)
assert type(dealloc0.memref) is OpResult
def test_memref_dealloca():
my_i32 = IntegerType.from_width(32)
alloc0 = Alloca.get(my_i32, 64, [3, 1, 2])
dealloc0 = Dealloca.get(alloc0)
assert type(dealloc0.memref) is OpResult
def test_memref_dim():
idx = arith.Constant.from_int_and_width(1, IndexType())
alloc0 = Alloc.get(i32, 64, [3, 1, 2])
dim_1 = memref.Dim.from_source_and_index(alloc0, idx)
assert dim_1.source is alloc0.memref
assert dim_1.index is idx.result
assert isinstance(dim_1.result.typ, IndexType)
def test_memref_rank():
alloc0 = Alloc.get(i32, 64, [3, 1, 2])
dim_1 = memref.Rank.from_memref(alloc0)
assert dim_1.source is alloc0.memref
assert isinstance(dim_1.rank.typ, IndexType)
def test_memref_matmul_verify():
memref_f64_rank2 = memref.MemRefType.from_element_type_and_shape(
builtin.f64, [-1, -1])
module = builtin.ModuleOp.from_region_or_ops([
func.FuncOp.from_callable(
'matmul',
[memref_f64_rank2, memref_f64_rank2],
[memref_f64_rank2],
lambda a, b: [
lit0 := arith.Constant.from_int_and_width(0, builtin.IndexType()),
lit1 := arith.Constant.from_int_and_width(1, builtin.IndexType()),
dim_a0 := memref.Dim.from_source_and_index(a, lit0),
dim_a1 := memref.Dim.from_source_and_index(a, lit1),
dim_b0 := memref.Dim.from_source_and_index(b, lit0),
dim_b1 := memref.Dim.from_source_and_index(b, lit1),
out := memref.Alloca.get(builtin.f64, 0, [-1, -1], [dim_a0, dim_b1]),
# TODO: assert dim_a0 == dim_b1
lit0_f := arith.Constant.from_float_and_width(0.0, builtin.f64),
scf.For.get(lit0, dim_a0, lit1, [], Block.from_callable([builtin.IndexType()], lambda i: [
# outer loop start, loop_var = i
scf.For.get(lit0, dim_b0, lit1, [], Block.from_callable([builtin.IndexType()], lambda j: [
# mid loop start, loop_var = j
memref.Store.get(lit0_f, out, [i, j]),
scf.For.get(lit0, dim_a1, lit1, [], Block.from_callable([builtin.IndexType()], lambda k: [
# inner loop, loop_var = k
elem_a_i_k := memref.Load.get(a, [i, k]),
elem_b_k_j := memref.Load.get(b, [k, j]),
mul := arith.Mulf.get(elem_a_i_k, elem_b_k_j),
out_i_j := memref.Load.get(out, [i, j]),
new_out_val := arith.Addf.get(out_i_j, mul),
memref.Store.get(new_out_val, out, [i, j]),
scf.Yield()
])),
scf.Yield()
])),
scf.Yield()
])),
func.Return.get(out)
]
)
]) # yapf: disable
# check that it verifies correctly
module.verify()