Skip to content

Commit f10c33d

Browse files
committed
docs: add info to DebuggingTheCompiler on how to identify an optimizer bug.
1 parent 6e0d771 commit f10c33d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/DebuggingTheCompiler.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,44 @@ One can also use shell regex to visit multiple files in the same directory. Exam
457457

458458
clang-tidy -p=$PATH_TO_BUILD/swift-macosx-x86_64/compile_commands.json $FULL_PATH_TO_DIR/*.cpp
459459

460+
Identifying an optimizer bug
461+
----------------------------
462+
463+
If a compiled executable is crashing when built with optimizations, but not
464+
crashing when built with -Onone, it's most likely one of the SIL optimizations
465+
which causes the miscompile.
466+
467+
Currently there is no tool to automatically identify the bad optimization, but
468+
it's quite easy to do this manually:
469+
470+
1. Find the offending optimization with bisecting:
471+
472+
a. Add the compiler option ``-Xllvm -sil-opt-pass-count=<n>``, where ``<n>``
473+
is the number of optimizations to run.
474+
b. Bisect: find n where the executable crashes, but does not crash with n-1.
475+
Note that n can be quite large, e.g. > 100000 (just try
476+
n = 10, 100, 1000, 10000, etc. to find an upper bound).
477+
c. Add another option ``-Xllvm -sil-print-pass-name``. The output can be
478+
large, so it's best to redirect stderr to a file (``2> output``).
479+
In the output search for the last pass before ``stage Address Lowering``.
480+
It should be the ``Run #<n-1>``. This line tells you the name of the bad
481+
optimization pass and on which function it run.
482+
483+
2. Get the SIL before and after the bad optimization.
484+
485+
a. Add the compiler options
486+
``-Xllvm -sil-print-all -Xllvm -sil-print-only-function='<function>'``
487+
where ``<function>`` is the function name (including the preceding ``$``).
488+
For example:
489+
``-Xllvm -sil-print-all -Xllvm -sil-print-only-function='$s4test6testityS2iF'``.
490+
Again, the output can be large, so it's best to redirect stderr to a file.
491+
b. From the output, copy the SIL of the function *before* the bad
492+
run into a separate file and the SIL *after* the bad run into a file.
493+
c. Compare both SIL files and try to figure out what the optimization pass
494+
did wrong. To simplify the comparison, it's sometimes helpful to replace
495+
all SIL values (e.g. ``%27``) with a constant string (e.g. ``%x``).
496+
497+
460498
Debugging Swift Executables
461499
===========================
462500

0 commit comments

Comments
 (0)