Stream: cranelift

Topic: Sharing values between blocks without block parameters


view this post on Zulip Stanley (May 23 2026 at 04:38):

Is it possible to share values between blocks without block parameters? Like if I have a Value created in one block, can I use it in another block without needing block parameters?

Also, the documentation on block parameters seems to be contradictory. For instance: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.FunctionBuilder.html#method.block_params
claims that it

Retrieves all the parameters for a Block currently inferred from the jump instructions inserted that target it and the SSA construction.

But, the documentation comment for the switch_to_block method on the same page claims that

You must declare the types of the Block arguments you will use here

Additionally, bjorn3 said:

If you pass a slice of values as block args, you need to call append_block_param an equivalent amount of times on the target block

so what is actually the case? Can the block parameters be inferred by the jump instructions inserted that target it?

Also, the Switch API does not seem to allow us to define the arguments to pass to the blocks that are the cases of the switch statement. Is there a way to do this?

view this post on Zulip Alex Crichton (May 23 2026 at 10:16):

You should be able to use a value in any block so long as the defining block dominates it

view this post on Zulip Alex Crichton (May 23 2026 at 10:17):

Block params are only necessary for non-dominating definitions or where the value in a block differs depending on control flow

view this post on Zulip Stanley (May 23 2026 at 19:38):

Thanks, that makes sense. Unfortunately in my case, the values are different depending on control flow. For my other question, Is it actually true that can the block parameters be inferred by the jump instructions inserted that target it? Or do we need to manually specify block parameter count/types? Also is there a way to pass block parameters in a Switch?

view this post on Zulip Alex Crichton (May 23 2026 at 19:46):

There's no inference, the jump and the block have to have the same number of params. Cranelift doesn't have a "Switch" instruction so I don't know what you're referring to

view this post on Zulip Stanley (May 23 2026 at 19:52):

Alex Crichton said:

Cranelift doesn't have a "Switch" instruction so I don't know what you're referring to

I was referring to this API: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.Switch.html

Alex Crichton said:

There's no inference, the jump and the block have to have the same number of params

I see, so that means we must manually define the block parameters for all blocks right?
Also, could you please explain what it means on this page then: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.FunctionBuilder.html#method.block_params
Where it says that "Retrieves all the parameters for a Block currently inferred from the jump instructions inserted that target it and the SSA construction."

view this post on Zulip Alex Crichton (May 23 2026 at 20:01):

oh sorry, misunderstood! I don't know what that Switch is, so someone else will have to chime in on that.

As for those docs, I fear someone else may also need to weigh in on that. That may be oudated documentation, or that may mean I'm just flat out wrong. Unsure

view this post on Zulip Stanley (May 23 2026 at 20:10):

Got it, I appreciate your help. Hopefully someone has some more information about this, because I am quite confused :sweat_smile:

view this post on Zulip bjorn3 (May 24 2026 at 11:15):

Is it actually true that can the block parameters be inferred by the jump instructions inserted that target it? Or do we need to manually specify block parameter count/types?

cranelift-frontend will automatically insert block parameters as necessary provided that you define/use variables rather than SSA values. See the crate documentation of cranelift-frontend.

view this post on Zulip Chris Fallin (May 25 2026 at 00:36):

Yes, I think the confusion here is related to the frontend / SSA builder vs. the core cranelift-codegen API. In the latter, CLIF is very explicit: you define everything, and the block-call args in terminators (jumps) must match blockparams.

Unfortunately in my case, the values are different depending on control flow.

FWIW, in case it helps thinking about this: the entire point of SSA is to surface these join-points and make them explicit. May seem a little annoying at first when you're constructing the IR, but it makes optimization and reasoning about the IR's semantics extremely straightforward vs. the classical "overwrite a variable at any point" IR design

view this post on Zulip Stanley (May 25 2026 at 18:53):

I see, thank you for the explanation. I am using SSA Values (not cranelift-frontend Variables), so I am manually setting the block parameters (with append_block_param), and it seems to be working now. Thanks!


Last updated: Jun 01 2026 at 09:49 UTC