From 84812fa7862888d1b57cde1e7a106eca216a40d4 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 02:04:18 -0700 Subject: [PATCH 01/11] Reduces repeated sorting and hashing to speed up adding debug info. --- llvmlite/ir/_utils.py | 15 +++++-- llvmlite/ir/module.py | 14 ++++--- llvmlite/tests/test_ir.py | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/llvmlite/ir/_utils.py b/llvmlite/ir/_utils.py index 8287d77af..9b23e12eb 100644 --- a/llvmlite/ir/_utils.py +++ b/llvmlite/ir/_utils.py @@ -1,4 +1,3 @@ -from collections import defaultdict class DuplicatedNameError(NameError): @@ -8,7 +7,7 @@ class DuplicatedNameError(NameError): class NameScope(object): def __init__(self): self._useset = set(['']) - self._basenamemap = defaultdict(int) + self._basenamemap = {} def is_used(self, name): return name in self._useset @@ -23,10 +22,18 @@ def register(self, name, deduplicate=False): def deduplicate(self, name): basename = name + + try: + ident = self._basenamemap[basename] + except KeyError: + ident = 0 + while self.is_used(name): - ident = self._basenamemap[basename] + 1 - self._basenamemap[basename] = ident + ident += 1 name = "{0}.{1}".format(basename, ident) + + self._basenamemap[basename] = ident + return name def get_child(self): diff --git a/llvmlite/ir/module.py b/llvmlite/ir/module.py index 464f91ec3..bb18a2d74 100644 --- a/llvmlite/ir/module.py +++ b/llvmlite/ir/module.py @@ -52,11 +52,11 @@ def add_metadata(self, operands): if not isinstance(operands, (list, tuple)): raise TypeError("expected a list or tuple of metadata values, " "got %r" % (operands,)) - operands = self._fix_metadata_operands(operands) - key = tuple(operands) + key = hash(tuple(operands)) if key not in self._metadatacache: n = len(self.metadata) - md = values.MDValue(self, operands, name=str(n)) + fixed_operands = self._fix_metadata_operands(operands) + md = values.MDValue(self, fixed_operands, name=str(n)) self._metadatacache[key] = md else: md = self._metadatacache[key] @@ -72,11 +72,13 @@ def add_debug_info(self, kind, operands, is_distinct=False): A DIValue instance is returned, it can then be associated to e.g. an instruction. """ - operands = tuple(sorted(self._fix_di_operands(operands.items()))) - key = (kind, operands, is_distinct) + sorted_operands = sorted(operands.items()) + key = hash((kind, tuple(sorted_operands), is_distinct)) if key not in self._metadatacache: n = len(self.metadata) - di = values.DIValue(self, is_distinct, kind, operands, name=str(n)) + fixed_operands = self._fix_di_operands(sorted_operands) + di = values.DIValue( + self, is_distinct, kind, fixed_operands, name=str(n)) self._metadatacache[key] = di else: di = self._metadatacache[key] diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 8516b1651..8350f03ad 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -7,6 +7,7 @@ import pickle import re import textwrap +import timeit import unittest from . import TestCase @@ -526,6 +527,88 @@ def test_debug_info_unicode_string(self): name = ''.join(map(lambda x: f"\\{x:02x}", "∆".encode())) self.assert_ir_line(f'!0 = !DILocalVariable(name: "a{name}")', strmod) + def test_debug_info_caching(self): + mod = None + foo = None + builder = None + + def setup_test(): + nonlocal mod, foo, builder + mod = self.module() + foo = ir.Function(mod, ir.FunctionType(ir.VoidType(), []), 'foo') + builder = ir.IRBuilder(foo.append_basic_block('')) + + def do_test(): + for i in range(500): + di_file = mod.add_debug_info( + 'DIFile', + { + 'directory': 'my_directory', + 'filename': 'my_path.foo', + }) + + di_compile_unit = mod.add_debug_info( + 'DICompileUnit', + { + 'emissionKind': ir.DIToken('FullDebug'), + 'file': di_file, + 'isOptimized': False, + 'language': ir.DIToken('DW_LANG_C99'), + 'producer': 'llvmlite-test', + }) + + di_subprogram = mod.add_debug_info( + 'DISubprogram', + { + 'file': di_file, + 'line': 123, + 'name': 'my function', + 'scope': di_file, + 'scopeLine': 123, + 'unit': di_compile_unit, + }, + is_distinct=True) + + di_location = mod.add_debug_info( + 'DILocation', + { + 'scope': di_subprogram, + 'line': i, + 'column': 15 + }) + + builder.debug_metadata = di_location + + builder.and_( + ir.Constant(ir.IntType(bits=32), i), + ir.Constant(ir.IntType(bits=32), i + 1)) + + # Use this section to measure overall performance + # total_time = timeit.timeit( + # 'do_test()', + # 'setup_test()', + # number=200, + # globals=locals()) + + # print(f'test_debug_info_performance took {total_time} to finish') + + # Use this section to profile the caching behavior + setup_test() + + import cProfile, pstats + from pstats import SortKey + with cProfile.Profile() as pr: + for i in range(200): + do_test() + + stats = pstats.Stats(pr) + stats = stats.strip_dirs() + stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) + stats.print_stats() + stats.print_callers() + + self.assertEqual(300001, len(mod._metadatacache)) + def test_inline_assembly(self): mod = self.module() foo = ir.Function(mod, ir.FunctionType(ir.VoidType(), []), 'foo') From 1edf17dbbb37244e4d8e5187fba548c6b77ab9f9 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 02:11:41 -0700 Subject: [PATCH 02/11] Making adjustments to make module run faster --- llvmlite/ir/module.py | 35 ++++++++++++++++++++--------------- llvmlite/tests/test_ir.py | 36 ++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/llvmlite/ir/module.py b/llvmlite/ir/module.py index bb18a2d74..eda7a55f2 100644 --- a/llvmlite/ir/module.py +++ b/llvmlite/ir/module.py @@ -53,13 +53,15 @@ def add_metadata(self, operands): raise TypeError("expected a list or tuple of metadata values, " "got %r" % (operands,)) key = hash(tuple(operands)) - if key not in self._metadatacache: - n = len(self.metadata) - fixed_operands = self._fix_metadata_operands(operands) - md = values.MDValue(self, fixed_operands, name=str(n)) - self._metadatacache[key] = md - else: - md = self._metadatacache[key] + try: + return self._metadatacache[key] + except KeyError: + pass + + n = len(self.metadata) + fixed_operands = self._fix_metadata_operands(operands) + md = values.MDValue(self, fixed_operands, name=str(n)) + self._metadatacache[key] = md return md def add_debug_info(self, kind, operands, is_distinct=False): @@ -74,14 +76,17 @@ def add_debug_info(self, kind, operands, is_distinct=False): """ sorted_operands = sorted(operands.items()) key = hash((kind, tuple(sorted_operands), is_distinct)) - if key not in self._metadatacache: - n = len(self.metadata) - fixed_operands = self._fix_di_operands(sorted_operands) - di = values.DIValue( - self, is_distinct, kind, fixed_operands, name=str(n)) - self._metadatacache[key] = di - else: - di = self._metadatacache[key] + + try: + return self._metadatacache[key] + except KeyError: + pass + + n = len(self.metadata) + fixed_operands = self._fix_di_operands(sorted_operands) + di = values.DIValue( + self, is_distinct, kind, fixed_operands, name=str(n)) + self._metadatacache[key] = di return di def add_named_metadata(self, name, element=None): diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 8350f03ad..4d790163f 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -584,30 +584,30 @@ def do_test(): ir.Constant(ir.IntType(bits=32), i + 1)) # Use this section to measure overall performance - # total_time = timeit.timeit( - # 'do_test()', - # 'setup_test()', - # number=200, - # globals=locals()) + total_time = timeit.timeit( + 'do_test()', + 'setup_test()', + number=500, + globals=locals()) - # print(f'test_debug_info_performance took {total_time} to finish') + print(f'test_debug_info_performance took {total_time} to finish') # Use this section to profile the caching behavior - setup_test() + # setup_test() - import cProfile, pstats - from pstats import SortKey - with cProfile.Profile() as pr: - for i in range(200): - do_test() + # import cProfile, pstats + # from pstats import SortKey + # with cProfile.Profile() as pr: + # for i in range(200): + # do_test() - stats = pstats.Stats(pr) - stats = stats.strip_dirs() - stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) - stats.print_stats() - stats.print_callers() + # stats = pstats.Stats(pr) + # stats = stats.strip_dirs() + # stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) + # stats.print_stats() + # stats.print_callers() - self.assertEqual(300001, len(mod._metadatacache)) + self.assertEqual(750001, len(mod._metadatacache)) def test_inline_assembly(self): mod = self.module() From 2b9dc3376c1b3590f32cdebcdc64e36dd65c59c0 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 03:12:06 -0700 Subject: [PATCH 03/11] Adjusting test parameters --- llvmlite/ir/module.py | 1 + llvmlite/tests/test_ir.py | 43 ++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/llvmlite/ir/module.py b/llvmlite/ir/module.py index eda7a55f2..e67d4771c 100644 --- a/llvmlite/ir/module.py +++ b/llvmlite/ir/module.py @@ -52,6 +52,7 @@ def add_metadata(self, operands): if not isinstance(operands, (list, tuple)): raise TypeError("expected a list or tuple of metadata values, " "got %r" % (operands,)) + key = hash(tuple(operands)) try: return self._metadatacache[key] diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 4d790163f..b0516bc9d 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -566,8 +566,7 @@ def do_test(): 'scope': di_file, 'scopeLine': 123, 'unit': di_compile_unit, - }, - is_distinct=True) + }) di_location = mod.add_debug_info( 'DILocation', @@ -584,28 +583,30 @@ def do_test(): ir.Constant(ir.IntType(bits=32), i + 1)) # Use this section to measure overall performance - total_time = timeit.timeit( - 'do_test()', - 'setup_test()', - number=500, - globals=locals()) + # total_time = timeit.timeit( + # 'do_test()', + # 'setup_test()', + # number=500, + # globals=locals()) + + # print(f'test_debug_info_performance took {total_time} to finish') - print(f'test_debug_info_performance took {total_time} to finish') + print(mod) # Use this section to profile the caching behavior - # setup_test() - - # import cProfile, pstats - # from pstats import SortKey - # with cProfile.Profile() as pr: - # for i in range(200): - # do_test() - - # stats = pstats.Stats(pr) - # stats = stats.strip_dirs() - # stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) - # stats.print_stats() - # stats.print_callers() + setup_test() + + import cProfile, pstats + from pstats import SortKey + with cProfile.Profile() as pr: + for i in range(500): + do_test() + + stats = pstats.Stats(pr) + stats = stats.strip_dirs() + stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) + stats.print_stats() + stats.print_callers() self.assertEqual(750001, len(mod._metadatacache)) From e5585c769ae812f9ac5f792268ab119b854f707e Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 03:18:48 -0700 Subject: [PATCH 04/11] Simplifying test --- llvmlite/ir/module.py | 5 ++-- llvmlite/tests/test_ir.py | 62 ++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/llvmlite/ir/module.py b/llvmlite/ir/module.py index e67d4771c..470df0645 100644 --- a/llvmlite/ir/module.py +++ b/llvmlite/ir/module.py @@ -75,8 +75,8 @@ def add_debug_info(self, kind, operands, is_distinct=False): A DIValue instance is returned, it can then be associated to e.g. an instruction. """ - sorted_operands = sorted(operands.items()) - key = hash((kind, tuple(sorted_operands), is_distinct)) + fixed_operands = self._fix_di_operands(sorted(operands.items())) + key = hash((kind, tuple(fixed_operands), is_distinct)) try: return self._metadatacache[key] @@ -84,7 +84,6 @@ def add_debug_info(self, kind, operands, is_distinct=False): pass n = len(self.metadata) - fixed_operands = self._fix_di_operands(sorted_operands) di = values.DIValue( self, is_distinct, kind, fixed_operands, name=str(n)) self._metadatacache[key] = di diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index b0516bc9d..e35a4c261 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -531,43 +531,45 @@ def test_debug_info_caching(self): mod = None foo = None builder = None + di_subprogram = None def setup_test(): - nonlocal mod, foo, builder + nonlocal mod, foo, builder, di_subprogram mod = self.module() + + di_file = mod.add_debug_info( + 'DIFile', + { + 'directory': 'my_directory', + 'filename': 'my_path.foo', + }) + + di_compile_unit = mod.add_debug_info( + 'DICompileUnit', + { + 'emissionKind': ir.DIToken('FullDebug'), + 'file': di_file, + 'isOptimized': False, + 'language': ir.DIToken('DW_LANG_C99'), + 'producer': 'llvmlite-test', + }) + + di_subprogram = mod.add_debug_info( + 'DISubprogram', + { + 'file': di_file, + 'line': 123, + 'name': 'my function', + 'scope': di_file, + 'scopeLine': 123, + 'unit': di_compile_unit, + }) + foo = ir.Function(mod, ir.FunctionType(ir.VoidType(), []), 'foo') builder = ir.IRBuilder(foo.append_basic_block('')) def do_test(): for i in range(500): - di_file = mod.add_debug_info( - 'DIFile', - { - 'directory': 'my_directory', - 'filename': 'my_path.foo', - }) - - di_compile_unit = mod.add_debug_info( - 'DICompileUnit', - { - 'emissionKind': ir.DIToken('FullDebug'), - 'file': di_file, - 'isOptimized': False, - 'language': ir.DIToken('DW_LANG_C99'), - 'producer': 'llvmlite-test', - }) - - di_subprogram = mod.add_debug_info( - 'DISubprogram', - { - 'file': di_file, - 'line': 123, - 'name': 'my function', - 'scope': di_file, - 'scopeLine': 123, - 'unit': di_compile_unit, - }) - di_location = mod.add_debug_info( 'DILocation', { @@ -608,7 +610,7 @@ def do_test(): stats.print_stats() stats.print_callers() - self.assertEqual(750001, len(mod._metadatacache)) + self.assertEqual(503, len(mod._metadatacache)) def test_inline_assembly(self): mod = self.module() From 10320465d86acebf1a601a56210423e772384432 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 03:23:42 -0700 Subject: [PATCH 05/11] Tests are showing 15% faster at adding debug info --- llvmlite/tests/test_ir.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index e35a4c261..bafad3f65 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -585,30 +585,30 @@ def do_test(): ir.Constant(ir.IntType(bits=32), i + 1)) # Use this section to measure overall performance - # total_time = timeit.timeit( - # 'do_test()', - # 'setup_test()', - # number=500, - # globals=locals()) + total_time = timeit.timeit( + 'do_test()', + 'setup_test()', + number=500, + globals=locals()) - # print(f'test_debug_info_performance took {total_time} to finish') + print(f'test_debug_info_performance took {total_time} to finish') print(mod) # Use this section to profile the caching behavior - setup_test() - - import cProfile, pstats - from pstats import SortKey - with cProfile.Profile() as pr: - for i in range(500): - do_test() - - stats = pstats.Stats(pr) - stats = stats.strip_dirs() - stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) - stats.print_stats() - stats.print_callers() + # setup_test() + + # import cProfile, pstats + # from pstats import SortKey + # with cProfile.Profile() as pr: + # for i in range(500): + # do_test() + + # stats = pstats.Stats(pr) + # stats = stats.strip_dirs() + # stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) + # stats.print_stats() + # stats.print_callers() self.assertEqual(503, len(mod._metadatacache)) From 6e4590c4c0fed397078f3ceb5015e6cbc620680f Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 03:28:22 -0700 Subject: [PATCH 06/11] Fixed a bug --- llvmlite/ir/module.py | 4 ++-- llvmlite/tests/test_ir.py | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/llvmlite/ir/module.py b/llvmlite/ir/module.py index 470df0645..48a858cf4 100644 --- a/llvmlite/ir/module.py +++ b/llvmlite/ir/module.py @@ -53,14 +53,14 @@ def add_metadata(self, operands): raise TypeError("expected a list or tuple of metadata values, " "got %r" % (operands,)) - key = hash(tuple(operands)) + fixed_operands = self._fix_metadata_operands(operands) + key = hash(tuple(fixed_operands)) try: return self._metadatacache[key] except KeyError: pass n = len(self.metadata) - fixed_operands = self._fix_metadata_operands(operands) md = values.MDValue(self, fixed_operands, name=str(n)) self._metadatacache[key] = md return md diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index bafad3f65..3e94edd65 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -588,12 +588,10 @@ def do_test(): total_time = timeit.timeit( 'do_test()', 'setup_test()', - number=500, + number=1000, globals=locals()) - print(f'test_debug_info_performance took {total_time} to finish') - - print(mod) + # print(f'test_debug_info_performance took {total_time} to finish') # Use this section to profile the caching behavior # setup_test() @@ -601,7 +599,7 @@ def do_test(): # import cProfile, pstats # from pstats import SortKey # with cProfile.Profile() as pr: - # for i in range(500): + # for i in range(1000): # do_test() # stats = pstats.Stats(pr) From 02819796d2e1ccb33ac9f7760a7f13791dcb9925 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 03:48:49 -0700 Subject: [PATCH 07/11] Adding a cache on the hashes --- llvmlite/ir/values.py | 24 ++++++++++++++++++++++-- llvmlite/tests/test_ir.py | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/llvmlite/ir/values.py b/llvmlite/ir/values.py index 853927724..6a9d40d48 100644 --- a/llvmlite/ir/values.py +++ b/llvmlite/ir/values.py @@ -667,6 +667,7 @@ def __init__(self, parent, values, name): types.MetaDataType(), name=name) self.operands = tuple(values) + self.hash_cache = None parent.metadata.append(self) def descr(self, buf): @@ -694,8 +695,17 @@ def __eq__(self, other): def __ne__(self, other): return not self.__eq__(other) + def __getstate__(self): + # Ensure that the hash is not cached between Python invocations + # due to pickling or other serialization. The hash seed changes + # which will cause these not to match. + self.hash_cache = None + return self.__dict__ + def __hash__(self): - return hash(self.operands) + if self.hash_cache is None: + self.hash_cache = hash(self.operands) + return self.hash_cache class DIToken: @@ -725,6 +735,7 @@ def __init__(self, parent, is_distinct, kind, operands, name): self.is_distinct = is_distinct self.kind = kind self.operands = tuple(operands) + self.hash_cache = None parent.metadata.append(self) def descr(self, buf): @@ -767,8 +778,17 @@ def __eq__(self, other): def __ne__(self, other): return not self.__eq__(other) + def __getstate__(self): + # Ensure that the hash is not cached between Python invocations + # due to pickling or other serialization. The hash seed changes + # which will cause these not to match. + self.hash_cache = None + return self.__dict__ + def __hash__(self): - return hash((self.is_distinct, self.kind, self.operands)) + if self.hash_cache is None: + self.hash_cache = hash(self.operands) + return self.hash_cache class GlobalValue(NamedValue, _ConstOpMixin, _HasMetadata): diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 3e94edd65..81f01a7ce 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -610,6 +610,26 @@ def do_test(): self.assertEqual(503, len(mod._metadatacache)) + def test_debug_info_pickle(self): + mod = self.module() + + di_file = mod.add_debug_info( + 'DIFile', + { + 'directory': 'my_directory', + 'filename': 'my_path.foo', + }) + self.assertEqual(hash(di_file), di_file.hash_cache) + found_di_file = pickle.loads(pickle.dumps(di_file)) + self.assertIsNone(found_di_file.hash_cache) + self.assertEqual(di_file, found_di_file) + + meta = mod.add_metadata([di_file]) + self.assertEqual(hash(meta), meta.hash_cache) + found_meta = pickle.loads(pickle.dumps(meta)) + self.assertIsNone(found_meta.hash_cache) + self.assertEqual(meta, found_meta) + def test_inline_assembly(self): mod = self.module() foo = ir.Function(mod, ir.FunctionType(ir.VoidType(), []), 'foo') From bffb1f32301585b9a9b53ca22f12d4fa2e108951 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 04:00:49 -0700 Subject: [PATCH 08/11] Benchmark now shows it takes 50% less time --- llvmlite/tests/test_ir.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 81f01a7ce..c1ca921fc 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -569,13 +569,14 @@ def setup_test(): builder = ir.IRBuilder(foo.append_basic_block('')) def do_test(): - for i in range(500): + for i in range(100_000): di_location = mod.add_debug_info( 'DILocation', { 'scope': di_subprogram, 'line': i, - 'column': 15 + 'column': 15, + 'other': [di_subprogram, di_subprogram], }) builder.debug_metadata = di_location @@ -588,10 +589,10 @@ def do_test(): total_time = timeit.timeit( 'do_test()', 'setup_test()', - number=1000, + number=10, globals=locals()) - # print(f'test_debug_info_performance took {total_time} to finish') + print(f'test_debug_info_performance took {total_time} to finish') # Use this section to profile the caching behavior # setup_test() @@ -599,7 +600,7 @@ def do_test(): # import cProfile, pstats # from pstats import SortKey # with cProfile.Profile() as pr: - # for i in range(1000): + # for i in range(10): # do_test() # stats = pstats.Stats(pr) @@ -608,7 +609,7 @@ def do_test(): # stats.print_stats() # stats.print_callers() - self.assertEqual(503, len(mod._metadatacache)) + self.assertEqual(100004, len(mod._metadatacache)) def test_debug_info_pickle(self): mod = self.module() From c5ad88672e5f22bf476cfc313356d9de67caf032 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 04:01:57 -0700 Subject: [PATCH 09/11] Turning off print message --- llvmlite/tests/test_ir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index c1ca921fc..c78d602dd 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -592,7 +592,7 @@ def do_test(): number=10, globals=locals()) - print(f'test_debug_info_performance took {total_time} to finish') + # print('test_debug_info_performance took', total_time, 'to finish') # Use this section to profile the caching behavior # setup_test() From 4960a209e42fdf04ffb78abf71853cf2ee88b9ca Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 04:03:06 -0700 Subject: [PATCH 10/11] Remove profiler and always print the performance information --- llvmlite/tests/test_ir.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index c78d602dd..5a2a59823 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -585,29 +585,13 @@ def do_test(): ir.Constant(ir.IntType(bits=32), i), ir.Constant(ir.IntType(bits=32), i + 1)) - # Use this section to measure overall performance total_time = timeit.timeit( 'do_test()', 'setup_test()', number=10, globals=locals()) - # print('test_debug_info_performance took', total_time, 'to finish') - - # Use this section to profile the caching behavior - # setup_test() - - # import cProfile, pstats - # from pstats import SortKey - # with cProfile.Profile() as pr: - # for i in range(10): - # do_test() - - # stats = pstats.Stats(pr) - # stats = stats.strip_dirs() - # stats = stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME, SortKey.NAME) - # stats.print_stats() - # stats.print_callers() + print('test_debug_info_performance took', total_time, 'to finish') self.assertEqual(100004, len(mod._metadatacache)) From 7f3392de32ab4961db928fda7935bdda02f90fd4 Mon Sep 17 00:00:00 2001 From: Brett Slatkin Date: Mon, 31 Oct 2022 08:34:38 -0700 Subject: [PATCH 11/11] Fixes flake8 errors --- llvmlite/ir/builder.py | 4 ++-- llvmlite/tests/test_ir.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/llvmlite/ir/builder.py b/llvmlite/ir/builder.py index e648869da..a9c126823 100644 --- a/llvmlite/ir/builder.py +++ b/llvmlite/ir/builder.py @@ -119,8 +119,8 @@ def wrapped(self, operand, flag, name=''): raise TypeError( "expected an integer type, got %s" % operand.type) - if not(isinstance(flag.type, types.IntType) and - flag.type.width == 1): + if not (isinstance(flag.type, types.IntType) and + flag.type.width == 1): raise TypeError("expected an i1 type, got %s" % flag.type) fn = self.module.declare_intrinsic( opname, [operand.type, flag.type]) diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index 5a2a59823..6560c5ffe 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -599,11 +599,11 @@ def test_debug_info_pickle(self): mod = self.module() di_file = mod.add_debug_info( - 'DIFile', - { - 'directory': 'my_directory', - 'filename': 'my_path.foo', - }) + 'DIFile', + { + 'directory': 'my_directory', + 'filename': 'my_path.foo', + }) self.assertEqual(hash(di_file), di_file.hash_cache) found_di_file = pickle.loads(pickle.dumps(di_file)) self.assertIsNone(found_di_file.hash_cache)