xref: /llvm-project/llvm/examples/Kaleidoscope/MCJIT/cached/split-lib.py (revision b71edfaa4ec3c998aadb35255ce2f60bba2940b0)
1#!/usr/bin/env python
2
3from __future__ import print_function
4
5
6class TimingScriptGenerator:
7    """Used to generate a bash script which will invoke the toy and time it"""
8
9    def __init__(self, scriptname, outputname):
10        self.shfile = open(scriptname, "w")
11        self.timeFile = outputname
12        self.shfile.write('echo "" > %s\n' % self.timeFile)
13
14    def writeTimingCall(self, irname, callname):
15        """Echo some comments and invoke both versions of toy"""
16        rootname = irname
17        if "." in irname:
18            rootname = irname[: irname.rfind(".")]
19        self.shfile.write(
20            'echo "%s: Calls %s" >> %s\n' % (callname, irname, self.timeFile)
21        )
22        self.shfile.write('echo "" >> %s\n' % self.timeFile)
23        self.shfile.write('echo "With MCJIT" >> %s\n' % self.timeFile)
24        self.shfile.write(
25            '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"'
26        )
27        self.shfile.write(" -o %s -a " % self.timeFile)
28        self.shfile.write(
29            "./toy-mcjit -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n"
30            % (irname, callname, rootname, rootname)
31        )
32        self.shfile.write('echo "" >> %s\n' % self.timeFile)
33        self.shfile.write('echo "With MCJIT again" >> %s\n' % self.timeFile)
34        self.shfile.write(
35            '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"'
36        )
37        self.shfile.write(" -o %s -a " % self.timeFile)
38        self.shfile.write(
39            "./toy-mcjit -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n"
40            % (irname, callname, rootname, rootname)
41        )
42        self.shfile.write('echo "" >> %s\n' % self.timeFile)
43        self.shfile.write('echo "With JIT" >> %s\n' % self.timeFile)
44        self.shfile.write(
45            '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"'
46        )
47        self.shfile.write(" -o %s -a " % self.timeFile)
48        self.shfile.write(
49            "./toy-jit -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n"
50            % (irname, callname, rootname, rootname)
51        )
52        self.shfile.write('echo "" >> %s\n' % self.timeFile)
53        self.shfile.write('echo "" >> %s\n' % self.timeFile)
54
55
56class LibScriptGenerator:
57    """Used to generate a bash script which will convert Kaleidoscope files to IR"""
58
59    def __init__(self, filename):
60        self.shfile = open(filename, "w")
61
62    def writeLibGenCall(self, libname, irname):
63        self.shfile.write("./toy-ir-gen < %s 2> %s\n" % (libname, irname))
64
65
66def splitScript(inputname, libGenScript, timingScript):
67    rootname = inputname[:-2]
68    libname = rootname + "-lib.k"
69    irname = rootname + "-lib.ir"
70    callname = rootname + "-call.k"
71    infile = open(inputname, "r")
72    libfile = open(libname, "w")
73    callfile = open(callname, "w")
74    print("Splitting %s into %s and %s" % (inputname, callname, libname))
75    for line in infile:
76        if not line.startswith("#"):
77            if line.startswith("print"):
78                callfile.write(line)
79            else:
80                libfile.write(line)
81    libGenScript.writeLibGenCall(libname, irname)
82    timingScript.writeTimingCall(irname, callname)
83
84
85# Execution begins here
86libGenScript = LibScriptGenerator("make-libs.sh")
87timingScript = TimingScriptGenerator("time-lib.sh", "lib-timing.txt")
88
89script_list = [
90    "test-5000-3-50-50.k",
91    "test-5000-10-100-10.k",
92    "test-5000-10-5-10.k",
93    "test-5000-10-1-0.k",
94    "test-1000-3-10-50.k",
95    "test-1000-10-100-10.k",
96    "test-1000-10-5-10.k",
97    "test-1000-10-1-0.k",
98    "test-200-3-2-50.k",
99    "test-200-10-40-10.k",
100    "test-200-10-2-10.k",
101    "test-200-10-1-0.k",
102]
103
104for script in script_list:
105    splitScript(script, libGenScript, timingScript)
106print("All done!")
107