compiler: Emit all-kernel_invariant objects as LLVM constants

This enables constant propagation optimisations, as verified by
the included test case. This is only a first stop-gap measure, though;
we should support optimisation based on kernel invariants on a more
fine-grained level.
This commit is contained in:
David Nadlinger 2016-11-08 01:05:27 +00:00 committed by whitequark
parent 124b257e05
commit bfbdba9205
2 changed files with 30 additions and 0 deletions

View File

@ -1384,6 +1384,7 @@ class LLVMIRGenerator:
def _quote_attributes(): def _quote_attributes():
llglobal = None llglobal = None
llfields = [] llfields = []
emit_as_constant = True
for attr in typ.attributes: for attr in typ.attributes:
if attr == "__objectid__": if attr == "__objectid__":
objectid = self.embedding_map.store_object(value) objectid = self.embedding_map.store_object(value)
@ -1404,6 +1405,8 @@ class LLVMIRGenerator:
not types.is_c_function(typ.attributes[attr])) not types.is_c_function(typ.attributes[attr]))
if is_class_function: if is_class_function:
attrvalue = self.embedding_map.specialize_function(typ.instance, attrvalue) attrvalue = self.embedding_map.specialize_function(typ.instance, attrvalue)
if not (types.is_instance(typ) and attr in typ.constant_attributes):
emit_as_constant = False
llattrvalue = self._quote(attrvalue, typ.attributes[attr], llattrvalue = self._quote(attrvalue, typ.attributes[attr],
lambda: path() + [attr]) lambda: path() + [attr])
llfields.append(llattrvalue) llfields.append(llattrvalue)
@ -1411,6 +1414,7 @@ class LLVMIRGenerator:
llclosureptr = self.get_global_closure_ptr(typ, attr) llclosureptr = self.get_global_closure_ptr(typ, attr)
llclosureptr.initializer = llattrvalue llclosureptr.initializer = llattrvalue
llglobal.global_constant = emit_as_constant
llglobal.initializer = ll.Constant(llty.pointee, llfields) llglobal.initializer = ll.Constant(llty.pointee, llfields)
llglobal.linkage = "private" llglobal.linkage = "private"
return llglobal return llglobal

View File

@ -0,0 +1,26 @@
# RUN: env ARTIQ_DUMP_LLVM=%t %python -m artiq.compiler.testbench.embedding +compile %s
# RUN: OutputCheck %s --file-to-check=%t.ll
from artiq.language.core import *
from artiq.language.types import *
class Class:
kernel_invariants = {"foo"}
def __init__(self):
self.foo = True
@kernel
def run(self):
if self.foo:
print("bar")
else:
# Make sure all the code for this branch will be completely elided:
# CHECK-NOT: baz
print("baz")
obj = Class()
@kernel
def entrypoint():
obj.run()