Skip to content

Debugging Bad Code Generation

Jeff Bush edited this page Jun 2, 2019 · 2 revisions

If the compiler generates invalid code, first try to compile with -O0. If the code is correct in this case, the problem is probably caused by the optimizer. Add the command line arguments to clang:

-mllvm -opt-bisect-limit=n

Where n is a number, around 0-2000. Perform a binary search to isolate the number where the failure first occurs. (https://llvm.org/docs/OptBisect.html) The failing pass name can be derived by looking at the debug output,

To turn this into a minimal test case, run the following (where n is the pass before the pass that caused the issue)

clang -mllv -opt-bisect-limit=n -S -emit-llvm <source file>

This will result in a .ll file. To reproduce the issue, you can use opt:

opt < <llvm IR code> -passes=<failing pass name> -S

To see verbose debugging of optimization from clang:

-mllvm -debug