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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""
Abstract syntax trees.
This module defines classes that can be used to create abstract syntax trees
for patern matching an rewriting of cranelift instructions.
"""
# noqa
# noqa
# noqa
# noqa
=
pass
# type: (Expr, VarAtomMap) -> Expr
"""
Given a var v return either m[v] or a new variable v' (and remember
m[v]=v'). Otherwise return the argument unchanged
"""
= # type: Atom
=
return
return
"""
An AST definition associates a set of variables with the values produced by
an expression.
Example:
>>> from base.instructions import iadd_cout, iconst
>>> x = Var('x')
>>> y = Var('y')
>>> x << iconst(4)
(Var(x),) << Apply(iconst, (4,))
>>> (x, y) << iadd_cout(4, 5)
(Var(x), Var(y)) << Apply(iadd_cout, (4, 5))
The `<<` operator is used to create variable definitions.
:param defs: Single variable or tuple of variables to be defined.
:param expr: Expression generating the values.
"""
# type: (Union[Var, Tuple[Var, ...]], Apply) -> None
= # type: Tuple[Var, ...]
=
assert
=
# type: () -> str
return
# type: () -> str
return
return
# type: (VarAtomMap) -> Def
"""
Return a copy of this Def with vars replaced with fresh variables,
in accordance with the map m. Update m as neccessary.
"""
=
= # type: List[Var]
=
assert
return
# type: () -> Set[Var]
""" Return the set of all Vars that are defined by self"""
return
# type: () -> Set[Var]
""" Return the set of all Vars that are used(read) by self"""
return
# type: () -> Set[Var]
"""Return the set of all Vars in self that correspond to SSA values"""
return
# type: (Def, VarAtomMap) -> Optional[VarAtomMap]
"""
If the Defs self and other agree structurally, return a variable
substitution to transform self to other. Otherwise return None. Two
Defs agree structurally if there exists a Var substitution, that can
transform one into the other. See Apply.substitution() for more
details.
"""
=
return
assert ==
assert not in # Guaranteed by SSA form
=
return
"""
An AST expression.
"""
"""
An Atom in the DSL is either a literal or a Var
"""
"""
A free variable.
When variables are used in `XForms` with source and destination patterns,
they are classified as follows:
Input values
Uses in the source pattern with no preceding def. These may appear as
inputs in the destination pattern too, but no new inputs can be
introduced.
Output values
Variables that are defined in both the source and destination pattern.
These values may have uses outside the source pattern, and the
destination pattern must compute the same value.
Intermediate values
Values that are defined in the source pattern, but not in the
destination pattern. These may have uses outside the source pattern, so
the defining instruction can't be deleted immediately.
Temporary values
Values that are defined only in the destination pattern.
"""
# type: (str, TypeVar) -> None
=
# The `Def` defining this variable in a source pattern.
= None # type: Def
# The `Def` defining this variable in a destination pattern.
= None # type: Def
# TypeVar representing the type of this variable.
= # type: TypeVar
# The original 'typeof(x)' type variable that was created for this Var.
# This one doesn't change. `self.typevar` above may be changed to
# another typevar by type inference.
= # type: TypeVar
# type: () -> str
return
# type: () -> str
=
+=
+=
return
# Context bits for `set_def` indicating which pattern has defines of this
# var.
= 1
= 2
# type: (int, Def) -> None
"""
Set the `Def` that defines this variable in the given context.
The `context` must be one of `SRCCTX` or `DSTCTX`
"""
=
=
# type: (int) -> Def
"""
Get the def of this variable in context.
The `context` must be one of `SRCCTX` or `DSTCTX`
"""
return
return
# type: () -> bool
"""Is this an input value to the src pattern?"""
return is None and is None
# type: () -> bool
"""Is this an output value, defined in both src and dst patterns?"""
return is not None and is not None
# type: () -> bool
"""Is this an intermediate value, defined only in the src pattern?"""
return is not None and is None
# type: () -> bool
"""Is this a temp value, defined only in the dst pattern?"""
return is None and is not None
# type: () -> TypeVar
"""Get the type variable representing the type of this variable."""
# Create a TypeVar allowing all types.
=
=
=
return
# type: (TypeVar) -> None
=
# type: () -> bool
"""
Check if this variable has a free type variable.
If not, the type of this variable is computed from the type of another
variable.
"""
return False
return is
# type: () -> str
"""
Get a Rust expression that computes the type of this variable.
It is assumed that local variables exist corresponding to the free type
variables.
"""
return
"""
Apply an instruction to arguments.
An `Apply` AST expression is created by using function call syntax on
instructions. This applies to both bound and unbound polymorphic
instructions:
>>> from base.instructions import jump, iadd
>>> jump('next', ())
Apply(jump, ('next', ()))
>>> iadd.i32('x', 'y')
Apply(iadd.i32, ('x', 'y'))
:param inst: The instruction being applied, an `Instruction` or
`BoundInstruction` instance.
:param args: Tuple of arguments.
"""
# type: (instructions.MaybeBoundInst, Tuple[Expr, ...]) -> None # noqa
=
=
assert
=
=
=
assert ==
# Check that the kinds of Literals arguments match the expected Operand
=
=
assert == , \
\
# type: (Union[Var, Tuple[Var, ...]]) -> Def
"""
Define variables using `var << expr` or `(v1, v2) << expr`.
"""
return
# type: () -> str
=
+=
return
# type: () -> str
return
# type: () -> str
=
return
# type: (Sequence[Var]) -> str
"""
Return a Rust Builder method call for instantiating this instruction
application.
The `defs` argument should be a list of variables defined by this
instruction. It is used to construct a result type if necessary.
"""
=
# Do we need to pass an explicit type argument?
= + +
=
return
# type: () -> PredNode
"""
Construct an instruction predicate that verifies the immediate operands
on this instruction.
Immediate operands in a source pattern can be either free variables or
constants like `ConstantInt` and `Enumerator`. We don't currently
support constraints on free variables, but we may in the future.
"""
= None # type: PredNode
=
# Examine all of the immediate operands.
=
# Ignore free variables for now. We may add variable predicates
# later.
continue
=
# Add checks for any bound secondary type variables.
# We can't check the controlling type variable this way since it may
# not appear as the type of an operand.
continue
=
=
return
# type: () -> PredNode
"""
Same as `inst_predicate()`, but also check the controlling type
variable.
"""
=
=
= None # type: PredNode
# Prefer to look at the types of input operands.
=
=
=
return
# type: (VarAtomMap) -> Apply
"""
Return a copy of this Expr with vars replaced with fresh variables,
in accordance with the map m. Update m as neccessary.
"""
return
# type: () -> Set[Var]
"""Return the set of all Vars in self that correspond to SSA values"""
=
=
assert
return
# type: (Apply, VarAtomMap) -> Optional[VarAtomMap]
"""
If there is a substituion from Var->Atom that converts self to other,
return it, otherwise return None. Note that this is strictly weaker
than unification (see TestXForm.test_subst_enum_bad_var_const for
example).
"""
return None
# Guaranteed by self.inst == other.inst
assert
assert and
=
return None
assert
=
return None
assert
# Guaranteed by self.inst == other.inst
assert ==
return None
return
"""
Base Class for all literal expressions in the DSL.
"""
# type: (ImmediateKind, Any) -> None
=
=
# type: (Any) -> bool
return False
return False
# Can't just compare value here, as comparison Any <> Any returns Any
return ==
# type: (Any) -> bool
return not
# type: () -> str
return
"""
A value of an integer immediate operand.
Immediate operands like `imm64` or `offset32` can be specified in AST
expressions using the call syntax: `imm64(5)` which greates a `ConstantInt`
node.
"""
# type: (ImmediateKind, int) -> None
# type: () -> str
# If the value is in the signed imm64 range, print it as-is.
return
# Otherwise if the value is in the unsigned imm64 range, print its
# bitwise counterpart in the signed imm64 range.
return
assert False,
"""
A bitwise value of an immediate operand.
This is used to create bitwise exact floating point constants using
`ieee32.bits(0x80000000)`.
"""
# type: (ImmediateKind, int) -> None
=
# type: () -> str
"""
Get the Rust expression form of this constant.
"""
return
"""
A value of an enumerated immediate operand.
Some immediate operand kinds like `intcc` and `floatcc` have an enumerated
range of values corresponding to a Rust enum type. An `Enumerator` object
is an AST leaf node representing one of the values.
:param kind: The enumerated `ImmediateKind` containing the value.
:param value: The textual IR representation of the value.
`Enumerator` nodes are not usually created directly. They are created by
using the dot syntax on immediate kinds: `intcc.ult`.
"""
# type: (ImmediateKind, str) -> None
# type: () -> str
"""
Get the Rust expression form of this enumerator.
"""
return