Skip to content

Commit

Permalink
compiler: Emit all-kernel_invariant objects as LLVM constants
Browse files Browse the repository at this point in the history
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.
dnadlinger authored and whitequark committed Nov 9, 2016
1 parent 124b257 commit bfbdba9
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions artiq/compiler/transforms/llvm_ir_generator.py
Original file line number Diff line number Diff line change
@@ -1384,6 +1384,7 @@ def _quote(self, value, typ, path):
def _quote_attributes():
llglobal = None
llfields = []
emit_as_constant = True
for attr in typ.attributes:
if attr == "__objectid__":
objectid = self.embedding_map.store_object(value)
@@ -1404,13 +1405,16 @@ def _quote_attributes():
not types.is_c_function(typ.attributes[attr]))
if is_class_function:
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],
lambda: path() + [attr])
llfields.append(llattrvalue)
if is_class_function:
llclosureptr = self.get_global_closure_ptr(typ, attr)
llclosureptr.initializer = llattrvalue

llglobal.global_constant = emit_as_constant
llglobal.initializer = ll.Constant(llty.pointee, llfields)
llglobal.linkage = "private"
return llglobal
26 changes: 26 additions & 0 deletions artiq/test/lit/embedding/invariant_propagation.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit bfbdba9

Please sign in to comment.