Unexpected behavior of Builder.position_at_beginning

4 views
Skip to first unread message

Baptiste Lepilleur

unread,
Dec 29, 2009, 8:12:35 AM12/29/09
to llvm-py
I wanted to change the basic block the builder was working on to a newly created basic block.

I basically add something like this:

basic_block_then = self.l_func.append_basic_block( '%s_then' % if_id )
self.builder.position_at_beginning( basic_block_then )

but Builder.position_at_beginning translated into a no op. I managed to get this to work using:
self.builder.position_at_end( basic_block_then )

Shouldn't position_at_beginning semantic be the same as position_at_end when the block contains no instruction? (or at least blow up with an exception).

The current behavior is caused by the absence of an "else" in the condition below:

    def position_at_beginning(self, bblk):
        """Position the builder at the beginning of the given block.

        Next instruction inserted will be first one in the block."""
        check_is_basic_block(bblk)
        # Avoids using "blk.instructions", which will fetch all the
        # instructions into a list. Don't try this at home, though.
        inst_ptr = _core.LLVMGetFirstInstruction(bblk.ptr)
        if inst_ptr:
            # Issue #10: inst_ptr can be None if b/b has no insts.
            inst = _make_value(inst_ptr)
            self.position_before(inst)

MD

unread,
Dec 29, 2009, 8:29:44 AM12/29/09
to llvm-py: Python Bindings for LLVM
Hi,

On Dec 29, 6:12 pm, Baptiste Lepilleur <baptiste.lepill...@gmail.com>
wrote:


> I wanted to change the basic block the builder was working on to a newly
> created basic block.
>
> I basically add something like this:
>
> basic_block_then = self.l_func.append_basic_block( '%s_then' % if_id )
> self.builder.position_at_beginning( basic_block_then )
>
> but Builder.position_at_beginning translated into a no op. I managed to get
> this to work using:
> self.builder.position_at_end( basic_block_then )
>
> Shouldn't position_at_beginning semantic be the same as position_at_end when
> the block contains no instruction? (or at least blow up with an exception).

Throwing an exception seems to be the proper thing to do, as
positioning is valid only when the basic block contains instructions.

Please raise an issue in the tracker; if possible include patch to
code and unit tests.

Thanks & Regards,
-Mahadevan.

Reply all
Reply to author
Forward
0 new messages