Commit be00b05b authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Merge pull request #28 from dbrabera/optimizations

Optimizations
parents 82a0fc41 dde057ab
......@@ -23,6 +23,7 @@ from singledispatch import singledispatch
from llvm import core as lc
from llvm import ee as le
from llvm import passes
import ogAST
import Helper
......@@ -377,14 +378,14 @@ class CompileError(Exception):
@singledispatch
def generate(ast, ctx=None):
def generate(ast, ctx=None, options=None):
''' Generate the IR for an AST node '''
raise CompileError('Unsupported AST construct "%s"' % ast.__class__.__name__)
# Processing of the AST
@generate.register(ogAST.Process)
def _process(process, ctx=None):
def _process(process, ctx=None, options=None):
''' Generate the IR for a process '''
process_name = str(process.processName)
LOG.info('Generating LLVM IR code for process ' + process_name)
......@@ -463,6 +464,17 @@ def _process(process, ctx=None):
ctx.module.verify()
if options and options.optimization:
LOG.info('Optimizing generated LLVM IR code for process %s at level %d'
% (ctx.name, options.optimization))
pm = passes.PassManager.new()
pmb = passes.PassManagerBuilder.new()
pmb.opt_level = options.optimization
pmb.populate(pm)
pm.run(ctx.module)
with open(ctx.name + '.ll', 'w') as ll_file:
ll_file.write(str(ctx.module))
......
all: compile-all
test-parse:
@$(MAKE) -s -C tests/regression test-parse
@$(MAKE) -s -C tests/regression $@
test-ada:
@$(MAKE) -s -C tests/regression test-ada
@$(MAKE) -s -C tests/regression $@
test-llvm:
@$(MAKE) -s -C tests/regression test-llvm
@$(MAKE) -s -C tests/regression $@
benchmark:
@$(MAKE) -s -C tests/regression benchmark
@$(MAKE) -s -C tests/regression $@
benchmark-O1:
@$(MAKE) -s -C tests/regression $@
benchmark-O2:
@$(MAKE) -s -C tests/regression $@
benchmark-O3:
@$(MAKE) -s -C tests/regression $@
coverage:
@$(MAKE) -s -C tests/regression coverage
@$(MAKE) -s -C tests/regression $@
flake8:
@echo Generating flake8_report file
......@@ -45,9 +54,9 @@ publish: install
@python setup.py sdist upload
clean:
@$(MAKE) -s -C tests/regression clean
@$(MAKE) -s -C tests/regression $@
@find . -name '*~' | xargs rm -f
@find . -name '*.o' | xargs rm -f
.PHONY: all test-parse test-ada test-llvm benchmark flake8 coverage \
compile-all install publish clean
.PHONY: all test-parse test-ada test-llvm benchmark benchmark-O1 benchmark-O2 \
benchmark-O3 flake8 coverage compile-all install publish clean
......@@ -24,7 +24,6 @@ import re
import code
import pprint
from functools import partial
from collections import deque
from itertools import chain
# Added to please py2exe - NOQA makes flake8 ignore the following lines:
......@@ -410,6 +409,7 @@ class SDL_Scene(QtGui.QGraphicsScene, object):
def render_everything(self, ast):
''' Render a process and its children scenes, recursively '''
already_created = []
def recursive_render(content, dest_scene):
''' Process the rendering in scenes and nested scenes '''
if isinstance(content, ogAST.Process):
......@@ -420,7 +420,7 @@ class SDL_Scene(QtGui.QGraphicsScene, object):
# Render top-level items and their children:
for each in Renderer.render(content, dest_scene):
G_SYMBOLS.add(each)
except TypeError as err:
except TypeError:
LOG.error(traceback.format_exc())
# Render nested scenes, recursively:
......@@ -978,7 +978,6 @@ class SDL_Scene(QtGui.QGraphicsScene, object):
pprint.pprint(selection.__dict__, None, 2, 1)
code.interact('type your command:', local=locals())
def create_subscene(self, context):
''' Create a new SDL scene, e.g. for nested symbols '''
subscene = SDL_Scene(context=context)
......@@ -986,7 +985,6 @@ class SDL_Scene(QtGui.QGraphicsScene, object):
subscene.parent_scene = self
return subscene
def place_symbol(self, item_type, parent, pos=None):
''' Draw a symbol on the scene '''
item = item_type()
......@@ -1803,6 +1801,9 @@ def parse_args():
help='Generate Ada code for the .pr file')
parser.add_argument('--llvm', dest='llvm', action='store_true',
help='Generate LLVM IR code for the .pr file (experimental)')
parser.add_argument("-O", dest="optimization", metavar="level", type=int,
action="store", choices=[0, 1, 2, 3], default=0,
help="Set optimization level for the generated LLVM IR code")
parser.add_argument('--png', dest='png', action='store_true',
help='Generate a PNG file for the process')
parser.add_argument('--pdf', dest='pdf', action='store_true',
......@@ -1883,7 +1884,7 @@ def generate(process, options):
if options.llvm:
LOG.info('Generating LLVM code')
try:
LlvmGenerator.generate(process)
LlvmGenerator.generate(process, options=options)
except (TypeError, ValueError, NameError) as err:
LOG.error(str(err))
LOG.debug(str(traceback.format_exc()))
......
DISABLED_TESTS=test-branchcoverage/
TEST_CASES=$(filter-out $(DISABLED_TESTS), $(sort $(dir $(wildcard */))))
DISABLED_BENCHMARKS=test1/ test2/ test3/ test4/ test5/ test6/ test7/ test9/ test12/ test-branchcoverage/
DISABLED_BENCHMARKS=test1/ test2/ test3/ test4/ test5/ test6/ test7/ test9/ \
test12/ test-branchcoverage/
BENCHMARK_CASES=$(filter-out $(DISABLED_BENCHMARKS), $(sort $(dir $(wildcard */))))
test-parse:
......@@ -16,6 +17,15 @@ test-llvm:
benchmark:
@python benchmark.py $(BENCHMARK_CASES)
benchmark-O1:
@python benchmark.py $(BENCHMARK_CASES) -O1
benchmark-O2:
@python benchmark.py $(BENCHMARK_CASES) -O2
benchmark-O3:
@python benchmark.py $(BENCHMARK_CASES) -O3
coverage:
@for v in $(TEST_CASES) ; \
do $(MAKE) -C $$v coverage && mv $$v/.coverage* . || exit 1 ; \
......@@ -27,4 +37,5 @@ clean:
do $(MAKE) -s -C $$v clean ; \
done
.PHONY: test-parse test-ada test-llvm benchmark coverage clean
.PHONY: test-parse test-ada test-llvm benchmark benchmark-O1 benchmark-O2 \
benchmark-O3 coverage clean
import argparse
import os
import string
import subprocess
......@@ -8,12 +9,13 @@ from tabulate import tabulate
def main():
options = parse_args()
start = t.time()
results = []
testfolders = sys.argv[1:]
for testfolder in testfolders:
results.append(benchmark(testfolder))
for testfolder in options.testfolders:
results.append(benchmark(testfolder, options.optimization))
make(testfolder, 'clean')
sys.stdout.write('.')
sys.stdout.flush()
......@@ -21,16 +23,26 @@ def main():
print ""
elapsed = t.time() - start
sys.exit(summarize(results, elapsed))
sys.exit(summarize(results, elapsed, options))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-O", dest="optimization", metavar="level", type=int,
action="store", choices=[0, 1, 2, 3], default=0,
help="set optimization level")
parser.add_argument('testfolders', metavar='testfolder', type=str, nargs='*',
help='test folder(s)')
return parser.parse_args()
def benchmark(testfolder):
def benchmark(testfolder, opt_level):
result = {
"name": testfolder[:-1],
}
for rule in ("test-llvm", "test-ada"):
if make(testfolder, rule) != 0:
if make(testfolder, rule, "O=%d" % opt_level) != 0:
result["status"] = "ERROR"
return result
......@@ -68,7 +80,7 @@ def time(file, iters=1000):
return (t.time() - start) / iters
def summarize(results, elapsed):
def summarize(results, elapsed, options):
print ""
print "Summary"
print "-------"
......@@ -94,6 +106,9 @@ def summarize(results, elapsed):
print "No results"
return 1
if options.optimization != 0:
print "Optimization level: %d" % options.optimization
print ""
print "Size: Ada %.2f%% LLVM %.2f%%" % diff([r["size"] for r in valid_results])
print "Time: Ada %.2f%% LLVM %.2f%%" % diff([r["time"] for r in valid_results])
print ""
......@@ -122,8 +137,10 @@ def mean(values):
return sum(values) / len(values)
def make(path, rule):
return call(["make", "-C", path, rule])[0]
def make(path, rule, *args):
call_args = ["make", "-C", path, rule]
call_args.extend(args)
return call(call_args)[0]
def call(args):
......
......@@ -5,6 +5,7 @@ LLC=llc
GNATMAKE=gnatmake
GNATBIND=gnatbind
GNATLINK=gnatlink
O=0
clean:
rm -rf *.adb *.ads *.pyc runSpark.sh spark.idx *.o *.ali gnat.cfg \
......@@ -12,4 +13,23 @@ clean:
real.c xer.c ber.c acn.c asn1crt.c asn1crt.h test_ada test_llvm \
*.autosave
%.o: %.pr FORCE
$(OPENGEODE) $< system_structure.pr --llvm -O$(O)
$(LLC) $*.ll
$(CC) -O$(O) -c $*.s
%.ali: %.pr FORCE
$(OPENGEODE) $< system_structure.pr --toAda
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -O$(O) -c *.adb
%.o: %.asn FORCE
$(ASN1SCC) -c $< -typePrefix asn1Scc -equal
$(CC) -O$(O) -c $*.c
%.o: %.c FORCE
$(CC) -O$(O) -c $<
FORCE:
.PHONY: clean
\ No newline at end of file
......@@ -8,23 +8,12 @@ edit:
test-parse:
$(OPENGEODE) controlflow.pr system_structure.pr --check
test-ada:
$(OPENGEODE) controlflow.pr system_structure.pr --check --toAda
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: controlflow.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n controlflow.ali
$(GNATLINK) test_ada.o controlflow.ali -lgnat -lm -o test_ada
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) controlflow.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) controlflow.ll
$(CC) -c controlflow.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: controlflow.o dataview-uniq.o | test_llvm.o
$(CC) controlflow.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......
......@@ -8,22 +8,12 @@ edit:
test-parse:
$(OPENGEODE) challenge.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda challenge.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: challenge.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n challenge.ali
$(GNATLINK) -o test_ada test_ada.o challenge.ali -lgnat -lm
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) challenge.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) challenge.ll
$(CC) -c challenge.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: challenge.o dataview-uniq.o | test_llvm.o
$(CC) challenge.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......
......@@ -8,23 +8,12 @@ edit:
test-parse:
$(OPENGEODE) expressions.pr system_structure.pr --check
test-ada:
$(OPENGEODE) expressions.pr system_structure.pr --check --toAda
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: expressions.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n expressions.ali
$(GNATLINK) test_ada.o expressions.ali -lgnat -lm -o test_ada
./test_ada
test-llvm:
$(OPENGEODE) expressions.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) expressions.ll
$(CC) -c expressions.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: expressions.o dataview-uniq.o | test_llvm.o
$(CC) expressions.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm
......
......@@ -8,23 +8,12 @@ edit:
test-parse:
$(OPENGEODE) operators.pr system_structure.pr --check
test-ada:
$(OPENGEODE) operators.pr system_structure.pr --check --toAda
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: operators.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n operators.ali
$(GNATLINK) test_ada.o operators.ali -lgnat -lm -o test_ada
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) operators.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) operators.ll
$(CC) -c operators.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: operators.o dataview-uniq.o | test_llvm.o
$(CC) operators.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......
......@@ -8,23 +8,12 @@ edit:
test-parse:
$(OPENGEODE) myfunction.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda myfunction.pr system_structure.pr || exit 1
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc
$(GNATMAKE) -c myfunction.adb
$(CC) -c test_ada.c
test-ada: myfunction.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n myfunction.ali
$(GNATLINK) -o test_ada test_ada.o myfunction.ali -lgnat
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) myfunction.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -equal -typePrefix asn1Scc
$(LLC) myfunction.ll
$(CC) -c myfunction.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: myfunction.o dataview-uniq.o | test_llvm.o
$(CC) myfunction.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......
......@@ -8,15 +8,9 @@ edit:
test-parse:
$(OPENGEODE) og.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda og.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c og.adb
test-ada: og.ali
test-llvm:
$(OPENGEODE) og.pr system_structure.pr --llvm
$(LLC) og.ll
$(CC) -c og.s
test-llvm: og.o
coverage:
coverage run -p $(OPENGEODE) og.pr system_structure.pr --toAda
......
......@@ -8,22 +8,12 @@ edit:
test-parse:
$(OPENGEODE) challenge.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda challenge.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: challenge.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n challenge.ali
$(GNATLINK) -o test_ada test_ada.o challenge.ali -lgnat -lm
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) challenge.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) challenge.ll
$(CC) -c challenge.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: challenge.o dataview-uniq.o | test_llvm.o
$(CC) challenge.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm| diff expected -
......
......@@ -8,22 +8,12 @@ edit:
test-parse:
$(OPENGEODE) og.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda og.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c *.adb
$(CC) -c test_ada.c
test-ada: og.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n og.ali
$(GNATLINK) -o test_ada test_ada.o og.ali -lgnat
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) og.pr system_structure.pr --llvm
$(ASN1SCC) -c dataview-uniq.asn -equal -typePrefix asn1Scc
$(LLC) og.ll
$(CC) -c og.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: og.o dataview-uniq.o | test_llvm.o
$(CC) og.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......
......@@ -8,18 +8,9 @@ edit:
test-parse:
$(OPENGEODE) trafficlight.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda trafficlight.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c *.adb
#$(CC) -c test.c
#$(GNATBIND) -n trafficlight.ali
#$(GNATLINK) -o testcase test.o trafficlight.ali -lgnat -lm
test-llvm:
$(OPENGEODE) trafficlight.pr system_structure.pr --llvm
$(LLC) trafficlight.ll
$(CC) -c trafficlight.s
test-ada: trafficlight.ali
test-llvm: trafficlight.o
test-export:
$(OPENGEODE) --png --pdf --svg trafficlight.pr system_structure.pr
......
......@@ -8,15 +8,9 @@ edit:
test-parse:
$(OPENGEODE) orchestrator.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda orchestrator.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c orchestrator.adb
test-ada: orchestrator.ali
test-llvm:
$(OPENGEODE) orchestrator.pr system_structure.pr --llvm
$(LLC) orchestrator.ll
$(CC) -c orchestrator.s
test-llvm: orchestrator.o
coverage:
coverage run -p $(OPENGEODE) orchestrator.pr system_structure.pr --toAda
......
......@@ -8,15 +8,9 @@ edit:
test-parse:
$(OPENGEODE) fce.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda fce.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c fce.adb
test-ada: fce.ali
test-llvm:
$(OPENGEODE) fce.pr system_structure.pr --llvm
$(LLC) fce.ll
$(CC) -c fce.s
test-llvm: fce.o
coverage:
coverage run -p $(OPENGEODE) fce.pr system_structure.pr --toAda
......
......@@ -8,15 +8,9 @@ edit:
test-parse:
$(OPENGEODE) orchestrator.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda orchestrator.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c orchestrator.adb
test-ada: orchestrator.ali
test-llvm:
$(OPENGEODE) orchestrator.pr system_structure.pr --llvm
$(LLC) orchestrator.ll
$(CC) -c orchestrator.s
test-llvm: orchestrator.o
coverage:
coverage run -p $(OPENGEODE) orchestrator.pr system_structure.pr --toAda
......
......@@ -8,15 +8,9 @@ edit:
test-parse:
$(OPENGEODE) function0.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda function0.pr system_structure.pr
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -c function0.adb
test-ada: function0.ali
test-llvm:
$(OPENGEODE) function0.pr system_structure.pr --llvm
$(LLC) function0.ll
$(CC) -c function0.s
test-llvm: function0.o
coverage:
coverage run -p $(OPENGEODE) function0.pr system_structure.pr --toAda
......
......@@ -8,23 +8,12 @@ edit:
test-parse:
$(OPENGEODE) myfunction.pr system_structure.pr --check
test-ada:
$(OPENGEODE) --toAda myfunction.pr system_structure.pr || exit 1
$(ASN1SCC) -Ada dataview-uniq.asn -typePrefix asn1Scc -equal
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc
$(GNATMAKE) -c myfunction.adb
$(CC) -c test_ada.c
test-ada: myfunction.ali dataview-uniq.o | test_ada.o
$(GNATBIND) -n myfunction.ali
$(GNATLINK) -o test_ada test_ada.o myfunction.ali -lgnat
./test_ada | diff expected -
test-llvm:
$(OPENGEODE) myfunction.pr system_structure.pr --check --llvm
$(ASN1SCC) -c dataview-uniq.asn -typePrefix asn1Scc -equal
$(LLC) myfunction.ll
$(CC) -c myfunction.s
$(CC) -c dataview-uniq.c
$(CC) -c test_llvm.c
test-llvm: myfunction.o dataview-uniq.o | test_llvm.o
$(CC) myfunction.o dataview-uniq.o test_llvm.o -o test_llvm -lm
./test_llvm | diff expected -
......