Skip to content

Commit 96197cb

Browse files
Added a bit of functionality to IsolatePass, fixed a small bug (#3307)
Signed-off-by: Alexandre Eichenberger <[email protected]>
1 parent a6f3181 commit 96197cb

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

docs/DebuggingNumericalError.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ This script is found here `utils/onnx-mlir-print.sh`, and it is used by adding a
7676
That compiler log may typically have 10-100 passes. We have a tool to isolate a given pass. Use `utils/IsolatePass.py -m <log-file-name> -l` to list the name of all of these passes. You can then decide to investigate the compiler output of a given pass.
7777
For example, if interested in the transformation of ONNX to Krnl dialect, you can add the `-p "convert-onnx-to-krnl"` option, where the `convert-onnx-to-krnl` is the name of the actual pass. The `-p` option just take a regex matching a pass as listed with the `-l` option. Alternatively, option `-n 34` will isolate the 34th pass as listed with the `-l` option.
7878

79-
Say you are interested in the pass just before or just after `convert-onnx-to-krnl`, you can use, respectively, the `-a -1` or the `-a 1` additional option. Full options are listed under the `--help` flag.
79+
Say you are interested in the pass just before or just after `convert-onnx-to-krnl`, you can use, respectively, the `-a -1` or the `-a 1` additional option.
80+
The `-a` option can also list a REGEX, in which case, it will print the next pass that matches that REGEX.
81+
82+
Full options are listed under the `--help` flag.
8083

8184

8285
## Debugging the Code Generated for an Operator.

utils/IsolatePass.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
# global variables
2020
pass_name_to_id = {}
21+
id_to_pass_name = []
2122
pass_listing = []
2223
debug = 0
2324
# Max length of a single line. If there are very long constants, truncate them.
@@ -50,9 +51,10 @@ def get_args():
5051
parser.add_argument(
5152
"-a",
5253
"--after",
53-
help="Print the pass NUM(th) after the pass indicated by the "
54-
"-p or -n options. Number can be negative.",
55-
metavar="NUM",
54+
help="If STR is a number, N, print the pass Nth after the pass indicated by the "
55+
"-p or -n options. Number N can be negative. Otherwise, print the next pass that "
56+
"matches the regex STR.",
57+
metavar="STR",
5658
)
5759
parser.add_argument(
5860
"-l",
@@ -93,8 +95,9 @@ def process_line(str, length=max_line_length):
9395

9496

9597
def scan_listing(filename, print_list_name):
96-
global pass_name_to_id, pass_listing
98+
global pass_name_to_id, id_to_pass_name, pass_listing
9799
pass_name_to_id = {}
100+
id_to_pass_name = []
98101
pass_listing = []
99102

100103
current_listing = []
@@ -112,13 +115,14 @@ def scan_listing(filename, print_list_name):
112115
# Save current listing.
113116
id = len(pass_listing)
114117
pass_listing.append("".join(current_listing))
118+
id_to_pass_name.append(current_name)
115119
if current_name in pass_name_to_id:
116120
pass_name_to_id[current_name].append(id)
117121
else:
118122
pass_name_to_id[current_name] = [id]
119123
# Print info if requested.
120124
if print_list_name:
121-
print(f"{id}: {pass_name}")
125+
print(f"{id}: {current_name}")
122126
# Save new current name
123127
current_name = pass_name
124128
current_listing = []
@@ -164,23 +168,37 @@ def locate_pass(name, num):
164168
return ids[0]
165169

166170

167-
def print_pass(filename, id, num, name=None):
171+
def print_pass(filename, id, after, pass_name=None):
172+
global pass_name_to_id, id_to_pass_name, pass_listing
168173
n = 0
169-
if num:
170-
n = int(num)
174+
if after:
175+
if re.fullmatch(r"[+-]?\d+", after) is not None:
176+
# After is a number.
177+
n = int(after)
178+
else:
179+
# After is a pass name, locate it.
180+
regex = re.compile(after)
181+
n = 1
182+
while True:
183+
if id + n >= len(id_to_pass_name):
184+
usage(f"Did not find pass {after} after pass {pass_name}")
185+
if regex.search(id_to_pass_name[id + n]):
186+
# Found it
187+
break
188+
n += 1
171189
id += n
172190
if id < 0 or id >= len(pass_listing):
173191
print(f"Out of bound id {id}, should be in [0..{len(pass_listing)}) range.")
174192
exit(1)
175193
message = f"// Printing pass with id {id}"
176-
if name:
194+
if pass_name:
177195
if n != 0:
178196
if n > 0:
179-
message += f", {n}(th) pass(s) after {name}"
197+
message += f", {n}(th) pass(s) after {pass_name}"
180198
else:
181-
message += f", {-n}(th) pass(s) before {name}"
199+
message += f", {-n}(th) pass(s) before {pass_name}"
182200
else:
183-
message += f" with name {name}"
201+
message += f" with name {pass_name}"
184202
message += f' from file "{filename}".'
185203
print(f"{message}\n\n", pass_listing[id], f"\n\n{message}")
186204

utils/onnx-mlir-truncate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# Unterminated strings then cause issues with the VSCode mlir code prettifier.
2323
maxLineLength=800
2424

25-
echo "Command:" | tee ${@: -1}
25+
echo "Command on `date`" | tee ${@: -1}
2626
echo "onnx-mlir ${@:1:$#-1}" | tee -a ${@: -1}
2727
echo "" | tee -a ${@: -1}
2828

0 commit comments

Comments
 (0)