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
use crate::{
    common::*,
    pattern::{matcher::MatchAll, search::PatternSetSearcher, visitors::*},
};

#[derive(Debug)]
pub struct CheckDag<'a> {
    patterns: MatchAll<'a>,
}
impl<'a> CheckDag<'a> {
    pub fn new(patterns: MatchAll<'a>) -> Self {
        Self { patterns }
    }

    #[inline]
    pub fn first_pattern_span(&self) -> SourceSpan {
        self.patterns.first_pattern_span()
    }
}
impl<'check> Spanned for CheckDag<'check> {
    fn span(&self) -> SourceSpan {
        self.patterns.span()
    }
}
impl<'check> Rule for CheckDag<'check> {
    fn kind(&self) -> Check {
        Check::Dag
    }

    fn apply<'input, 'context, C>(&self, context: &mut C) -> DiagResult<Matches<'input>>
    where
        C: Context<'input, 'context> + ?Sized,
    {
        let mut guard = context.protect();
        let input = guard.search_block();
        let result = match self.patterns {
            MatchAll::Literal(ref matcher) => {
                let mut searcher = matcher.search(input)?;
                let mut visitor = StaticPatternSetVisitor::new(&mut searcher);
                visitor.try_match_all(&mut guard)
            }
            MatchAll::Regex(ref matcher) => {
                let mut searcher = matcher.search(input);
                let mut visitor = StaticPatternSetVisitor::new(&mut searcher);
                visitor.try_match_all(&mut guard)
            }
            MatchAll::RegexPrefix {
                ref prefixes,
                ref suffixes,
                ..
            } => {
                let mut searcher = prefixes.search(input);
                let mut visitor = DynamicPatternSetVisitor::new(&mut searcher, suffixes);
                visitor.try_match_all(&mut guard)
            }
            MatchAll::SubstringPrefix {
                ref prefixes,
                ref suffixes,
            } => {
                let mut searcher = prefixes.search(input)?;
                let mut visitor = DynamicPatternSetVisitor::new(&mut searcher, suffixes);
                visitor.try_match_all(&mut guard)
            }
            MatchAll::AnyPrefix {
                ref prefixes,
                ref suffixes,
            } => {
                let mut searcher = PatternSetSearcher::new(input, prefixes)?;
                let mut visitor = DynamicPatternSetVisitor::new(&mut searcher, suffixes);
                visitor.try_match_all(&mut guard)
            }
        };
        match result {
            Ok(result) if result.is_ok() => {
                guard.save();
                Ok(result)
            }
            result => result,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::check::CheckSection;
    use std::assert_matches::assert_matches;
    use std::collections::VecDeque;

    /// This test ensures that the ordering of CHECK-DAG patterns is not
    /// significant even in the presence of a common prefix. The test can
    /// only pass if this is true, as line matched by the first directive
    /// appears after the line matched by the second.
    #[test]
    fn check_dag_common_prefix_non_overlapping_patterns_test() -> DiagResult<()> {
        let mut context = TestContext::new();
        context
            .with_checks(
                "
CHECK-DAG: v[[#]] = add v9, v0
CHECK-DAG: v[[#]] = add v6, v7
",
            )
            .with_input(
                "
function foo(i32) -> i32 {
block0(v0: i32):
  v2 = const.i32 0
  v1 = const.i32 1
  br block2(v1, v2)

block1(v6: i32, v7: i32):
  v8 = add v6, v7
  v9 = const.i32 0
  v10 = add v9, v0
  br block3(v8, v10)

block2(v3: i32, v4: i32):
  v5 = mul v3, v4
  br block1

block3(v11):
  ret v11
}
",
            );
        let match_file = context.match_file();
        let match_file_source = match_file.as_ref();
        let check_file = context.parse(match_file_source)?;

        let lines = check_file.into_lines();

        let mut mctx = context.match_context();
        let match_all = MatchAll::compile(lines, &mctx.config, mctx.env.interner())?;
        let rule = CheckDag::new(match_all);
        let result = rule
            .apply(&mut mctx)
            .expect("expected non-fatal application of rule");

        let test_result = TestResult::from_matches(result, &mctx);
        test_result.into_result()?;

        Ok(())
    }

    #[test]
    fn check_dag_common_prefix_regex_non_overlapping_patterns_test() -> DiagResult<()> {
        let mut context = TestContext::new();
        context
            .with_checks(
                r"
CHECK-DAG: {{v[\d]+}} = add v9, v0
CHECK-DAG: {{v[\d]+}} = add v6, v7
",
            )
            .with_input(
                "
function foo(i32) -> i32 {
block0(v0: i32):
  v2 = const.i32 0
  v1 = const.i32 1
  br block2(v1, v2)

block1(v6: i32, v7: i32):
  v8 = add v6, v7
  v9 = const.i32 0
  v10 = add v9, v0
  br block3(v8, v10)

block2(v3: i32, v4: i32):
  v5 = mul v3, v4
  br block1

block3(v11):
  ret v11
}
",
            );
        let match_file = context.match_file();
        let match_file_source = match_file.as_ref();
        let check_file = context.parse(match_file_source)?;

        let lines = check_file.into_lines();

        let mut mctx = context.match_context();
        let match_all = MatchAll::compile(lines, &mctx.config, mctx.env.interner())?;
        let rule = CheckDag::new(match_all);
        let result = rule
            .apply(&mut mctx)
            .expect("expected non-fatal application of rule");

        let test_result = TestResult::from_matches(result, &mctx);
        test_result.into_result()?;

        Ok(())
    }

    /// In this test, the first of the two CHECK-DAG directives can match the
    /// same content as the second, while the second only has one possible match.
    /// This test can only succeed if the ordering of the directives is not significant,
    /// at least in this specific case. However, the catch here is that the reason the
    /// test does not fail is because we always take the longest match first, so even
    /// though the first directive would match the only line that the second can match,
    /// the second directive wins because it matches a longer span of input.
    #[test]
    fn check_dag_overlapping_test() -> DiagResult<()> {
        let mut context = TestContext::new();
        context
            .with_checks(
                "
CHECK-LABEL: block0:
CHECK-DAG: v[[#]] = {{[a-z]+[.][a-z]+}}
CHECK-DAG: v[[#]] = const.i32 0
CHECK-LABEL: block1(
CHECK: br block2(v[[#]], v[[#]])
",
            )
            .with_input(
                "
function foo(i32) -> i32 {
block0(v0: i32):
  v2 = const.i32 0
  v1 = const.i32 1
  br block2(v1, v2)

block1(v6: i32, v7: i32):
  v8 = add v6, v7
  v9 = const.i32 0
  v10 = add v9, v0
  br block3(v8, v10)

block2(v3: i32, v4: i32):
  v5 = mul v3, v4
  br block1

block3(v11):
  ret v11
}
",
            );
        let match_file = context.match_file();
        let match_file_source = match_file.as_ref();
        let check_file = context.parse(match_file_source)?;

        let mut lines = VecDeque::from(check_file.into_lines());
        lines.pop_back();
        lines.pop_back();
        lines.pop_front();
        let lines = Vec::from(lines);

        let mut mctx = context.match_context();
        let match_all = MatchAll::compile(lines, &mctx.config, mctx.env.interner())?;
        let rule = CheckDag::new(match_all);
        let result = rule
            .apply(&mut mctx)
            .expect("expected non-fatal application of rule");
        let test_result = TestResult::from_matches(result, &mctx);
        test_result.into_result()?;

        Ok(())
    }

    /// This test ensures that a CHECK-DAG does not consider matches successful
    /// outside the current block
    #[test]
    fn check_dag_does_not_search_beyond_check_label_boundaries_test() -> DiagResult<()> {
        let mut context = TestContext::new();
        context
            .with_checks(
                "
CHECK-LABEL: block0(
CHECK-DAG: v1 = const.i32 1
CHECK-DAG: v8 = add v6, v7
CHECK-LABEL: block1(
CHECK-DAG: v8 = add v6, v7
",
            )
            .with_input(
                "
function foo(i32) -> i32 {
block0(v0: i32):
  v2 = const.i32 0
  v1 = const.i32 1
  br block2(v1, v2)

block1(v6: i32, v7: i32):
  v8 = add v6, v7
  v9 = const.i32 0
  v10 = add v9, v0
  br block3(v8, v10)

block2(v3: i32, v4: i32):
  v5 = mul v3, v4
  br block1

block3(v11):
  ret v11
}
",
            );
        let match_file = context.match_file();
        let match_file_source = match_file.as_ref();
        let check_file = context.parse(match_file_source)?;
        let mut program = context.compile(check_file)?;

        let mut mctx = context.match_context();
        let mut blocks = crate::check::discover_blocks(&program, &mut mctx)?;
        assert_eq!(blocks.len(), 2);

        blocks.pop().unwrap();
        let block = blocks.pop().unwrap();
        assert!(block.start.is_some());
        assert_eq!(block.sections, Some(Range::new(0, 1)));
        assert!(!block.range().is_empty());

        mctx.enter_block(block.range());
        assert_matches!(&program.sections[0], CheckSection::Block { .. });
        program.sections.pop();

        let test_result = crate::check::check_blocks([block], &program, &mut mctx);
        assert!(test_result.is_failed());
        if let [CheckFailedError::MatchNoneButExpected { .. }] = test_result.errors() {
            Ok(())
        } else {
            Ok(test_result.into_result().map(|_| ())?)
        }
    }

    /// This test ensures that all of the successful matches are contained within
    /// their respective blocks.
    #[test]
    fn check_dag_check_label_boundaries_test() -> DiagResult<()> {
        let mut context = TestContext::new();
        context
            .with_checks(
                "
CHECK-LABEL: block0(
CHECK-DAG: v1 = const.i32 1
CHECK-DAG: v2 = const.i32 0
CHECK-LABEL: block1(
CHECK-DAG: v8 = add v6, v7
",
            )
            .with_input(
                "
function foo(i32) -> i32 {
block0(v0: i32):
  v2 = const.i32 0
  v1 = const.i32 1
  br block2(v1, v2)

block1(v6: i32, v7: i32):
  v8 = add v6, v7
  v9 = const.i32 0
  v10 = add v9, v0
  br block3(v8, v10)

block2(v3: i32, v4: i32):
  v5 = mul v3, v4
  br block1

block3(v11):
  ret v11
}
",
            );
        let match_file = context.match_file();
        let match_file_source = match_file.as_ref();
        let check_file = context.parse(match_file_source)?;
        let program = context.compile(check_file)?;

        let mut mctx = context.match_context();
        let blocks = crate::check::discover_blocks(&program, &mut mctx)?;
        assert_eq!(blocks.len(), 2);

        let block = &blocks[0];
        assert!(block.start.is_some());
        assert_eq!(block.sections, Some(Range::new(0, 1)));
        assert!(!block.range().is_empty());

        mctx.enter_block(block.range());
        let block_ranges = blocks
            .iter()
            .map(|blk| blk.range())
            .collect::<SmallVec<[_; 2]>>();
        let matches = crate::check::check_blocks(blocks, &program, &mut mctx).into_result()?;

        assert_eq!(matches.len(), 5);
        assert!(matches[0].span.end() <= block_ranges[0].start); // CHECK-LABEL
        assert!(matches[1].span.end() <= block_ranges[0].end);
        assert!(matches[2].span.end() <= block_ranges[0].end);
        let match3_span = matches[3].span; // CHECK-LABEL
        assert!(
            match3_span.start() >= block_ranges[0].end
                && match3_span.end() <= block_ranges[1].start
        );
        let match4_span = matches[4].span;
        assert!(
            match4_span.start() >= block_ranges[1].start
                && match4_span.end() <= block_ranges[1].end
        );
        let match5_span = matches[4].span;
        assert!(
            match5_span.start() >= block_ranges[1].start
                && match5_span.end() <= block_ranges[1].end
        );

        Ok(())
    }
}