forked from M-Labs/artiq
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:
parent
124b257e05
commit
bfbdba9205
@ -1384,6 +1384,7 @@ class LLVMIRGenerator:
|
||||
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,6 +1405,8 @@ class LLVMIRGenerator:
|
||||
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)
|
||||
@ -1411,6 +1414,7 @@ class LLVMIRGenerator:
|
||||
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
artiq/test/lit/embedding/invariant_propagation.py
Normal file
26
artiq/test/lit/embedding/invariant_propagation.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user