当前位置: 首页>>代码示例>>Python>>正文


Python Builder.position_before方法代码示例

本文整理汇总了Python中pykit.ir.Builder.position_before方法的典型用法代码示例。如果您正苦于以下问题:Python Builder.position_before方法的具体用法?Python Builder.position_before怎么用?Python Builder.position_before使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pykit.ir.Builder的用法示例。


在下文中一共展示了Builder.position_before方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: insert_allocations

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def insert_allocations(func, env):
    b = Builder(func)

    # IR positions and list of ops
    positions = dict((op, idx) for idx, op in enumerate(func.ops))
    oplist = list(func.ops)

    for op in func.ops:
        if op.opcode == 'ckernel':
            ckernel, args = op.args
            alloc   = Op('alloc', op.type, args=[])

            # TODO: Insert alloc in args list of ckernel

            # Replace uses of ckernel with temporary allocation
            op.replace_uses(alloc)
            op.set_args([ckernel, [alloc] + args])

            # Emit allocation before first use
            b.position_before(op)
            b.emit(alloc)

            # Emit deallocation after last use, unless we are returning
            # the result
            idx = max(positions[u] for u in func.uses[alloc])
            last_op = oplist[idx]
            if not last_op.opcode == 'ret':
                b.position_after(last_op)
                dealloc = Op('dealloc', types.Void, [alloc])
                b.emit(dealloc)

    return func, env
开发者ID:zeeshanali,项目名称:blaze,代码行数:34,代码来源:allocation.py

示例2: rewrite_obj_return

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def rewrite_obj_return(func, env):
    """
    Handle returning stack-allocated objects.
    """
    if should_skip(env):
        return

    context = env['flypy.typing.context']
    restype = env['flypy.typing.restype']
    envs =  env['flypy.state.envs']

    builder = Builder(func)

    stack_alloc = representation.byref(restype)

    if stack_alloc:
        out = func.add_arg(func.temp("out"), opaque_t)
        context[out] = Pointer[restype]
        func.type = types.Function(types.Void, func.type.argtypes, False)

    for arg in func.args:
        arg.type = opaque_t
    func.type = types.Function(func.type.restype, (opaque_t,) * len(func.args),
                               False)

    is_generator = env['flypy.state.generator']
    for op in func.ops:
        if (op.opcode == 'ret' and op.args[0] is not None and
                stack_alloc and not is_generator):
            # ret val =>
            #     store (load val) out ; ret void
            [val] = op.args
            builder.position_before(op)
            newval = builder.load(val)
            builder.store(newval, out)
            op.set_args([None])

            # Update context
            context[newval] = StackVar[context[val]]

        elif op.opcode == 'call' and op.type != types.Void:
            # result = call(f, ...) =>
            #     alloca result ; call(f, ..., &result)
            ty = context[op]
            if conversion.byref(ty):
                f, args = op.args
                if not is_flypy_cc(f) or should_skip(envs[f]):
                    continue

                builder.position_before(op)
                retval = builder.alloca(opaque_t)
                builder.position_after(op)
                op.replace_uses(retval)

                newargs = args + [retval]
                op.set_args([f, newargs])

                # Update context
                context[retval] = context[op]
                context[op] = void
开发者ID:filmackay,项目名称:flypy,代码行数:62,代码来源:objects.py

示例3: lower_fields

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def lower_fields(func, env):
    b = Builder(func)
    opbuilder = OpBuilder()

    for op in func.ops:
        if op.opcode not in ("getfield", "setfield"):
            continue

        if op.args[0].type.is_pointer:
            b.position_before(op)

            # Load the pointer and update the argument
            p = op.args[0]
            load = b.load(p)
            args = [load] + op.args[1:]
            op.set_args(args)

            if op.opcode == "setfield":
                # Write back result
                b.position_after(op)
                op.type = load.type
                b.store(op, p)

        if op.opcode == "getfield":
            struct, attr = op.args
            newop = opbuilder.extractfield(op.type, struct, attr,
                                           result=op.result)
        else:
            struct, attr, value = op.args
            newop = opbuilder.insertfield(op.type, struct, attr, value,
                                          result=op.result)

        op.replace(newop)
开发者ID:flypy,项目名称:pykit,代码行数:35,代码来源:lower_fields.py

示例4: lower_fields

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def lower_fields(func, env=None):
    b = Builder(func)

    for op in func.ops:
        if op.opcode in ("getfield", "setfield") and op.args[0].type.is_pointer:
            b.position_before(op)
            p = op.args[0]
            load = b.load(p.type.base, [p])
            args = [load] + op.args[1:]
            op.set_args(args)

            if op.opcode == "setfield":
                b.position_after(op)
                b.store(op, p)
开发者ID:aterrel,项目名称:pykit,代码行数:16,代码来源:lower_fields.py

示例5: inline

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def inline(func, call, uses=None):
    """
    Inline the call instruction into func.

    :param uses: defuse information
    """
    callee = call.args[0]
    # assert_inlinable(func, call, callee, uses)

    builder = Builder(func)
    builder.position_before(call)
    inline_header, inline_exit = builder.splitblock()
    new_callee = copy_function(callee, temper=func.temp)
    result = rewrite_return(new_callee)

    # Fix up arguments
    for funcarg, arg in zip(new_callee.args, call.args[1]):
        funcarg.replace_uses(arg)

    # Copy blocks
    after = inline_header
    for block in new_callee.blocks:
        block.parent = None
        func.add_block(block, after=after)
        after = block

    # Fix up wiring
    builder.jump(new_callee.startblock)
    with builder.at_end(new_callee.exitblock):
        builder.jump(inline_exit)

    # Fix up final result of call
    if result is not None:
        # non-void return
        result.unlink()
        result.result = call.result
        call.replace(result)
    else:
        call.delete()

    func.reset_uses()
    verify(func)
开发者ID:B-Rich,项目名称:pykit,代码行数:44,代码来源:inline.py

示例6: rewrite_raise_exc_type

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def rewrite_raise_exc_type(func, env):
    """
    Rewrite 'raise Exception' to 'raise Exception()'
    """
    context = env['numba.typing.context']
    b = Builder(func)

    for op in func.ops:
        if op.opcode == 'exc_throw':
            [exc_type] = op.args
            if isinstance(exc_type, Const):
                ty = context[exc_type]
                if ty.impl == Type: # Type[Exception[]]
                    # Generate constructor application
                    b.position_before(op)
                    exc_obj = b.call(ptypes.Opaque, exc_type, [])
                    op.set_args([exc_obj])

                    type = ty.parameters[0]
                    context[exc_obj] = type
开发者ID:liuzhenhai,项目名称:numba-lang,代码行数:22,代码来源:constructors.py

示例7: generate_copies

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def generate_copies(func, phis):
    """
    Emit stores to stack variables in predecessor blocks.
    """
    builder = Builder(func)
    vars = {}
    loads = {}

    # Allocate a stack variable for each phi
    builder.position_at_beginning(func.startblock)
    for block in phis:
        for phi in phis[block]:
            vars[phi] = builder.alloca(types.Pointer(phi.type))

    # Generate loads in blocks containing the phis
    for block in phis:
        leaders = list(block.leaders)
        last_leader = leaders[-1] if leaders else block.head
        builder.position_after(last_leader)
        for phi in phis[block]:
            loads[phi] = builder.load(vars[phi])

    # Generate copies (store to stack variables)
    for block in phis:
        for phi in phis[block]:
            preds, args = phi.args
            var = vars[phi]
            phi_args = [loads.get(arg, arg) for arg in args]
            for pred, arg in zip(preds, phi_args):
                builder.position_before(pred.terminator)
                builder.store(arg, var)

    # Replace phis
    for block in phis:
        for phi in phis[block]:
            phi.replace_uses(loads[phi])
            phi.delete()

    return vars, loads
开发者ID:flypy,项目名称:pykit,代码行数:41,代码来源:reg2mem.py

示例8: convert_retval

# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_before [as 别名]
def convert_retval(func, env):
    """
    Rewrite 'return x' to 'return (restype) x'
    """
    if env['numba.state.opaque']:
        return

    restype = func.type.restype
    context = env['numba.typing.context']

    b = Builder(func)
    for op in func.ops:
        if op.opcode != 'ret' or op.args[0] is None:
            continue

        [retval] = op.args
        if retval.type != restype:
            b.position_before(op)
            converted = b.convert(restype, retval)
            op.set_args([converted])

            # Update type context
            context[converted] = context[retval]
开发者ID:liuzhenhai,项目名称:numba-lang,代码行数:25,代码来源:conversion.py


注:本文中的pykit.ir.Builder.position_before方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。