-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmatch.py
590 lines (418 loc) · 15.6 KB
/
match.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
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
582
583
584
585
586
587
588
589
590
"""
.. autoclass:: Matchable
.. autoclass:: StackMatchComponent
.. autoclass:: StackMatch
.. autofunction:: parse_match
.. autofunction:: parse_stack_match
.. autodata:: ToStackMatchConvertible
Match expressions
^^^^^^^^^^^^^^^^^
.. autoclass:: MatchExpressionBase
.. autoclass:: All
.. autoclass:: And
.. autoclass:: Or
.. autoclass:: Not
.. autoclass:: Id
.. autoclass:: ObjTagged
.. autoclass:: Tagged
.. autoclass:: Writes
.. autoclass:: Reads
.. autoclass:: InKernel
.. autoclass:: Iname
"""
from __future__ import annotations
__copyright__ = "Copyright (C) 2012 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass
from sys import intern
from typing import TYPE_CHECKING, Protocol, Sequence, Union
from typing_extensions import TypeAlias
from loopy.kernel.instruction import InstructionBase
NoneType = type(None)
from pytools.lex import RE
if TYPE_CHECKING:
import pytools.tag
from loopy.kernel import LoopKernel
def re_from_glob(s: str) -> re.Pattern:
from fnmatch import translate
return re.compile("^"+translate(s.strip())+"$")
# {{{ parsing
# {{{ lexer data
_and = intern("and")
_or = intern("or")
_not = intern("not")
_openpar = intern("openpar")
_closepar = intern("closepar")
_id = intern("_id")
_tag = intern("_tag")
_writes = intern("_writes")
_reads = intern("_reads")
_in_kernel = intern("_in_kernel")
_iname = intern("_iname")
_whitespace = intern("_whitespace")
# }}}
_LEX_TABLE = [
(_and, RE(r"and\b")),
(_or, RE(r"or\b")),
(_not, RE(r"not\b")),
(_openpar, RE(r"\(")),
(_closepar, RE(r"\)")),
# TERMINALS
(_id, RE(r"id:([\w?*]+)")),
(_tag, RE(r"tag:([\w?*]+)")),
(_writes, RE(r"writes:([\w?*]+)")),
(_reads, RE(r"reads:([\w?*]+)")),
(_in_kernel, RE(r"in_kernel:([\w?*]+)")),
(_iname, RE(r"iname:([\w?*]+)")),
(_whitespace, RE("[ \t]+")),
]
_TERMINALS = ([_id, _tag, _writes, _reads, _in_kernel, _iname])
# {{{ operator precedence
_PREC_OR = 10
_PREC_AND = 20
_PREC_NOT = 30
# }}}
# }}}
# {{{ match expression
class Matchable(Protocol):
"""
.. attribute:: tags
"""
@property
def tags(self) -> frozenset[pytools.tag.Tag]:
...
class MatchExpressionBase(ABC):
@abstractmethod
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
raise NotImplementedError
def __ne__(self, other):
return not self.__eq__(other)
def __and__(self, other):
return And((self, other))
def __or__(self, other):
return Or((self, other))
def __inv__(self):
return Not(self)
class All(MatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
return True
def __str__(self):
return "all"
def __repr__(self):
return "%s()" % (type(self).__name__)
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, "all_match_expr")
def __eq__(self, other):
return type(self) is type(other)
def __hash__(self):
return hash(type(self))
@dataclass(frozen=True, eq=True)
class MultiChildMatchExpressionBase(MatchExpressionBase):
children: Sequence[MatchExpressionBase]
def __str__(self):
joiner = " %s " % type(self).__name__.lower()
return "(%s)" % (joiner.join(str(ch) for ch in self.children))
def __repr__(self):
return "{}({})".format(
type(self).__name__,
", ".join(repr(ch) for ch in self.children))
class And(MultiChildMatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
return all(ch(kernel, matchable) for ch in self.children)
class Or(MultiChildMatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
return any(ch(kernel, matchable) for ch in self.children)
@dataclass(frozen=True, eq=True)
class Not(MatchExpressionBase):
child: MatchExpressionBase
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
return not self.child(kernel, matchable)
def __str__(self):
return "(not %s)" % str(self.child)
def __repr__(self):
return "{}({!r})".format(type(self).__name__, self.child)
@dataclass(frozen=True, eq=True)
class ObjTagged(MatchExpressionBase):
"""Match if the object is tagged with a given :class:`~pytools.tag.Tag`.
.. note::
These instance-based tags will, in the not-too-distant future, replace
the string-based tags matched by :class:`Tagged`.
"""
tag: pytools.tag.Tag
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
return self.tag in matchable.tags
class GlobMatchExpressionBase(MatchExpressionBase):
def __init__(self, glob: str) -> None:
self.glob = glob
import re
from fnmatch import translate
self.re = re.compile("^"+translate(glob.strip())+"$")
def __str__(self):
descr = type(self).__name__
return descr.lower() + ":" + self.glob
def __repr__(self):
return "{}({!r})".format(type(self).__name__, self. glob)
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, type(self).__name__)
key_builder.rec(key_hash, self.glob)
def __eq__(self, other):
return (type(self) is type(other)
and self.glob == other.glob)
def __hash__(self):
return hash((type(self), self.glob))
class Id(GlobMatchExpressionBase):
def __call__(self, kernel, matchable):
return self.re.match(matchable.id)
class Tagged(GlobMatchExpressionBase):
"""Match a string-based tagged using a glob expression.
.. note::
These string-based tags will, in the not-too-distant future, be replace
by instance-based tags matched by :class:`ObjTagged`.
"""
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
from loopy.kernel.instruction import LegacyStringInstructionTag
if matchable.tags:
return any(
self.re.match(tag.value)
if isinstance(tag, LegacyStringInstructionTag)
else
False
for tag in matchable.tags)
else:
return False
class Writes(GlobMatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
if not isinstance(matchable, InstructionBase):
return False
return any(self.re.match(name)
for name in matchable.assignee_var_names())
class Reads(GlobMatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
if not isinstance(matchable, InstructionBase):
return False
return any(self.re.match(name)
for name in matchable.read_dependency_names())
class InKernel(GlobMatchExpressionBase):
def __call__(self, kernel, matchable):
return self.re.match(kernel.name)
class Iname(GlobMatchExpressionBase):
def __call__(self, kernel: LoopKernel, matchable: Matchable) -> bool:
if not isinstance(matchable, InstructionBase):
return False
return any(self.re.match(name)
for name in matchable.within_inames)
# }}}
# {{{ parser
def parse_match(expr):
"""Syntax examples::
* ``id:yoink and writes:a_temp``
* ``id:yoink and (not writes:a_temp or tag:input)``
"""
if not expr:
return All()
def parse_terminal(pstate):
next_tag = pstate.next_tag()
if next_tag is _id:
result = Id(pstate.next_match_obj().group(1))
pstate.advance()
return result
elif next_tag is _tag:
result = Tagged(pstate.next_match_obj().group(1))
pstate.advance()
return result
elif next_tag is _writes:
result = Writes(pstate.next_match_obj().group(1))
pstate.advance()
return result
elif next_tag is _reads:
result = Reads(pstate.next_match_obj().group(1))
pstate.advance()
return result
elif next_tag is _in_kernel:
result = InKernel(pstate.next_match_obj().group(1))
pstate.advance()
return result
elif next_tag is _iname:
result = Iname(pstate.next_match_obj().group(1))
pstate.advance()
return result
else:
pstate.expected("terminal")
def inner_parse(pstate, min_precedence=0):
pstate.expect_not_end()
if pstate.is_next(_not):
pstate.advance()
left_query = Not(inner_parse(pstate, _PREC_NOT))
elif pstate.is_next(_openpar):
pstate.advance()
left_query = inner_parse(pstate)
pstate.expect(_closepar)
pstate.advance()
else:
left_query = parse_terminal(pstate)
did_something = True
while did_something:
did_something = False
if pstate.is_at_end():
return left_query
next_tag = pstate.next_tag()
if next_tag is _and and _PREC_AND > min_precedence:
pstate.advance()
left_query = And(
(left_query, inner_parse(pstate, _PREC_AND)))
did_something = True
elif next_tag is _or and _PREC_OR > min_precedence:
pstate.advance()
left_query = Or(
(left_query, inner_parse(pstate, _PREC_OR)))
did_something = True
return left_query
if isinstance(expr, MatchExpressionBase):
return expr
from pytools.lex import InvalidTokenError, LexIterator, lex
try:
pstate = LexIterator(
[(tag, s, idx, matchobj)
for (tag, s, idx, matchobj) in lex(_LEX_TABLE, expr,
match_objects=True)
if tag is not _whitespace], expr)
except InvalidTokenError as e:
from loopy.diagnostic import LoopyError
raise LoopyError(
"invalid match expression: '{match_expr}' ({err_type}: {err_str})"
.format(
match_expr=expr,
err_type=type(e).__name__,
err_str=str(e))) from e
if pstate.is_at_end():
pstate.raise_parse_error("unexpected end of input")
result = inner_parse(pstate)
if not pstate.is_at_end():
pstate.raise_parse_error("leftover input after completed parse")
return result
# }}}
# {{{ stack match objects
class StackMatchComponent(ABC):
"""
.. automethod:: __call__
"""
@abstractmethod
def __call__(self, kernel: LoopKernel, stack: Sequence[Matchable]) -> bool:
pass
def __ne__(self, other):
return not self.__eq__(other)
class StackAllMatchComponent(StackMatchComponent):
def __call__(self, kernel: LoopKernel, stack: Sequence[Matchable]) -> bool:
return True
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, "all_match")
def __eq__(self, other):
return type(self) is type(other)
class StackBottomMatchComponent(StackMatchComponent):
def __call__(self, kernel: LoopKernel, stack: Sequence[Matchable]) -> bool:
return not stack
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, "bottom_match")
def __eq__(self, other):
return type(self) is type(other)
@dataclass(eq=True, frozen=True)
class StackItemMatchComponent(StackMatchComponent):
match_expr: MatchExpressionBase
inner_match: StackMatchComponent
def __call__(self, kernel: LoopKernel, stack: Sequence[Matchable]) -> bool:
if not stack:
return False
outer = stack[0]
if not self.match_expr(kernel, outer):
return False
return self.inner_match(kernel, stack[1:])
@dataclass(eq=True, frozen=True)
class StackWildcardMatchComponent(StackMatchComponent):
inner_match: StackMatchComponent
def __call__(self, kernel: LoopKernel, stack: Sequence[Matchable]) -> bool:
for i in range(0, len(stack)):
if self.inner_match(kernel, stack[i:]):
return True
return False
# }}}
# {{{ stack matcher
@dataclass(eq=True, frozen=True)
class RuleInvocationMatchable:
id: str
tags: frozenset[pytools.tag.Tag]
def write_dependency_names(self):
raise TypeError("writes: query may not be applied to rule invocations")
def read_dependency_names(self):
raise TypeError("reads: query may not be applied to rule invocations")
def inames(self, kernel):
raise TypeError("inames: query may not be applied to rule invocations")
@dataclass(eq=True, frozen=True)
class StackMatch:
"""
.. automethod:: __call__
"""
root_component: StackMatchComponent
def __call__(
self, kernel: LoopKernel, insn: InstructionBase,
rule_stack: Sequence[tuple[str, frozenset[pytools.tag.Tag]]]) -> bool:
"""
:arg rule_stack: a tuple of (name, tags) rule invocation, outermost first
"""
stack_of_matchables: list[Matchable] = [insn]
for id, tags in rule_stack:
stack_of_matchables.append(RuleInvocationMatchable(id, tags))
return self.root_component(kernel, stack_of_matchables)
# }}}
# {{{ stack match parsing
ToStackMatchConvertible: TypeAlias = Union[StackMatch, str, None]
def parse_stack_match(smatch: ToStackMatchConvertible) -> StackMatch:
"""Syntax example::
... > outer > ... > next > innermost $
insn > next
insn > ... > next > innermost $
``...`` matches an arbitrary number of intervening stack levels.
Each of the entries is a match expression as understood by
:func:`parse_match`.
"""
if isinstance(smatch, StackMatch):
return smatch
if isinstance(smatch, MatchExpressionBase):
return StackMatch(
StackItemMatchComponent(
smatch, StackAllMatchComponent()))
if smatch is None:
return StackMatch(StackAllMatchComponent())
smatch = smatch.strip()
match: StackMatchComponent = StackAllMatchComponent()
if smatch[-1] == "$":
match = StackBottomMatchComponent()
smatch = smatch[:-1]
smatch = smatch.strip()
components = smatch.split(">")
for comp in components[::-1]:
comp = comp.strip()
if comp == "...":
match = StackWildcardMatchComponent(match)
else:
match = StackItemMatchComponent(parse_match(comp), match)
return StackMatch(match)
# }}}
# vim: foldmethod=marker