def compile_code(src_code, args, block_size, nr_reg = 8, no_sequencer=True): ''' Compile a kernel and return a codegen function. ''' comp = CompilerDriver(nr_reg, no_sequencer=True) main_object = comp.run(src_code) patched_object = Compiler.patch_arguments_before_run(main_object, [args[argname] for argname in main_object.arguments]) def codegen_func(code, block_size, args): for x in patched_object.code: yield InstrAdapter(x, use_reg_wrapper=True) code = Code() code.set_generator(codegen_func, block_size, args) return code
def test_2D_const_list(): ''' Test code generation with 2D constants list. Note that this looks easy to optimise, but it is not so simple, because q is only know right before execution. ''' from blip.code.BlipCompiler import CompilerDriver, Compiler src = ''' @kernel def main(q): a = [[1, 2, 3], [4, 5, 6]] acc = 0 for i in range(2): for j in range(3): acc += q*a[i][j] return acc ''' comp = CompilerDriver(16, no_sequencer=True) main_object = comp.run(src) patched_object = Compiler.patch_arguments_before_run(main_object, [41]) code = [InstrAdapter(x) for x in patched_object.code] pattern = ''' imm r0,41 imm r1,0 imm r2,0 imm r3,0 imm r4,1 mul r5,r0,r4 add r4,r1,r5 imm r1,1 imm r5,2 mul r6,r0,r5 add r5,r4,r6 imm r4,2 imm r6,3 mul r7,r0,r6 add r6,r5,r7 imm r5,1 imm r7,0 imm r8,4 mul r9,r0,r8 add r8,r6,r9 imm r6,1 imm r9,5 mul r10,r0,r9 add r9,r8,r10 imm r8,2 imm r10,6 mul r11,r0,r10 add r0,r9,r11 mov r9,r0 '''.strip() assert match_code(code, pattern)
def test_patch_arguments(): from blip.code.BlipCompiler import CompilerDriver, Compiler src = ''' @kernel def main(q): return q + 1 ''' comp = CompilerDriver(8) main_object = comp.run(src) patched_object = Compiler.patch_arguments_before_run(main_object, [41]) code = [InstrAdapter(x) for x in patched_object.code] pattern = ''' imm r0,41 imm r1,1 add r2,r0,r1 mov _, r2 '''.strip() assert match_code(code, pattern)
def test_patch_reg_arguments(): ''' Patching of arguments before run, now with registers as arguments. ''' from blip.code.BlipCompiler import Compiler, NamedValue src = ''' @kernel def main(p, q): b = p - 2 return q + b ''' compiler = Compiler() main_object = compiler.compile(src)[0] patched_object = Compiler.patch_arguments_before_run(main_object, [NamedValue('test_value@0'), 41]) code = [InstrAdapter(x) for x in patched_object.code] pattern = ''' mov p@0,test_value@0 imm q@0,41 imm tmp_0@0,2 sub b@0,p@0,tmp_0@0 add tmp_1@0,q@0,b@0 mov main___return@0,tmp_1@0 '''.strip() assert match_code(code, pattern)