From bfbdba92057c021f1876db004e5851ac2b991918 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Tue, 8 Nov 2016 01:05:27 +0000 Subject: [PATCH] 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. --- .../compiler/transforms/llvm_ir_generator.py | 4 +++ .../lit/embedding/invariant_propagation.py | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 artiq/test/lit/embedding/invariant_propagation.py diff --git a/artiq/compiler/transforms/llvm_ir_generator.py b/artiq/compiler/transforms/llvm_ir_generator.py index b99c6ed3b..1095c2e64 100644 --- a/artiq/compiler/transforms/llvm_ir_generator.py +++ b/artiq/compiler/transforms/llvm_ir_generator.py @@ -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 diff --git a/artiq/test/lit/embedding/invariant_propagation.py b/artiq/test/lit/embedding/invariant_propagation.py new file mode 100644 index 000000000..e1af5c9c1 --- /dev/null +++ b/artiq/test/lit/embedding/invariant_propagation.py @@ -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()