Skip to content

Commit bfbdba9

Browse files
dnadlingerwhitequark
authored and
whitequark
committedNov 9, 2016
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.
1 parent 124b257 commit bfbdba9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎artiq/compiler/transforms/llvm_ir_generator.py

+4
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ def _quote(self, value, typ, path):
13841384
def _quote_attributes():
13851385
llglobal = None
13861386
llfields = []
1387+
emit_as_constant = True
13871388
for attr in typ.attributes:
13881389
if attr == "__objectid__":
13891390
objectid = self.embedding_map.store_object(value)
@@ -1404,13 +1405,16 @@ def _quote_attributes():
14041405
not types.is_c_function(typ.attributes[attr]))
14051406
if is_class_function:
14061407
attrvalue = self.embedding_map.specialize_function(typ.instance, attrvalue)
1408+
if not (types.is_instance(typ) and attr in typ.constant_attributes):
1409+
emit_as_constant = False
14071410
llattrvalue = self._quote(attrvalue, typ.attributes[attr],
14081411
lambda: path() + [attr])
14091412
llfields.append(llattrvalue)
14101413
if is_class_function:
14111414
llclosureptr = self.get_global_closure_ptr(typ, attr)
14121415
llclosureptr.initializer = llattrvalue
14131416

1417+
llglobal.global_constant = emit_as_constant
14141418
llglobal.initializer = ll.Constant(llty.pointee, llfields)
14151419
llglobal.linkage = "private"
14161420
return llglobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# RUN: env ARTIQ_DUMP_LLVM=%t %python -m artiq.compiler.testbench.embedding +compile %s
2+
# RUN: OutputCheck %s --file-to-check=%t.ll
3+
4+
from artiq.language.core import *
5+
from artiq.language.types import *
6+
7+
class Class:
8+
kernel_invariants = {"foo"}
9+
10+
def __init__(self):
11+
self.foo = True
12+
13+
@kernel
14+
def run(self):
15+
if self.foo:
16+
print("bar")
17+
else:
18+
# Make sure all the code for this branch will be completely elided:
19+
# CHECK-NOT: baz
20+
print("baz")
21+
22+
obj = Class()
23+
24+
@kernel
25+
def entrypoint():
26+
obj.run()

0 commit comments

Comments
 (0)
Please sign in to comment.