Sorry for the noob question, but I can't find what I want in the docs. I'm using FunctionBuilder to translate my own basic blocks to cranelift IR. Is there an easy way to check if a cranelift Block has a terminator/is filled? Or do I need to keep track of whether I have added a terminator instruction myself? Thanks!
Happy to answer questions! You can use builder.func.layout.last_inst(block)
to get the last instruction of a block (which will return None
if the block is empty). Then builder.func.dfg.insts[inst].is_terminator()
should tell you whether that instruction is valid for ending the block. But you might find it easier to keep track of this yourself.
Great, thank you! That's what I was looking for (although as you said, maybe it's better I keep track of things separately)
Raekye has marked this topic as resolved.
Found this when faced with the same problem. Looks like FunctionBuilder::is_filled() gives this information, but it's not public. Maybe it's worth making it public?
I had the same problem, but on further reflection, I think this was the wrong solution. I was trying to handle early returns from functions. In case anyone else is here for similar reasons: I think its better to create an extra block and trust cranelift to simply throw away that block since its obviously unreachable. This way you don't need a bunch of guards all over the place checking if a block is filled in case you slip some stray jump instructions into that block.
Scratch my bad advice. It ends up being nearly as complicated. I ended up sometimes having jumps to blocks with no instructions. And those empty blocks never get added to functions, which cranelift did not like. I went back to manually tracking my terminating instructions which is more mentally straightforward.
Last updated: Feb 27 2025 at 23:03 UTC