Stream: cranelift

Topic: ✔ Any way to check if a block is filled with FunctionBuil...


view this post on Zulip Raekye (Feb 03 2023 at 23:39):

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!

view this post on Zulip Jamey Sharp (Feb 03 2023 at 23:48):

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.

view this post on Zulip Raekye (Feb 04 2023 at 17:54):

Great, thank you! That's what I was looking for (although as you said, maybe it's better I keep track of things separately)

view this post on Zulip Notification Bot (Feb 04 2023 at 17:54):

Raekye has marked this topic as resolved.

view this post on Zulip Piotr (Jan 04 2025 at 19:32):

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?

view this post on Zulip blazzy (Jan 30 2025 at 18:05):

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.

view this post on Zulip blazzy (Jan 31 2025 at 06:36):

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