File: test_elaborate.py

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (392 lines) | stat: -rw-r--r-- 11,919 bytes parent folder | download | duplicates (2)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from __future__ import absolute_import
from base.instructions import vselect, vsplit, vconcat, iconst, iadd, bint
from base.instructions import b1, icmp, ireduce, iadd_cout
from base.immediates import intcc, imm64
from base.types import i64, i8, b32, i32, i16, f32
from cdsl.typevar import TypeVar
from cdsl.ast import Var
from cdsl.xform import Rtl
from unittest import TestCase
from .elaborate import elaborate
from .primitives import prim_to_bv, bvsplit, prim_from_bv, bvconcat, bvadd, \
    bvult, bv_from_imm64, bvite
import base.semantics  # noqa


def concrete_rtls_eq(r1, r2):
    # type: (Rtl, Rtl) -> bool
    """
    Check whether 2 concrete Rtls are equivalent. That is:
        1) They are structurally the same (i.e. there is a substitution between
        them)
        2) Corresponding Vars between them have the same singleton type.
    """
    assert r1.is_concrete()
    assert r2.is_concrete()

    s = r1.substitution(r2, {})

    if s is None:
        return False

    for (v, v1) in s.items():
        if v.get_typevar().singleton_type() !=\
           v1.get_typevar().singleton_type():
            return False

    return True


class TestCleanupConcreteRtl(TestCase):
    """
    Test cleanup_concrete_rtl(). cleanup_concrete_rtl() should take Rtls for
    which we can infer a single concrete typing, and update the TypeVars
    in-place to singleton TVs.
    """
    def test_cleanup_concrete_rtl(self):
        # type: () -> None
        typ = i64.by(4)
        x = Var('x')
        lo = Var('lo')
        hi = Var('hi')

        r = Rtl(
                (lo, hi) << vsplit(x),
        )
        r1 = r.copy({})
        s = r.substitution(r1, {})

        s[x].set_typevar(TypeVar.singleton(typ))
        r1.cleanup_concrete_rtl()
        assert s is not None
        assert s[x].get_typevar().singleton_type() == typ
        assert s[lo].get_typevar().singleton_type() == i64.by(2)
        assert s[hi].get_typevar().singleton_type() == i64.by(2)

    def test_cleanup_concrete_rtl_fail(self):
        # type: () -> None
        x = Var('x')
        lo = Var('lo')
        hi = Var('hi')
        r = Rtl(
                (lo, hi) << vsplit(x),
        )

        with self.assertRaises(AssertionError):
            r.cleanup_concrete_rtl()

    def test_cleanup_concrete_rtl_ireduce(self):
        # type: () -> None
        x = Var('x')
        y = Var('y')
        r = Rtl(
                y << ireduce(x),
        )
        r1 = r.copy({})
        s = r.substitution(r1, {})
        s[x].set_typevar(TypeVar.singleton(i8.by(2)))
        r1.cleanup_concrete_rtl()

        assert s is not None
        assert s[x].get_typevar().singleton_type() == i8.by(2)
        assert s[y].get_typevar().singleton_type() == i8.by(2)

    def test_cleanup_concrete_rtl_ireduce_bad(self):
        # type: () -> None
        x = Var('x')
        y = Var('y')
        x.set_typevar(TypeVar.singleton(i16.by(1)))
        r = Rtl(
                y << ireduce(x),
        )

        with self.assertRaises(AssertionError):
            r.cleanup_concrete_rtl()

    def test_vselect_icmpimm(self):
        # type: () -> None
        x = Var('x')
        y = Var('y')
        z = Var('z')
        w = Var('w')
        v = Var('v')
        zeroes = Var('zeroes')
        imm0 = Var("imm0")

        r = Rtl(
                zeroes << iconst(imm0),
                y << icmp(intcc.eq, x, zeroes),
                v << vselect(y, z, w),
        )
        r1 = r.copy({})

        s = r.substitution(r1, {})
        s[zeroes].set_typevar(TypeVar.singleton(i32.by(4)))
        s[z].set_typevar(TypeVar.singleton(f32.by(4)))

        r1.cleanup_concrete_rtl()

        assert s is not None
        assert s[zeroes].get_typevar().singleton_type() == i32.by(4)
        assert s[x].get_typevar().singleton_type() == i32.by(4)
        assert s[y].get_typevar().singleton_type() == b32.by(4)
        assert s[z].get_typevar().singleton_type() == f32.by(4)
        assert s[w].get_typevar().singleton_type() == f32.by(4)
        assert s[v].get_typevar().singleton_type() == f32.by(4)

    def test_bint(self):
        # type: () -> None
        x = Var('x')
        y = Var('y')
        z = Var('z')
        w = Var('w')
        v = Var('v')
        u = Var('u')

        r = Rtl(
            z << iadd(x, y),
            w << bint(v),
            u << iadd(z, w)
        )
        r1 = r.copy({})
        s = r.substitution(r1, {})

        s[x].set_typevar(TypeVar.singleton(i32.by(8)))
        s[z].set_typevar(TypeVar.singleton(i32.by(8)))
        # TODO: Relax this to simd=True
        s[v].set_typevar(TypeVar('v', '', bools=(1, 1), simd=(8, 8)))
        r1.cleanup_concrete_rtl()

        assert s is not None
        assert s[x].get_typevar().singleton_type() == i32.by(8)
        assert s[y].get_typevar().singleton_type() == i32.by(8)
        assert s[z].get_typevar().singleton_type() == i32.by(8)
        assert s[w].get_typevar().singleton_type() == i32.by(8)
        assert s[u].get_typevar().singleton_type() == i32.by(8)
        assert s[v].get_typevar().singleton_type() == b1.by(8)


class TestElaborate(TestCase):
    """
    Test semantics elaboration.
    """
    def setUp(self):
        # type: () -> None
        self.v0 = Var("v0")
        self.v1 = Var("v1")
        self.v2 = Var("v2")
        self.v3 = Var("v3")
        self.v4 = Var("v4")
        self.v5 = Var("v5")
        self.v6 = Var("v6")
        self.v7 = Var("v7")
        self.v8 = Var("v8")
        self.v9 = Var("v9")
        self.imm0 = Var("imm0")
        self.IxN_nonscalar = TypeVar("IxN_nonscalar", "", ints=True,
                                     scalars=False, simd=True)
        self.TxN = TypeVar("TxN", "", ints=True, bools=True, floats=True,
                           scalars=False, simd=True)
        self.b1 = TypeVar.singleton(b1)

    def test_elaborate_vsplit(self):
        # type: () -> None
        i32.by(4)  # Make sure i32x4 exists.
        i32.by(2)  # Make sure i32x2 exists.
        r = Rtl(
                (self.v0, self.v1) << vsplit.i32x4(self.v2),
        )
        r.cleanup_concrete_rtl()
        sem = elaborate(r)
        bvx = Var('bvx')
        bvlo = Var('bvlo')
        bvhi = Var('bvhi')
        x = Var('x')
        lo = Var('lo')
        hi = Var('hi')

        exp = Rtl(
            bvx << prim_to_bv.i32x4(x),
            (bvlo, bvhi) << bvsplit.bv128(bvx),
            lo << prim_from_bv.i32x2(bvlo),
            hi << prim_from_bv.i32x2(bvhi)
        )
        exp.cleanup_concrete_rtl()

        assert concrete_rtls_eq(sem, exp)

    def test_elaborate_vconcat(self):
        # type: () -> None
        i32.by(4)  # Make sure i32x4 exists.
        i32.by(2)  # Make sure i32x2 exists.
        r = Rtl(
                self.v0 << vconcat.i32x2(self.v1, self.v2),
        )
        r.cleanup_concrete_rtl()
        sem = elaborate(r)
        bvx = Var('bvx')
        bvlo = Var('bvlo')
        bvhi = Var('bvhi')
        x = Var('x')
        lo = Var('lo')
        hi = Var('hi')

        exp = Rtl(
            bvlo << prim_to_bv.i32x2(lo),
            bvhi << prim_to_bv.i32x2(hi),
            bvx << bvconcat.bv64(bvlo, bvhi),
            x << prim_from_bv.i32x4(bvx)
        )
        exp.cleanup_concrete_rtl()

        assert concrete_rtls_eq(sem, exp)

    def test_elaborate_iadd_simple(self):
        # type: () -> None
        i32.by(2)  # Make sure i32x2 exists.
        x = Var('x')
        y = Var('y')
        a = Var('a')
        bvx = Var('bvx')
        bvy = Var('bvy')
        bva = Var('bva')
        r = Rtl(
                a << iadd.i32(x, y),
        )
        r.cleanup_concrete_rtl()
        sem = elaborate(r)
        exp = Rtl(
            bvx << prim_to_bv.i32(x),
            bvy << prim_to_bv.i32(y),
            bva << bvadd.bv32(bvx, bvy),
            a << prim_from_bv.i32(bva)
        )
        exp.cleanup_concrete_rtl()

        assert concrete_rtls_eq(sem, exp)

    def test_elaborate_iadd_elaborate_1(self):
        # type: () -> None
        i32.by(2)  # Make sure i32x2 exists.
        r = Rtl(
                self.v0 << iadd.i32x2(self.v1, self.v2),
        )
        r.cleanup_concrete_rtl()
        sem = elaborate(r)
        x = Var('x')
        y = Var('y')
        a = Var('a')
        bvx_1 = Var('bvx_1')
        bvx_2 = Var('bvx_2')
        bvx_5 = Var('bvx_5')
        bvlo_1 = Var('bvlo_1')
        bvlo_2 = Var('bvlo_2')
        bvhi_1 = Var('bvhi_1')
        bvhi_2 = Var('bvhi_2')

        bva_3 = Var('bva_3')
        bva_4 = Var('bva_4')

        exp = Rtl(
            bvx_1 << prim_to_bv.i32x2(x),
            (bvlo_1, bvhi_1) << bvsplit.bv64(bvx_1),
            bvx_2 << prim_to_bv.i32x2(y),
            (bvlo_2, bvhi_2) << bvsplit.bv64(bvx_2),
            bva_3 << bvadd.bv32(bvlo_1, bvlo_2),
            bva_4 << bvadd.bv32(bvhi_1, bvhi_2),
            bvx_5 << bvconcat.bv32(bva_3, bva_4),
            a << prim_from_bv.i32x2(bvx_5)
        )
        exp.cleanup_concrete_rtl()

        assert concrete_rtls_eq(sem, exp)

    def test_elaborate_iadd_elaborate_2(self):
        # type: () -> None
        i8.by(4)  # Make sure i32x2 exists.
        r = Rtl(
                self.v0 << iadd.i8x4(self.v1, self.v2),
        )
        r.cleanup_concrete_rtl()

        sem = elaborate(r)
        x = Var('x')
        y = Var('y')
        a = Var('a')
        bvx_1 = Var('bvx_1')
        bvx_2 = Var('bvx_2')
        bvx_5 = Var('bvx_5')
        bvx_10 = Var('bvx_10')
        bvx_15 = Var('bvx_15')

        bvlo_1 = Var('bvlo_1')
        bvlo_2 = Var('bvlo_2')
        bvlo_6 = Var('bvlo_6')
        bvlo_7 = Var('bvlo_7')
        bvlo_11 = Var('bvlo_11')
        bvlo_12 = Var('bvlo_12')

        bvhi_1 = Var('bvhi_1')
        bvhi_2 = Var('bvhi_2')
        bvhi_6 = Var('bvhi_6')
        bvhi_7 = Var('bvhi_7')
        bvhi_11 = Var('bvhi_11')
        bvhi_12 = Var('bvhi_12')

        bva_8 = Var('bva_8')
        bva_9 = Var('bva_9')
        bva_13 = Var('bva_13')
        bva_14 = Var('bva_14')

        exp = Rtl(
            bvx_1 << prim_to_bv.i8x4(x),
            (bvlo_1, bvhi_1) << bvsplit.bv32(bvx_1),
            bvx_2 << prim_to_bv.i8x4(y),
            (bvlo_2, bvhi_2) << bvsplit.bv32(bvx_2),
            (bvlo_6, bvhi_6) << bvsplit.bv16(bvlo_1),
            (bvlo_7, bvhi_7) << bvsplit.bv16(bvlo_2),
            bva_8 << bvadd.bv8(bvlo_6, bvlo_7),
            bva_9 << bvadd.bv8(bvhi_6, bvhi_7),
            bvx_10 << bvconcat.bv8(bva_8, bva_9),
            (bvlo_11, bvhi_11) << bvsplit.bv16(bvhi_1),
            (bvlo_12, bvhi_12) << bvsplit.bv16(bvhi_2),
            bva_13 << bvadd.bv8(bvlo_11, bvlo_12),
            bva_14 << bvadd.bv8(bvhi_11, bvhi_12),
            bvx_15 << bvconcat.bv8(bva_13, bva_14),
            bvx_5 << bvconcat.bv16(bvx_10, bvx_15),
            a << prim_from_bv.i8x4(bvx_5)
        )
        exp.cleanup_concrete_rtl()
        assert concrete_rtls_eq(sem, exp)

    def test_elaborate_iadd_cout_simple(self):
        # type: () -> None
        x = Var('x')
        y = Var('y')
        a = Var('a')
        c_out = Var('c_out')
        bvc_out = Var('bvc_out')
        bc_out = Var('bc_out')
        bvx = Var('bvx')
        bvy = Var('bvy')
        bva = Var('bva')
        bvone = Var('bvone')
        bvzero = Var('bvzero')
        r = Rtl(
                (a, c_out) << iadd_cout.i32(x, y),
        )
        r.cleanup_concrete_rtl()
        sem = elaborate(r)
        exp = Rtl(
            bvx << prim_to_bv.i32(x),
            bvy << prim_to_bv.i32(y),
            bva << bvadd.bv32(bvx, bvy),
            bc_out << bvult.bv32(bva, bvx),
            bvone << bv_from_imm64(imm64(1)),
            bvzero << bv_from_imm64(imm64(0)),
            bvc_out << bvite(bc_out, bvone, bvzero),
            a << prim_from_bv.i32(bva),
            c_out << prim_from_bv.b1(bvc_out)
        )
        exp.cleanup_concrete_rtl()
        assert concrete_rtls_eq(sem, exp)