Skip to content

Commit a3eeef8

Browse files
authored
[FileCheck] Avoid capturing group for {{regex}} (#72136)
For `{{regex}}` we don't really need a capturing group, and only add it to properly handle cases like `{{foo|bar}}`. This is problematic, because the use of capturing groups makes our regex implementation slower (we have to go through the "dissect" stage, which can have quadratic complexity). Unfortunately, our regex implementation does not support non-capturing groups like `(?:regex)`. So instead, avoid adding the group entirely if the regex doesn't contain any alternations. This causes a slight difference in escaping behavior, where previously it was possible to write `{{{{}}` and get the same behavior as `{{\{\{}}`. This will no longer work. I don't think this is a problem, especially as we recently taught update_analyze_test_checks.py to emit `{{\{\{}}`, so this shouldn't get introduced in any new tests. For CodeGen/X86/vector-interleaved-store-i16-stride-7.ll (our slowest X86 test) this drops FileCheck time from 6s to 5s (the remainder is spent in a different regex issue). I expect similar speedups in other tests using a lot of `{{}}`.
1 parent c9832da commit a3eeef8

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,16 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
832832
// capturing the result for any purpose. This is required in case the
833833
// expression contains an alternation like: CHECK: abc{{x|z}}def. We
834834
// want this to turn into: "abc(x|z)def" not "abcx|zdef".
835-
RegExStr += '(';
836-
++CurParen;
835+
bool HasAlternation = PatternStr.contains('|');
836+
if (HasAlternation) {
837+
RegExStr += '(';
838+
++CurParen;
839+
}
837840

838841
if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
839842
return true;
840-
RegExStr += ')';
843+
if (HasAlternation)
844+
RegExStr += ')';
841845

842846
PatternStr = PatternStr.substr(End + 2);
843847
continue;

llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ define void @test_00() {
1818
; CHECK: %sum4 = add i32 %sum3, %phi6
1919
; CHECK-NEXT: --> {159,+,6}<%loop2>
2020
; CHECK: %s1 = add i32 %phi1, %phi4
21-
; CHECK-NEXT: --> {{{{}}73,+,1}<nuw><nsw><%loop1>,+,1}<nw><%loop2>
21+
; CHECK-NEXT: --> {{\{\{}}73,+,1}<nuw><nsw><%loop1>,+,1}<nw><%loop2>
2222
; CHECK: %s2 = add i32 %phi5, %phi2
23-
; CHECK-NEXT: --> {{{{}}57,+,2}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
23+
; CHECK-NEXT: --> {{\{\{}}57,+,2}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
2424
; CHECK: %s3 = add i32 %sum1, %sum3
25-
; CHECK-NEXT: --> {{{{}}130,+,3}<%loop1>,+,3}<%loop2>
25+
; CHECK-NEXT: --> {{\{\{}}130,+,3}<%loop1>,+,3}<%loop2>
2626
; CHECK: %s4 = add i32 %sum4, %sum2
27-
; CHECK-NEXT: --> {{{{}}179,+,6}<%loop1>,+,6}<%loop2>
27+
; CHECK-NEXT: --> {{\{\{}}179,+,6}<%loop1>,+,6}<%loop2>
2828
; CHECK: %s5 = add i32 %phi3, %sum3
29-
; CHECK-NEXT: --> {{{{}}122,+,3}<nuw><nsw><%loop1>,+,3}<%loop2>
29+
; CHECK-NEXT: --> {{\{\{}}122,+,3}<nuw><nsw><%loop1>,+,3}<%loop2>
3030
; CHECK: %s6 = add i32 %sum2, %phi6
31-
; CHECK-NEXT: --> {{{{}}63,+,6}<%loop1>,+,3}<nw><%loop2>
31+
; CHECK-NEXT: --> {{\{\{}}63,+,6}<%loop1>,+,3}<nw><%loop2>
3232

3333
entry:
3434
br label %loop1
@@ -359,17 +359,17 @@ define void @test_06() {
359359

360360
; CHECK-LABEL: Classifying expressions for: @test_06
361361
; CHECK: %s1 = add i32 %phi1, %phi2
362-
; CHECK-NEXT: --> {{{{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
362+
; CHECK-NEXT: --> {{\{\{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
363363
; CHECK: %s2 = add i32 %phi2, %phi1
364-
; CHECK-NEXT: --> {{{{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
364+
; CHECK-NEXT: --> {{\{\{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
365365
; CHECK: %s3 = add i32 %phi1, %phi3
366-
; CHECK-NEXT: --> {{{{}}40,+,1}<nuw><nsw><%loop1>,+,3}<nw><%loop3>
366+
; CHECK-NEXT: --> {{\{\{}}40,+,1}<nuw><nsw><%loop1>,+,3}<nw><%loop3>
367367
; CHECK: %s4 = add i32 %phi3, %phi1
368-
; CHECK-NEXT: --> {{{{}}40,+,1}<nuw><nsw><%loop1>,+,3}<nw><%loop3>
368+
; CHECK-NEXT: --> {{\{\{}}40,+,1}<nuw><nsw><%loop1>,+,3}<nw><%loop3>
369369
; CHECK: %s5 = add i32 %phi2, %phi3
370-
; CHECK-NEXT: --> {{{{}}50,+,2}<nuw><nsw><%loop2>,+,3}<nw><%loop3>
370+
; CHECK-NEXT: --> {{\{\{}}50,+,2}<nuw><nsw><%loop2>,+,3}<nw><%loop3>
371371
; CHECK: %s6 = add i32 %phi3, %phi2
372-
; CHECK-NEXT: --> {{{{}}50,+,2}<nuw><nsw><%loop2>,+,3}<nw><%loop3>
372+
; CHECK-NEXT: --> {{\{\{}}50,+,2}<nuw><nsw><%loop2>,+,3}<nw><%loop3>
373373

374374
entry:
375375
br label %loop1
@@ -411,17 +411,17 @@ define void @test_07() {
411411

412412
; CHECK-LABEL: Classifying expressions for: @test_07
413413
; CHECK: %s1 = add i32 %phi1, %phi2
414-
; CHECK-NEXT: --> {{{{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
414+
; CHECK-NEXT: --> {{\{\{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
415415
; CHECK: %s2 = add i32 %phi2, %phi1
416-
; CHECK-NEXT: --> {{{{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
416+
; CHECK-NEXT: --> {{\{\{}}30,+,1}<nuw><nsw><%loop1>,+,2}<nw><%loop2>
417417
; CHECK: %s3 = add i32 %phi1, %phi3
418-
; CHECK-NEXT: --> {{{{}}40,+,3}<nuw><nsw><%loop3>,+,1}<nw><%loop1>
418+
; CHECK-NEXT: --> {{\{\{}}40,+,3}<nuw><nsw><%loop3>,+,1}<nw><%loop1>
419419
; CHECK: %s4 = add i32 %phi3, %phi1
420-
; CHECK-NEXT: --> {{{{}}40,+,3}<nuw><nsw><%loop3>,+,1}<nw><%loop1>
420+
; CHECK-NEXT: --> {{\{\{}}40,+,3}<nuw><nsw><%loop3>,+,1}<nw><%loop1>
421421
; CHECK: %s5 = add i32 %phi2, %phi3
422-
; CHECK-NEXT: --> {{{{}}50,+,3}<nuw><nsw><%loop3>,+,2}<nw><%loop2>
422+
; CHECK-NEXT: --> {{\{\{}}50,+,3}<nuw><nsw><%loop3>,+,2}<nw><%loop2>
423423
; CHECK: %s6 = add i32 %phi3, %phi2
424-
; CHECK-NEXT: --> {{{{}}50,+,3}<nuw><nsw><%loop3>,+,2}<nw><%loop2>
424+
; CHECK-NEXT: --> {{\{\{}}50,+,3}<nuw><nsw><%loop3>,+,2}<nw><%loop2>
425425

426426
entry:
427427
br label %loop3

0 commit comments

Comments
 (0)