[NFC][MLIR][Linalg] Refactor linalg.matmul tablegen ODS and related C++ code. (#116377)
[llvm-project.git] / llvm / utils / reduce_pipeline_test / fake_opt.py
blob2d3b9592b7e5cd2e2ed76369fb94212c427416ec
1 #!/usr/bin/env python3
3 # Automatically formatted with yapf (https://github.com/google/yapf)
5 # Fake 'opt' program that can be made to crash on request. For testing
6 # the 'reduce_pipeline.py' automatic 'opt' NPM pipeline reducer.
8 import argparse
9 import os
10 import shutil
11 import signal
13 parser = argparse.ArgumentParser()
14 parser.add_argument("-passes", action="store", dest="passes", required=True)
15 parser.add_argument(
16 "-print-pipeline-passes", dest="print_pipeline_passes", action="store_true"
18 parser.add_argument("-crash-seq", action="store", dest="crash_seq", required=True)
19 parser.add_argument("-o", action="store", dest="output")
20 parser.add_argument("input")
21 [args, unknown_args] = parser.parse_known_args()
23 # Expand pipeline if '-print-pipeline-passes'.
24 if args.print_pipeline_passes:
25 if args.passes == "EXPAND_a_to_f":
26 print("a,b,c,d,e,f")
27 else:
28 print(args.passes)
29 exit(0)
31 # Parse '-crash-seq'.
32 crash_seq = []
33 tok = ""
34 for c in args.crash_seq:
35 if c == ",":
36 if tok != "":
37 crash_seq.append(tok)
38 tok = ""
39 else:
40 tok += c
41 if tok != "":
42 crash_seq.append(tok)
43 print(crash_seq)
45 # Parse '-passes' and see if we need to crash.
46 tok = ""
47 for c in args.passes:
48 if c == ",":
49 if len(crash_seq) > 0 and crash_seq[0] == tok:
50 crash_seq.pop(0)
51 tok = ""
52 elif c == "(":
53 tok = ""
54 elif c == ")":
55 if len(crash_seq) > 0 and crash_seq[0] == tok:
56 crash_seq.pop(0)
57 tok = ""
58 else:
59 tok += c
60 if len(crash_seq) > 0 and crash_seq[0] == tok:
61 crash_seq.pop(0)
63 # Copy input to output.
64 if args.output:
65 shutil.copy(args.input, args.output)
67 # Crash if all 'crash_seq' passes occurred in right order.
68 if len(crash_seq) == 0:
69 print("crash")
70 os.kill(os.getpid(), signal.SIGKILL)
71 else:
72 print("no crash")
73 exit(0)