From 4ede58e44b5469e21ba7715390c353962b7c058c Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 6 Jul 2021 15:23:24 +0800 Subject: [PATCH] compiler: reduce calls to TypedTreeHasher We need to check if our inference reached a fixed point. This is checked using hash of the types in the AST, which is very slow. This patch avoids computing the hash if we can make sure that the AST is definitely changed, which is when we parse a new function. For some simple programs with many functions, this can significantly reduce the compile time by up to ~30%. --- artiq/compiler/embedding.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 4ff303274..6fa6078f3 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -737,6 +737,7 @@ class Stitcher: self.embedding_map = EmbeddingMap() self.value_map = defaultdict(lambda: []) + self.definitely_changed = False def stitch_call(self, function, args, kwargs, callback=None): # We synthesize source code for the initial call so that @@ -757,13 +758,19 @@ class Stitcher: old_attr_count = None while True: inferencer.visit(self.typedtree) - typedtree_hash = typedtree_hasher.visit(self.typedtree) - attr_count = self.embedding_map.attribute_count() + if self.definitely_changed: + changed = True + self.definitely_changed = False + else: + typedtree_hash = typedtree_hasher.visit(self.typedtree) + attr_count = self.embedding_map.attribute_count() + changed = old_attr_count != attr_count or \ + old_typedtree_hash != typedtree_hash + old_typedtree_hash = typedtree_hash + old_attr_count = attr_count - if old_typedtree_hash == typedtree_hash and old_attr_count == attr_count: + if not changed: break - old_typedtree_hash = typedtree_hash - old_attr_count = attr_count # After we've discovered every referenced attribute, check if any kernel_invariant # specifications refers to ones we didn't encounter. @@ -891,6 +898,9 @@ class Stitcher: return types.TVar() def _quote_embedded_function(self, function, flags): + # we are now parsing new functions... definitely changed the type + self.definitely_changed = True + if isinstance(function, SpecializedFunction): host_function = function.host_function else: