xref: /llvm-project/llvm/utils/bisect (revision cfe8bedca034182fa5a6d4970c983c4d8ee3db5e)
155fcf347SMichael Gottesman#!/usr/bin/env python
29106f8c8SJinsong Ji#
39106f8c8SJinsong Ji# The way you use this is you create a script that takes in as its first
49106f8c8SJinsong Ji# argument a count. The script passes into LLVM the count via a command
59106f8c8SJinsong Ji# line flag that disables a pass after LLVM has run after the pass has
69106f8c8SJinsong Ji# run for count number of times. Then the script invokes a test of some
79106f8c8SJinsong Ji# sort and indicates whether LLVM successfully compiled the test via the
89106f8c8SJinsong Ji# scripts exit status. Then you invoke bisect as follows:
99106f8c8SJinsong Ji#
109106f8c8SJinsong Ji# bisect --start=<start_num> --end=<end_num> ./script.sh "%(count)s"
119106f8c8SJinsong Ji#
129106f8c8SJinsong Ji# And bisect will continually call ./script.sh with various counts using
139106f8c8SJinsong Ji# the exit status to determine success and failure.
149106f8c8SJinsong Ji#
15*cfe8bedcSMikhail Maltsevfrom __future__ import print_function
1655fcf347SMichael Gottesmanimport os
1755fcf347SMichael Gottesmanimport sys
1855fcf347SMichael Gottesmanimport argparse
1955fcf347SMichael Gottesmanimport subprocess
2055fcf347SMichael Gottesman
2155fcf347SMichael Gottesmanparser = argparse.ArgumentParser()
2255fcf347SMichael Gottesman
2355fcf347SMichael Gottesmanparser.add_argument('--start', type=int, default=0)
2455fcf347SMichael Gottesmanparser.add_argument('--end', type=int, default=(1 << 32))
2555fcf347SMichael Gottesmanparser.add_argument('command', nargs='+')
2655fcf347SMichael Gottesman
2755fcf347SMichael Gottesmanargs = parser.parse_args()
2855fcf347SMichael Gottesman
2955fcf347SMichael Gottesmanstart = args.start
3055fcf347SMichael Gottesmanend = args.end
3155fcf347SMichael Gottesman
3255fcf347SMichael Gottesmanprint("Bisect Starting!")
3355fcf347SMichael Gottesmanprint("Start: %d" % start)
3455fcf347SMichael Gottesmanprint("End: %d" % end)
3555fcf347SMichael Gottesman
3655fcf347SMichael Gottesmanlast = None
3755fcf347SMichael Gottesmanwhile start != end and start != end-1:
38*cfe8bedcSMikhail Maltsev    count = start + (end - start)//2
3955fcf347SMichael Gottesman    print("Visiting Count: %d with (Start, End) = (%d,%d)" % (count, start, end))
4055fcf347SMichael Gottesman    cmd = [x % {'count':count} for x in args.command]
41*cfe8bedcSMikhail Maltsev    print(cmd)
4255fcf347SMichael Gottesman    result = subprocess.call(cmd)
4355fcf347SMichael Gottesman    if result == 0:
4455fcf347SMichael Gottesman        print("    PASSES! Setting start to count")
4555fcf347SMichael Gottesman        start = count
4655fcf347SMichael Gottesman    else:
4755fcf347SMichael Gottesman        print("    FAILS! Setting end to count")
4855fcf347SMichael Gottesman        end = count
4955fcf347SMichael Gottesman
5055fcf347SMichael Gottesmanprint("Last good count: %d" % start)
51