Example #1
0
 def unroll_and_optimize(self,
                         loop,
                         call_pure_results=None,
                         jump_values=None):
     self.add_guard_future_condition(loop)
     jump_op = loop.operations[-1]
     assert jump_op.getopnum() == rop.JUMP
     celltoken = JitCellToken()
     runtime_boxes = self.pack_into_boxes(jump_op, jump_values)
     jump_op.setdescr(celltoken)
     call_pure_results = self._convert_call_pure_results(call_pure_results)
     t = convert_loop_to_trace(loop, self.metainterp_sd)
     preamble_data = compile.PreambleCompileData(
         t, runtime_boxes, call_pure_results, enable_opts=self.enable_opts)
     start_state, preamble_ops = preamble_data.optimize_trace(
         self.metainterp_sd, None, {})
     preamble_data.forget_optimization_info()
     loop_data = compile.UnrolledLoopData(preamble_data.trace,
                                          celltoken,
                                          start_state,
                                          call_pure_results,
                                          enable_opts=self.enable_opts)
     loop_info, ops = loop_data.optimize_trace(self.metainterp_sd, None, {})
     preamble = TreeLoop('preamble')
     preamble.inputargs = start_state.renamed_inputargs
     start_label = ResOperation(rop.LABEL, start_state.renamed_inputargs)
     preamble.operations = ([start_label] + preamble_ops +
                            loop_info.extra_same_as + [loop_info.label_op])
     loop.inputargs = loop_info.label_op.getarglist()[:]
     loop.operations = [loop_info.label_op] + ops
     return Info(preamble, loop_info.target_token.short_preamble,
                 start_state.virtual_state)
Example #2
0
 def unroll_and_optimize(self, loop, call_pure_results=None):
     self.add_guard_future_condition(loop)
     jump_op = loop.operations[-1]
     assert jump_op.getopnum() == rop.JUMP
     ops = loop.operations[:-1]
     jump_op.setdescr(JitCellToken())
     start_label = ResOperation(rop.LABEL, loop.inputargs,
                                jump_op.getdescr())
     end_label = jump_op.copy_and_change(opnum=rop.LABEL)
     call_pure_results = self._convert_call_pure_results(call_pure_results)
     preamble_data = compile.LoopCompileData(start_label, end_label, ops,
                                             call_pure_results)
     start_state, preamble_ops = self._do_optimize_loop(preamble_data)
     preamble_data.forget_optimization_info()
     loop_data = compile.UnrolledLoopData(start_label, jump_op,
                                          ops, start_state,
                                          call_pure_results)
     loop_info, ops = self._do_optimize_loop(loop_data)
     preamble = TreeLoop('preamble')
     preamble.inputargs = start_state.renamed_inputargs
     start_label = ResOperation(rop.LABEL, start_state.renamed_inputargs)
     preamble.operations = ([start_label] + preamble_ops +
                            loop_info.extra_same_as + [loop_info.label_op])
     loop.inputargs = loop_info.label_op.getarglist()[:]
     loop.operations = [loop_info.label_op] + ops
     return Info(preamble, loop_info.target_token.short_preamble,
                 start_state.virtual_state)
Example #3
0
 def unroll_and_optimize(self, loop, call_pure_results=None,
                         jump_values=None):
     self.add_guard_future_condition(loop)
     jump_op = loop.operations[-1]
     assert jump_op.getopnum() == rop.JUMP
     celltoken = JitCellToken()
     runtime_boxes = self.pack_into_boxes(jump_op, jump_values)
     jump_op.setdescr(celltoken)
     #start_label = ResOperation(rop.LABEL, loop.inputargs,
     #                           descr=jump_op.getdescr())
     #end_label = jump_op.copy_and_change(opnum=rop.LABEL)
     call_pure_results = self._convert_call_pure_results(call_pure_results)
     t = convert_loop_to_trace(loop, FakeMetaInterpStaticData(self.cpu))
     preamble_data = compile.LoopCompileData(t, runtime_boxes,
                                             call_pure_results)
     start_state, preamble_ops = self._do_optimize_loop(preamble_data)
     preamble_data.forget_optimization_info()
     loop_data = compile.UnrolledLoopData(preamble_data.trace,
         celltoken, start_state, call_pure_results)
     loop_info, ops = self._do_optimize_loop(loop_data)
     preamble = TreeLoop('preamble')
     preamble.inputargs = start_state.renamed_inputargs
     start_label = ResOperation(rop.LABEL, start_state.renamed_inputargs)
     preamble.operations = ([start_label] + preamble_ops +
                            loop_info.extra_same_as + [loop_info.label_op])
     loop.inputargs = loop_info.label_op.getarglist()[:]
     loop.operations = [loop_info.label_op] + ops
     return Info(preamble, loop_info.target_token.short_preamble,
                 start_state.virtual_state)
Example #4
0
 def xxx_test_random_snapshot(self, lst):
     inputargs, ops = lst
     t = Trace(inputargs, metainterp_sd)
     for op in ops:
         newop = FakeOp(t.record_op(op.getopnum(), op.getarglist()))
         newop.orig_op = op
         if newop.is_guard():
             resume.capture_resumedata(op.framestack, None, [], t)
         op.position = newop.get_position()
     inpargs, l, iter = self.unpack(t)
     loop1 = TreeLoop("loop1")
     loop1.inputargs = inputargs
     loop1.operations = ops
     loop2 = TreeLoop("loop2")
     loop2.inputargs = inpargs
     loop2.operations = l
     BaseTest.assert_equal(loop1, loop2)
Example #5
0
 def xxx_test_random_snapshot(self, lst):
     inputargs, ops = lst
     t = Trace(inputargs, metainterp_sd)
     for op in ops:
         newop = FakeOp(t.record_op(op.getopnum(), op.getarglist()))
         newop.orig_op = op
         if newop.is_guard():
             resume.capture_resumedata(op.framestack,
                 None, [], t)
         op.position = newop.get_position()
     inpargs, l, iter = self.unpack(t)
     loop1 = TreeLoop("loop1")
     loop1.inputargs = inputargs
     loop1.operations = ops
     loop2 = TreeLoop("loop2")
     loop2.inputargs = inpargs
     loop2.operations = l
     BaseTest.assert_equal(loop1, loop2)
Example #6
0
def convert_old_style_to_targets(loop, jump):
    newloop = TreeLoop(loop.name)
    newloop.inputargs = loop.inputargs
    newloop.operations = [ResOperation(rop.LABEL, loop.inputargs, None, descr=FakeDescr())] + \
                      loop.operations
    if not jump:
        assert newloop.operations[-1].getopnum() == rop.JUMP
        newloop.operations[-1] = ResOperation(rop.LABEL, newloop.operations[-1].getarglist(), None, descr=FakeDescr())
    return newloop
Example #7
0
def convert_old_style_to_targets(loop, jump):
    newloop = TreeLoop(loop.name)
    newloop.inputargs = loop.inputargs
    newloop.operations = [ResOperation(rop.LABEL, loop.inputargs, None, descr=FakeDescr())] + \
                      loop.operations
    if not jump:
        assert newloop.operations[-1].getopnum() == rop.JUMP
        newloop.operations[-1] = ResOperation(rop.LABEL, newloop.operations[-1].getarglist(), None, descr=FakeDescr())
    return newloop
Example #8
0
    def unroll_and_optimize(self, loop, call_pure_results=None):
        self.add_guard_future_condition(loop)
        operations = loop.operations
        jumpop = operations[-1]
        assert jumpop.getopnum() == rop.JUMP
        inputargs = loop.inputargs

        jump_args = jumpop.getarglist()[:]
        operations = operations[:-1]
        cloned_operations = [op.clone() for op in operations]

        preamble = TreeLoop('preamble')
        preamble.inputargs = inputargs

        token = JitCellToken()
        preamble.operations = [ResOperation(rop.LABEL, inputargs, None, descr=TargetToken(token))] + \
                              operations +  \
                              [ResOperation(rop.LABEL, jump_args, None, descr=token)]
        start_state = self._do_optimize_loop(preamble,
                                             call_pure_results,
                                             export_state=True)

        assert preamble.operations[-1].getopnum() == rop.LABEL

        inliner = Inliner(inputargs, jump_args)
        loop.operations = [preamble.operations[-1]] + \
                          [inliner.inline_op(op, clone=False) for op in cloned_operations] + \
                          [ResOperation(rop.JUMP, [inliner.inline_arg(a) for a in jump_args],
                                        None, descr=token)]
        #[inliner.inline_op(jumpop)]
        assert loop.operations[-1].getopnum() == rop.JUMP
        assert loop.operations[0].getopnum() == rop.LABEL
        loop.inputargs = loop.operations[0].getarglist()

        self._do_optimize_loop(loop,
                               call_pure_results,
                               start_state,
                               export_state=False)
        extra_same_as = []
        while loop.operations[0].getopnum() != rop.LABEL:
            extra_same_as.append(loop.operations[0])
            del loop.operations[0]

        # Hack to prevent random order of same_as ops
        extra_same_as.sort(
            key=lambda op: str(preamble.operations).find(str(op.getarg(0))))

        for op in extra_same_as:
            preamble.operations.insert(-1, op)

        return preamble
Example #9
0
 def optimize(self, ops):
     loop = self.parse(ops)
     self.add_guard_future_condition(loop)
     operations = loop.operations
     jumpop = operations[-1]
     assert jumpop.getopnum() == rop.JUMP
     inputargs = loop.inputargs
     preamble = TreeLoop('preamble')
     trace = oparser.convert_loop_to_trace(loop, self.metainterp_sd)
     compile_data = PreambleCompileData(
         trace, inputargs, enable_opts=self.enable_opts)
     start_state, newops = compile_data.optimize_trace(
         self.metainterp_sd, None, {})
     preamble.operations = newops
     preamble.inputargs = start_state.renamed_inputargs
     return start_state, loop, preamble
Example #10
0
    def unroll_and_optimize(self, loop, call_pure_results=None):
        self.add_guard_future_condition(loop)
        operations =  loop.operations
        jumpop = operations[-1]
        assert jumpop.getopnum() == rop.JUMP
        inputargs = loop.inputargs

        jump_args = jumpop.getarglist()[:]
        operations = operations[:-1]
        cloned_operations = [op.clone() for op in operations]

        preamble = TreeLoop('preamble')
        preamble.inputargs = inputargs

        token = JitCellToken()
        preamble.operations = [ResOperation(rop.LABEL, inputargs, None, descr=TargetToken(token))] + \
                              operations +  \
                              [ResOperation(rop.LABEL, jump_args, None, descr=token)]
        start_state = self._do_optimize_loop(preamble, call_pure_results,
                                             export_state=True)

        assert preamble.operations[-1].getopnum() == rop.LABEL

        inliner = Inliner(inputargs, jump_args)
        loop.operations = [preamble.operations[-1]] + \
                          [inliner.inline_op(op, clone=False) for op in cloned_operations] + \
                          [ResOperation(rop.JUMP, [inliner.inline_arg(a) for a in jump_args],
                                        None, descr=token)]
                          #[inliner.inline_op(jumpop)]
        assert loop.operations[-1].getopnum() == rop.JUMP
        assert loop.operations[0].getopnum() == rop.LABEL
        loop.inputargs = loop.operations[0].getarglist()

        self._do_optimize_loop(loop, call_pure_results, start_state,
                               export_state=False)
        extra_same_as = []
        while loop.operations[0].getopnum() != rop.LABEL:
            extra_same_as.append(loop.operations[0])
            del loop.operations[0]

        # Hack to prevent random order of same_as ops
        extra_same_as.sort(key=lambda op: str(preamble.operations).find(str(op.getarg(0))))

        for op in extra_same_as:
            preamble.operations.insert(-1, op)

        return preamble
Example #11
0
    def build_random_loop(self, cpu, builder_factory, r, startvars,
                          allow_delay):

        loop = TreeLoop('test_random_function')
        loop.inputargs = startvars[:]
        loop.operations = []
        loop._jitcelltoken = JitCellToken()
        builder = builder_factory(cpu, loop, startvars[:])
        if allow_delay:
            needs_a_label = True
        else:
            self.insert_label(loop, 0, r)
            needs_a_label = False
        self.generate_ops(builder, r, loop, startvars, needs_a_label=needs_a_label)
        self.builder = builder
        self.loop = loop
        dump(loop)
        cpu.compile_loop(loop.inputargs, loop.operations, loop._jitcelltoken)
        if self.output:
            builder.print_loop(self.output)
Example #12
0
    def optimize(self, ops):
        loop = self.parse(ops)
        self.add_guard_future_condition(loop)
        operations =  loop.operations
        jumpop = operations[-1]
        assert jumpop.getopnum() == rop.JUMP
        inputargs = loop.inputargs

        jump_args = jumpop.getarglist()[:]
        operations = operations[:-1]

        preamble = TreeLoop('preamble')

        token = JitCellToken()
        trace = oparser.convert_loop_to_trace(loop, FakeMetaInterpStaticData(self.cpu))
        compile_data = LoopCompileData(trace, inputargs)
        start_state, newops = self._do_optimize_loop(compile_data)
        preamble.operations = newops
        preamble.inputargs = start_state.renamed_inputargs
        return start_state, loop, preamble
    def optimize(self, ops):
        loop = self.parse(ops)
        self.add_guard_future_condition(loop)
        operations =  loop.operations
        jumpop = operations[-1]
        assert jumpop.getopnum() == rop.JUMP
        inputargs = loop.inputargs

        jump_args = jumpop.getarglist()[:]
        operations = operations[:-1]

        preamble = TreeLoop('preamble')

        token = JitCellToken()
        trace = oparser.convert_loop_to_trace(loop, FakeMetaInterpStaticData(self.cpu))
        compile_data = LoopCompileData(trace, inputargs)
        start_state, newops = self._do_optimize_loop(compile_data)
        preamble.operations = newops
        preamble.inputargs = start_state.renamed_inputargs
        return start_state, loop, preamble
Example #14
0
    def optimize(self, ops):
        loop = self.parse(ops, postprocess=self.postprocess)
        self.add_guard_future_condition(loop)
        operations =  loop.operations
        jumpop = operations[-1]
        assert jumpop.getopnum() == rop.JUMP
        inputargs = loop.inputargs

        jump_args = jumpop.getarglist()[:]
        operations = operations[:-1]

        preamble = TreeLoop('preamble')

        token = JitCellToken()
        start_label = ResOperation(rop.LABEL, inputargs, descr=TargetToken(token))
        stop_label = ResOperation(rop.LABEL, jump_args, descr=token)
        compile_data = LoopCompileData(start_label, stop_label, operations)
        start_state, newops = self._do_optimize_loop(compile_data)
        preamble.operations = newops
        preamble.inputargs = start_state.renamed_inputargs
        return start_state, loop, preamble
Example #15
0
    def build_random_loop(self, cpu, builder_factory, r, startvars,
                          allow_delay):

        loop = TreeLoop('test_random_function')
        loop.inputargs = startvars[:]
        loop.operations = []
        loop._jitcelltoken = JitCellToken()
        builder = builder_factory(cpu, loop, startvars[:])
        if allow_delay:
            needs_a_label = True
        else:
            self.insert_label(loop, 0, r)
            needs_a_label = False
        self.generate_ops(builder,
                          r,
                          loop,
                          startvars,
                          needs_a_label=needs_a_label)
        self.builder = builder
        self.loop = loop
        dump(loop)
        cpu.compile_loop(loop.inputargs, loop.operations, loop._jitcelltoken)
        if self.output:
            builder.print_loop(self.output)
Example #16
0
    def optimize_loop(self, ops, expected, expected_shorts=None):
        loop = self.parse(ops)
        if expected != "crash!":
            expected = self.parse(expected)

        part = TreeLoop('part')
        part.inputargs = loop.inputargs
        token = loop.original_jitcell_token

        optimized = TreeLoop('optimized')
        optimized.inputargs = loop.inputargs
        optimized.operations = []

        labels = [i for i, op in enumerate(loop.operations) \
                  if op.getopnum()==rop.LABEL]
        prv = 0
        last_label = []
        state = None
        for nxt in labels + [len(loop.operations)]:
            assert prv != nxt
            operations = last_label + loop.operations[prv:nxt]
            if nxt < len(loop.operations):
                label = loop.operations[nxt]
                assert label.getopnum() == rop.LABEL
                if label.getdescr() is None:
                    label.setdescr(token)
                operations.append(label)
            part.operations = operations

            self.add_guard_future_condition(part)
            state = self._do_optimize_loop(part,
                                           None,
                                           state,
                                           export_state=True)
            if part.operations[-1].getopnum() == rop.LABEL:
                last_label = [part.operations.pop()]
            else:
                last_label = []

            optimized.operations.extend(part.operations)
            prv = nxt + 1

        #
        print
        print "Optimized:"
        if optimized.operations:
            print '\n'.join([str(o) for o in optimized.operations])
        else:
            print 'Failed!'
        print

        shorts = [
            op.getdescr().short_preamble for op in optimized.operations
            if op.getopnum() == rop.LABEL
        ]

        if expected_shorts:
            for short in shorts:
                print
                print "Short preamble:"
                print '\n'.join([str(o) for o in short])

        assert expected != "crash!", "should have raised an exception"
        self.assert_equal(optimized, expected)

        if expected_shorts:
            assert len(shorts) == len(expected_shorts)
            for short, expected_short in zip(shorts, expected_shorts):
                expected_short = self.parse(expected_short)
                short_preamble = TreeLoop('short preamble')
                assert short[0].getopnum() == rop.LABEL
                short_preamble.inputargs = short[0].getarglist()
                short_preamble.operations = short
                self.assert_equal(short_preamble,
                                  expected_short,
                                  text_right='expected short preamble')

        return optimized
Example #17
0
    def optimize_loop(self, ops, expected, expected_shorts=None):
        loop = self.parse(ops)
        if expected != "crash!":
            expected = self.parse(expected)

        part = TreeLoop('part')
        part.inputargs = loop.inputargs
        token = loop.original_jitcell_token

        optimized = TreeLoop('optimized')
        optimized.inputargs = loop.inputargs
        optimized.operations = []

        labels = [i for i, op in enumerate(loop.operations) \
                  if op.getopnum()==rop.LABEL]
        prv = 0
        last_label = []
        state = None
        for nxt in labels + [len(loop.operations)]:
            assert prv != nxt
            operations = last_label + loop.operations[prv:nxt]
            if nxt < len(loop.operations):
                label = loop.operations[nxt]
                assert label.getopnum() == rop.LABEL
                if label.getdescr() is None:
                    label.setdescr(token)
                operations.append(label)
            part.operations = operations

            self.add_guard_future_condition(part)
            state = self._do_optimize_loop(part, None, state,
                                           export_state=True)
            if part.operations[-1].getopnum() == rop.LABEL:
                last_label = [part.operations.pop()]
            else:
                last_label = []

            optimized.operations.extend(part.operations)
            prv = nxt + 1

        #
        print
        print "Optimized:"
        if optimized.operations:
            print '\n'.join([str(o) for o in optimized.operations])
        else:
            print 'Failed!'
        print

        shorts = [op.getdescr().short_preamble
                  for op in optimized.operations
                  if op.getopnum() == rop.LABEL]

        if expected_shorts:
            for short in shorts:
                print
                print "Short preamble:"
                print '\n'.join([str(o) for o in short])


        assert expected != "crash!", "should have raised an exception"
        self.assert_equal(optimized, expected)

        if expected_shorts:
            assert len(shorts) == len(expected_shorts)
            for short, expected_short in zip(shorts, expected_shorts):
                expected_short = self.parse(expected_short)
                short_preamble = TreeLoop('short preamble')
                assert short[0].getopnum() == rop.LABEL
                short_preamble.inputargs = short[0].getarglist()
                short_preamble.operations = short
                self.assert_equal(short_preamble, expected_short,
                                  text_right='expected short preamble')


        return optimized