brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · 4d12191 Raw
126 lines · plain
1===================2Bisecting LLVM code3===================4 5Introduction6============7 8``git bisect`` is a useful tool for finding which revision caused a bug.9 10This document describes how to use ``git bisect``. In particular, while LLVM11has a mostly linear history, it has a few merge commits that added projects --12and these merged the linear history of those projects. As a consequence, the13LLVM repository has multiple roots: One "normal" root, and then one for each14toplevel project that was developed out-of-tree and then merged later.15As of early 2020, the only such merged project is MLIR, but flang will likely16be merged in a similar way soon.17 18Basic operation19===============20 21See https://git-scm.com/docs/git-bisect for a good overview. In summary:22 23  .. code-block:: bash24 25     git bisect start26     git bisect bad main27     git bisect good f00ba28 29git will check out a revision in between. Try to reproduce your problem at30that revision, and run ``git bisect good`` or ``git bisect bad``.31 32If you can't repro at the current commit (maybe the build is broken), run33``git bisect skip`` and git will pick a nearby alternate commit.34 35(To abort a bisect, run ``git bisect reset``, and if git complains about not36being able to reset, do the usual ``git checkout -f main; git reset --hard37origin/main`` dance and try again).38 39``git bisect run``40==================41 42A single bisect step often requires first building clang, and then compiling43a large code base with just-built clang. This can take a long time, so it's44good if it can happen completely automatically. ``git bisect run`` can do45this for you if you write a run script that reproduces the problem46automatically. Writing the script can take 10-20 minutes, but it's almost47always worth it -- you can do something else while the bisect runs (such48as writing this document).49 50Here's an example run script. It assumes that you're in ``llvm-project`` and51that you have a sibling ``llvm-build-project`` build directory where you52configured CMake to use Ninja. You have a file ``repro.c`` in the current53directory that makes clang crash at trunk, but it worked fine at revision54``f00ba``.55 56  .. code-block:: bash57 58     # Build clang. If the build fails, `exit 125` causes this59     # revision to be skipped60     ninja -C ../llvm-build-project clang || exit 12561 62     ../llvm-build-project/bin/clang repro.c63 64To make sure your run script works, it's a good idea to run ``./run.sh`` by65hand and tweak the script until it works, then run ``git bisect good`` or66``git bisect bad`` manually once based on the result of the script67(check ``echo $?`` after your script ran), and only then run ``git bisect run68./run.sh``. Don't forget to mark your run script as executable -- ``git bisect69run`` doesn't check for that, it just assumes the run script failed each time.70 71Once your run script works, run ``git bisect run ./run.sh`` and a few hours72later you'll know which commit caused the regression.73 74(This is a very simple run script. Often, you want to use just-built clang75to build a different project and then run a built executable of that project76in the run script.)77 78Bisecting across multiple roots79===============================80 81Here's how LLVM's history currently looks:82 83  .. code-block:: none84 85     A-o-o-......-o-D-o-o-HEAD86                   /87       B-o-...-o-C-88 89``A`` is the first commit in LLVM ever, ``97724f18c79c``.90 91``B`` is the first commit in MLIR, ``aed0d21a62db``.92 93``D`` is the merge commit that merged MLIR into the main LLVM repository,94``0f0d0ed1c78f``.95 96``C`` is the last commit in MLIR before it got merged, ``0f0d0ed1c78f^2``. (The97``^n`` modifier selects the n'th parent of a merge commit.)98 99``git bisect`` goes through all parent revisions. Due to the way MLIR was100merged, at every revision at ``C`` or earlier, *only* the ``mlir/`` directory101exists, and nothing else does.102 103As of early 2020, there is no flag to ``git bisect`` to tell it to not104descend into all reachable commits. Ideally, we'd want to tell it to only105follow the first parent of ``D``.106 107The best workaround is to pass a list of directories to ``git bisect``:108If you know the bug is due to a change in llvm, clang, or compiler-rt, use109 110  .. code-block:: bash111 112     git bisect start -- clang llvm compiler-rt113 114That way, the commits in ``mlir`` are never evaluated.115 116Alternatively, ``git bisect skip aed0d21a6 aed0d21a6..0f0d0ed1c78f`` explicitly117skips all commits on that branch. It takes 1.5 minutes to run on a fast118machine, and makes ``git bisect log`` output unreadable. (``aed0d21a6`` is119listed twice because git ranges exclude the revision listed on the left,120so it needs to be ignored explicitly.)121 122More Resources123==============124 125https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection126