Skip to content

Commit 81d4fea

Browse files
committed
Refactor IR extraction and checking
1 parent cac2faa commit 81d4fea

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

clang/utils/reduce-clang-crash.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,26 +502,32 @@ def run_llvm_reduce(self):
502502
p = subprocess.Popen(full_llvm_reduce_cmd)
503503
p.communicate()
504504
except KeyboardInterrupt:
505-
# Hack to kill C-Reduce because it jumps into its own pgid
506505
print("\n\nctrl-c detected, killed reduction tool")
507506
p.kill()
508507

509508
def classify_crash(self) -> FailureType:
510-
print("classifying crash ...")
509+
print("Classifying crash ...")
511510
if self.check_expected_output(args=self.clang_args + ["-fsyntax-only"]):
512511
print("Found Frontend Crash")
513512
return FailureType.FrontEnd
514513

515514
print("Found Middle/Backend failure")
516515

517516
self.opt_level = extract_opt_level(self.clang_args) or "-O2"
517+
backend_result = self.check_backend()
518+
if backend_result == FailureType.BackEnd:
519+
return backend_result
520+
518521
# Try running w/ -emit-llvm to generate an IR file,
519522
# if it succeeds we have a backend failure, and can use llc.
520523
if not self.check_expected_output(
521-
args=self.clang_args + ["-emit-llvm", "-o", self.ir_file]):
524+
args=self.clang_args + ["-emit-llvm", "-o", self.ir_file]
525+
):
522526
print("Checking llc for failure")
523527
if self.check_expected_output(
524-
cmd=self.llc, args=[self.opt_level, "-filetype=obj"], filename=self.ir_file
528+
cmd=self.llc,
529+
args=[self.opt_level, "-filetype=obj"],
530+
filename=self.ir_file,
525531
):
526532
print("Found BackEnd Crash")
527533
return FailureType.BackEnd
@@ -530,6 +536,37 @@ def classify_crash(self) -> FailureType:
530536
print(f"clean up {self.ir_file}, since we can't repro w/ llc")
531537
os.remove(self.ir_file)
532538

539+
# The IR file will be in 'self.ir_file'
540+
self.extract_crashing_ir()
541+
return self.check_middle_end()
542+
543+
def check_llc_failure(self) -> bool:
544+
return self.check_expected_output(
545+
cmd=self.llc, args=[self.opt_level, "-filetype=obj"], filename=self.ir_file
546+
)
547+
548+
def extract_backend_ir(self) -> bool:
549+
return not self.check_expected_output(
550+
args=self.clang_args + ["-emit-llvm", "-o", self.ir_file]
551+
)
552+
553+
def check_backend(self) -> Optional[FailureType]:
554+
# Try running w/ -emit-llvm to generate an IR file,
555+
# if it succeeds we have a backend failure, and can use llc.
556+
if self.extract_backend_ir():
557+
print("Checking llc for failure")
558+
if self.check_llc_failure():
559+
print("Found BackEnd Crash")
560+
return FailureType.BackEnd
561+
elif os.path.exists(self.ir_file):
562+
# clean up the IR file if we generated it, but won't use it.
563+
print(f"clean up {self.ir_file}, since we can't repro w/ llc")
564+
os.remove(self.ir_file)
565+
566+
def extract_crashing_ir(self):
567+
"""
568+
Extract IR just before the crash using --print-on-crash
569+
"""
533570
args = self.clang_args + [
534571
"-mllvm",
535572
"--print-on-crash",
@@ -545,6 +582,7 @@ def classify_crash(self) -> FailureType:
545582
# The output from --print-on-crash has an invalid first line (pass name).
546583
remove_first_line(self.ir_file)
547584

585+
def check_middle_end(self) -> FailureType:
548586
# TODO: parse the exact pass from the backtrace and set the pass
549587
# pipeline directly via -passes="...".
550588
print("Checking opt for failure")
@@ -555,7 +593,9 @@ def classify_crash(self) -> FailureType:
555593
):
556594
print("Found MiddleEnd Crash")
557595
return FailureType.MiddleEnd
558-
print("Could not automatically detect crash type, falling back to creduce/cvise.")
596+
print(
597+
"Could not automatically detect crash type, falling back to creduce/cvise."
598+
)
559599
return FailureType.Unknown
560600

561601
def reduce_ir_crash(self, new_cmd: List[str]):
@@ -657,7 +697,7 @@ def main():
657697
r.reduce_ir_crash(new_cmd)
658698
return
659699
case FailureType.BackEnd:
660-
new_cmd = [llc_cmd, "-filetype=obj", r.opt_level]
700+
new_cmd = [llc_cmd, "-filetype=obj", r.opt_level]
661701
r.reduce_ir_crash(new_cmd)
662702
return
663703

0 commit comments

Comments
 (0)