def test_test1(self): typingctx = typing.Context() targetctx = cpu.CPUContext(typingctx) test_ir = compiler.run_frontend(test1) with cpu_target.nested_context(typingctx, targetctx): one_arg = numba.types.npytypes.Array( numba.types.scalars.Float(name="float64"), 1, 'C') args = (one_arg, one_arg, one_arg, one_arg, one_arg) tp = TestPipeline(typingctx, targetctx, args, test_ir) numba.rewrites.rewrite_registry.apply('before-inference', tp, tp.func_ir) tp.typemap, tp.return_type, tp.calltypes = compiler.type_inference_stage( tp.typingctx, tp.func_ir, tp.args, None) type_annotation = type_annotations.TypeAnnotation( func_ir=tp.func_ir, typemap=tp.typemap, calltypes=tp.calltypes, lifted=(), lifted_from=None, args=tp.args, return_type=tp.return_type, html_output=config.HTML) numba.rewrites.rewrite_registry.apply('after-inference', tp, tp.func_ir) parfor_pass = numba.parfor.ParforPass(tp.func_ir, tp.typemap, tp.calltypes, tp.return_type, tp.typingctx) parfor_pass.run() self.assertTrue(countParfors(test_ir) == 1)
def test1(self): typingctx = typing.Context() targetctx = cpu.CPUContext(typingctx) test_ir = compiler.run_frontend(test_will_propagate) #print("Num blocks = ", len(test_ir.blocks)) #print(test_ir.dump()) with cpu_target.nested_context(typingctx, targetctx): typingctx.refresh() targetctx.refresh() args = (types.int64, types.int64, types.int64) typemap, return_type, calltypes = compiler.type_inference_stage(typingctx, test_ir, args, None) #print("typemap = ", typemap) #print("return_type = ", return_type) type_annotation = type_annotations.TypeAnnotation( func_ir=test_ir, typemap=typemap, calltypes=calltypes, lifted=(), lifted_from=None, args=args, return_type=return_type, html_output=config.HTML) remove_dels(test_ir.blocks) in_cps, out_cps = copy_propagate(test_ir.blocks, typemap) apply_copy_propagate(test_ir.blocks, in_cps, get_name_var_table(test_ir.blocks), typemap, calltypes) remove_dead(test_ir.blocks, test_ir.arg_names, test_ir) self.assertFalse(findLhsAssign(test_ir, "x"))
def compile_isolated(func, args, return_type=None, flags=DEFAULT_FLAGS, locals={}): """ Compile the function is an isolated environment. Good for testing. """ typingctx = typing.Context() targetctx = cpu.CPUContext(typingctx) return compile_extra(typingctx, targetctx, func, args, return_type, flags, locals)
def mk_pipeline(cls, args, return_type=None, flags=None, locals={}, library=None, typing_context=None, target_context=None): if not flags: flags = Flags() flags.nrt = True if typing_context is None: typing_context = typing.Context() if target_context is None: target_context = cpu.CPUContext(typing_context) return cls(typing_context, target_context, library, args, return_type, flags, locals)
def compile_isolated(func, args, return_type=None, flags=DEFAULT_FLAGS, locals={}): """ Compile the function in an isolated environment (typing and target context). Good for testing. """ from .targets.registry import cpu_target typingctx = typing.Context() targetctx = cpu.CPUContext(typingctx) # Register the contexts in case for nested @jit or @overload calls with cpu_target.nested_context(typingctx, targetctx): return compile_extra(typingctx, targetctx, func, args, return_type, flags, locals)
def countParfors(test_func, args, **kws): typingctx = typing.Context() targetctx = cpu.CPUContext(typingctx) test_ir = compiler.run_frontend(test_func) if kws: options = cpu.ParallelOptions(kws) else: options = cpu.ParallelOptions(True) with cpu_target.nested_context(typingctx, targetctx): tp = TestPipeline(typingctx, targetctx, args, test_ir) inline_pass = inline_closurecall.InlineClosureCallPass( tp.func_ir, options) inline_pass.run() numba.rewrites.rewrite_registry.apply('before-inference', tp, tp.func_ir) tp.typemap, tp.return_type, tp.calltypes = compiler.type_inference_stage( tp.typingctx, tp.func_ir, tp.args, None) type_annotations.TypeAnnotation(func_ir=tp.func_ir, typemap=tp.typemap, calltypes=tp.calltypes, lifted=(), lifted_from=None, args=tp.args, return_type=tp.return_type, html_output=config.HTML) preparfor_pass = numba.parfor.PreParforPass(tp.func_ir, tp.typemap, tp.calltypes, tp.typingctx, options) preparfor_pass.run() numba.rewrites.rewrite_registry.apply('after-inference', tp, tp.func_ir) parfor_pass = numba.parfor.ParforPass(tp.func_ir, tp.typemap, tp.calltypes, tp.return_type, tp.typingctx, options) parfor_pass.run() ret_count = 0 for label, block in test_ir.blocks.items(): for i, inst in enumerate(block.body): if isinstance(inst, numba.parfor.Parfor): ret_count += 1 return ret_count
def _context_builder_sig_args(self): typing_context = typing.Context() context = cpu.CPUContext(typing_context) module = lc.Module("test_module") sig = typing.signature(types.int32, types.int32) llvm_fnty = context.call_conv.get_function_type( sig.return_type, sig.args) function = module.get_or_insert_function(llvm_fnty, name='test_fn') args = context.call_conv.get_arguments(function) assert function.is_declaration entry_block = function.append_basic_block('entry') builder = lc.Builder(entry_block) return context, builder, sig, args
def test_cache(self): def times2(i): return 2 * i def times3(i): return i * 3 i32 = lc.Type.int(32) llvm_fnty = lc.Type.function(i32, [i32]) module = lc.Module.new("test_module") function = module.get_or_insert_function(llvm_fnty, name='test_fn') assert function.is_declaration entry_block = function.append_basic_block('entry') builder = lc.Builder.new(entry_block) sig = typing.signature(types.int32, types.int32) typing_context = typing.Context() context = cpu.CPUContext(typing_context) # Ensure the cache is empty to begin with self.assertEqual(0, len(context.cached_internal_func)) # After one compile, it should contain one entry context.compile_internal(builder, times2, sig, function.args) self.assertEqual(1, len(context.cached_internal_func)) # After a second compilation of the same thing, it should still contain # one entry context.compile_internal(builder, times2, sig, function.args) self.assertEqual(1, len(context.cached_internal_func)) # After compilation of another function, the cache should have grown by # one more. context.compile_internal(builder, times3, sig, function.args) self.assertEqual(2, len(context.cached_internal_func)) sig2 = typing.signature(types.float64, types.float64) f64 = lc.Type.double() llvm_fnty2 = lc.Type.function(f64, [f64]) function2 = module.get_or_insert_function(llvm_fnty2, name='test_fn_2') assert function2.is_declaration entry_block2 = function2.append_basic_block('entry') builder2 = lc.Builder.new(entry_block2) # Ensure that the same function with a different signature does not # reuse an entry from the cache in error context.compile_internal(builder2, times3, sig2, function2.args) self.assertEqual(3, len(context.cached_internal_func))
def __init__(self): self.typingctx = typing.Context() self.targetctx = cpu.CPUContext(self.typingctx) self.cr_cache = {}
def setUp(self): typing_context = typing.Context() self.context = cpu.CPUContext(typing_context)
class CPUTarget(TargetDescriptor): options = cpu.CPUTargetOptions typing_context = typing.Context() target_context = cpu.CPUContext(typing_context)
def setUp(self): super(BaseTestWithLifting, self).setUp() self.typingctx = typing.Context() self.targetctx = cpu.CPUContext(self.typingctx) self.flags = DEFAULT_FLAGS
def test_cache(self): def times2(i): return 2 * i def times3(i): return i * 3 def make_closure(x, y): def f(z): return y + z return f typing_context = typing.Context() context = cpu.CPUContext(typing_context) module = lc.Module.new("test_module") sig = typing.signature(types.int32, types.int32) llvm_fnty = context.call_conv.get_function_type( sig.return_type, sig.args) function = module.get_or_insert_function(llvm_fnty, name='test_fn') args = context.call_conv.get_arguments(function) assert function.is_declaration entry_block = function.append_basic_block('entry') builder = lc.Builder.new(entry_block) # Ensure the cache is empty to begin with self.assertEqual(0, len(context.cached_internal_func)) # After one compile, it should contain one entry context.compile_internal(builder, times2, sig, args) self.assertEqual(1, len(context.cached_internal_func)) # After a second compilation of the same thing, it should still contain # one entry context.compile_internal(builder, times2, sig, args) self.assertEqual(1, len(context.cached_internal_func)) # After compilation of another function, the cache should have grown by # one more. context.compile_internal(builder, times3, sig, args) self.assertEqual(2, len(context.cached_internal_func)) sig2 = typing.signature(types.float64, types.float64) llvm_fnty2 = context.call_conv.get_function_type( sig2.return_type, sig2.args) function2 = module.get_or_insert_function(llvm_fnty2, name='test_fn_2') args2 = context.call_conv.get_arguments(function2) assert function2.is_declaration entry_block2 = function2.append_basic_block('entry') builder2 = lc.Builder.new(entry_block2) # Ensure that the same function with a different signature does not # reuse an entry from the cache in error context.compile_internal(builder2, times3, sig2, args2) self.assertEqual(3, len(context.cached_internal_func)) # Closures with distinct cell contents must each be compiled. clo11 = make_closure(1, 1) clo12 = make_closure(1, 2) clo22 = make_closure(2, 2) res1 = context.compile_internal(builder, clo11, sig, args) self.assertEqual(4, len(context.cached_internal_func)) res2 = context.compile_internal(builder, clo12, sig, args) self.assertEqual(5, len(context.cached_internal_func)) # Same cell contents as above (first parameter isn't captured) res3 = context.compile_internal(builder, clo22, sig, args) self.assertEqual(5, len(context.cached_internal_func))