matcher.cpp 13.1 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#include <migraph/matcher.hpp>
#include <migraph/iterator_for.hpp>
#include <test.hpp>
#include <basic_ops.hpp>

Paul's avatar
Paul committed
6
namespace match = migraph::match;
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
template <class M>
Paul's avatar
Paul committed
9
migraph::match::matcher_result find_match(migraph::program& p, M&& m)
Paul's avatar
Paul committed
10
{
Paul's avatar
Paul committed
11
    migraph::match::matcher_result result;
Paul's avatar
Paul committed
12
    for(auto ins : migraph::iterator_for(p))
Paul's avatar
Paul committed
13
    {
Paul's avatar
Paul committed
14
        result = migraph::match::match_instruction(p, ins, m);
Paul's avatar
Paul committed
15
16
17
18
19
20
21
22
23
24
        if(result.result != p.end())
            return result;
    }
    return result;
}

void match1()
{
    migraph::program p;
    auto l = p.add_literal(1);
Paul's avatar
Paul committed
25
    auto m = match::standard_shape();
Paul's avatar
Paul committed
26
27
28
29
30
31
32
    auto r = find_match(p, m);
    EXPECT(bool{r.result == l});
}

void match_name1()
{
    migraph::program p;
Paul's avatar
Paul committed
33
34
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
35
36
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
37
    auto m = match::name("sum");
Paul's avatar
Paul committed
38
39
40
41
42
43
44
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_name2()
{
    migraph::program p;
Paul's avatar
Paul committed
45
46
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
47
48
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
49
    auto m = match::name("min");
Paul's avatar
Paul committed
50
51
52
53
54
55
56
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_name3()
{
    migraph::program p;
Paul's avatar
Paul committed
57
58
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
59
60
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
61
    auto m = match::name("sum")(match::standard_shape());
Paul's avatar
Paul committed
62
63
64
65
66
67
68
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg1()
{
    migraph::program p;
Paul's avatar
Paul committed
69
70
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
71
72
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
73
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
74
75
76
77
78
79
80
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg2()
{
    migraph::program p;
Paul's avatar
Paul committed
81
82
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
83
84
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
85
    auto m = match::name("sum")(match::arg(0)(match::name("sum")), match::standard_shape());
Paul's avatar
Paul committed
86
87
88
89
90
91
92
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_arg3()
{
    migraph::program p;
Paul's avatar
Paul committed
93
94
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
95
96
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
97
    auto m = match::name("sum")(match::arg(1)(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
98
99
100
101
102
103
104
105
106
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg4()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
107
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
108
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
109
110
    auto m    = match::name("pass")(match::arg(0)(match::name("sum")), match::standard_shape());
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
111
112
113
114
115
116
    EXPECT(bool{r.result == pass});
}

void match_arg5()
{
    migraph::program p;
Paul's avatar
Paul committed
117
118
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
119
120
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
121
    auto m = match::name("pass")(match::arg(1)(match::name("sum")), match::standard_shape());
Paul's avatar
Paul committed
122
123
124
125
126
127
128
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_arg6()
{
    migraph::program p;
Paul's avatar
Paul committed
129
130
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
131
132
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
133
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")));
Paul's avatar
Paul committed
134
135
136
137
138
139
140
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_arg7()
{
    migraph::program p;
Paul's avatar
Paul committed
141
142
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
143
144
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
145
    auto m = match::name("sum")(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
146
                                match::arg(1)(match::name("@literal")));
Paul's avatar
Paul committed
147
148
149
150
151
152
153
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

void match_args1()
{
    migraph::program p;
Paul's avatar
Paul committed
154
155
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
156
157
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
158
159
    auto m = match::name("sum")(match::args(match::name("@literal"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
160
161
162
163
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
164
165
166
167
168
169
170
void match_args2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
171
172
    auto m = match::name("sum")(match::args(match::name("@literal"), match::name("sum")),
                                match::standard_shape());
Paul's avatar
Paul committed
173
174
175
176
177
178
179
180
181
182
183
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_args3()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
184
    auto m = match::name("sum")(match::args(match::name("@literal")), match::standard_shape());
Paul's avatar
Paul committed
185
    auto r = find_match(p, m);
186
    EXPECT(bool{r.result == p.end()});
Paul's avatar
Paul committed
187
188
189
190
191
}

void match_args4()
{
    migraph::program p;
Paul's avatar
Paul committed
192
193
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
194
195
196
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
Paul's avatar
Paul committed
197
198
    auto m = match::name("sum")(match::args(match::name("sum"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
199
200
201
202
203
204
205
206
207
208
209
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum2});
}

void match_args5()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
210
211
    auto m = match::name("sum")(match::args(match::name("sum"), match::name("@literal")),
                                match::standard_shape());
Paul's avatar
Paul committed
212
213
214
215
216
217
218
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_args6()
{
    migraph::program p;
Paul's avatar
Paul committed
219
220
221
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
222
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
223
224
    auto m    = match::name("pass")(match::args(match::name("sum")), match::standard_shape());
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
225
226
227
228
229
230
    EXPECT(bool{r.result == pass});
}

void match_args7()
{
    migraph::program p;
Paul's avatar
Paul committed
231
232
233
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
234
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
235
    auto m    = match::name("pass")(match::args(match::name("sum")(match::args(
Paul's avatar
Paul committed
236
237
                                     match::name("@literal"), match::name("@literal")))),
                                 match::standard_shape());
Paul's avatar
Paul committed
238
    auto r    = find_match(p, m);
Paul's avatar
Paul committed
239
240
241
    EXPECT(bool{r.result == pass});
}

Paul's avatar
Paul committed
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
void match_either_args1()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum1  = p.add_instruction(sum_op{}, one, two);
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
    auto m    = match::name("sum")(match::either_arg(0, 1)(match::name("sum"), match::name("@literal")));
    auto r    = find_match(p, m);
    EXPECT(bool{r.result == sum2});
}

void match_either_args2()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum1  = p.add_instruction(sum_op{}, one, two);
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
    auto m    = match::name("sum")(match::either_arg(0, 1)(match::name("@literal"), match::name("sum")));
    auto r    = find_match(p, m);
    EXPECT(bool{r.result == sum2});
}

void match_either_args3()
{
    migraph::program p;
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum1  = p.add_instruction(sum_op{}, one, two);
    auto sum2 = p.add_instruction(sum_op{}, sum1, two);
    p.add_instruction(pass_op{}, sum2);
    auto m    = match::name("sum")(match::either_arg(0, 1)(match::name("pass"), match::name("@literal")));
    auto r    = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

Paul's avatar
Paul committed
281
void match_all_of1()
Paul's avatar
Paul committed
282
283
284
285
286
287
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
288
    auto m = match::name("sum")(match::all_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
289
                                              match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
290
291
292
293
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
294
295
296
297
298
299
300
void match_all_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
301
302
    auto m = match::name("sum")(
        match::all_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
303
304
305
306
307
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_any_of1()
Paul's avatar
Paul committed
308
309
310
311
312
313
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
314
315
    auto m = match::name("sum")(
        match::any_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
316
317
318
319
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
320
321
322
323
324
325
326
void match_any_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
327
328
    auto m = match::name("sum")(
        match::any_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("sum"))));
Paul's avatar
Paul committed
329
330
331
332
333
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

void match_none_of1()
Paul's avatar
Paul committed
334
335
336
337
338
339
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
340
341
    auto m = match::name("sum")(
        match::none_of(match::arg(0)(match::name("sum")), match::arg(1)(match::name("sum"))));
Paul's avatar
Paul committed
342
343
344
345
    auto r = find_match(p, m);
    EXPECT(bool{r.result == sum});
}

Paul's avatar
Paul committed
346
347
348
349
350
351
352
void match_none_of2()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
353
    auto m = match::name("sum")(match::none_of(match::arg(0)(match::name("@literal")),
Paul's avatar
Paul committed
354
                                               match::arg(1)(match::name("@literal"))));
Paul's avatar
Paul committed
355
356
357
358
    auto r = find_match(p, m);
    EXPECT(bool{r.result == p.end()});
}

Paul's avatar
Paul committed
359
360
361
void match_bind1()
{
    migraph::program p;
Paul's avatar
Paul committed
362
363
364
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
    auto sum  = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
365
    auto pass = p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
366
    auto m    = match::name("pass")(
Paul's avatar
Paul committed
367
368
369
                 match::args(match::name("sum")(match::args(match::name("@literal").bind("one"),
                                                            match::name("@literal").bind("two")))
                                 .bind("sum")),
Paul's avatar
Paul committed
370
                 match::standard_shape())
Paul's avatar
Paul committed
371
                 .bind("pass");
Paul's avatar
Paul committed
372
373
374
375
376
377
378
379
    auto r = find_match(p, m);
    EXPECT(bool{r.instructions.at("one") == one});
    EXPECT(bool{r.instructions.at("two") == two});
    EXPECT(bool{r.instructions.at("sum") == sum});
    EXPECT(bool{r.instructions.at("pass") == pass});
    EXPECT(bool{r.result == pass});
}

Paul's avatar
Paul committed
380
381
382
struct match_find_sum
{
    migraph::instruction_ref ins;
Paul's avatar
Paul committed
383
    auto matcher() const { return match::name("sum"); }
Paul's avatar
Paul committed
384

Paul's avatar
Paul committed
385
    void apply(migraph::program&, match::matcher_result r) const { EXPECT(bool{r.result == ins}); }
Paul's avatar
Paul committed
386
387
388
389
390
};

struct match_find_literal
{
    migraph::instruction_ref ins;
Paul's avatar
Paul committed
391
    auto matcher() const { return match::name("@literal"); }
Paul's avatar
Paul committed
392

Paul's avatar
Paul committed
393
    void apply(migraph::program&, match::matcher_result r) const
Paul's avatar
Paul committed
394
395
396
397
398
399
400
401
402
403
404
405
406
    {
        EXPECT(bool{r.result != ins});
        EXPECT(r.result->name() == "@literal");
    }
};

void match_finder()
{
    migraph::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
407
    match::find_matches(p, match_find_sum{sum}, match_find_literal{sum});
Paul's avatar
Paul committed
408
409
}

Paul's avatar
Paul committed
410
411
int main()
{
Paul's avatar
Paul committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    match1();
    match_name1();
    match_name2();
    match_name3();

    match_arg1();
    match_arg2();
    match_arg3();
    match_arg4();
    match_arg5();
    match_arg6();
    match_arg7();

    match_args1();
Paul's avatar
Paul committed
426
427
428
429
430
431
    match_args2();
    match_args3();
    match_args4();
    match_args5();
    match_args6();
    match_args7();
Paul's avatar
Paul committed
432
433
434
435
    
    match_either_args1();
    match_either_args2();
    match_either_args3();
Paul's avatar
Paul committed
436

Paul's avatar
Paul committed
437
438
439
440
441
    match_all_of1();
    match_all_of2();

    match_any_of1();
    match_any_of2();
Paul's avatar
Paul committed
442

Paul's avatar
Paul committed
443
444
    match_none_of1();
    match_none_of2();
Paul's avatar
Paul committed
445

Paul's avatar
Paul committed
446
    match_bind1();
Paul's avatar
Paul committed
447
448

    match_finder();
Paul's avatar
Paul committed
449
}